Няма описание

RubricsController.php 10KB

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