No Description

RubricsController.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. class RubricsController extends \BaseController
  3. {
  4. /**
  5. * Show the form for creating a new rubric
  6. *
  7. * @return Response
  8. */
  9. public function newRubric($activity_id)
  10. {
  11. $activity = Activity::find($activity_id);
  12. // If activity does not exist, display 404
  13. if (!$activity)
  14. App::abort('404');
  15. $title = 'Rubric for <em>' . $activity->name . '</em>';
  16. // Select templates that belong to everyone or belong to the activity's course's school
  17. $templates = Template::where('is_visible', '=', 1)
  18. ->where(function ($query) use ($activity) {
  19. if (Auth::user()->role != 1) {
  20. $query
  21. ->where('school_id', $activity->course->program->school->id)
  22. ->orWhere('school_id', '=', NULL);
  23. }
  24. })
  25. ->orderBy('name', 'ASC')->get();
  26. $rubrics = Auth::user()->rubrics;
  27. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  28. $criteria = Criterion::orderBy('name', 'ASC')->get();
  29. $rubric = $activity->rubric;
  30. return View::make('local.professors.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'rubrics', 'activity', 'rubric'));
  31. }
  32. /**
  33. * Save a new rubric
  34. *
  35. * @return Response
  36. */
  37. public function create()
  38. {
  39. DB::beginTransaction();
  40. try {
  41. // Get rubric contents
  42. $rubric_contents = json_decode(Input::get('contents'));
  43. // Process rubric
  44. $rubric = new Rubric;
  45. $rubric->name = Input::get('name');
  46. $rubric->contents = json_encode($rubric_contents);
  47. $rubric->expected_percentage = Input::get('expected_percentage');
  48. $rubric->expected_points = Input::get('expected_points');
  49. $rubric->user_id = Auth::id();
  50. $rubric->save();
  51. // Process activity
  52. $activity = Activity::find(Input::get('activity_id'));
  53. $activity->rubric_id = $rubric->id;
  54. $activity->save();
  55. DB::commit();
  56. Session::flash('status', 'success');
  57. Session::flash('message', 'Rubric assigned.');
  58. return action('ActivitiesController@show', array($activity->id));
  59. } catch (Exception $e) {
  60. DB::rollBack();
  61. echo $e->getMessage();
  62. //Session::flash('status', 'danger');
  63. //Session::flash('message', 'Error creating Rubric. Try again later.');
  64. }
  65. }
  66. /**
  67. * Return a specific template
  68. *
  69. * @return Template
  70. */
  71. public function fetch()
  72. {
  73. return Rubric::find(Input::get('id'));
  74. }
  75. /**
  76. * Update a rubric
  77. *
  78. * @return Response
  79. */
  80. public function update()
  81. {
  82. $rubric = Rubric::find(Input::get('id'));
  83. $rubric->name = Input::get('name');
  84. $rubric->contents = Input::get('contents');
  85. $rubric->expected_percentage = Input::get('expected_percentage');
  86. $rubric->expected_points = Input::get('expected_points');
  87. DB::beginTransaction();
  88. try {
  89. // Get associated activity
  90. $activity = Activity::where('rubric_id', '=', $rubric->id)->first();
  91. // If the associated activity has been assessed, delete the records
  92. if ($activity->outcomes_attempted != NULL) {
  93. DB::table('assessments')->where('activity_id', '=', $activity->id)->delete();
  94. $activity->criteria_achieved_percentage = NULL;
  95. $activity->criteria_achieved = NULL;
  96. $activity->outcomes_achieved = NULL;
  97. $activity->outcomes_attempted = NULL;
  98. }
  99. $rubric->save();
  100. $activity->save();
  101. // Get all the course's activities
  102. $course = Course::find($activity->course->id);
  103. $activities = $course->activities;
  104. // Check if any assessed activities remain
  105. $remainingAssessed = false;
  106. foreach ($course->activities as $activity) {
  107. if ($activity->outcomes_attempted != NULL) {
  108. $remainingAssessed = true;
  109. break;
  110. }
  111. }
  112. //If there are still evaluated activities in the course, recalculate course outcomes
  113. if (!$activities->isEmpty() && $remainingAssessed) {
  114. // Variables to hold recalculated outcomes for the course
  115. $course_outcomes_attempted = array_fill(1, Outcome::all()->count(), 0);
  116. $course_outcomes_achieved = array_fill(1, Outcome::all()->count(), 0);
  117. // For each activity
  118. foreach ($activities as $activity) {
  119. // If activity has been assessed
  120. if ($activity->outcomes_attempted != NULL) {
  121. // Get the achieved criteria
  122. $criteria_achievement = json_decode($activity->criteria_achieved, true);
  123. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  124. // Find corresponding learning outcome;
  125. $criterion = Criterion::find($criterion_id);
  126. $outcome = Outcome::find($criterion->outcome_id);
  127. // If criterion is achieved (1), add 1 to both arrays
  128. if ($criterion_achieved === 1) {
  129. $course_outcomes_attempted[$outcome->id] += 1;
  130. $course_outcomes_achieved[$outcome->id] += 1;
  131. }
  132. // Else, only add to the attempted outcomes arrays
  133. elseif ($criterion_achieved === 0) {
  134. $course_outcomes_attempted[$outcome->id] += 1;
  135. }
  136. }
  137. }
  138. }
  139. // Update course
  140. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  141. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  142. } else {
  143. // Update course
  144. $course->outcomes_achieved = NULL;
  145. $course->outcomes_attempted = NULL;
  146. }
  147. $course->save();
  148. DB::commit();
  149. Session::flash('status', 'success');
  150. Session::flash('message', 'Rubric updated.');
  151. return action('ActivitiesController@show', array($activity->id));
  152. } catch (Exception $e) {
  153. DB::rollBack();
  154. Session::flash('status', 'danger');
  155. Session::flash('message', 'Error: The rubric could not be updated. Try again later.');
  156. }
  157. }
  158. /**
  159. * Remove the specified resource from storage.
  160. *
  161. * @return Response
  162. */
  163. public function destroy()
  164. {
  165. $rubric = Rubric::find(Input::get('id'));
  166. if ($rubric->delete()) {
  167. Session::flash('status', 'success');
  168. Session::flash('message', 'Rubric deleted.');
  169. } else {
  170. Session::flash('status', 'danger');
  171. Session::flash('message', 'Error: The rubric could not be deleted. Try again later.');
  172. }
  173. return;
  174. }
  175. /**
  176. * Show a specific rubric
  177. *
  178. * @return Response
  179. */
  180. public function show($activity_id)
  181. {
  182. $activity = Activity::find($activity_id);
  183. // Get activity's course
  184. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  185. // If activity does not belong to the requesting user, display 403
  186. if ($course->user_id != Auth::id())
  187. App::abort('403', 'Access Forbidden');
  188. $rubric = Rubric::where('id', '=', $activity->rubric_id)->firstOrFail();
  189. $title = $activity->name . ': ' . $rubric->name;
  190. return View::make('local.professors.viewrubric', compact('rubric', 'activity', 'title', 'course'));
  191. }
  192. /**
  193. * Show a specific rubric without some course and user information
  194. *
  195. * @return Response
  196. */
  197. public function show_limited($rubric_id)
  198. {
  199. // If user is a professor, display 403.
  200. if (Auth::user()->role == 4)
  201. App::abort('403', 'Access Forbidden');
  202. $rubric = Rubric::where('id', '=', $rubric_id)->firstOrFail();
  203. $title = $rubric->name;
  204. $role = Auth::user()->role;
  205. return View::make('local.managers.shared.view_rubric_limited', compact('rubric', 'title', 'role'));
  206. }
  207. public function download($activity_id, $rubric_id)
  208. {
  209. $activity = Activity::find($activity_id);
  210. // Get activity's course
  211. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  212. // If activity does not belong to the requesting user, display 403
  213. if ($course->user_id != Auth::id())
  214. App::abort('403', 'Access Forbidden');
  215. $rubric = Rubric::where('id', '=', $activity->rubric_id)->firstOrFail();
  216. $title = $activity->name . ': ' . $rubric->name;
  217. return View::make('local.professors.downloadrubric', compact('rubric', 'activity', 'title', 'course'));
  218. }
  219. public function printview($activity_id, $rubric_id)
  220. {
  221. $activity = Activity::find($activity_id);
  222. // Get activity's course
  223. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  224. // If activity does not belong to the requesting user, display 403
  225. if ($course->user_id != Auth::id())
  226. App::abort('403', 'Access Forbidden');
  227. $rubric = Rubric::where('id', '=', $activity->rubric_id)->firstOrFail();
  228. $title = $activity->name . ': ' . $rubric->name;
  229. return View::make('local.professors.printrubric', compact('rubric', 'activity', 'title', 'course'));
  230. }
  231. public function fetchRubricCriterion()
  232. {
  233. $rubric = Rubric::findOrFail(Input::get('rubric_id'));
  234. $criterion_id = Input::get('criterion_id');
  235. $rubric_contents = json_decode($rubric->contents);
  236. foreach ($rubric_contents as $key => $criterion) {
  237. if ($criterion->id == $criterion_id) {
  238. return json_encode($criterion);
  239. }
  240. }
  241. }
  242. }