Nessuna descrizione

ThreeYearPlanController.php 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. ->groupBy('courses.code','courses.number')
  460. ->get();
  461. #los cursos resultantes creo que deberian ser: todos los que se estan dando en mi programa
  462. $objectives_->available_courses = array();
  463. foreach ($result_courses as $courses_) {
  464. array_push($objectives_->available_courses, $courses_);
  465. }
  466. // search for previusly selected courses
  467. $typ_semester_objective_id = $objectives_->typ_semester_objectives_id;
  468. $result_courses = DB::table('typ_semester_courses')
  469. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  470. ->where('typ_semester_courses.typ_semester_objective_id', $typ_semester_objective_id)
  471. ->select('courses.id as course_id', 'courses.*', 'typ_semester_courses.*')
  472. ->orderBy('courses.name', 'asc')
  473. ->get();
  474. $objectives_->selected_courses = array();
  475. foreach ($result_courses as $courses_) {
  476. array_push($objectives_->selected_courses, $courses_);
  477. }
  478. array_push($semesters_->selected_objectives, $objectives_);
  479. }
  480. array_push($outcome_->selected_semesters, $semesters_);
  481. }
  482. array_push($info, $outcome_);
  483. }
  484. }
  485. return $info;
  486. }
  487. // save or delete courses associated with objectives
  488. public function section3_on_change()
  489. {
  490. $typ_id = Input::get('typ_id');
  491. $outcome_id = Input::get('outcome_id');
  492. $semester_id = Input::get('semester_id');
  493. $objective_id = Input::get('objective_id');
  494. $previous_course_id = Input::get('previous_course_id');
  495. $new_course_id = Input::get('new_course_id');
  496. // get program_id
  497. $program_id = Input::get('program_id');
  498. // get typ_program_id
  499. $result = DB::table('typ_program')
  500. ->where('three_year_plan_id', $typ_id)
  501. ->where('program_id', $program_id)
  502. ->get();
  503. $typ_program_id = $result[0]->id;
  504. // get typ_semester_outcome_id
  505. $result = DB::table('typ_semester_outcome')
  506. ->where('typ_program_id', $typ_program_id)
  507. ->where('semester_id', $semester_id)
  508. ->where('outcome_id', $outcome_id)
  509. ->get();
  510. $typ_semester_outcome_id = $result[0]->id;
  511. // get typ_semester_objective_id
  512. $result = DB::table('typ_semester_objectives')
  513. ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
  514. ->where('objective_id', $objective_id)
  515. ->get();
  516. $typ_semester_objective_id = $result[0]->id;
  517. //delete old objective relation if it exists
  518. if ($previous_course_id != 'nothing_selected') {
  519. $result = DB::table('typ_semester_courses')
  520. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  521. ->where('course_id', $previous_course_id)
  522. ->get();
  523. if (count($result) != 0) {
  524. DB::table('typ_semester_courses')
  525. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  526. ->where('course_id', $previous_course_id)
  527. ->delete();
  528. }
  529. }
  530. //create new objective relation
  531. if ($new_course_id != 'nothing_selected') {
  532. $result = DB::table('typ_semester_courses')
  533. ->where('typ_semester_objective_id', $typ_semester_objective_id)
  534. ->where('course_id', $new_course_id)
  535. ->get();
  536. if (count($result) == 0) {
  537. $_ = DB::table('typ_semester_courses')->insertGetId(
  538. array(
  539. 'typ_semester_objective_id' => $typ_semester_objective_id,
  540. 'course_id' => $new_course_id
  541. )
  542. );
  543. }
  544. }
  545. return 'update succes?';
  546. }
  547. public function verifyAndCreate($program_id)
  548. {
  549. $typ_id = Input::get("typ_id");
  550. $typ_program_id = DB::table('typ_program')
  551. ->where('three_year_plan_id', $typ_id)
  552. ->where('program_id', $program_id)
  553. ->first();
  554. $information_thats_missing = [];
  555. $objectives_missing = DB::table('typ_semester_outcome')
  556. ->leftJoin("typ_semester_objectives", 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
  557. ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
  558. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  559. ->where('typ_program_id', $typ_program_id->id)
  560. ->whereNull('typ_semester_objectives.id')
  561. ->select('semesters.id as semester_id', 'semesters.name as semester_name', 'semesters.code', 'outcomes.name as outcome_name', 'outcomes.id as outcome_id')
  562. ->get();
  563. if (count($objectives_missing) != 0) {
  564. $objective_array = [];
  565. foreach ($objectives_missing as $ob) {
  566. if (!isset($objective_array[$ob->semester_id])) {
  567. $objective_array[$ob->semester_id] = array(
  568. 'semester_name' => $ob->semester_name . ' (' . $ob->code . ') ',
  569. 'outcome_text' => [],
  570. );
  571. }
  572. $objective_array[$ob->semester_id]['outcome_text'][] = $ob->outcome_name;
  573. }
  574. $information_thats_missing['objectives_missing'] = $objective_array;
  575. }
  576. $courses_missing = DB::table('typ_semester_outcome')
  577. ->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
  578. ->leftJoin('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  579. ->join('objectives', 'typ_semester_objectives.objective_id', '=', 'objectives.id')
  580. ->join('outcomes', 'typ_semester_outcome.outcome_id', '=', 'outcomes.id')
  581. ->join('semesters', 'typ_semester_outcome.semester_id', '=', 'semesters.id')
  582. ->where('typ_program_id', $typ_program_id->id)
  583. ->whereNull('typ_semester_courses.id')
  584. ->select('objectives.id as objective_id', 'objectives.text as objective_text')
  585. ->addSelect('outcomes.id as outcome_id', 'outcomes.name as outcome_name')
  586. ->addSelect('semesters.id as semester_id', 'semesters.name as semester_name', 'semesters.code as semester_code')
  587. ->get();
  588. if (count($courses_missing) != 0) {
  589. $courses_array = [];
  590. foreach ($courses_missing as $co) {
  591. if (!isset($courses_array[$co->semester_id])) {
  592. $courses_array[$co->semester_id] = array(
  593. 'semester_name' => $co->semester_name . ' (' . $co->semester_code . ') ',
  594. 'outcomes' => []
  595. );
  596. }
  597. if (!isset($courses_array[$co->semester_id]['outcomes'][$co->outcome_id])) {
  598. $courses_array[$co->semester_id]['outcomes'][$co->outcome_id] = array(
  599. 'outcome_name' => $co->outcome_name,
  600. 'objectives' => []
  601. );
  602. }
  603. $courses_array[$co->semester_id]['outcomes'][$co->outcome_id]['objectives'][] = $co->objective_text;
  604. }
  605. $information_thats_missing['courses_missing'] = $courses_array;
  606. }
  607. if (count($information_thats_missing) != 0) {
  608. return $information_thats_missing;
  609. } else {
  610. $this->createAnnualPlan($program_id);
  611. }
  612. }
  613. public function createAnnualPlan($program_id)
  614. {
  615. $input_id = Input::get('typ_id');
  616. $current_typ = DB::select("select * from three_year_plan where id = {$input_id}")[0];
  617. $count = 0;
  618. $yearStartPlusOne = $current_typ->year_start + 1;
  619. $yearStart = $current_typ->year_start;
  620. //Log::info($yearStart);
  621. //Log::info($yearStartPlusOne);
  622. while ($count < 3) {
  623. $firstSemester = DB::select("select * from semesters where name like 'First Semester {$yearStart}-{$yearStartPlusOne}'");
  624. $secondSemester = DB::select("select * from semesters where name like 'Second Semester {$yearStart}-{$yearStartPlusOne}'");
  625. if (count($firstSemester) && count($secondSemester)) {
  626. Log::info($firstSemester);
  627. Log::info($secondSemester);
  628. $query = DB::select("select * from annual_cycle where semester_start ={$firstSemester[0]->id} and semester_end ={$secondSemester[0]->id} ");
  629. if (!count($query)) {
  630. DB::insert("insert into annual_cycle (academic_year, semester_start, semester_end) values ('{$yearStart}-{$yearStartPlusOne}', {$firstSemester[0]->id}, {$secondSemester[0]->id})");
  631. }
  632. $an_cycle = DB::table('annual_cycle')->where('semester_start', '=', $firstSemester[0]->id)
  633. ->where('semester_end', '=', $secondSemester[0]->id)
  634. ->first();
  635. $query = DB::table('annual_plans')->where("annual_cycle_id", '=', $an_cycle->id)
  636. ->where('program_id', '=', $program_id)->first();
  637. if (!$query) {
  638. DB::insert("insert into annual_plans (`annual_cycle_id`, `program_id`) values ({$an_cycle->id}, {$program_id})");
  639. }
  640. }
  641. $count++;
  642. $yearStart++;
  643. $yearStartPlusOne++;
  644. }
  645. $this->createThreeYearPlanPdf($program_id, $input_id);
  646. }
  647. public function createThreeYearPlanPdf($program_id, $typ)
  648. {
  649. $program = DB::table('programs')->where('id', $program_id)->first();
  650. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  651. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  652. ->where('program_id', $program->id)
  653. ->first()->id;
  654. $typ_semesters = DB::table('semesters')
  655. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  656. ->where('typ_id', $three_year_plan->id)
  657. ->orderBy('semester_id')
  658. ->get();
  659. //Log::info("Stop 2");
  660. foreach ($typ_semesters as $semester) {
  661. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  662. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  663. ->where('typ_program_id', $typ_program_id)
  664. ->where('semester_id', $semester->semester_id)
  665. ->select('typ_semester_outcome.id as typ_id')
  666. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  667. ->get();
  668. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  669. $outcome->objectives = DB::table('typ_semester_objectives')
  670. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  671. ->where('typ_semester_outcome_id', $outcome->typ_id)
  672. ->select('typ_semester_objectives.id as typ_obj_id')
  673. ->addSelect('objectives.text', 'objectives.id as objective_id')
  674. ->get();
  675. foreach ($outcome->objectives as $objective) {
  676. $objective->courses = DB::table('typ_semester_courses')
  677. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  678. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  679. ->get();
  680. }
  681. }
  682. }
  683. //html para salvar
  684. $submit = 1;
  685. $view = 0;
  686. //$pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');
  687. //$pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
  688. // ->setOrientation("landscape")
  689. // ->setPaper('legal', 'landscape');
  690. $path = app_path() . '/views/three_year_plan_htmls/' . date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.blade.php';
  691. $name = date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.blade.php';
  692. //is a path already there, if it is, leave the same path
  693. $it_exists = DB::table("paths_for_three_year_plans")
  694. ->where('path_to_pdf', $path)
  695. ->first();
  696. //Log::info("pero aqui?");
  697. if (isset($it_exists)) {
  698. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  699. }
  700. //else save the new path and update time
  701. DB::table("paths_for_three_year_plans")
  702. ->where('typ_program_id', $typ_program_id)
  703. ->where('user_id', Auth::user()->id)
  704. ->update(array(
  705. 'last' => 0
  706. ));
  707. DB::table('paths_for_three_year_plans')->insert(array(
  708. "path_to_pdf" => $path,
  709. 'typ_program_id' => $typ_program_id,
  710. 'last' => 1,
  711. 'user_id' => Auth::user()->id,
  712. 'date_posted' => date('Y-m-d')
  713. ));
  714. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  715. }
  716. public function printPlan($program_id, $typ, $submit = NULL)
  717. {
  718. $view = 0;
  719. $program = DB::table('programs')->where('id', $program_id)->first();
  720. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  721. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  722. ->where('program_id', $program->id)
  723. ->first()->id;
  724. $typ_semesters = DB::table('semesters')
  725. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  726. ->where('typ_id', $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($typ_semester_outcome);
  753. //TEMP
  754. //$app_path = app_path() . '/jsPDF-master/src/jspdf.js';
  755. //$submit = 1;
  756. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  757. }
  758. public function viewPlan($program_id, $typ)
  759. {
  760. $view = 1;
  761. $program = DB::table('programs')->where('id', $program_id)->first();
  762. $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();
  763. $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
  764. ->where('program_id', $program->id)
  765. ->first()->id;
  766. $typ_semesters = DB::table('semesters')
  767. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  768. ->where('typ_id', $three_year_plan->id)
  769. ->orderBy('semester_id')
  770. ->get();
  771. foreach ($typ_semesters as $semester) {
  772. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  773. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  774. ->where('typ_program_id', $typ_program_id)
  775. ->where('semester_id', $semester->semester_id)
  776. ->select('typ_semester_outcome.id as typ_id')
  777. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  778. ->get();
  779. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  780. $outcome->objectives = DB::table('typ_semester_objectives')
  781. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  782. ->where('typ_semester_outcome_id', $outcome->typ_id)
  783. ->select('typ_semester_objectives.id as typ_obj_id')
  784. ->addSelect('objectives.text', 'objectives.id as objective_id')
  785. ->get();
  786. foreach ($outcome->objectives as $objective) {
  787. $objective->courses = DB::table('typ_semester_courses')
  788. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  789. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  790. ->get();
  791. }
  792. }
  793. }
  794. Log::info($typ_semester_outcome);
  795. return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view'));
  796. }
  797. public function showPDFs($program_id)
  798. {
  799. $role = Auth::user()->role;
  800. switch ($role) {
  801. case 1:
  802. case 2:
  803. case 3:
  804. $last = [1, 0];
  805. break;
  806. case 4:
  807. $last = [1];
  808. break;
  809. default:
  810. App::abort('404');
  811. }
  812. $title = "Three Year Plans for " . Program::findOrFail($program_id)->name;
  813. $paths_with_users = DB::table('paths_for_three_year_plans')
  814. ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
  815. ->join('three_year_plan', 'typ_program.three_year_plan_id', '=', 'three_year_plan.id')
  816. ->join('users', 'users.id', '=', 'paths_for_three_year_plans.user_id')
  817. ->whereIn('last', $last)
  818. ->where('typ_program.program_id', $program_id)
  819. ->orderBy('date_posted', 'desc')
  820. ->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')
  821. ->get();
  822. return View::make('local.managers.shared.new_view_three_year_plans', compact('paths_with_users', 'title', 'last', 'program_id'));
  823. }
  824. /*public function downloadPDF($download, $path_id)
  825. {
  826. $pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');
  827. $queryToPath = DB::table('paths_for_three_year_plans')
  828. ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
  829. ->where('paths_for_three_year_plans.id', $path_id)
  830. ->first();
  831. //Log::info("ERES TU? creo que no");
  832. // $annualPlan = AnnualPlan::findOrFail($queryToPath->annual_plan_id);
  833. $program = Program::findOrFail($queryToPath->program_id);
  834. $typ_program_id = $queryToPath->typ_program_id;
  835. $three_year_plan = DB::table('three_year_plan')
  836. ->where('id', $queryToPath->three_year_plan_id)
  837. ->first();
  838. $typ_semesters = DB::table('semesters')
  839. ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
  840. ->where('typ_id', $queryToPath->three_year_plan_id)
  841. ->orderBy('semester_id')
  842. ->get();
  843. foreach ($typ_semesters as $semester) {
  844. $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
  845. ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
  846. ->where('typ_program_id', $typ_program_id)
  847. ->where('semester_id', $semester->semester_id)
  848. ->select('typ_semester_outcome.id as typ_id')
  849. ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
  850. ->get();
  851. foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
  852. $outcome->objectives = DB::table('typ_semester_objectives')
  853. ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
  854. ->where('typ_semester_outcome_id', $outcome->typ_id)
  855. ->select('typ_semester_objectives.id as typ_obj_id')
  856. ->addSelect('objectives.text', 'objectives.id as objective_id')
  857. ->get();
  858. foreach ($outcome->objectives as $objective) {
  859. $objective->courses = DB::table('typ_semester_courses')
  860. ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
  861. ->where('typ_semester_objective_id', $objective->typ_obj_id)
  862. ->get();
  863. }
  864. }
  865. }
  866. //Log::info("ERES TU?");
  867. $pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
  868. ->setOrientation("landscape")
  869. ->setPaper('legal', 'landscape');
  870. if ($download == "download")
  871. return $pdf->download(basename($queryToPath->path_to_pdf));
  872. else
  873. return $pdf->stream(basename($queryToPath->path_to_pdf));
  874. }
  875. */
  876. ///////////////////
  877. //
  878. //Pair everything
  879. //
  880. ////////////////////
  881. public function pairEveryObjective()
  882. {
  883. Log::info(Input::all());
  884. $selected_objectives = Input::get('selected_objectives');
  885. $typ_id = Input::get('typ_id');
  886. $program_id = Input::get('program_id');
  887. for ($i = 0; $i < count($selected_objectives); $i += 3) {
  888. Input::replace([
  889. 'program_id' => $program_id,
  890. 'new_objective_id' => $selected_objectives[$i + 2],
  891. 'outcome_id' => $selected_objectives[$i],
  892. 'semester_id' => $selected_objectives[$i + 1],
  893. 'typ_id' => $typ_id,
  894. 'previous_objective_id' => 'nothing_selected'
  895. ]);
  896. if ($this->section2_on_change() != 'update was succes')
  897. return 0;
  898. }
  899. return 200;
  900. }
  901. public function pairEveryCourse()
  902. {
  903. Log::info(Input::all());
  904. $typ_id = Input::get('typ_id');
  905. $program_id = Input::get('program_id');
  906. $pairing_array = Input::get('pairing_array');
  907. $type_of_work = Input::get("type_of_work");
  908. //$typ_id = Input::get('typ_id');
  909. //$outcome_id = Input::get('outcome_id');
  910. //$semester_id = Input::get('semester_id');
  911. //$objective_id = Input::get('objective_id');
  912. //$previous_course_id = Input::get('previous_course_id');
  913. //$new_course_id = Input::get('new_course_id');
  914. // get program_id
  915. //$program_id = Input::get('program_id');
  916. if ($type_of_work == 'este_semestre') {
  917. foreach ($pairing_array as $course_array) {
  918. Input::replace([
  919. 'outcome_id' => $course_array[0],
  920. 'semester_id' => $course_array[1],
  921. 'objective_id' => $course_array[2],
  922. 'new_course_id' => $course_array[3],
  923. 'typ_id' => $typ_id,
  924. 'previous_course_id' => 'nothing_selected',
  925. 'program_id' => $program_id,
  926. ]);
  927. if ($this->section3_on_change() != 'update succes?')
  928. return 0;
  929. }
  930. } else {
  931. foreach ($pairing_array as $semester_array) {
  932. foreach ($semester_array as $course_array) {
  933. Input::replace([
  934. 'outcome_id' => $course_array[0],
  935. 'semester_id' => $course_array[1],
  936. 'objective_id' => $course_array[2],
  937. 'new_course_id' => $course_array[3],
  938. 'typ_id' => $typ_id,
  939. 'previous_course_id' => 'nothing_selected',
  940. 'program_id' => $program_id,
  941. ]);
  942. if ($this->section3_on_change() != 'update succes?')
  943. return 0;
  944. }
  945. }
  946. }
  947. return 200;
  948. }
  949. }