Nenhuma descrição

ThreeYearPlanController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. use Illuminate\Database\Eloquent\Collection;
  3. class ThreeYearPlanController extends \BaseController
  4. {
  5. // load the Three year Plan page
  6. public function threeYearsReport()
  7. {
  8. $user_id = auth::user()->id;
  9. // el ID de los semestres que el usuario tiene seleccionado.
  10. $semesters_ids = Session::get('semesters_ids');
  11. // buscar informacion de los semestres seleccionados
  12. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  13. $typs = DB::table('three_year_plan')
  14. ->orderBy('year_start', 'asc')
  15. ->get();
  16. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  17. $program_id = DB::table('program_user')
  18. ->where('user_id', $user_id)
  19. ->select('program_id')
  20. ->get();
  21. $program_id = $program_id[0]->program_id;
  22. // se annadio la nueva variable
  23. return View::make('global.view-three-year-plan', compact('title', 'outcomes', 'typs', 'criteria', 'semesters', 'program_id'));
  24. }
  25. //when the user select a three year period from the left column, load the corresponding info
  26. public function fetchThreeYears()
  27. {
  28. $id = Input::get('id');
  29. $semesters = DB::table('typ_semesters')
  30. ->join('semesters','semesters.id','=','typ_semesters.semester_id')
  31. ->where('typ_semesters.typ_id',$id)
  32. ->orderBy('typ_semesters.semester_id', 'asc')
  33. ->get();
  34. $typs = DB::table('three_year_plan')
  35. ->where('id',$id)
  36. ->orderBy('year_start', 'asc')
  37. ->get();
  38. $typs = $typs[0];
  39. $typs->semesters = $semesters;
  40. return array(
  41. 'typ' => $typs,
  42. );
  43. }
  44. // add or remove a semester associated with an outcome when a user selects a checkbox
  45. public function update_typ_outcomes_semesters()
  46. {
  47. $outcomeSemesterArray = json_decode(Input::get('outcomeSemesterArray'), true);
  48. $typ_id = Input::get('typ_id');
  49. $user_id = auth::user()->id;
  50. $program_id = DB::table('program_user')
  51. ->where('user_id',$user_id)
  52. ->select('program_id')
  53. ->get();
  54. $program_id = $program_id[0]->program_id;
  55. $result = DB::table('typ_program')
  56. ->where('three_year_plan_id', $typ_id)
  57. ->where('program_id', $program_id)
  58. ->get();
  59. // create the relation if it doesnt exist
  60. if (count($result) == 0) {
  61. $typ_program_id = DB::table('typ_program')->insertGetId(
  62. array(
  63. 'three_year_plan_id' => $typ_id,
  64. 'program_id' => $program_id
  65. )
  66. );
  67. } else {
  68. $typ_program_id = $result[0]->id;
  69. }
  70. //go through array
  71. foreach ($outcomeSemesterArray as $outcome_semester) {
  72. // foreach of the 6 semesters
  73. foreach ($outcome_semester["semesters"] as $semester) {
  74. $insert = $semester[1];
  75. $outcome_id = explode("-sem", $semester[0])[0];
  76. $semester_code = explode("-sem", $semester[0])[1];
  77. $result = DB::table('semesters')
  78. ->where('code', $semester_code)
  79. ->select('id')
  80. ->get();
  81. if (count($result) == 0) {
  82. continue;
  83. }
  84. $semester_id = $result[0]->id;
  85. // verify if it exists
  86. $result = DB::table('typ_semester_outcome')
  87. ->where('typ_program_id', $typ_program_id)
  88. ->where('semester_id', $semester_id)
  89. ->where('outcome_id', $outcome_id)
  90. ->get();
  91. // if true, it should get inserted
  92. if ($insert == true) {
  93. // if == 0, then insert
  94. if (count($result) == 0) {
  95. $_ = DB::table('typ_semester_outcome')->insertGetId(
  96. array(
  97. 'typ_program_id' => $typ_program_id,
  98. 'semester_id' => $semester_id,
  99. 'outcome_id' => $outcome_id
  100. )
  101. );
  102. }
  103. //if exists, then do nothing
  104. }
  105. // if false, it should get deleted
  106. else { //value == false
  107. // if == 0, it doesnt exist. we do nothing
  108. if (count($result) == 0) {
  109. //pass
  110. }
  111. //if exists, then do nothing
  112. else {
  113. DB::table('typ_semester_outcome')
  114. ->where('typ_program_id', $typ_program_id)
  115. ->where('semester_id', $semester_id)
  116. ->where('outcome_id', $outcome_id)
  117. ->delete();
  118. }
  119. }
  120. }
  121. }
  122. return;
  123. }
  124. // load semesters asociated with the outcomes if previously selected
  125. public function lookup_typ_semester_outcome()
  126. {
  127. $box_value = array();
  128. $info = Input::get('info');
  129. foreach ($info as $values) {
  130. $outcome_id = $values[0];
  131. $semester_code = $values[1];
  132. $program_id = $values[2];
  133. $box_id = $values[3];
  134. $typ_id = $values[4];
  135. //buscar id del typ
  136. $result = DB::table('typ_program')
  137. ->where('three_year_plan_id', $typ_id)
  138. ->where('program_id', $program_id)
  139. ->get();
  140. $typ_program_id = $result[0]->id;
  141. // buscar si existe el outcome_semester
  142. $result = DB::table('typ_semester_outcome')
  143. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  144. ->where('typ_program_id', $typ_program_id)
  145. ->where('semesters.code', $semester_code)
  146. ->where('outcome_id', $outcome_id)
  147. ->get();
  148. // if it doesnt exist
  149. if (count($result) == 0) {
  150. array_push($box_value, array($box_id, 0));
  151. } else {
  152. array_push($box_value, array($box_id, 1));
  153. }
  154. }
  155. return array(
  156. 'box_value' => ($box_value)
  157. );
  158. }
  159. // save or delete Objectives associated with the outcomes and semsters
  160. public function section2_on_change()
  161. {
  162. $typ_id = Input::get('typ_id');
  163. $outcome_id = Input::get('outcome_id');
  164. $semester_id = Input::get('semester_id');
  165. $previous_objective_id = Input::get('previous_objective_id');
  166. $new_objective_id = Input::get('new_objective_id');
  167. // get program_id
  168. $user_id = auth::user()->id;
  169. $program_id = DB::table('program_user')
  170. ->where('user_id',$user_id)
  171. ->select('program_id')
  172. ->get();
  173. $program_id = $program_id[0]->program_id;
  174. // get typ_program_id
  175. $result = DB::table('typ_program')
  176. ->where('three_year_plan_id', $typ_id)
  177. ->where('program_id', $program_id)
  178. ->get();
  179. $typ_program_id = $result[0]->id;
  180. // get typ_semester_outcome_id
  181. $result = DB::table('typ_semester_outcome')
  182. ->where('typ_program_id', $typ_program_id)
  183. ->where('semester_id', $semester_id)
  184. ->where('outcome_id', $outcome_id)
  185. ->get();
  186. $typ_semester_outcome_id = $result[0]->id;
  187. //delete old objective relation if it exists
  188. if ($previous_objective_id != 'nothing_selected') {
  189. $result = DB::table('typ_semester_objectives')
  190. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  191. ->where('objective_id', $previous_objective_id)
  192. ->get();
  193. if (count($result) != 0) {
  194. DB::table('typ_semester_objectives')
  195. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  196. ->where('objective_id', $previous_objective_id)
  197. ->delete();
  198. }
  199. }
  200. //create new objective relation
  201. if ($new_objective_id != 'nothing_selected') {
  202. $result = DB::table('typ_semester_objectives')
  203. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  204. ->where('objective_id', $new_objective_id)
  205. ->get();
  206. if (count($result) == 0) {
  207. $_ = DB::table('typ_semester_objectives')->insertGetId(
  208. array(
  209. 'typ_semester_outcome_id' => $typ_semester_outcome_id,
  210. 'objective_id' => $new_objective_id
  211. )
  212. );
  213. }
  214. }
  215. return 'update was succes';
  216. }
  217. // load the corresponding info after associating semesters with outcomes
  218. public function section2_arrive()
  219. {
  220. // get program_id
  221. $user_id = auth::user()->id;
  222. $program_id = DB::table('program_user')
  223. ->where('user_id',$user_id)
  224. ->select('program_id')
  225. ->get();
  226. $program_id = $program_id[0]->program_id;
  227. // get typ_program_id
  228. $typ_id = Input::get('typ_id');
  229. $result = DB::table('typ_program')
  230. ->where('three_year_plan_id', $typ_id)
  231. ->where('program_id', $program_id)
  232. ->get();
  233. $typ_program_id = $result[0]->id;
  234. //search selected outcomes,semesters and get their objectives
  235. $selected_outcomes = DB::table('typ_semester_outcome')
  236. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  237. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  238. ->distinct('typ_semester_outcome.outcome_id')
  239. ->select('typ_semester_outcome.outcome_id')
  240. ->orderBy('outcomes.name', 'asc')
  241. ->get();
  242. $selected_objectives_array = array();
  243. $outcomes_info_array = array();
  244. // $objectives_info_array = array();
  245. $semesters_array = array();
  246. $info = array();
  247. foreach ($selected_outcomes as $outcome) {
  248. $outcome_id = $outcome->outcome_id;
  249. $result_outcomes = DB::table('outcomes')
  250. ->where('outcomes.id', $outcome_id)
  251. ->orderBy('outcomes.name', 'asc')
  252. ->get();
  253. foreach ($result_outcomes as $outcome_) {
  254. $result_semesters = DB::table('typ_semester_outcome')
  255. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  256. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  257. ->where('typ_semester_outcome.outcome_id', $outcome_->id)
  258. ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
  259. ->get();
  260. // buscar info sobre selected_semsters
  261. $outcome_->selected_semesters = array();
  262. foreach ($result_semesters as $semesters_) {
  263. $typ_semester_outcome_id = $semesters_->id;
  264. // lookup previusly selected objectives
  265. $result_objectives = DB::table('typ_semester_objectives')
  266. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  267. ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
  268. ->orderBy('typ_semester_objectives.id', 'asc')
  269. ->get();
  270. $semesters_->selected_objectives = array();
  271. foreach ($result_objectives as $objectives_) {
  272. array_push($semesters_->selected_objectives, $objectives_);
  273. }
  274. // lookup available objectives
  275. $result_objectives = DB::table('objective_outcome')
  276. ->join('outcomes','outcomes.id','=','objective_outcome.outcome_id')
  277. ->join('objectives','objectives.id','=','objective_outcome.objective_id')
  278. ->join('objective_program','objective_program.objective_id','=','objectives.id')
  279. ->where('objective_program.program_id',$program_id)
  280. ->where('objective_outcome.outcome_id',$outcome_id)
  281. ->select('objectives.id','objectives.text','outcomes.name as outcome_name')
  282. ->orderBy('objectives.text', 'asc')
  283. ->get();
  284. $semesters_->available_objectives = array();
  285. foreach ($result_objectives as $objectives_) {
  286. array_push($semesters_->available_objectives, $objectives_);
  287. }
  288. array_push($outcome_->selected_semesters, $semesters_);
  289. }
  290. array_push($info, $outcome_);
  291. }
  292. }
  293. return $info;
  294. }
  295. // load the corresponding info after associating objectives to semesters outcomes
  296. public function section3_arrive()
  297. {
  298. // get program_id
  299. $user_id = auth::user()->id;
  300. $program_id = DB::table('program_user')
  301. ->where('user_id',$user_id)
  302. ->select('program_id')
  303. ->get();
  304. $program_id = $program_id[0]->program_id;
  305. // get typ_program_id
  306. $typ_id = Input::get('typ_id');
  307. $result = DB::table('typ_program')
  308. ->where('three_year_plan_id', $typ_id)
  309. ->where('program_id', $program_id)
  310. ->get();
  311. $typ_program_id = $result[0]->id;
  312. //search selected outcomes,semesters and get their objectives
  313. $selected_outcomes = DB::table('typ_semester_outcome')
  314. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  315. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  316. ->distinct('typ_semester_outcome.outcome_id')
  317. ->select('typ_semester_outcome.outcome_id')
  318. ->orderBy('outcomes.name', 'asc')
  319. ->get();
  320. $selected_objectives_array = array();
  321. $outcomes_info_array = array();
  322. // $objectives_info_array = array();
  323. $semesters_array = array();
  324. $info = array();
  325. foreach ($selected_outcomes as $outcome) {
  326. $outcome_id = $outcome->outcome_id;
  327. $result_outcomes = DB::table('outcomes')
  328. ->where('outcomes.id', $outcome_id)
  329. ->get();
  330. foreach ($result_outcomes as $outcome_) {
  331. $result_semesters = DB::table('typ_semester_outcome')
  332. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  333. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  334. ->where('typ_semester_outcome.outcome_id', $outcome_->id)
  335. ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
  336. ->get();
  337. // buscar info sobre selected_semsters
  338. $outcome_->selected_semesters = array();
  339. foreach ($result_semesters as $semesters_) {
  340. $typ_semester_outcome_id = $semesters_->id;
  341. $result_objectives = DB::table('typ_semester_objectives')
  342. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  343. ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
  344. ->select('typ_semester_objectives.id as typ_semester_objectives_id', 'objectives.*', 'typ_semester_objectives.*')
  345. ->orderBy('typ_semester_objectives.id', 'asc')
  346. ->get();
  347. $semesters_->selected_objectives = array();
  348. foreach ($result_objectives as $objectives_) {
  349. $result_courses = DB::table('courses')
  350. ->where('courses.program_id', $program_id)
  351. ->select('courses.id as course_id','courses.name','courses.code','courses.program_id')
  352. ->orderBy('courses.name', 'asc')
  353. ->groupBy('courses.name','courses.code')
  354. ->get();
  355. #los cursos resultantes creo que deberian ser: todos los que se estan dando en mi programa
  356. $objectives_->available_courses = array();
  357. foreach ($result_courses as $courses_) {
  358. array_push($objectives_->available_courses, $courses_);
  359. }
  360. // search for previusly selected courses
  361. $typ_semester_objective_id = $objectives_->typ_semester_objectives_id;
  362. $result_courses = DB::table('typ_semester_courses')
  363. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  364. ->where('typ_semester_courses.typ_semester_objective_id', $typ_semester_objective_id)
  365. ->select('courses.id as course_id', 'courses.*', 'typ_semester_courses.*')
  366. ->orderBy('courses.name', 'asc')
  367. ->get();
  368. $objectives_->selected_courses = array();
  369. foreach ($result_courses as $courses_) {
  370. array_push($objectives_->selected_courses, $courses_);
  371. }
  372. array_push($semesters_->selected_objectives, $objectives_);
  373. }
  374. array_push($outcome_->selected_semesters, $semesters_);
  375. }
  376. array_push($info, $outcome_);
  377. }
  378. }
  379. return $info;
  380. }
  381. // save or delete courses associated with objectives
  382. public function section3_on_change()
  383. {
  384. $typ_id = Input::get('typ_id');
  385. $outcome_id = Input::get('outcome_id');
  386. $semester_id = Input::get('semester_id');
  387. $objective_id = Input::get('objective_id');
  388. $previous_course_id = Input::get('previous_course_id');
  389. $new_course_id = Input::get('new_course_id');
  390. // get program_id
  391. $user_id = auth::user()->id;
  392. $program_id = DB::table('program_user')
  393. ->where('user_id',$user_id)
  394. ->select('program_id')
  395. ->get();
  396. $program_id = $program_id[0]->program_id;
  397. // get typ_program_id
  398. $result = DB::table('typ_program')
  399. ->where('three_year_plan_id', $typ_id)
  400. ->where('program_id', $program_id)
  401. ->get();
  402. $typ_program_id = $result[0]->id;
  403. // get typ_semester_outcome_id
  404. $result = DB::table('typ_semester_outcome')
  405. ->where('typ_program_id', $typ_program_id)
  406. ->where('semester_id', $semester_id)
  407. ->where('outcome_id', $outcome_id)
  408. ->get();
  409. $typ_semester_outcome_id = $result[0]->id;
  410. // get typ_semester_objective_id
  411. $result = DB::table('typ_semester_objectives')
  412. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  413. ->where('objective_id', $objective_id)
  414. ->get();
  415. $typ_semester_objective_id = $result[0]->id;
  416. //delete old objective relation if it exists
  417. if ($previous_course_id != 'nothing_selected') {
  418. $result = DB::table('typ_semester_courses')
  419. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  420. ->where('course_id', $previous_course_id)
  421. ->get();
  422. if (count($result) != 0) {
  423. DB::table('typ_semester_courses')
  424. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  425. ->where('course_id', $previous_course_id)
  426. ->delete();
  427. }
  428. }
  429. //create new objective relation
  430. if ($new_course_id != 'nothing_selected') {
  431. $result = DB::table('typ_semester_courses')
  432. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  433. ->where('course_id', $new_course_id)
  434. ->get();
  435. if (count($result) == 0) {
  436. $_ = DB::table('typ_semester_courses')->insertGetId(
  437. array(
  438. 'typ_semester_objective_id' => $typ_semester_objective_id,
  439. 'course_id' => $new_course_id
  440. )
  441. );
  442. }
  443. }
  444. return 'update succes?';
  445. }
  446. public function createAnnualPlan($program_id)
  447. {
  448. $current_typ = DB::select("select * from three_year_plan where year_start <=" . date('Y') . " and year_end >=" . date('Y'))[0];
  449. $count = 0;
  450. $yearStartPlusOne = $current_typ->year_start + 1;
  451. $yearStart = $current_typ->year_start;
  452. Log::info($yearStart);
  453. Log::info($yearStartPlusOne);
  454. while ($count < 3) {
  455. $firstSemester = DB::select("select * from semesters where name like 'First Semester {$yearStart}-{$yearStartPlusOne}'");
  456. $secondSemester = DB::select("select * from semesters where name like 'Second Semester {$yearStart}-{$yearStartPlusOne}'");
  457. if (count($firstSemester) && count($secondSemester)) {
  458. Log::info($firstSemester);
  459. Log::info($secondSemester);
  460. $query = DB::select("select * from annual_plans where semester_start ={$firstSemester[0]->id} and semester_end ={$secondSemester[0]->id} and program_id = {$program_id}");
  461. if (!count($query)) {
  462. DB::insert("insert into annual_plans (academic_year, semester_start, semester_end, program_id) values ('{$yearStart}-{$yearStartPlusOne}', {$firstSemester[0]->id}, {$secondSemester[0]->id}, {$program_id})");
  463. }
  464. }
  465. $count++;
  466. $yearStart++;
  467. $yearStartPlusOne++;
  468. }
  469. }
  470. }