Ei kuvausta

ThreeYearPlanController.php 21KB

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