No Description

ThreeYearPlanController.php 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. <?php
  2. use Illuminate\Database\Eloquent\Collection;
  3. use Barryvdh\DomPDF\PDF as PDF;
  4. use Illuminate\Support\Facades\View;
  5. class ThreeYearPlanController extends \BaseController
  6. {
  7. public function create()
  8. {
  9. try {
  10. DB::beginTransaction();
  11. $yearStart = Input::get('years') + 0;
  12. $yearEnd = $yearStart + 3;
  13. $program_id = Input::get('program_id');
  14. DB::insert("insert into three_year_plan (`year_start`, `year_end`) values ({$yearStart}, {$yearEnd})");
  15. $typ = DB::table('three_year_plan')->where('year_start', '=', $yearStart)
  16. ->where('year_end', '=', $yearEnd)
  17. ->first();
  18. for ($i = $yearStart; $i < $yearEnd; $i++) {
  19. $string = 'First Semester ' . $i . '-' . ($i + 1);
  20. $string2 = 'Second Semester ' . $i . '-' . ($i + 1);
  21. $Firstsemester = DB::table('semesters')->where('name', '=', $string)->first();
  22. $secondSemester = DB::table('semesters')->where('name', '=', $string2)->first();
  23. Log::info($Firstsemester->id);
  24. Log::info($secondSemester->id);
  25. Log::info($typ->id);
  26. DB::insert("insert into typ_semesters (typ_id, semester_id) values ({$typ->id}, {$Firstsemester->id}), ({$typ->id}, {$secondSemester->id})");
  27. }
  28. DB::commit();
  29. Session::flash('status', 'success');
  30. Session::flash('message', '<p> Cycle created.</p>');
  31. return Redirect::to("three-years-plan/{$program_id}");
  32. } catch (Exception $e) {
  33. Session::flash('status', 'danger');
  34. Session::flash('message', "<p>Could not create cycle</p>");
  35. Log::info($e);
  36. DB::rollback();
  37. return Redirect::to("three-years-plan/{$program_id}");
  38. }
  39. }
  40. public function adminIndex()
  41. {
  42. Log::info("Entre" . json_encode($this));
  43. $title = "Three Years Plans";
  44. $programs = Program::orderBy('name', "ASC")->get();
  45. $typ = DB::table("three_year_plan")
  46. ->orderBy('year_start', "DESC")
  47. ->get();
  48. return View::make('local.managers.admins.show-typ-program', compact('title', 'programs', 'typ'));
  49. }
  50. public function fetchSubmitted()
  51. {
  52. $typ = Input::get('typ');
  53. return DB::table('typ_program')
  54. ->where('three_year_plan_id', $typ)
  55. ->get();
  56. }
  57. public function manageSemesterCycle()
  58. {
  59. // Log::info("Entre".json_encode($this));
  60. $title = "Semesters Cycles";
  61. // $programs = Program::get();
  62. return $this->adminIndex();
  63. // return View::make('local.managers.admins.show-typ-program', compact('title', 'programs'));
  64. }
  65. //submit
  66. public function submit($program_id)
  67. {
  68. $typ_id = Input::get('typ_id');
  69. $rows = DB::table('typ_program')
  70. ->where('three_year_plan_id', $typ_id)
  71. ->where('program_id', $program_id)
  72. ->update(array(
  73. "is_submitted" => 1
  74. ));
  75. if (isset($rows)) {
  76. return "estamos ready";
  77. } else {
  78. return 500;
  79. }
  80. }
  81. //busca los links de public y los loadea
  82. public function findHTML($path_id)
  83. {
  84. $path = DB::table('paths_for_three_year_plans')->where('id', $path_id)->first();
  85. if (isset($path)) {
  86. $path = trim($path->path_to_pdf, app_path() . "/views/three_year_plan_htmls/");
  87. $path = trim($path, ".blade.php");
  88. return View::make("three_year_plan_htmls." . $path);
  89. }
  90. Session::flash('status', 'warning');
  91. Session::flash('message', 'Error loading typ. Please try again later.');
  92. return Redirect::back();
  93. }
  94. // load the Three year Plan page
  95. public function threeYearsReport($program_id)
  96. {
  97. $user_id = Auth::user()->id;
  98. $last_year = DB::table('three_year_plan')->select('year_end')
  99. ->orderBy('year_end', 'desc')->first();
  100. if (!$last_year) {
  101. // $last_year = date('Y');
  102. $last_year = 2019;
  103. } else {
  104. $last_year = $last_year->year_end;
  105. }
  106. $program = Program::find($program_id);
  107. Log::info($program);
  108. // el ID de los semestres que el usuario tiene seleccionado.
  109. $semesters_ids = Session::get('semesters_ids');
  110. // buscar informacion de los semestres seleccionados
  111. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  112. $title = "Three Year Plan for " . $program->name;
  113. $typs = DB::table('three_year_plan')
  114. ->orderBy('year_start', 'asc')
  115. ->get();
  116. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  117. /* $program_id = DB::table('program_user')
  118. ->where('user_id', $user_id)
  119. ->select('program_id')
  120. ->get();
  121. $program_id = $program_id[0]->program_id;*/
  122. if ($program->is_graduate == 0) {
  123. $outcomes = DB::table('outcomes')->where('deactivation_date', '=', null)->orderBy('name', 'ASC')->get();
  124. } else {
  125. $outcomes = DB::table('outcomes')->where('deactivation_date', '=', null)->where('level', '=', 3)->orderBy('name', 'ASC')->get();
  126. }
  127. // se annadio la nueva variable
  128. return View::make('global.view-three-year-plan', compact('title', 'last_year', 'outcomes', 'typs', 'criteria', 'semesters', 'program_id'));
  129. }
  130. //when the user select a three year period from the left column, load the corresponding info
  131. public function fetchThreeYears()
  132. {
  133. $id = Input::get('id');
  134. $semesters = DB::table('typ_semesters')
  135. ->join('semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  136. ->where('typ_semesters.typ_id', $id)
  137. ->orderBy('typ_semesters.semester_id', 'asc')
  138. ->get();
  139. $typs = DB::table('three_year_plan')
  140. ->where('id', $id)
  141. ->orderBy('year_start', 'asc')
  142. ->get();
  143. $typs = $typs[0];
  144. $typs->semesters = $semesters;
  145. return array(
  146. 'typ' => $typs,
  147. );
  148. }
  149. // add or remove a semester associated with an outcome when a user selects a checkbox
  150. public function update_typ_outcomes_semesters()
  151. {
  152. $outcomeSemesterArray = json_decode(Input::get('outcomeSemesterArray'), true);
  153. $typ_id = Input::get('typ_id');
  154. $user_id = auth::user()->id;
  155. $program_id = Input::get('program_id');
  156. $result = DB::table('typ_program')
  157. ->where('three_year_plan_id', $typ_id)
  158. ->where('program_id', $program_id)
  159. ->get();
  160. // create the relation if it doesnt exist
  161. if (count($result) == 0) {
  162. $typ_program_id = DB::table('typ_program')->insertGetId(
  163. array(
  164. 'three_year_plan_id' => $typ_id,
  165. 'program_id' => $program_id
  166. )
  167. );
  168. } else {
  169. $typ_program_id = $result[0]->id;
  170. }
  171. //go through array
  172. foreach ($outcomeSemesterArray as $outcome_semester) {
  173. // foreach of the 6 semesters
  174. foreach ($outcome_semester["semesters"] as $semester) {
  175. $insert = $semester[1];
  176. $outcome_id = explode("-sem", $semester[0])[0];
  177. $semester_code = explode("-sem", $semester[0])[1];
  178. $result = DB::table('semesters')
  179. ->where('code', $semester_code)
  180. ->select('id')
  181. ->get();
  182. if (count($result) == 0) {
  183. continue;
  184. }
  185. $semester_id = $result[0]->id;
  186. // verify if it exists
  187. $result = DB::table('typ_semester_outcome')
  188. ->where('typ_program_id', $typ_program_id)
  189. ->where('semester_id', $semester_id)
  190. ->where('outcome_id', $outcome_id)
  191. ->get();
  192. // if true, it should get inserted
  193. if ($insert == true) {
  194. // if == 0, then insert
  195. if (count($result) == 0) {
  196. $_ = DB::table('typ_semester_outcome')->insertGetId(
  197. array(
  198. 'typ_program_id' => $typ_program_id,
  199. 'semester_id' => $semester_id,
  200. 'outcome_id' => $outcome_id
  201. )
  202. );
  203. }
  204. //if exists, then do nothing
  205. }
  206. // if false, it should get deleted
  207. else { //value == false
  208. // if == 0, it doesnt exist. we do nothing
  209. if (count($result) == 0) {
  210. //pass
  211. }
  212. //if exists, then do nothing
  213. else {
  214. DB::table('typ_semester_outcome')
  215. ->where('typ_program_id', $typ_program_id)
  216. ->where('semester_id', $semester_id)
  217. ->where('outcome_id', $outcome_id)
  218. ->delete();
  219. }
  220. }
  221. }
  222. }
  223. return;
  224. }
  225. // load semesters asociated with the outcomes if previously selected
  226. public function lookup_typ_semester_outcome()
  227. {
  228. $box_value = array();
  229. $info = Input::get('info');
  230. foreach ($info as $values) {
  231. $outcome_id = $values[0];
  232. $semester_code = $values[1];
  233. $program_id = $values[2];
  234. $box_id = $values[3];
  235. $typ_id = $values[4];
  236. //buscar id del typ
  237. $result = DB::table('typ_program')
  238. ->where('three_year_plan_id', $typ_id)
  239. ->where('program_id', $program_id)
  240. ->get();
  241. $typ_program_id = $result[0]->id;
  242. // buscar si existe el outcome_semester
  243. $result = DB::table('typ_semester_outcome')
  244. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  245. ->where('typ_program_id', $typ_program_id)
  246. ->where('semesters.code', $semester_code)
  247. ->where('outcome_id', $outcome_id)
  248. ->get();
  249. // if it doesnt exist
  250. if (count($result) == 0) {
  251. array_push($box_value, array($box_id, 0));
  252. } else {
  253. array_push($box_value, array($box_id, 1));
  254. }
  255. }
  256. return array(
  257. 'box_value' => ($box_value)
  258. );
  259. }
  260. // save or delete Objectives associated with the outcomes and semsters
  261. public function section2_on_change()
  262. {
  263. $typ_id = Input::get('typ_id');
  264. $outcome_id = Input::get('outcome_id');
  265. $semester_id = Input::get('semester_id');
  266. $previous_objective_id = Input::get('previous_objective_id');
  267. $new_objective_id = Input::get('new_objective_id');
  268. $program_id = Input::get('program_id');
  269. // get program_id
  270. //$user_id = auth::user()->id;
  271. //Log::info(Input::all());
  272. // get typ_program_id
  273. $result = DB::table('typ_program')
  274. ->where('three_year_plan_id', $typ_id)
  275. ->where('program_id', $program_id)
  276. ->get();
  277. $typ_program_id = $result[0]->id;
  278. // get typ_semester_outcome_id
  279. $result = DB::table('typ_semester_outcome')
  280. ->where('typ_program_id', $typ_program_id)
  281. ->where('semester_id', $semester_id)
  282. ->where('outcome_id', $outcome_id)
  283. ->get();
  284. $typ_semester_outcome_id = $result[0]->id;
  285. //delete old objective relation if it exists
  286. if ($previous_objective_id != 'nothing_selected') {
  287. $result = DB::table('typ_semester_objectives')
  288. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  289. ->where('objective_id', $previous_objective_id)
  290. ->get();
  291. if (count($result) != 0) {
  292. DB::table('typ_semester_objectives')
  293. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  294. ->where('objective_id', $previous_objective_id)
  295. ->delete();
  296. }
  297. }
  298. //create new objective relation
  299. if ($new_objective_id != 'nothing_selected') {
  300. $result = DB::table('typ_semester_objectives')
  301. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  302. ->where('objective_id', $new_objective_id)
  303. ->get();
  304. if (count($result) == 0) {
  305. $_ = DB::table('typ_semester_objectives')->insertGetId(
  306. array(
  307. 'typ_semester_outcome_id' => $typ_semester_outcome_id,
  308. 'objective_id' => $new_objective_id
  309. )
  310. );
  311. }
  312. }
  313. return 'update was succes';
  314. }
  315. // load the corresponding info after associating semesters with outcomes
  316. public function section2_arrive()
  317. {
  318. // get program_id
  319. $user_id = auth::user()->id;
  320. $program_id = Input::get('program_id');
  321. // get typ_program_id
  322. $typ_id = Input::get('typ_id');
  323. $result = DB::table('typ_program')
  324. ->where('three_year_plan_id', $typ_id)
  325. ->where('program_id', $program_id)
  326. ->get();
  327. $typ_program_id = $result[0]->id;
  328. //search selected outcomes,semesters and get their objectives
  329. $selected_outcomes = DB::table('typ_semester_outcome')
  330. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  331. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  332. ->distinct('typ_semester_outcome.outcome_id')
  333. ->select('typ_semester_outcome.outcome_id')
  334. ->orderBy('outcomes.name', 'asc')
  335. ->get();
  336. $selected_objectives_array = array();
  337. $outcomes_info_array = array();
  338. // $objectives_info_array = array();
  339. $semesters_array = array();
  340. $info = array();
  341. foreach ($selected_outcomes as $outcome) {
  342. $outcome_id = $outcome->outcome_id;
  343. $result_outcomes = DB::table('outcomes')
  344. ->where('outcomes.id', $outcome_id)
  345. ->orderBy('outcomes.name', 'asc')
  346. ->get();
  347. foreach ($result_outcomes as $outcome_) {
  348. $result_semesters = DB::table('typ_semester_outcome')
  349. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  350. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  351. ->where('typ_semester_outcome.outcome_id', $outcome_->id)
  352. ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
  353. ->orderBy('semesters.code')
  354. ->get();
  355. // buscar info sobre selected_semsters
  356. $outcome_->selected_semesters = array();
  357. foreach ($result_semesters as $semesters_) {
  358. $typ_semester_outcome_id = $semesters_->id;
  359. // lookup previusly selected objectives
  360. $result_objectives = DB::table('typ_semester_objectives')
  361. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  362. ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
  363. ->orderBy('typ_semester_objectives.id', 'asc')
  364. ->get();
  365. $semesters_->selected_objectives = array();
  366. foreach ($result_objectives as $objectives_) {
  367. array_push($semesters_->selected_objectives, $objectives_);
  368. }
  369. // lookup available objectives
  370. $result_objectives = DB::table('objective_outcome')
  371. ->join('outcomes', 'outcomes.id', '=', 'objective_outcome.outcome_id')
  372. ->join('objectives', 'objectives.id', '=', 'objective_outcome.objective_id')
  373. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  374. ->where('objective_program.program_id', $program_id)
  375. ->where('objective_outcome.outcome_id', $outcome_id)
  376. ->where('objective_outcome.objective_id', "<>", 0)
  377. ->select('objectives.id', 'objectives.text', 'outcomes.name as outcome_name')
  378. ->orderBy('objectives.text', 'asc')
  379. ->get();
  380. $semesters_->available_objectives = array();
  381. foreach ($result_objectives as $objectives_) {
  382. array_push($semesters_->available_objectives, $objectives_);
  383. }
  384. array_push($outcome_->selected_semesters, $semesters_);
  385. }
  386. array_push($info, $outcome_);
  387. }
  388. }
  389. return $info;
  390. }
  391. // load the corresponding info after associating objectives to semesters outcomes
  392. public function section3_arrive()
  393. {
  394. // get program_id
  395. $user_id = auth::user()->id;
  396. $program_id = Input::get('program_id');
  397. // get typ_program_id
  398. $typ_id = Input::get('typ_id');
  399. $result = DB::table('typ_program')
  400. ->where('three_year_plan_id', $typ_id)
  401. ->where('program_id', $program_id)
  402. ->get();
  403. $typ_program_id = $result[0]->id;
  404. //search selected outcomes,semesters and get their objectives
  405. $selected_outcomes = DB::table('typ_semester_outcome')
  406. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  407. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  408. ->distinct('typ_semester_outcome.outcome_id')
  409. ->select('typ_semester_outcome.outcome_id')
  410. ->orderBy('outcomes.name', 'asc')
  411. ->get();
  412. $selected_objectives_array = array();
  413. $outcomes_info_array = array();
  414. // $objectives_info_array = array();
  415. $semesters_array = array();
  416. $info = array();
  417. foreach ($selected_outcomes as $outcome) {
  418. $outcome_id = $outcome->outcome_id;
  419. $result_outcomes = DB::table('outcomes')
  420. ->where('outcomes.id', $outcome_id)
  421. ->get();
  422. foreach ($result_outcomes as $outcome_) {
  423. $result_semesters = DB::table('typ_semester_outcome')
  424. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  425. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  426. ->where('typ_semester_outcome.outcome_id', $outcome_->id)
  427. ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
  428. ->orderBy('semesters.code')
  429. ->get();
  430. // buscar info sobre selected_semsters
  431. $outcome_->selected_semesters = array();
  432. foreach ($result_semesters as $semesters_) {
  433. $typ_semester_outcome_id = $semesters_->id;
  434. $result_objectives = DB::table('typ_semester_objectives')
  435. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  436. ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
  437. ->select('typ_semester_objectives.id as typ_semester_objectives_id', 'objectives.*', 'typ_semester_objectives.*')
  438. ->orderBy('typ_semester_objectives.id', 'asc')
  439. ->get();
  440. $semesters_->selected_objectives = array();
  441. foreach ($result_objectives as $objectives_) {
  442. // $result_courses = DB::table('courses')
  443. // ->where('courses.program_id', $program_id)
  444. // ->select('courses.id as course_id', 'courses.name', 'courses.code', 'courses.number', 'courses.program_id')
  445. // ->orderBy('courses.number', 'asc')
  446. // ->groupBy('courses.name', 'courses.code')
  447. // ->get();
  448. $result_courses = DB::table('courses')
  449. ->where('courses.program_id', $program_id)
  450. ->orWhereIn('courses.program_id', function ($q) use ($program_id) {
  451. $q->select('other_program_id')
  452. ->from('other_programs_courses_typ')
  453. ->where('program_id', $program_id);
  454. })
  455. ->select('courses.id as course_id', 'courses.name', 'courses.code', 'courses.number', 'courses.program_id')
  456. ->orderBy('courses.code', 'asc')
  457. ->orderBy('courses.number', 'asc')
  458. ->groupBy('courses.name', 'courses.code')
  459. ->get();
  460. #los cursos resultantes creo que deberian ser: todos los que se estan dando en mi programa
  461. $objectives_->available_courses = array();
  462. foreach ($result_courses as $courses_) {
  463. array_push($objectives_->available_courses, $courses_);
  464. }
  465. // search for previusly selected courses
  466. $typ_semester_objective_id = $objectives_->typ_semester_objectives_id;
  467. $result_courses = DB::table('typ_semester_courses')
  468. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  469. ->where('typ_semester_courses.typ_semester_objective_id', $typ_semester_objective_id)
  470. ->select('courses.id as course_id', 'courses.*', 'typ_semester_courses.*')
  471. ->orderBy('courses.name', 'asc')
  472. ->get();
  473. $objectives_->selected_courses = array();
  474. foreach ($result_courses as $courses_) {
  475. array_push($objectives_->selected_courses, $courses_);
  476. }
  477. array_push($semesters_->selected_objectives, $objectives_);
  478. }
  479. array_push($outcome_->selected_semesters, $semesters_);
  480. }
  481. array_push($info, $outcome_);
  482. }
  483. }
  484. return $info;
  485. }
  486. // save or delete courses associated with objectives
  487. public function section3_on_change()
  488. {
  489. $typ_id = Input::get('typ_id');
  490. $outcome_id = Input::get('outcome_id');
  491. $semester_id = Input::get('semester_id');
  492. $objective_id = Input::get('objective_id');
  493. $previous_course_id = Input::get('previous_course_id');
  494. $new_course_id = Input::get('new_course_id');
  495. // get program_id
  496. $program_id = Input::get('program_id');
  497. // get typ_program_id
  498. $result = DB::table('typ_program')
  499. ->where('three_year_plan_id', $typ_id)
  500. ->where('program_id', $program_id)
  501. ->get();
  502. $typ_program_id = $result[0]->id;
  503. // get typ_semester_outcome_id
  504. $result = DB::table('typ_semester_outcome')
  505. ->where('typ_program_id', $typ_program_id)
  506. ->where('semester_id', $semester_id)
  507. ->where('outcome_id', $outcome_id)
  508. ->get();
  509. $typ_semester_outcome_id = $result[0]->id;
  510. // get typ_semester_objective_id
  511. $result = DB::table('typ_semester_objectives')
  512. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  513. ->where('objective_id', $objective_id)
  514. ->get();
  515. $typ_semester_objective_id = $result[0]->id;
  516. //delete old objective relation if it exists
  517. if ($previous_course_id != 'nothing_selected') {
  518. $result = DB::table('typ_semester_courses')
  519. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  520. ->where('course_id', $previous_course_id)
  521. ->get();
  522. if (count($result) != 0) {
  523. DB::table('typ_semester_courses')
  524. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  525. ->where('course_id', $previous_course_id)
  526. ->delete();
  527. }
  528. }
  529. //create new objective relation
  530. if ($new_course_id != 'nothing_selected') {
  531. $result = DB::table('typ_semester_courses')
  532. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  533. ->where('course_id', $new_course_id)
  534. ->get();
  535. if (count($result) == 0) {
  536. $_ = DB::table('typ_semester_courses')->insertGetId(
  537. array(
  538. 'typ_semester_objective_id' => $typ_semester_objective_id,
  539. 'course_id' => $new_course_id
  540. )
  541. );
  542. }
  543. }
  544. return 'update succes?';
  545. }
  546. public function verifyAndCreate($program_id)
  547. {
  548. $typ_id = Input::get("typ_id");
  549. $typ_program_id = DB::table('typ_program')
  550. ->where('three_year_plan_id', $typ_id)
  551. ->where('program_id', $program_id)
  552. ->first();
  553. $information_thats_missing = [];
  554. $objectives_missing = DB::table('typ_semester_outcome')
  555. ->leftJoin("typ_semester_objectives", 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
  556. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  557. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  558. ->where('typ_program_id', $typ_program_id->id)
  559. ->whereNull('typ_semester_objectives.id')
  560. ->select('semesters.id as semester_id', 'semesters.name as semester_name', 'semesters.code', 'outcomes.name as outcome_name', 'outcomes.id as outcome_id')
  561. ->get();
  562. if (count($objectives_missing) != 0) {
  563. $objective_array = [];
  564. foreach ($objectives_missing as $ob) {
  565. if (!isset($objective_array[$ob->semester_id])) {
  566. $objective_array[$ob->semester_id] = array(
  567. 'semester_name' => $ob->semester_name . ' (' . $ob->code . ') ',
  568. 'outcome_text' => [],
  569. );
  570. }
  571. $objective_array[$ob->semester_id]['outcome_text'][] = $ob->outcome_name;
  572. }
  573. $information_thats_missing['objectives_missing'] = $objective_array;
  574. }
  575. $courses_missing = DB::table('typ_semester_outcome')
  576. ->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
  577. ->leftJoin('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  578. ->join('objectives', 'typ_semester_objectives.objective_id', '=', 'objectives.id')
  579. ->join('outcomes', 'typ_semester_outcome.outcome_id', '=', 'outcomes.id')
  580. ->join('semesters', 'typ_semester_outcome.semester_id', '=', 'semesters.id')
  581. ->where('typ_program_id', $typ_program_id->id)
  582. ->whereNull('typ_semester_courses.id')
  583. ->select('objectives.id as objective_id', 'objectives.text as objective_text')
  584. ->addSelect('outcomes.id as outcome_id', 'outcomes.name as outcome_name')
  585. ->addSelect('semesters.id as semester_id', 'semesters.name as semester_name', 'semesters.code as semester_code')
  586. ->get();
  587. if (count($courses_missing) != 0) {
  588. $courses_array = [];
  589. foreach ($courses_missing as $co) {
  590. if (!isset($courses_array[$co->semester_id])) {
  591. $courses_array[$co->semester_id] = array(
  592. 'semester_name' => $co->semester_name . ' (' . $co->semester_code . ') ',
  593. 'outcomes' => []
  594. );
  595. }
  596. if (!isset($courses_array[$co->semester_id]['outcomes'][$co->outcome_id])) {
  597. $courses_array[$co->semester_id]['outcomes'][$co->outcome_id] = array(
  598. 'outcome_name' => $co->outcome_name,
  599. 'objectives' => []
  600. );
  601. }
  602. $courses_array[$co->semester_id]['outcomes'][$co->outcome_id]['objectives'][] = $co->objective_text;
  603. }
  604. $information_thats_missing['courses_missing'] = $courses_array;
  605. }
  606. if (count($information_thats_missing) != 0) {
  607. return $information_thats_missing;
  608. } else {
  609. $this->createAnnualPlan($program_id);
  610. }
  611. }
  612. public function createAnnualPlan($program_id)
  613. {
  614. $input_id = Input::get('typ_id');
  615. $current_typ = DB::select("select * from three_year_plan where id = {$input_id}")[0];
  616. $count = 0;
  617. $yearStartPlusOne = $current_typ->year_start + 1;
  618. $yearStart = $current_typ->year_start;
  619. //Log::info($yearStart);
  620. //Log::info($yearStartPlusOne);
  621. while ($count < 3) {
  622. $firstSemester = DB::select("select * from semesters where name like 'First Semester {$yearStart}-{$yearStartPlusOne}'");
  623. $secondSemester = DB::select("select * from semesters where name like 'Second Semester {$yearStart}-{$yearStartPlusOne}'");
  624. if (count($firstSemester) && count($secondSemester)) {
  625. Log::info($firstSemester);
  626. Log::info($secondSemester);
  627. $query = DB::select("select * from annual_cycle where semester_start ={$firstSemester[0]->id} and semester_end ={$secondSemester[0]->id} ");
  628. if (!count($query)) {
  629. DB::insert("insert into annual_cycle (academic_year, semester_start, semester_end) values ('{$yearStart}-{$yearStartPlusOne}', {$firstSemester[0]->id}, {$secondSemester[0]->id})");
  630. }
  631. $an_cycle = DB::table('annual_cycle')->where('semester_start', '=', $firstSemester[0]->id)
  632. ->where('semester_end', '=', $secondSemester[0]->id)
  633. ->first();
  634. $query = DB::table('annual_plans')->where("annual_cycle_id", '=', $an_cycle->id)
  635. ->where('program_id', '=', $program_id)->first();
  636. if (!$query) {
  637. DB::insert("insert into annual_plans (`annual_cycle_id`, `program_id`) values ({$an_cycle->id}, {$program_id})");
  638. }
  639. }
  640. $count++;
  641. $yearStart++;
  642. $yearStartPlusOne++;
  643. }
  644. $this->createThreeYearPlanPdf($program_id, $input_id);
  645. }
  646. public function createThreeYearPlanPdf($program_id, $typ)
  647. {
  648. $program = DB::table('programs')->where('id', $program_id)->first();
  649. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  650. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  651. ->where('program_id', $program->id)
  652. ->first()->id;
  653. $typ_semesters = DB::table('semesters')
  654. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  655. ->where('typ_id', $three_year_plan->id)
  656. ->orderBy('semester_id')
  657. ->get();
  658. //Log::info("Stop 2");
  659. foreach ($typ_semesters as $semester) {
  660. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  661. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  662. ->where('typ_program_id', $typ_program_id)
  663. ->where('semester_id', $semester->semester_id)
  664. ->select('typ_semester_outcome.id as typ_id')
  665. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  666. ->get();
  667. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  668. $outcome->objectives = DB::table('typ_semester_objectives')
  669. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  670. ->where('typ_semester_outcome_id', $outcome->typ_id)
  671. ->select('typ_semester_objectives.id as typ_obj_id')
  672. ->addSelect('objectives.text', 'objectives.id as objective_id')
  673. ->get();
  674. foreach ($outcome->objectives as $objective) {
  675. $objective->courses = DB::table('typ_semester_courses')
  676. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  677. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  678. ->get();
  679. }
  680. }
  681. }
  682. //html para salvar
  683. $submit = 1;
  684. $view = 0;
  685. //$pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');
  686. //$pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
  687. // ->setOrientation("landscape")
  688. // ->setPaper('legal', 'landscape');
  689. $path = app_path() . '/views/three_year_plan_htmls/' . date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.blade.php';
  690. $name = date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.blade.php';
  691. //is a path already there, if it is, leave the same path
  692. $it_exists = DB::table("paths_for_three_year_plans")
  693. ->where('path_to_pdf', $path)
  694. ->first();
  695. //Log::info("pero aqui?");
  696. if (isset($it_exists)) {
  697. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  698. }
  699. //else save the new path and update time
  700. DB::table("paths_for_three_year_plans")
  701. ->where('typ_program_id', $typ_program_id)
  702. ->where('user_id', Auth::user()->id)
  703. ->update(array(
  704. 'last' => 0
  705. ));
  706. DB::table('paths_for_three_year_plans')->insert(array(
  707. "path_to_pdf" => $path,
  708. 'typ_program_id' => $typ_program_id,
  709. 'last' => 1,
  710. 'user_id' => Auth::user()->id,
  711. 'date_posted' => date('Y-m-d')
  712. ));
  713. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  714. }
  715. public function printPlan($program_id, $typ, $submit = NULL)
  716. {
  717. $view = 0;
  718. $program = DB::table('programs')->where('id', $program_id)->first();
  719. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  720. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  721. ->where('program_id', $program->id)
  722. ->first()->id;
  723. $typ_semesters = DB::table('semesters')
  724. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  725. ->where('typ_id', $three_year_plan->id)
  726. ->orderBy('semester_id')
  727. ->get();
  728. foreach ($typ_semesters as $semester) {
  729. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  730. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  731. ->where('typ_program_id', $typ_program_id)
  732. ->where('semester_id', $semester->semester_id)
  733. ->select('typ_semester_outcome.id as typ_id')
  734. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  735. ->get();
  736. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  737. $outcome->objectives = DB::table('typ_semester_objectives')
  738. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  739. ->where('typ_semester_outcome_id', $outcome->typ_id)
  740. ->select('typ_semester_objectives.id as typ_obj_id')
  741. ->addSelect('objectives.text', 'objectives.id as objective_id')
  742. ->get();
  743. foreach ($outcome->objectives as $objective) {
  744. $objective->courses = DB::table('typ_semester_courses')
  745. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  746. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  747. ->get();
  748. }
  749. }
  750. }
  751. //Log::info($typ_semester_outcome);
  752. //TEMP
  753. //$app_path = app_path() . '/jsPDF-master/src/jspdf.js';
  754. //$submit = 1;
  755. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  756. }
  757. public function viewPlan($program_id, $typ)
  758. {
  759. $view = 1;
  760. $program = DB::table('programs')->where('id', $program_id)->first();
  761. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  762. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  763. ->where('program_id', $program->id)
  764. ->first()->id;
  765. $typ_semesters = DB::table('semesters')
  766. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  767. ->where('typ_id', $three_year_plan->id)
  768. ->orderBy('semester_id')
  769. ->get();
  770. foreach ($typ_semesters as $semester) {
  771. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  772. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  773. ->where('typ_program_id', $typ_program_id)
  774. ->where('semester_id', $semester->semester_id)
  775. ->select('typ_semester_outcome.id as typ_id')
  776. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  777. ->get();
  778. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  779. $outcome->objectives = DB::table('typ_semester_objectives')
  780. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  781. ->where('typ_semester_outcome_id', $outcome->typ_id)
  782. ->select('typ_semester_objectives.id as typ_obj_id')
  783. ->addSelect('objectives.text', 'objectives.id as objective_id')
  784. ->get();
  785. foreach ($outcome->objectives as $objective) {
  786. $objective->courses = DB::table('typ_semester_courses')
  787. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  788. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  789. ->get();
  790. }
  791. }
  792. }
  793. Log::info($typ_semester_outcome);
  794. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view'));
  795. }
  796. public function showPDFs($program_id)
  797. {
  798. $role = Auth::user()->role;
  799. switch ($role) {
  800. case 1:
  801. case 2:
  802. case 3:
  803. $last = [1, 0];
  804. break;
  805. case 4:
  806. $last = [1];
  807. break;
  808. default:
  809. App::abort('404');
  810. }
  811. $title = "Three Year Plans for " . Program::findOrFail($program_id)->name;
  812. $paths_with_users = DB::table('paths_for_three_year_plans')
  813. ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
  814. ->join('three_year_plan', 'typ_program.three_year_plan_id', '=', 'three_year_plan.id')
  815. ->join('users', 'users.id', '=', 'paths_for_three_year_plans.user_id')
  816. ->whereIn('last', $last)
  817. ->where('typ_program.program_id', $program_id)
  818. ->orderBy('date_posted', 'desc')
  819. ->select('paths_for_three_year_plans.path_to_pdf', 'users.*', 'date_posted', 'three_year_plan.*', 'paths_for_three_year_plans.last', 'paths_for_three_year_plans.id as path_id')
  820. ->get();
  821. return View::make('local.managers.shared.new_view_three_year_plans', compact('paths_with_users', 'title', 'last', 'program_id'));
  822. }
  823. /*public function downloadPDF($download, $path_id)
  824. {
  825. $pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');
  826. $queryToPath = DB::table('paths_for_three_year_plans')
  827. ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
  828. ->where('paths_for_three_year_plans.id', $path_id)
  829. ->first();
  830. //Log::info("ERES TU? creo que no");
  831. // $annualPlan = AnnualPlan::findOrFail($queryToPath->annual_plan_id);
  832. $program = Program::findOrFail($queryToPath->program_id);
  833. $typ_program_id = $queryToPath->typ_program_id;
  834. $three_year_plan = DB::table('three_year_plan')
  835. ->where('id', $queryToPath->three_year_plan_id)
  836. ->first();
  837. $typ_semesters = DB::table('semesters')
  838. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  839. ->where('typ_id', $queryToPath->three_year_plan_id)
  840. ->orderBy('semester_id')
  841. ->get();
  842. foreach ($typ_semesters as $semester) {
  843. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  844. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  845. ->where('typ_program_id', $typ_program_id)
  846. ->where('semester_id', $semester->semester_id)
  847. ->select('typ_semester_outcome.id as typ_id')
  848. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  849. ->get();
  850. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  851. $outcome->objectives = DB::table('typ_semester_objectives')
  852. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  853. ->where('typ_semester_outcome_id', $outcome->typ_id)
  854. ->select('typ_semester_objectives.id as typ_obj_id')
  855. ->addSelect('objectives.text', 'objectives.id as objective_id')
  856. ->get();
  857. foreach ($outcome->objectives as $objective) {
  858. $objective->courses = DB::table('typ_semester_courses')
  859. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  860. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  861. ->get();
  862. }
  863. }
  864. }
  865. //Log::info("ERES TU?");
  866. $pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
  867. ->setOrientation("landscape")
  868. ->setPaper('legal', 'landscape');
  869. if ($download == "download")
  870. return $pdf->download(basename($queryToPath->path_to_pdf));
  871. else
  872. return $pdf->stream(basename($queryToPath->path_to_pdf));
  873. }
  874. */
  875. ///////////////////
  876. //
  877. //Pair everything
  878. //
  879. ////////////////////
  880. public function pairEveryObjective()
  881. {
  882. Log::info(Input::all());
  883. $selected_objectives = Input::get('selected_objectives');
  884. $typ_id = Input::get('typ_id');
  885. $program_id = Input::get('program_id');
  886. for ($i = 0; $i < count($selected_objectives); $i += 3) {
  887. Input::replace([
  888. 'program_id' => $program_id,
  889. 'new_objective_id' => $selected_objectives[$i + 2],
  890. 'outcome_id' => $selected_objectives[$i],
  891. 'semester_id' => $selected_objectives[$i + 1],
  892. 'typ_id' => $typ_id,
  893. 'previous_objective_id' => 'nothing_selected'
  894. ]);
  895. if ($this->section2_on_change() != 'update was succes')
  896. return 0;
  897. }
  898. return 200;
  899. }
  900. public function pairEveryCourse()
  901. {
  902. Log::info(Input::all());
  903. $typ_id = Input::get('typ_id');
  904. $program_id = Input::get('program_id');
  905. $pairing_array = Input::get('pairing_array');
  906. $type_of_work = Input::get("type_of_work");
  907. //$typ_id = Input::get('typ_id');
  908. //$outcome_id = Input::get('outcome_id');
  909. //$semester_id = Input::get('semester_id');
  910. //$objective_id = Input::get('objective_id');
  911. //$previous_course_id = Input::get('previous_course_id');
  912. //$new_course_id = Input::get('new_course_id');
  913. // get program_id
  914. //$program_id = Input::get('program_id');
  915. if ($type_of_work == 'este_semestre') {
  916. foreach ($pairing_array as $course_array) {
  917. Input::replace([
  918. 'outcome_id' => $course_array[0],
  919. 'semester_id' => $course_array[1],
  920. 'objective_id' => $course_array[2],
  921. 'new_course_id' => $course_array[3],
  922. 'typ_id' => $typ_id,
  923. 'previous_course_id' => 'nothing_selected',
  924. 'program_id' => $program_id,
  925. ]);
  926. if ($this->section3_on_change() != 'update succes?')
  927. return 0;
  928. }
  929. } else {
  930. foreach ($pairing_array as $semester_array) {
  931. foreach ($semester_array as $course_array) {
  932. Input::replace([
  933. 'outcome_id' => $course_array[0],
  934. 'semester_id' => $course_array[1],
  935. 'objective_id' => $course_array[2],
  936. 'new_course_id' => $course_array[3],
  937. 'typ_id' => $typ_id,
  938. 'previous_course_id' => 'nothing_selected',
  939. 'program_id' => $program_id,
  940. ]);
  941. if ($this->section3_on_change() != 'update succes?')
  942. return 0;
  943. }
  944. }
  945. }
  946. return 200;
  947. }
  948. }