Без опису

ObjectivesController.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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->program;
  184. //$objective->criteria;
  185. return array(
  186. 'objective' => $objective
  187. );
  188. }
  189. }