暫無描述

RubricsController.php 13KB

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