Geen omschrijving

RubricsController.php 14KB

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