Ingen beskrivning

Objective2Controller.php 15KB

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