Nav apraksta

Objective2Controller.php 19KB

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