Aucune description

Objective2Controller.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. use Illuminate\Support\Facades\Auth;
  3. class Objective2Controller extends \BaseController
  4. {
  5. // display index Objectives
  6. public function viewObjectives()
  7. {
  8. $title = "Learning Objectives and Criteria";
  9. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  10. return View::make('global.view-objectives-criteria', compact('title', 'outcomes'));
  11. }
  12. /**
  13. * Show the form for creating a new resource.
  14. *
  15. * @return Response
  16. */
  17. public function fetchObjectivesForOutcome()
  18. {
  19. $outcome = Outcome::find(Input::get('id'));
  20. $role = Auth::user()->role;
  21. switch ($role) {
  22. case 1:
  23. $programs = DB::table('programs')->lists('id');
  24. break;
  25. case 2:
  26. $programs = DB::table('programs')
  27. ->where('school_id', Auth::user()->school_id)
  28. ->lists('id');
  29. break;
  30. case 3:
  31. case 4:
  32. $programs = DB::table('program_user')
  33. ->where("user_id", Auth::user()->id)
  34. ->lists('program_id');
  35. }
  36. $objectives = $outcome->objectivesFromProgram($programs)->get();
  37. return $objectives;
  38. }
  39. public function isObjectiveUnique($input, $existing_Objective = NULL)
  40. {
  41. Log::info('isObjectiveUnique');
  42. if (Input::get('program_id') != 0)
  43. $program_id = $input['program_id'];
  44. else
  45. $program_id = NULL;
  46. $saved_Objective = DB::table('objectives')->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  47. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  48. ->where('objectives.text', '=', $input['text'])
  49. ->whereIn('objective_outcome.outcome_id', $input['outcome_id'])
  50. ->whereIn('objective_program.program_id', $input['program_id'])
  51. ->get();
  52. if (count($saved_Objective))
  53. return false;
  54. else
  55. return true;
  56. }
  57. //edit == true or edit == false
  58. private function makeValidator($clean_input, $edit)
  59. {
  60. /** Validation rules */
  61. if ($edit) {
  62. return Validator::make(
  63. array(
  64. //'text' => $clean_input['text'],
  65. 'outcome_id' => $clean_input['outcome_id'],
  66. 'program_id' => $clean_input['program_id']
  67. ),
  68. array(
  69. 'outcome_id' => 'required|array',
  70. 'program_id' => 'required|array'
  71. )
  72. );
  73. } else {
  74. return Validator::make(
  75. array(
  76. 'text' => $clean_input['text'],
  77. 'outcome_id' => $clean_input['outcome_id'],
  78. 'program_id' => $clean_input['program_id']
  79. ),
  80. array(
  81. 'text' => 'required|string|unique:objectives,text',
  82. 'outcome_id' => 'required|array',
  83. 'program_id' => 'required|array'
  84. )
  85. );
  86. }
  87. }
  88. private function cleanInput()
  89. {
  90. $clean_input = array();
  91. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  92. $clean_input['outcome_id'] = Input::get('outcome');
  93. $counter = Input::get('counter') + 0;
  94. // Log::info($clean_input);
  95. foreach ($clean_input['outcome_id'] as $index => $outcome_id) {
  96. $clean_input['outcome_id'][$index] = trim(preg_replace('/\t+/', '', $clean_input['outcome_id'][$index]));
  97. }
  98. $clean_input['program_id'] = Input::get('program_id');
  99. // Log::info(Input::get('program_id'));
  100. return $clean_input;
  101. }
  102. private function cleanAssocInput()
  103. {
  104. $clean_input = array();
  105. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  106. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  107. Log::info(Input::all());
  108. $clean_input['program_id'] = Input::get('program_id');
  109. $clean_input['pair_criteria'] = Input::get('pair_every_criteria_with_objective');
  110. return $clean_input;
  111. }
  112. public function fetchObjective()
  113. {
  114. return Objective::find(Input::get('id'));
  115. }
  116. public function fetchObjectiveWithTrashed()
  117. {
  118. $json = array();
  119. $json['program'] = DB::select("select program_id from objective_program where objective_id = ?", array(Input::get('id')));
  120. //$json['outcome'] = DB::select("select outcome_id from objective_outcome outc where outc.objective_id = ?", array(Input::get('id')));
  121. $json['objective'] = DB::select("select text, id from objectives where id =?", array(Input::get('id')));
  122. $json['assessment'] = DB::select("select * from assessments where activity_criterion_id in (select id from activity_criterion where criterion_id in (select criterion_id from criterion_objective_outcome where objective_id = ?))", array(Input::get('id')));
  123. /*
  124. select objective_outcome.*, typ_semester_outcome.id as typ_semester_outcome_id,count(criterion_id) from objectives join objective_outcome on objective_outcome.objective_id = objectives.id left join criterion_objective_outcome on criterion_objective_outcome.objective_id = objectives.id and objective_outcome.outcome_id = criterion_objective_outcome.outcome_id
  125. left join typ_semester_objectives on typ_semester_objectives.objective_id = objectives.id left join typ_semester_outcome on typ_semester_outcome.outcome_id = objective_outcome.outcome_id and typ_semester_outcome.id = typ_semester_objectives.typ_semester_outcome_id group by objectives.id, objective_outcome.outcome_id
  126. */
  127. $json['outcome'] = DB::table('objective_outcome')
  128. ->leftJoin('criterion_objective_outcome', function ($j) {
  129. $j->on('criterion_objective_outcome.objective_id', '=', 'objective_outcome.objective_id')
  130. ->on('criterion_objective_outcome.outcome_id', '=', 'objective_outcome.outcome_id');
  131. })
  132. ->leftJoin('typ_semester_objectives', 'typ_semester_objectives.objective_id', '=', 'objective_outcome.objective_id')
  133. ->leftJoin('typ_semester_outcome', function ($j) {
  134. $j->on('typ_semester_outcome.outcome_id', '=', 'objective_outcome.outcome_id')
  135. ->on('typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id');
  136. })
  137. ->groupBy("objective_outcome.objective_id", 'objective_outcome.outcome_id')
  138. ->where('objective_outcome.objective_id', Input::get('id'))
  139. ->select(
  140. 'objective_outcome.*',
  141. DB::raw("count(criterion_id) as count_criterion_id"),
  142. 'typ_semester_outcome.id as typ_semester_outcome_id'
  143. )
  144. ->get();
  145. $json['typ_semester_objectives'] = DB::table('typ_semester_objectives')
  146. ->where('objective_id', Input::get('id'))
  147. ->get();
  148. $json['assoc_criteria'] = DB::select("select name from criteria where id in(select criterion_id from criterion_objective_outcome where objective_id =?)", array(Input::get('id')));
  149. Log::info('is here');
  150. // Log::info(json_encode($json));
  151. return json_encode($json);
  152. }
  153. public function fetchAllobjectives()
  154. {
  155. $program_id = Input::get('program_fetch');
  156. $outcome_id = Input::get('outcome_fetch');
  157. $json = array();
  158. $json['objective'] = DB::select("SELECT * FROM `objectives` where id in (select objective_id from objective_outcome where outcome_id ={$outcome_id}) and id in (select objective_id from objective_program where program_id = {$program_id} and objective_id <> 0)");
  159. Log::info('En mi sueño');
  160. Log::info($json);
  161. return json_encode($json);
  162. }
  163. public function delete()
  164. {
  165. DB::delete("delete from objectives where id = ?", array(Input::get('deleteObj')));
  166. return Redirect::to('objectives')->withInput();
  167. /*$role = Auth::user()['role'];
  168. switch ($role) {
  169. case 1:
  170. return Redirect::to('objectives')->withInput();
  171. case 2:
  172. return Redirect::to('school-objective')->withInput();
  173. case 3:
  174. return Redirect::to('program-objective')->withInput();
  175. }*/
  176. }
  177. /**
  178. * Create a new Objective.
  179. *
  180. * @return Redirect Redirect back to form page
  181. */
  182. public function create()
  183. {
  184. $clean_input = $this->cleanInput();
  185. Log::info($clean_input);
  186. /** Validation rules */
  187. $validator = $this->makeValidator($clean_input, false);
  188. /** If validation fails */
  189. if ($validator->fails()) {
  190. /** Prepare error message */
  191. $message = '<p>Error(s) creating a new Objective:</p><ul>';
  192. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  193. $message .= $validationError;
  194. }
  195. $message .= '</ul>';
  196. /** Send error message and old data */
  197. Session::flash('status', 'danger');
  198. Session::flash('message', $message);
  199. return Redirect::to('objectives')->withInput();
  200. /*$role = Auth::user()['role'];
  201. switch ($role) {
  202. case 1:
  203. return Redirect::to('objective')->withInput();
  204. case 2:
  205. return Redirect::to('school-objective')->withInput();
  206. case 3:
  207. return Redirect::to('program-objective')->withInput();
  208. }*/
  209. } else {
  210. // Check Objective uniqueness
  211. if (!$this->isObjectiveUnique($clean_input)) {
  212. /** Send error message and old data */
  213. Session::flash('status', 'danger');
  214. Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
  215. return Redirect::to('objectives')->withInput();
  216. /*$role = Auth::user()['role'];
  217. switch ($role) {
  218. case 1:
  219. return Redirect::to('objective')->withInput();
  220. case 2:
  221. return Redirect::to('school-objective')->withInput();
  222. case 3:
  223. return Redirect::to('program-objective')->withInput();
  224. }*/
  225. }
  226. /** Instantiate new Objective */
  227. $objective = new Objective;
  228. $objective->text = $clean_input['text'];
  229. /** If Objective is saved, send success message */
  230. if ($objective->save()) {
  231. $objectiveId = $objective->id;
  232. Log::info($clean_input['outcome_id']);
  233. foreach ($clean_input['program_id'] as $program_id) {
  234. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  235. }
  236. foreach ($clean_input['outcome_id'] as $outcome_id) {
  237. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  238. //DB::raw("insert ignore into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  239. /*if (!($objectiveOutcome->save())) {
  240. Session::flash('status', 'danger');
  241. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  242. return Redirect::to('objective')->withInput();
  243. }*/
  244. }
  245. DB::table('criterion_objective_outcome')
  246. ->join('program_criterion', 'criterion_objective_outcome.criterion_id', "=", 'program_criterion.criterion_id')
  247. ->whereIn('program_id', $clean_input['program_id'])
  248. ->whereIn('outcome_id', $clean_input['outcome_id'])
  249. ->where('objective_id', '=', 0)
  250. ->update(array('objective_id' => $objectiveId));
  251. // update("update criterion_objective_outcome coo join program_criterion pc on coo.criterion_id=pc.criterion_id set
  252. // objective_id= {$objectiveId} where program_id in (".$clean_input['program_id'].") and objective_id=0 and outcome_id in (".$clean_input['outcome_id'].")");
  253. Session::flash('status', 'success');
  254. Session::flash('message', 'Objective created: "' . $objective->text . '".');
  255. return Redirect::to('objectives')->withInput(Input::only('outcome_id'));
  256. /*$role = Auth::user()['role'];
  257. switch ($role) {
  258. case 1:
  259. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  260. case 2:
  261. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  262. case 3:
  263. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  264. }*/
  265. }
  266. /** If saving fails, send error message and old data */
  267. else {
  268. Session::flash('status', 'danger');
  269. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  270. return Redirect::to('objectives')->withInput();
  271. /*$role = Auth::user()['role'];
  272. switch ($role) {
  273. case 1:
  274. return Redirect::to('objective')->withInput();
  275. case 2:
  276. return Redirect::to('school-objective')->withInput();
  277. case 3:
  278. return Redirect::to('program-objective')->withInput();
  279. }*/
  280. }
  281. }
  282. }
  283. /**
  284. * Store a newly created resource in storage.
  285. *
  286. * @return Response
  287. */
  288. public function store()
  289. {
  290. //
  291. }
  292. /**
  293. * Display the specified resource.
  294. *
  295. * @param int $id
  296. * @return Response
  297. */
  298. public function show($id)
  299. {
  300. //
  301. }
  302. /**
  303. * Show the form for editing the specified resource.
  304. *
  305. * @param int $id
  306. * @return Response
  307. */
  308. public function edit()
  309. {
  310. $title = "Objective";
  311. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  312. $role = Auth::user()->role;
  313. switch ($role) {
  314. case 1:
  315. $program_ids = DB::table("programs")
  316. ->lists('id');
  317. break;
  318. case 2:
  319. $program_ids = DB::table('programs')
  320. ->where('school_id', Auth::user()->school_id)
  321. ->lists('id');
  322. break;
  323. case 3:
  324. $program_ids = DB::table('program_user')
  325. ->where('user_id', Auth::user()->id)
  326. ->lists('program_id');
  327. break;
  328. }
  329. $objectives_from_program = DB::table('objective_program')
  330. ->where('program_id', $program_ids)
  331. ->where('objective_program.objective_id', "<>", 0)
  332. ->lists('objective_id');
  333. $objectives = Objective::withTrashed()->whereIn('id', $objectives_from_program)->orderBy('text', 'ASC')->get();
  334. $programs = Program::whereIn('id', $program_ids)->orderBy('name', 'ASC')->get();
  335. return View::make('local.managers.shared.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  336. }
  337. public function editProgram()
  338. {
  339. $userProgram = Auth::user()['id'];
  340. Log::info(Auth::user());
  341. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  342. $title = "Objective";
  343. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  344. $objectives_from_program = DB::table('objective_program')
  345. ->where('program_id', $userProgram[0]->program_id)
  346. ->where('objective_program.objective_id', "<>", 0)
  347. ->lists('objective_id');
  348. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_program)->get();
  349. $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
  350. return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  351. }
  352. public function editSchool()
  353. {
  354. $userSchool = Auth::user()['school_id'];
  355. Log::info($userSchool);
  356. $title = "Objective";
  357. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  358. $objectives_from_school = DB::table('programs')
  359. ->join('objective_program', 'objective_program.program_id', '=', 'programs.id')
  360. ->where('programs.school_id', $userSchool)
  361. ->where('objective_program.objective_id', "<>", 0)
  362. ->lists('objective_id');
  363. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_school)->get();
  364. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  365. return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  366. }
  367. /**
  368. * Update the specified resource in storage.
  369. *
  370. * @param int $id
  371. * @return Response
  372. */
  373. public function update()
  374. {
  375. $Objective = Objective::withTrashed()->find(Input::get('id'));
  376. $clean_input = $this->cleanAssocInput();
  377. //Log::info(print_r($clean_input, true));
  378. /** Validation rules */
  379. if ($clean_input['text'] == $Objective->text) {
  380. $validator = $this->makeValidator($clean_input, false);
  381. } else
  382. $validator = $this->makeValidator($clean_input, false);
  383. /** If validation fails */
  384. if ($validator->fails()) {
  385. /** Prepare error message */
  386. $message = 'Error(s) updating the Objective: <ul>';
  387. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  388. $message .= $validationError;
  389. }
  390. $message .= '</ul>';
  391. /** Send error message and old data */
  392. //Session::flash('status', 'danger');
  393. //Session::flash('message', $message);
  394. $MessageArray = array('status' => 'danger', 'message' => $message);
  395. return $MessageArray;
  396. /*$role = Auth::user()['role'];
  397. switch ($role) {
  398. case 1:
  399. return $MessageArray;
  400. return Redirect::to('objective')->withInput();
  401. case 2:
  402. return $MessageArray;
  403. return Redirect::to('school-objective')->withInput();
  404. case 3:
  405. return $MessageArray;
  406. return Redirect::to('program-objective')->withInput();
  407. }*/
  408. } else {
  409. /** Set info */
  410. Log::info($clean_input);
  411. $Objective->text = $clean_input['text'];
  412. // Set status
  413. /** If Objective is updated, send success message */
  414. if ($Objective->save()) {
  415. //TODO
  416. $objectiveId = $Objective->id;
  417. //DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
  418. DB::delete("delete from objective_program where objective_id = {$objectiveId}");
  419. foreach ($clean_input['program_id'] as $program_id) {
  420. DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
  421. }
  422. $criteria_assoc = DB::table('criterion_objective_outcome')
  423. ->join('program_criterion', 'program_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  424. ->whereIn('program_id', $clean_input['program_id'])
  425. ->where('objective_id', $objectiveId)
  426. ->groupBy('program_criterion.criterion_id')
  427. ->select('program_criterion.criterion_id')
  428. ->lists('program_criterion.criterion_id');
  429. $criterion_array = [];
  430. foreach ($clean_input['outcome_id'] as $outcome_id) {
  431. $check_if_already_inserted = DB::table('objective_outcome')
  432. ->where('objective_id', $objectiveId)
  433. ->where('outcome_id', $outcome_id)
  434. ->first();
  435. if (!isset($check_if_already_inserted)) {
  436. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  437. if ($clean_input['pair_criteria'] == '1') {
  438. foreach ($criteria_assoc as $criterion_id) {
  439. DB::table('criterion_objective_outcome')
  440. ->insert(array(
  441. "criterion_id" => $criterion_id,
  442. "objective_id" => $objectiveId,
  443. "outcome_id" => $outcome_id
  444. ));
  445. }
  446. }
  447. }
  448. }
  449. DB::table('objective_outcome')
  450. ->whereNotIn('outcome_id', $clean_input['outcome_id'])
  451. ->where('objective_id', $objectiveId)
  452. ->delete();
  453. //Session::flash('status', 'success');
  454. //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
  455. $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
  456. return $MessageArray;
  457. /*$role = Auth::user()['role'];
  458. switch ($role) {
  459. case 1:
  460. return $MessageArray;
  461. return Redirect::to('objective')->withInput();
  462. case 2:
  463. return $MessageArray;
  464. return Redirect::to('school-objective')->withInput();
  465. case 3:
  466. return $MessageArray;
  467. return Redirect::to('program-objective')->withInput();
  468. }*/
  469. }
  470. /** If saving fails, send error message and old data */
  471. else {
  472. //Session::flash('status', 'danger');
  473. ////Session::flash('message', 'Error updating the Objective. Please try again later.');
  474. $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
  475. return $MessageArray;
  476. $role = Auth::user()['role'];
  477. switch ($role) {
  478. case 1:
  479. return $MessageArray;
  480. return Redirect::to('objectives')->withInput();
  481. case 2:
  482. return $MessageArray;
  483. return Redirect::to('school-objective')->withInput();
  484. case 3:
  485. return $MessageArray;
  486. return Redirect::to('program-objective')->withInput();
  487. }
  488. }
  489. }
  490. }
  491. /**
  492. * Remove the specified resource from storage.
  493. *
  494. * @param int $id
  495. * @return Response
  496. */
  497. public function destroy($id)
  498. {
  499. //
  500. }
  501. }