Bez popisu

ThreeYearPlanController.php 34KB

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