Nenhuma descrição

ThreeYearPlanController.php 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. use Illuminate\Database\Eloquent\Collection;
  3. use Barryvdh\DomPDF\PDF as PDF;
  4. class ThreeYearPlanController extends \BaseController
  5. {
  6. public function create()
  7. {
  8. try {
  9. DB::beginTransaction();
  10. $yearStart = Input::get('years') + 0;
  11. $yearEnd = $yearStart + 3;
  12. $program_id = Input::get('program_id');
  13. DB::insert("insert into three_year_plan (`year_start`, `year_end`) values ({$yearStart}, {$yearEnd})");
  14. $typ = DB::table('three_year_plan')->where('year_start', '=', $yearStart)
  15. ->where('year_end', '=', $yearEnd)
  16. ->first();
  17. for ($i = $yearStart; $i < $yearEnd; $i++) {
  18. $string = 'First Semester ' . $i . '-' . ($i + 1);
  19. $string2 = 'Second Semester ' . $i . '-' . ($i + 1);
  20. $Firstsemester = DB::table('semesters')->where('name', '=', $string)->first();
  21. $secondSemester = DB::table('semesters')->where('name', '=', $string2)->first();
  22. Log::info($Firstsemester->id);
  23. Log::info($secondSemester->id);
  24. Log::info($typ->id);
  25. DB::insert("insert into typ_semesters (typ_id, semester_id) values ({$typ->id}, {$Firstsemester->id}), ({$typ->id}, {$secondSemester->id})");
  26. }
  27. DB::commit();
  28. Session::flash('status', 'success');
  29. Session::flash('message', '<p> Cycle created.</p>');
  30. return Redirect::to("three-years-plan/{$program_id}");
  31. } catch (Exception $e) {
  32. Session::flash('status', 'danger');
  33. Session::flash('message', "<p>Could not create cycle</p>");
  34. Log::info($e);
  35. DB::rollback();
  36. return Redirect::to("three-years-plan/{$program_id}");
  37. }
  38. }
  39. // load the Three year Plan page
  40. public function threeYearsReport($program_id)
  41. {
  42. $user_id = Auth::user()->id;
  43. $last_year = DB::table('three_year_plan')->select('year_end')
  44. ->orderBy('year_end', 'desc')->first();
  45. if (!$last_year) {
  46. // $last_year = date('Y');
  47. $last_year = 2019;
  48. } else {
  49. $last_year = $last_year->year_end;
  50. }
  51. $program = Program::find($program_id);
  52. Log::info($program);
  53. // el ID de los semestres que el usuario tiene seleccionado.
  54. $semesters_ids = Session::get('semesters_ids');
  55. // buscar informacion de los semestres seleccionados
  56. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  57. $title = "Three Year Plan for " . $program->name;
  58. $typs = DB::table('three_year_plan')
  59. ->orderBy('year_start', 'asc')
  60. ->get();
  61. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  62. /* $program_id = DB::table('program_user')
  63. ->where('user_id', $user_id)
  64. ->select('program_id')
  65. ->get();
  66. $program_id = $program_id[0]->program_id;*/
  67. if ($program->is_graduate == 0) {
  68. $outcomes = DB::table('outcomes')->where('deactivation_date', '=', null)->orderBy('name', 'ASC')->get();
  69. } else {
  70. $outcomes = DB::table('outcomes')->where('deactivation_date', '=', null)->where('level', '=', 3)->orderBy('name', 'ASC')->get();
  71. }
  72. // se annadio la nueva variable
  73. return View::make('global.view-three-year-plan', compact('title', 'last_year', 'outcomes', 'typs', 'criteria', 'semesters', 'program_id'));
  74. }
  75. //when the user select a three year period from the left column, load the corresponding info
  76. public function fetchThreeYears()
  77. {
  78. $id = Input::get('id');
  79. $semesters = DB::table('typ_semesters')
  80. ->join('semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  81. ->where('typ_semesters.typ_id', $id)
  82. ->orderBy('typ_semesters.semester_id', 'asc')
  83. ->get();
  84. $typs = DB::table('three_year_plan')
  85. ->where('id', $id)
  86. ->orderBy('year_start', 'asc')
  87. ->get();
  88. $typs = $typs[0];
  89. $typs->semesters = $semesters;
  90. return array(
  91. 'typ' => $typs,
  92. );
  93. }
  94. // add or remove a semester associated with an outcome when a user selects a checkbox
  95. public function update_typ_outcomes_semesters()
  96. {
  97. $outcomeSemesterArray = json_decode(Input::get('outcomeSemesterArray'), true);
  98. $typ_id = Input::get('typ_id');
  99. $user_id = auth::user()->id;
  100. $program_id = Input::get('program_id');
  101. $result = DB::table('typ_program')
  102. ->where('three_year_plan_id', $typ_id)
  103. ->where('program_id', $program_id)
  104. ->get();
  105. // create the relation if it doesnt exist
  106. if (count($result) == 0) {
  107. $typ_program_id = DB::table('typ_program')->insertGetId(
  108. array(
  109. 'three_year_plan_id' => $typ_id,
  110. 'program_id' => $program_id
  111. )
  112. );
  113. } else {
  114. $typ_program_id = $result[0]->id;
  115. }
  116. //go through array
  117. foreach ($outcomeSemesterArray as $outcome_semester) {
  118. // foreach of the 6 semesters
  119. foreach ($outcome_semester["semesters"] as $semester) {
  120. $insert = $semester[1];
  121. $outcome_id = explode("-sem", $semester[0])[0];
  122. $semester_code = explode("-sem", $semester[0])[1];
  123. $result = DB::table('semesters')
  124. ->where('code', $semester_code)
  125. ->select('id')
  126. ->get();
  127. if (count($result) == 0) {
  128. continue;
  129. }
  130. $semester_id = $result[0]->id;
  131. // verify if it exists
  132. $result = DB::table('typ_semester_outcome')
  133. ->where('typ_program_id', $typ_program_id)
  134. ->where('semester_id', $semester_id)
  135. ->where('outcome_id', $outcome_id)
  136. ->get();
  137. // if true, it should get inserted
  138. if ($insert == true) {
  139. // if == 0, then insert
  140. if (count($result) == 0) {
  141. $_ = DB::table('typ_semester_outcome')->insertGetId(
  142. array(
  143. 'typ_program_id' => $typ_program_id,
  144. 'semester_id' => $semester_id,
  145. 'outcome_id' => $outcome_id
  146. )
  147. );
  148. }
  149. //if exists, then do nothing
  150. }
  151. // if false, it should get deleted
  152. else { //value == false
  153. // if == 0, it doesnt exist. we do nothing
  154. if (count($result) == 0) {
  155. //pass
  156. }
  157. //if exists, then do nothing
  158. else {
  159. DB::table('typ_semester_outcome')
  160. ->where('typ_program_id', $typ_program_id)
  161. ->where('semester_id', $semester_id)
  162. ->where('outcome_id', $outcome_id)
  163. ->delete();
  164. }
  165. }
  166. }
  167. }
  168. return;
  169. }
  170. // load semesters asociated with the outcomes if previously selected
  171. public function lookup_typ_semester_outcome()
  172. {
  173. $box_value = array();
  174. $info = Input::get('info');
  175. foreach ($info as $values) {
  176. $outcome_id = $values[0];
  177. $semester_code = $values[1];
  178. $program_id = $values[2];
  179. $box_id = $values[3];
  180. $typ_id = $values[4];
  181. //buscar id del typ
  182. $result = DB::table('typ_program')
  183. ->where('three_year_plan_id', $typ_id)
  184. ->where('program_id', $program_id)
  185. ->get();
  186. $typ_program_id = $result[0]->id;
  187. // buscar si existe el outcome_semester
  188. $result = DB::table('typ_semester_outcome')
  189. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  190. ->where('typ_program_id', $typ_program_id)
  191. ->where('semesters.code', $semester_code)
  192. ->where('outcome_id', $outcome_id)
  193. ->get();
  194. // if it doesnt exist
  195. if (count($result) == 0) {
  196. array_push($box_value, array($box_id, 0));
  197. } else {
  198. array_push($box_value, array($box_id, 1));
  199. }
  200. }
  201. return array(
  202. 'box_value' => ($box_value)
  203. );
  204. }
  205. // save or delete Objectives associated with the outcomes and semsters
  206. public function section2_on_change()
  207. {
  208. $typ_id = Input::get('typ_id');
  209. $outcome_id = Input::get('outcome_id');
  210. $semester_id = Input::get('semester_id');
  211. $previous_objective_id = Input::get('previous_objective_id');
  212. $new_objective_id = Input::get('new_objective_id');
  213. $program_id = Input::get('program_id');
  214. // get program_id
  215. $user_id = auth::user()->id;
  216. Log::info(Input::all());
  217. // get typ_program_id
  218. $result = DB::table('typ_program')
  219. ->where('three_year_plan_id', $typ_id)
  220. ->where('program_id', $program_id)
  221. ->get();
  222. $typ_program_id = $result[0]->id;
  223. // get typ_semester_outcome_id
  224. $result = DB::table('typ_semester_outcome')
  225. ->where('typ_program_id', $typ_program_id)
  226. ->where('semester_id', $semester_id)
  227. ->where('outcome_id', $outcome_id)
  228. ->get();
  229. $typ_semester_outcome_id = $result[0]->id;
  230. //delete old objective relation if it exists
  231. if ($previous_objective_id != 'nothing_selected') {
  232. $result = DB::table('typ_semester_objectives')
  233. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  234. ->where('objective_id', $previous_objective_id)
  235. ->get();
  236. if (count($result) != 0) {
  237. DB::table('typ_semester_objectives')
  238. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  239. ->where('objective_id', $previous_objective_id)
  240. ->delete();
  241. }
  242. }
  243. //create new objective relation
  244. if ($new_objective_id != 'nothing_selected') {
  245. $result = DB::table('typ_semester_objectives')
  246. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  247. ->where('objective_id', $new_objective_id)
  248. ->get();
  249. if (count($result) == 0) {
  250. $_ = DB::table('typ_semester_objectives')->insertGetId(
  251. array(
  252. 'typ_semester_outcome_id' => $typ_semester_outcome_id,
  253. 'objective_id' => $new_objective_id
  254. )
  255. );
  256. }
  257. }
  258. return 'update was succes';
  259. }
  260. // load the corresponding info after associating semesters with outcomes
  261. public function section2_arrive()
  262. {
  263. // get program_id
  264. $user_id = auth::user()->id;
  265. $program_id = Input::get('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. ->orderBy('semesters.code')
  299. ->get();
  300. // buscar info sobre selected_semsters
  301. $outcome_->selected_semesters = array();
  302. foreach ($result_semesters as $semesters_) {
  303. $typ_semester_outcome_id = $semesters_->id;
  304. // lookup previusly selected objectives
  305. $result_objectives = DB::table('typ_semester_objectives')
  306. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  307. ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
  308. ->orderBy('typ_semester_objectives.id', 'asc')
  309. ->get();
  310. $semesters_->selected_objectives = array();
  311. foreach ($result_objectives as $objectives_) {
  312. array_push($semesters_->selected_objectives, $objectives_);
  313. }
  314. // lookup available objectives
  315. $result_objectives = DB::table('objective_outcome')
  316. ->join('outcomes', 'outcomes.id', '=', 'objective_outcome.outcome_id')
  317. ->join('objectives', 'objectives.id', '=', 'objective_outcome.objective_id')
  318. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  319. ->where('objective_program.program_id', $program_id)
  320. ->where('objective_outcome.outcome_id', $outcome_id)
  321. ->where('objective_outcome.objective_id', "<>", 0)
  322. ->select('objectives.id', 'objectives.text', 'outcomes.name as outcome_name')
  323. ->orderBy('objectives.text', 'asc')
  324. ->get();
  325. $semesters_->available_objectives = array();
  326. foreach ($result_objectives as $objectives_) {
  327. array_push($semesters_->available_objectives, $objectives_);
  328. }
  329. array_push($outcome_->selected_semesters, $semesters_);
  330. }
  331. array_push($info, $outcome_);
  332. }
  333. }
  334. return $info;
  335. }
  336. // load the corresponding info after associating objectives to semesters outcomes
  337. public function section3_arrive()
  338. {
  339. // get program_id
  340. $user_id = auth::user()->id;
  341. $program_id = Input::get('program_id');
  342. // get typ_program_id
  343. $typ_id = Input::get('typ_id');
  344. $result = DB::table('typ_program')
  345. ->where('three_year_plan_id', $typ_id)
  346. ->where('program_id', $program_id)
  347. ->get();
  348. $typ_program_id = $result[0]->id;
  349. //search selected outcomes,semesters and get their objectives
  350. $selected_outcomes = DB::table('typ_semester_outcome')
  351. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  352. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  353. ->distinct('typ_semester_outcome.outcome_id')
  354. ->select('typ_semester_outcome.outcome_id')
  355. ->orderBy('outcomes.name', 'asc')
  356. ->get();
  357. $selected_objectives_array = array();
  358. $outcomes_info_array = array();
  359. // $objectives_info_array = array();
  360. $semesters_array = array();
  361. $info = array();
  362. foreach ($selected_outcomes as $outcome) {
  363. $outcome_id = $outcome->outcome_id;
  364. $result_outcomes = DB::table('outcomes')
  365. ->where('outcomes.id', $outcome_id)
  366. ->get();
  367. foreach ($result_outcomes as $outcome_) {
  368. $result_semesters = DB::table('typ_semester_outcome')
  369. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  370. ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
  371. ->where('typ_semester_outcome.outcome_id', $outcome_->id)
  372. ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
  373. ->orderBy('semesters.code')
  374. ->get();
  375. // buscar info sobre selected_semsters
  376. $outcome_->selected_semesters = array();
  377. foreach ($result_semesters as $semesters_) {
  378. $typ_semester_outcome_id = $semesters_->id;
  379. $result_objectives = DB::table('typ_semester_objectives')
  380. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  381. ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
  382. ->select('typ_semester_objectives.id as typ_semester_objectives_id', 'objectives.*', 'typ_semester_objectives.*')
  383. ->orderBy('typ_semester_objectives.id', 'asc')
  384. ->get();
  385. $semesters_->selected_objectives = array();
  386. foreach ($result_objectives as $objectives_) {
  387. // $result_courses = DB::table('courses')
  388. // ->where('courses.program_id', $program_id)
  389. // ->select('courses.id as course_id', 'courses.name', 'courses.code', 'courses.number', 'courses.program_id')
  390. // ->orderBy('courses.number', 'asc')
  391. // ->groupBy('courses.name', 'courses.code')
  392. // ->get();
  393. $result_courses = DB::table('courses')
  394. ->where('courses.program_id', $program_id)
  395. ->orWhereIn('courses.program_id', function ($q) use ($program_id) {
  396. $q->select('other_program_id')
  397. ->from('other_programs_courses_typ')
  398. ->where('program_id', $program_id);
  399. })
  400. ->select('courses.id as course_id', 'courses.name', 'courses.code', 'courses.number', 'courses.program_id')
  401. ->orderBy('courses.code', 'asc')
  402. ->orderBy('courses.number', 'asc')
  403. ->groupBy('courses.name', 'courses.code')
  404. ->get();
  405. #los cursos resultantes creo que deberian ser: todos los que se estan dando en mi programa
  406. $objectives_->available_courses = array();
  407. foreach ($result_courses as $courses_) {
  408. array_push($objectives_->available_courses, $courses_);
  409. }
  410. // search for previusly selected courses
  411. $typ_semester_objective_id = $objectives_->typ_semester_objectives_id;
  412. $result_courses = DB::table('typ_semester_courses')
  413. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  414. ->where('typ_semester_courses.typ_semester_objective_id', $typ_semester_objective_id)
  415. ->select('courses.id as course_id', 'courses.*', 'typ_semester_courses.*')
  416. ->orderBy('courses.name', 'asc')
  417. ->get();
  418. $objectives_->selected_courses = array();
  419. foreach ($result_courses as $courses_) {
  420. array_push($objectives_->selected_courses, $courses_);
  421. }
  422. array_push($semesters_->selected_objectives, $objectives_);
  423. }
  424. array_push($outcome_->selected_semesters, $semesters_);
  425. }
  426. array_push($info, $outcome_);
  427. }
  428. }
  429. return $info;
  430. }
  431. // save or delete courses associated with objectives
  432. public function section3_on_change()
  433. {
  434. $typ_id = Input::get('typ_id');
  435. $outcome_id = Input::get('outcome_id');
  436. $semester_id = Input::get('semester_id');
  437. $objective_id = Input::get('objective_id');
  438. $previous_course_id = Input::get('previous_course_id');
  439. $new_course_id = Input::get('new_course_id');
  440. // get program_id
  441. $program_id = Input::get('program_id');
  442. // get typ_program_id
  443. $result = DB::table('typ_program')
  444. ->where('three_year_plan_id', $typ_id)
  445. ->where('program_id', $program_id)
  446. ->get();
  447. $typ_program_id = $result[0]->id;
  448. // get typ_semester_outcome_id
  449. $result = DB::table('typ_semester_outcome')
  450. ->where('typ_program_id', $typ_program_id)
  451. ->where('semester_id', $semester_id)
  452. ->where('outcome_id', $outcome_id)
  453. ->get();
  454. $typ_semester_outcome_id = $result[0]->id;
  455. // get typ_semester_objective_id
  456. $result = DB::table('typ_semester_objectives')
  457. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  458. ->where('objective_id', $objective_id)
  459. ->get();
  460. $typ_semester_objective_id = $result[0]->id;
  461. //delete old objective relation if it exists
  462. if ($previous_course_id != 'nothing_selected') {
  463. $result = DB::table('typ_semester_courses')
  464. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  465. ->where('course_id', $previous_course_id)
  466. ->get();
  467. if (count($result) != 0) {
  468. DB::table('typ_semester_courses')
  469. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  470. ->where('course_id', $previous_course_id)
  471. ->delete();
  472. }
  473. }
  474. //create new objective relation
  475. if ($new_course_id != 'nothing_selected') {
  476. $result = DB::table('typ_semester_courses')
  477. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  478. ->where('course_id', $new_course_id)
  479. ->get();
  480. if (count($result) == 0) {
  481. $_ = DB::table('typ_semester_courses')->insertGetId(
  482. array(
  483. 'typ_semester_objective_id' => $typ_semester_objective_id,
  484. 'course_id' => $new_course_id
  485. )
  486. );
  487. }
  488. }
  489. return 'update succes?';
  490. }
  491. public function createAnnualPlan($program_id)
  492. {
  493. $input_id = Input::get('typ_id');
  494. $current_typ = DB::select("select * from three_year_plan where id = {$input_id}")[0];
  495. $count = 0;
  496. $yearStartPlusOne = $current_typ->year_start + 1;
  497. $yearStart = $current_typ->year_start;
  498. Log::info($yearStart);
  499. Log::info($yearStartPlusOne);
  500. while ($count < 3) {
  501. $firstSemester = DB::select("select * from semesters where name like 'First Semester {$yearStart}-{$yearStartPlusOne}'");
  502. $secondSemester = DB::select("select * from semesters where name like 'Second Semester {$yearStart}-{$yearStartPlusOne}'");
  503. if (count($firstSemester) && count($secondSemester)) {
  504. Log::info($firstSemester);
  505. Log::info($secondSemester);
  506. $query = DB::select("select * from annual_cycle where semester_start ={$firstSemester[0]->id} and semester_end ={$secondSemester[0]->id} ");
  507. if (!count($query)) {
  508. DB::insert("insert into annual_cycle (academic_year, semester_start, semester_end) values ('{$yearStart}-{$yearStartPlusOne}', {$firstSemester[0]->id}, {$secondSemester[0]->id})");
  509. }
  510. $an_cycle = DB::table('annual_cycle')->where('semester_start', '=', $firstSemester[0]->id)
  511. ->where('semester_end', '=', $secondSemester[0]->id)
  512. ->first();
  513. $query = DB::table('annual_plans')->where("annual_cycle_id", '=', $an_cycle->id)
  514. ->where('program_id', '=', $program_id)->first();
  515. if (!$query) {
  516. DB::insert("insert into annual_plans (`annual_cycle_id`, `program_id`) values ({$an_cycle->id}, {$program_id})");
  517. }
  518. }
  519. $count++;
  520. $yearStart++;
  521. $yearStartPlusOne++;
  522. }
  523. $this->createThreeYearPlanPdf($program_id, $input_id);
  524. }
  525. public function createThreeYearPlanPdf($program_id, $typ)
  526. {
  527. $program = DB::table('programs')->where('id', $program_id)->first();
  528. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  529. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  530. ->where('program_id', $program->id)
  531. ->first()->id;
  532. $typ_semesters = DB::table('semesters')
  533. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  534. ->where('typ_id', $three_year_plan->id)
  535. ->orderBy('semester_id')
  536. ->get();
  537. foreach ($typ_semesters as $semester) {
  538. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  539. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  540. ->where('typ_program_id', $typ_program_id)
  541. ->where('semester_id', $semester->semester_id)
  542. ->select('typ_semester_outcome.id as typ_id')
  543. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  544. ->get();
  545. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  546. $outcome->objectives = DB::table('typ_semester_objectives')
  547. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  548. ->where('typ_semester_outcome_id', $outcome->typ_id)
  549. ->select('typ_semester_objectives.id as typ_obj_id')
  550. ->addSelect('objectives.text', 'objectives.id as objective_id')
  551. ->get();
  552. foreach ($outcome->objectives as $objective) {
  553. $objective->courses = DB::table('typ_semester_courses')
  554. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  555. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  556. ->get();
  557. }
  558. }
  559. }
  560. $pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');
  561. $pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
  562. ->setOrientation("landscape")
  563. ->setPaper('legal', 'landscape');
  564. $path = app_path() . '/storage/three_year_plan_pdfs/' . date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.pdf';
  565. $pdf->save($path);
  566. $name = date('d-m-Y') . '-for-' . $program->id . '.pdf';
  567. $pdf->download($name);
  568. //is a path already there
  569. $it_exists = DB::table("paths_for_three_year_plans")
  570. ->where('path_to_pdf', $path)
  571. ->first();
  572. if (isset($it_exists)) {
  573. return '200';
  574. }
  575. //$user_id = Auth::user()->id;
  576. DB::table("paths_for_three_year_plans")
  577. ->where('typ_program_id', $typ_program_id)
  578. ->where('user_id', Auth::user()->id)
  579. ->update(array(
  580. 'last' => 0
  581. ));
  582. DB::table('paths_for_three_year_plans')->insert(array(
  583. "path_to_pdf" => $path,
  584. 'typ_program_id' => $typ_program_id,
  585. 'last' => 1,
  586. 'user_id' => Auth::user()->id,
  587. 'date_posted' => date('Y-m-d')
  588. ));
  589. }
  590. public function printPlan($program_id, $typ)
  591. {
  592. $view = 0;
  593. $program = DB::table('programs')->where('id', $program_id)->first();
  594. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  595. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  596. ->where('program_id', $program->id)
  597. ->first()->id;
  598. $typ_semesters = DB::table('semesters')
  599. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  600. ->where('typ_id', $three_year_plan->id)
  601. ->orderBy('semester_id')
  602. ->get();
  603. foreach ($typ_semesters as $semester) {
  604. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  605. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  606. ->where('typ_program_id', $typ_program_id)
  607. ->where('semester_id', $semester->semester_id)
  608. ->select('typ_semester_outcome.id as typ_id')
  609. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  610. ->get();
  611. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  612. $outcome->objectives = DB::table('typ_semester_objectives')
  613. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  614. ->where('typ_semester_outcome_id', $outcome->typ_id)
  615. ->select('typ_semester_objectives.id as typ_obj_id')
  616. ->addSelect('objectives.text', 'objectives.id as objective_id')
  617. ->get();
  618. foreach ($outcome->objectives as $objective) {
  619. $objective->courses = DB::table('typ_semester_courses')
  620. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  621. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  622. ->get();
  623. }
  624. }
  625. }
  626. //Log::info($typ_semester_outcome);
  627. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view'));
  628. }
  629. public function viewPlan($program_id, $typ)
  630. {
  631. $view = 1;
  632. $program = DB::table('programs')->where('id', $program_id)->first();
  633. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  634. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  635. ->where('program_id', $program->id)
  636. ->first()->id;
  637. $typ_semesters = DB::table('semesters')
  638. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  639. ->where('typ_id', $three_year_plan->id)
  640. ->orderBy('semester_id')
  641. ->get();
  642. foreach ($typ_semesters as $semester) {
  643. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  644. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  645. ->where('typ_program_id', $typ_program_id)
  646. ->where('semester_id', $semester->semester_id)
  647. ->select('typ_semester_outcome.id as typ_id')
  648. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  649. ->get();
  650. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  651. $outcome->objectives = DB::table('typ_semester_objectives')
  652. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  653. ->where('typ_semester_outcome_id', $outcome->typ_id)
  654. ->select('typ_semester_objectives.id as typ_obj_id')
  655. ->addSelect('objectives.text', 'objectives.id as objective_id')
  656. ->get();
  657. foreach ($outcome->objectives as $objective) {
  658. $objective->courses = DB::table('typ_semester_courses')
  659. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  660. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  661. ->get();
  662. }
  663. }
  664. }
  665. Log::info($typ_semester_outcome);
  666. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view'));
  667. }
  668. public function showPDFs($program_id)
  669. {
  670. $role = Auth::user()->role;
  671. switch ($role) {
  672. case 1:
  673. case 2:
  674. case 3:
  675. $last = [1, 0];
  676. break;
  677. case 4:
  678. $last = [1];
  679. break;
  680. default:
  681. App::abort('404');
  682. }
  683. $title = "Three Year Plans for " . Program::findOrFail($program_id)->name;
  684. $paths_with_users = DB::table('paths_for_three_year_plans')
  685. ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
  686. ->join('three_year_plan', 'typ_program.three_year_plan_id', '=', 'three_year_plan.id')
  687. ->join('users', 'users.id', '=', 'paths_for_three_year_plans.user_id')
  688. ->whereIn('last', $last)
  689. ->where('typ_program.program_id', $program_id)
  690. ->orderBy('date_posted', 'desc')
  691. ->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')
  692. ->get();
  693. return View::make('local.managers.shared.new_view_three_year_plans', compact('paths_with_users', 'title', 'last', 'program_id'));
  694. }
  695. public function downloadPDF($download, $path_id)
  696. {
  697. $pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');
  698. $queryToPath = DB::table('paths_for_three_year_plans')
  699. ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
  700. ->where('paths_for_three_year_plans.id', $path_id)
  701. ->first();
  702. //Log::info("ERES TU? creo que no");
  703. // $annualPlan = AnnualPlan::findOrFail($queryToPath->annual_plan_id);
  704. $program = Program::findOrFail($queryToPath->program_id);
  705. $typ_program_id = $queryToPath->typ_program_id;
  706. $three_year_plan = DB::table('three_year_plan')
  707. ->where('id', $queryToPath->three_year_plan_id)
  708. ->first();
  709. $typ_semesters = DB::table('semesters')
  710. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  711. ->where('typ_id', $queryToPath->three_year_plan_id)
  712. ->orderBy('semester_id')
  713. ->get();
  714. foreach ($typ_semesters as $semester) {
  715. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  716. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  717. ->where('typ_program_id', $typ_program_id)
  718. ->where('semester_id', $semester->semester_id)
  719. ->select('typ_semester_outcome.id as typ_id')
  720. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  721. ->get();
  722. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  723. $outcome->objectives = DB::table('typ_semester_objectives')
  724. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  725. ->where('typ_semester_outcome_id', $outcome->typ_id)
  726. ->select('typ_semester_objectives.id as typ_obj_id')
  727. ->addSelect('objectives.text', 'objectives.id as objective_id')
  728. ->get();
  729. foreach ($outcome->objectives as $objective) {
  730. $objective->courses = DB::table('typ_semester_courses')
  731. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  732. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  733. ->get();
  734. }
  735. }
  736. }
  737. //Log::info("ERES TU?");
  738. $pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
  739. ->setOrientation("landscape")
  740. ->setPaper('legal', 'landscape');
  741. if ($download == "download")
  742. return $pdf->download(basename($queryToPath->path_to_pdf));
  743. else
  744. return $pdf->stream(basename($queryToPath->path_to_pdf));
  745. }
  746. }