Ingen beskrivning

ObjectivesController.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. class ObjectivesController extends \BaseController {
  3. /**
  4. * Display a listing of the resource.
  5. *
  6. * @return Response
  7. */
  8. public function fetch()
  9. {
  10. try
  11. {
  12. $outcome_id = Input::get('outcome_id');
  13. $program_id = Input::get('program_id');
  14. $format = Input::get('format');
  15. $objectives = Objective::select('id', 'outcome_id', 'program_id', 'text');
  16. if($outcome_id)
  17. {
  18. $objectives->where('outcome_id', $outcome_id);
  19. }
  20. if($program_id)
  21. {
  22. $objectives->where('program_id', $program_id);
  23. }
  24. if($format == 'select')
  25. {
  26. $string = '';
  27. foreach ($objectives->get() as $objective)
  28. {
  29. $string.='<option value="'.$objective->id.'">'.$objective->text.'</option>';
  30. }
  31. echo $string;
  32. return;
  33. }
  34. else
  35. {
  36. return $objectives->get();
  37. }
  38. }
  39. catch (Exception $e)
  40. {
  41. echo $e->getMessage();
  42. return;
  43. }
  44. }
  45. /**
  46. * Edit the learning objectives per learning outcome for a program
  47. */
  48. public function index($program)
  49. {
  50. $role = Auth::user()->role;
  51. // Redirect users if they try to access forbidden page
  52. if($role == 2 && $program->school_id != Auth::user()->school_id)
  53. {
  54. return Redirect::to('/');
  55. }
  56. else if($role == 3 && !in_array($program->id, Auth::user()->programs->lists('id')))
  57. {
  58. return Redirect::to('/');
  59. }
  60. else if($role == 4)
  61. {
  62. return Redirect::to('/');
  63. }
  64. $title = 'Learning Objectives ('.$program->name.')';
  65. $objectives = $program->objectives;
  66. // Eager load outcomes related to objectives
  67. // $program->load('outcomes');
  68. //
  69. $outcomes = Outcome::select('id', 'name')->get();
  70. return View::make('local.managers.shared.objectives.index', compact('title', 'objectives', 'role', 'program', 'outcomes'));
  71. }
  72. public function create()
  73. {
  74. try
  75. {
  76. $outcome_id = Input::get('outcome_id');
  77. $program_id = Input::get('program_id');
  78. $learning_objective = trim(Input::get('learning_objective'));
  79. $validator = Validator::make(
  80. array(
  81. 'outcome_id' => $outcome_id,
  82. 'program_id' => $program_id,
  83. 'learning_objective' => $learning_objective
  84. ),
  85. array(
  86. 'outcome_id' => 'required|integer',
  87. 'program_id' => 'required|integer',
  88. 'learning_objective' => 'required|min:1',
  89. )
  90. );
  91. if($validator->fails())
  92. {
  93. /** Prepare error message */
  94. $message = '<p>Error(s) creating a new Learning Objective: </p><ul>';
  95. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  96. {
  97. $message.=$validationError;
  98. }
  99. $message.='</ul>';
  100. /** Send error message and old data */
  101. Session::flash('status', 'danger');
  102. Session::flash('message', $message);
  103. return Redirect::back()->withInput();
  104. }
  105. DB::table('objectives')->insert(array(
  106. 'outcome_id' => $outcome_id,
  107. 'program_id' => $program_id,
  108. 'text' => $learning_objective
  109. ));
  110. /** Send success message */
  111. Session::flash('status', 'success');
  112. Session::flash('message', 'New Learning Objective successfully created.');
  113. return Redirect::back();
  114. }
  115. catch(Exception $e)
  116. {
  117. /** Send error message and old data */
  118. Session::flash('status', 'danger');
  119. Session::flash('message', 'An error ocurred trying to create a new Learning Objective. Please try again later.'.$e->getMessage());
  120. return Redirect::back()->withInput();
  121. }
  122. }
  123. /**
  124. * Update objective
  125. */
  126. public function update()
  127. {
  128. try
  129. {
  130. $outcome_id = Input::get('edit_outcome_id');
  131. $objective_id = Input::get('edit_objective_id');
  132. $active = Input::get('edit_active');
  133. $learning_objective = trim(Input::get('edit_learning_objective'));
  134. $validator = Validator::make(
  135. array(
  136. 'outcome_id' => $outcome_id,
  137. 'objective_id' => $objective_id,
  138. 'active' => $active,
  139. 'learning_objective' => $learning_objective
  140. ),
  141. array(
  142. 'outcome_id' => 'required|integer',
  143. 'objective_id' => 'required|integer',
  144. 'active' => 'required|integer|boolean',
  145. 'learning_objective' => 'required|min:1',
  146. )
  147. );
  148. if($validator->fails())
  149. {
  150. /** Prepare error message */
  151. $message = '<p>Error(s) updating the Learning Objective: </p><ul>';
  152. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  153. {
  154. $message.=$validationError;
  155. }
  156. $message.='</ul>';
  157. /** Send error message and old data */
  158. Session::flash('status', 'danger');
  159. Session::flash('message', $message);
  160. return Redirect::back()->withInput();
  161. }
  162. DB::table('objectives')
  163. ->where('id', $objective_id)
  164. ->update(array(
  165. 'outcome_id' => $outcome_id,
  166. 'text' => $learning_objective,
  167. 'active' => $active,
  168. ));
  169. /** Send success message */
  170. Session::flash('status', 'success');
  171. Session::flash('message', 'Learning Objective successfully updated.');
  172. return Redirect::back();
  173. }
  174. catch(Exception $e)
  175. {
  176. /** Send error message and old data */
  177. Session::flash('status', 'danger');
  178. Session::flash('message', 'An error ocurred trying to create a new Learning Objective. Please try again later.'.$e->getMessage());
  179. return Redirect::back()->withInput();
  180. }
  181. }
  182. /**
  183. * Check what this was for
  184. * @return [type] [description]
  185. */
  186. public function fetchObjective()
  187. {
  188. $validator = Validator::make(
  189. array(
  190. 'objective_id' => Input::get('objective_id')
  191. ),
  192. array(
  193. 'objective_id' => 'required|integer',
  194. )
  195. );
  196. if($validator->fails())
  197. {
  198. return '';
  199. }
  200. $res = DB::table('objectives')->where('id', Input::get('objective_id'))->first();
  201. return json_encode($res);
  202. }
  203. public function fetchObjectiveForCriteria()
  204. {
  205. $id = Input::get('id');
  206. $objective = Objective::find($id);
  207. $objective->program;
  208. $objective->criteria;
  209. return array
  210. (
  211. 'objective' => $objective
  212. );
  213. }
  214. }