No Description

ThreeYearPlanController.php 21KB

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