Nenhuma descrição

ThreeYearPlanController.php 35KB

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