暫無描述

ThreeYearPlanController.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 Planning";
  14. $outcomes = Outcome::orderBy('name', 'ASC')
  15. ->where('deactivation_date','=','0000-00-00')
  16. ->orWhereNull('deactivation_date')
  17. ->get();
  18. $typs = DB::table('three_year_plan')
  19. ->orderBy('year_start', 'asc')
  20. ->get();
  21. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  22. $program_id = DB::table('program_user')
  23. ->where('user_id',$user_id)
  24. ->select('program_id')
  25. ->get();
  26. $program_id = $program_id[0]->program_id;
  27. // se annadio la nueva variable
  28. return View::make('global.view-three-year-plan', compact('title', 'outcomes', 'typs', 'criteria', 'semesters', 'program_id'));
  29. }
  30. //when the user select a three year period from the left column, load the corresponding info
  31. public function fetchThreeYears()
  32. {
  33. $id = Input::get('id');
  34. $semesters = DB::table('typ_semesters')
  35. ->join('semesters','semesters.id','=','typ_semesters.semester_id')
  36. ->where('typ_semesters.typ_id',$id)
  37. ->orderBy('typ_semesters.semester_id', 'asc')
  38. ->get();
  39. $typs = DB::table('three_year_plan')
  40. ->where('id',$id)
  41. ->orderBy('year_start', 'asc')
  42. ->get();
  43. $typs = $typs[0];
  44. $typs->semesters = $semesters;
  45. return array
  46. (
  47. 'typ' => $typs,
  48. );
  49. }
  50. // add or remove a semester associated with an outcome when a user selects a checkbox
  51. public function update_typ_outcomes_semesters()
  52. {
  53. $outcomeSemesterArray = json_decode(Input::get('outcomeSemesterArray'), true);
  54. $typ_id = Input::get('typ_id');
  55. $user_id = auth::user()->id;
  56. $program_id = DB::table('program_user')
  57. ->where('user_id',$user_id)
  58. ->select('program_id')
  59. ->get();
  60. $program_id = $program_id[0]->program_id;
  61. $result = DB::table('typ_program')
  62. ->where('three_year_plan_id',$typ_id)
  63. ->where('program_id',$program_id)
  64. ->get();
  65. // create the relation if it doesnt exist
  66. if(count($result) == 0){
  67. $typ_program_id = DB::table('typ_program')->insertGetId(
  68. array('three_year_plan_id' => $typ_id,
  69. 'program_id' => $program_id)
  70. );
  71. } else {
  72. $typ_program_id = $result[0]->id;
  73. }
  74. //go through array
  75. foreach ($outcomeSemesterArray as $outcome_semester) {
  76. // foreach of the 6 semesters
  77. foreach ($outcome_semester["semesters"] as $semester) {
  78. $insert = $semester[1];
  79. $outcome_id = explode("-sem",$semester[0])[0];
  80. $semester_code = explode("-sem",$semester[0])[1];
  81. $result = DB::table('semesters')
  82. ->where('code',$semester_code)
  83. ->select('id')
  84. ->get();
  85. if(count($result) == 0){
  86. continue;
  87. }
  88. $semester_id = $result[0]->id;
  89. // verify if it exists
  90. $result = DB::table('typ_semester_outcome')
  91. ->where('typ_program_id',$typ_program_id)
  92. ->where('semester_id',$semester_id)
  93. ->where('outcome_id',$outcome_id)
  94. ->get();
  95. // if true, it should get inserted
  96. if ($insert == true) {
  97. // if == 0, then insert
  98. if(count($result) == 0){
  99. $_ = DB::table('typ_semester_outcome')->insertGetId(
  100. array('typ_program_id' => $typ_program_id,
  101. 'semester_id' => $semester_id,
  102. 'outcome_id' => $outcome_id)
  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. (
  159. 'box_value' => ($box_value)
  160. );
  161. }
  162. // save or delete Objectives associated with the outcomes and semsters
  163. public function section2_on_change()
  164. {
  165. $typ_id = Input::get('typ_id');
  166. $outcome_id = Input::get('outcome_id');
  167. $semester_id = Input::get('semester_id');
  168. $previous_objective_id = Input::get('previous_objective_id');
  169. $new_objective_id = Input::get('new_objective_id');
  170. // get program_id
  171. $user_id = auth::user()->id;
  172. $program_id = DB::table('program_user')
  173. ->where('user_id',$user_id)
  174. ->select('program_id')
  175. ->get();
  176. $program_id = $program_id[0]->program_id;
  177. // get typ_program_id
  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. // get typ_semester_outcome_id
  184. $result = DB::table('typ_semester_outcome')
  185. ->where('typ_program_id',$typ_program_id)
  186. ->where('semester_id',$semester_id)
  187. ->where('outcome_id',$outcome_id)
  188. ->get();
  189. $typ_semester_outcome_id = $result[0]->id;
  190. //delete old objective relation if it exists
  191. if ($previous_objective_id != 'nothing_selected') {
  192. $result = DB::table('typ_semester_objectives')
  193. ->where('typ_semester_outcome_id',$typ_semester_outcome_id)
  194. ->where('objective_id',$previous_objective_id)
  195. ->get();
  196. if (count($result) != 0) {
  197. DB::table('typ_semester_objectives')
  198. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  199. ->where('objective_id', $previous_objective_id)
  200. ->delete();
  201. }
  202. }
  203. //create new objective relation
  204. if ($new_objective_id != 'nothing_selected') {
  205. $result = DB::table('typ_semester_objectives')
  206. ->where('typ_semester_outcome_id',$typ_semester_outcome_id)
  207. ->where('objective_id',$new_objective_id)
  208. ->get();
  209. if (count($result) == 0) {
  210. $_ = DB::table('typ_semester_objectives')->insertGetId(
  211. array(
  212. 'typ_semester_outcome_id' => $typ_semester_outcome_id,
  213. 'objective_id' => $new_objective_id)
  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. return 'update succes?';
  446. }
  447. }