Keine Beschreibung

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