Ei kuvausta

RubricsController.php 12KB

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