설명 없음

Objective2Controller.php 7.7KB

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