Нет описания

ThreeYearPlanController.php 33KB

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