説明なし

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