暂无描述

RubricsController.php 11KB

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