설명 없음

Objective2Controller.php 10KB

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