Без опису

ThreeYearPlanController.php 41KB

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