Sin descripción

ObjectivesController.php 8.5KB

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