No Description

Objective2Controller.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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',
  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. for ($i = 0; $i < $counter; $i++) {
  56. $clean_input['outcome_id'][$i] = trim(preg_replace('/\t+/', '', $clean_input['outcome_id'][$i]));
  57. }
  58. $clean_input['program_id'] = Input::get('program_id');
  59. Log::info(Input::get('program_id'));
  60. return $clean_input;
  61. }
  62. private function cleanAssocInput()
  63. {
  64. $clean_input = array();
  65. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  66. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  67. Log::info(Input::all());
  68. $clean_input['program_id'] = Input::get('program_id');
  69. return $clean_input;
  70. }
  71. public function fetchObjective()
  72. {
  73. return Objective::find(Input::get('id'));
  74. }
  75. public function fetchObjectiveWithTrashed()
  76. {
  77. $json = array();
  78. $json['program'] = DB::select("select program_id from objective_program where objective_id = ?", array(Input::get('id')));
  79. $json['outcome'] = DB::select("select outcome_id from objective_outcome outc where outc.objective_id = ?", array(Input::get('id')));
  80. $json['objective'] = DB::select("select text, id from objectives where id =?", array(Input::get('id')));
  81. $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')));
  82. $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')));
  83. Log::info('is here');
  84. return json_encode($json);
  85. }
  86. public function fetchAllobjectives()
  87. {
  88. $program_id = Input::get('program_fetch');
  89. $outcome_id = Input::get('outcome_fetch');
  90. $json = array();
  91. $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})");
  92. return json_encode($json);
  93. }
  94. public function delete()
  95. {
  96. DB::delete("delete from objectives where id = ?", array(Input::get('deleteObj')));
  97. $role = Auth::user()['role'];
  98. switch ($role) {
  99. case 1:
  100. return Redirect::to('objectives')->withInput();
  101. case 2:
  102. return Redirect::to('school-objectives')->withInput();
  103. case 3:
  104. return Redirect::to('program-objectives')->withInput();
  105. }
  106. }
  107. /**
  108. * Create a new Objective.
  109. *
  110. * @return Redirect Redirect back to form page
  111. */
  112. public function create()
  113. {
  114. $clean_input = $this->cleanInput();
  115. /** Validation rules */
  116. $validator = $this->makeValidator($clean_input);
  117. /** If validation fails */
  118. if ($validator->fails()) {
  119. /** Prepare error message */
  120. $message = '<p>Error(s) creating a new Objective:</p><ul>';
  121. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  122. $message .= $validationError;
  123. }
  124. $message .= '</ul>';
  125. /** Send error message and old data */
  126. Session::flash('status', 'danger');
  127. Session::flash('message', $message);
  128. $role = Auth::user()['role'];
  129. switch ($role) {
  130. case 1:
  131. return Redirect::to('objective')->withInput();
  132. case 2:
  133. return Redirect::to('school-objective')->withInput();
  134. case 3:
  135. return Redirect::to('program-objective')->withInput();
  136. }
  137. } else {
  138. // Check Objective uniqueness
  139. if (!$this->isObjectiveUnique($clean_input)) {
  140. /** Send error message and old data */
  141. Session::flash('status', 'danger');
  142. Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
  143. $role = Auth::user()['role'];
  144. switch ($role) {
  145. case 1:
  146. return Redirect::to('objective')->withInput();
  147. case 2:
  148. return Redirect::to('school-objective')->withInput();
  149. case 3:
  150. return Redirect::to('program-objective')->withInput();
  151. }
  152. }
  153. /** Instantiate new Objective */
  154. $objective = new Objective;
  155. $objective->text = $clean_input['text'];
  156. /** If Objective is saved, send success message */
  157. if ($objective->save()) {
  158. $objectiveId = $objective->id;
  159. foreach ($clean_input['program_id'] as $program_id) {
  160. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  161. }
  162. foreach ($clean_input['outcome_id'] as $outcome_id) {
  163. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  164. /*if (!($objectiveOutcome->save())) {
  165. Session::flash('status', 'danger');
  166. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  167. return Redirect::to('objective')->withInput();
  168. }*/
  169. }
  170. Session::flash('status', 'success');
  171. Session::flash('message', 'Objective created: "' . $objective->text . '".');
  172. $role = Auth::user()['role'];
  173. switch ($role) {
  174. case 1:
  175. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  176. case 2:
  177. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  178. case 3:
  179. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  180. }
  181. }
  182. /** If saving fails, send error message and old data */
  183. else {
  184. Session::flash('status', 'danger');
  185. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  186. $role = Auth::user()['role'];
  187. switch ($role) {
  188. case 1:
  189. return Redirect::to('objective')->withInput();
  190. case 2:
  191. return Redirect::to('school-objective')->withInput();
  192. case 3:
  193. return Redirect::to('program-objective')->withInput();
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Store a newly created resource in storage.
  200. *
  201. * @return Response
  202. */
  203. public function store()
  204. {
  205. //
  206. }
  207. /**
  208. * Display the specified resource.
  209. *
  210. * @param int $id
  211. * @return Response
  212. */
  213. public function show($id)
  214. {
  215. //
  216. }
  217. /**
  218. * Show the form for editing the specified resource.
  219. *
  220. * @param int $id
  221. * @return Response
  222. */
  223. public function edit()
  224. {
  225. $title = "Objective";
  226. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  227. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->get();
  228. $programs = Program::orderBy('name', 'ASC')->get();
  229. return View::make('local.managers.admins.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  230. }
  231. public function editProgram()
  232. {
  233. $userProgram = Auth::user()['id'];
  234. Log::info(Auth::user());
  235. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  236. $title = "Objective";
  237. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  238. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->get();
  239. $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
  240. return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  241. }
  242. public function editSchool()
  243. {
  244. $userSchool = Auth::user()['school_id'];
  245. Log::info($userSchool);
  246. $title = "Objective";
  247. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  248. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->get();
  249. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  250. return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  251. }
  252. /**
  253. * Update the specified resource in storage.
  254. *
  255. * @param int $id
  256. * @return Response
  257. */
  258. public function update()
  259. {
  260. $Objective = Objective::withTrashed()->find(Input::get('id'));
  261. $clean_input = $this->cleanAssocInput();
  262. Log::info(print_r($clean_input, true));
  263. /** Validation rules */
  264. $validator = $this->makeValidator($clean_input);
  265. /** If validation fails */
  266. if ($validator->fails()) {
  267. /** Prepare error message */
  268. $message = 'Error(s) updating the Objective: <ul>';
  269. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  270. $message .= $validationError;
  271. }
  272. $message .= '</ul>';
  273. /** Send error message and old data */
  274. //Session::flash('status', 'danger');
  275. //Session::flash('message', $message);
  276. $MessageArray = array('status' => 'danger', 'message' => $message);
  277. $role = Auth::user()['role'];
  278. switch ($role) {
  279. case 1:
  280. return $MessageArray;
  281. return Redirect::to('objective')->withInput();
  282. case 2:
  283. return $MessageArray;
  284. return Redirect::to('school-objective')->withInput();
  285. case 3:
  286. return $MessageArray;
  287. return Redirect::to('program-objective')->withInput();
  288. }
  289. } else {
  290. /** Set info */
  291. Log::info($clean_input);
  292. $Objective->text = $clean_input['text'];
  293. // Set status
  294. /** If Objective is updated, send success message */
  295. if ($Objective->save()) {
  296. $objectiveId = $Objective->id;
  297. DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
  298. DB::delete("delete from objective_program where objective_id = {$objectiveId}");
  299. foreach ($clean_input['program_id'] as $program_id) {
  300. DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
  301. }
  302. foreach ($clean_input['outcome_id'] as $outcome_id) {
  303. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  304. }
  305. //Session::flash('status', 'success');
  306. //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
  307. $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
  308. $role = Auth::user()['role'];
  309. switch ($role) {
  310. case 1:
  311. return $MessageArray;
  312. return Redirect::to('objective')->withInput();
  313. case 2:
  314. return $MessageArray;
  315. return Redirect::to('school-objective')->withInput();
  316. case 3:
  317. return $MessageArray;
  318. return Redirect::to('program-objective')->withInput();
  319. }
  320. }
  321. /** If saving fails, send error message and old data */
  322. else {
  323. //Session::flash('status', 'danger');
  324. ////Session::flash('message', 'Error updating the Objective. Please try again later.');
  325. $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
  326. $role = Auth::user()['role'];
  327. switch ($role) {
  328. case 1:
  329. return $MessageArray;
  330. return Redirect::to('objective')->withInput();
  331. case 2:
  332. return $MessageArray;
  333. return Redirect::to('school-objective')->withInput();
  334. case 3:
  335. return $MessageArray;
  336. return Redirect::to('program-objective')->withInput();
  337. }
  338. }
  339. }
  340. }
  341. /**
  342. * Remove the specified resource from storage.
  343. *
  344. * @param int $id
  345. * @return Response
  346. */
  347. public function destroy($id)
  348. {
  349. //
  350. }
  351. }