Ingen beskrivning

RubricsController.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. Log::info($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::where('is_visible', '=', 1)
  42. ->where(function ($query) use ($activity) {
  43. if (Auth::user()->role != 1) {
  44. $query
  45. ->where('school_id', $activity->course->program->school->id)
  46. ->orWhere('school_id', '=', NULL);
  47. }
  48. })
  49. ->orderBy('name', 'ASC')->get();
  50. $rubrics = Auth::user()->rubrics;
  51. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  52. $criteria = Criterion::orderBy('name', 'ASC')->get();
  53. $rubric = $activity->rubric;
  54. return View::make('local.professors.rubrics', compact('title', 'templates', 'outcomes', 'criteria', 'rubrics', 'activity', 'rubric'));
  55. }
  56. /**
  57. * Save a new rubric
  58. *
  59. * @return Response
  60. */
  61. public function create()
  62. {
  63. DB::beginTransaction();
  64. // Get rubric contents
  65. $titles = Input::get('titles');
  66. $criteria = Input::get('criteria');
  67. // Process rubric
  68. $rubric = new Rubric;
  69. $rubric->name = Input::get('name');
  70. $rubric->expected_percentage = Input::get('expected_percentage');
  71. $rubric->expected_points = Input::get('expected_points');
  72. $rubric->user_id = Auth::id();
  73. $rubric->num_scales = count($titles);
  74. $rubric->max_score = Input::get('max_score');
  75. $defaultWeight = round(100 / count($criteria), 2);
  76. if ($rubric->save()) {
  77. // Process activity
  78. // $activity = Activity::find(Input::get('activity_id'));
  79. // $activity->rubric_id = $rubric->id;
  80. // $activity->save();
  81. DB::table('rubric_activity')->insert(array('activity_id' => Input::get('activity_id'), 'rubric_id' => $rubric->id));
  82. DB::commit();
  83. $rubricId = $rubric->id;
  84. foreach ($criteria as $index => $criterion_id) {
  85. DB::insert("insert into rubric_criterion (`rubric_id`,`criterion_id`, `position`) values ({$rubricId},{$criterion_id}, {$index})");
  86. DB::commit();
  87. $activity_id = Input::get("activity_id");
  88. DB::insert("insert into `activity_criterion` (`activity_id`,`criterion_id`, `weight`) values ({$activity_id}, {$criterion_id}, {$defaultWeight})");
  89. DB::commit();
  90. }
  91. foreach ($titles as $index => $id) {
  92. DB::insert("insert into `rubric_title` (`rubric_id`, `title_id`, `position`) values ({$rubricId}, {$id}, {$index} )");
  93. DB::commit();
  94. }
  95. Session::flash('status', 'success');
  96. Session::flash('message', 'Rubric assigned.');
  97. return action('ActivitiesController@show', array(Input::get('activity_id')));
  98. } else {
  99. DB::rollBack();
  100. Session::flash('status', 'danger');
  101. Session::flash('message', 'Error creating Rubric. Try again later.' . $e);
  102. }
  103. }
  104. /**
  105. * Return a specific template
  106. *
  107. * @return Template
  108. */
  109. public function fetch()
  110. {
  111. return Rubric::find(Input::get('id'));
  112. }
  113. /**
  114. * Update a rubric
  115. *
  116. * @return Response
  117. */
  118. public function update()
  119. {
  120. $rubric = Rubric::find(Input::get('id'));
  121. $titles = Input::get('titles');
  122. $criteria = Input::get('criteria');
  123. // Process rubric
  124. $rubric->name = Input::get('name');
  125. $rubric->expected_percentage = Input::get('expected_percentage');
  126. $rubric->expected_points = Input::get('expected_points');
  127. $rubric->num_scales = count($titles);
  128. $rubric->max_score = Input::get('max_score');
  129. $defaultWeight = round(100 / count($criteria), 2);
  130. DB::beginTransaction();
  131. // Get associated activity
  132. //$activity = Activity::where('rubric_id', '=', $rubric->id)->first();
  133. $activity_id = DB::table('activities')
  134. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  135. ->where('rubric_id', '=', $rubric->id)
  136. ->first();
  137. $activity = Activity::where('id', '=', $activity_id->activity_id)->first();
  138. // If the associated activity has been assessed, delete the records
  139. /* if ($activity->outcomes_attempted != NULL) {
  140. DB::table('assessments')->where('activity_id', '=', $activity->id)->delete();
  141. $activity->criteria_achieved_percentage = NULL;
  142. $activity->criteria_achieved = NULL;
  143. $activity->outcomes_achieved = NULL;
  144. $activity->outcomes_attempted = NULL;
  145. }*/
  146. $rubric->save();
  147. $activity->save();
  148. // Get all the course's activities
  149. Log::info($activity->course);
  150. $course = Course::find($activity->course->id);
  151. $activities = $course->activities;
  152. // Check if any assessed activities remain
  153. $remainingAssessed = false;
  154. foreach ($course->activities as $activity) {
  155. if ($activity->outcomes_attempted != NULL) {
  156. $remainingAssessed = true;
  157. break;
  158. }
  159. }
  160. Log::info('entré4???');
  161. //If there are still evaluated activities in the course, recalculate course outcomes
  162. /*
  163. if (!$activities->isEmpty() && $remainingAssessed) {
  164. // Variables to hold recalculated outcomes for the course
  165. $course_outcomes_attempted = array_fill(1, Outcome::all()->count(), 0);
  166. $course_outcomes_achieved = array_fill(1, Outcome::all()->count(), 0);
  167. // For each activity
  168. foreach ($activities as $activity) {
  169. // If activity has been assessed
  170. if ($activity->outcomes_attempted != NULL) {
  171. // Get the achieved criteria
  172. $criteria_achievement = json_decode($activity->criteria_achieved, true);
  173. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  174. // Find corresponding learning outcome;
  175. $criterion = Criterion::find($criterion_id);
  176. $outcome = Outcome::find($criterion->outcome_id);
  177. // If criterion is achieved (1), add 1 to both arrays
  178. if ($criterion_achieved === 1) {
  179. $course_outcomes_attempted[$outcome->id] += 1;
  180. $course_outcomes_achieved[$outcome->id] += 1;
  181. }
  182. // Else, only add to the attempted outcomes arrays
  183. elseif ($criterion_achieved === 0) {
  184. $course_outcomes_attempted[$outcome->id] += 1;
  185. }
  186. }
  187. }
  188. }
  189. Log::info('entré5???');
  190. // Update course
  191. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  192. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  193. } else {
  194. // Update course
  195. $course->outcomes_achieved = NULL;
  196. $course->outcomes_attempted = NULL;
  197. }
  198. $course->save();
  199. */
  200. Log::info('entré6???');
  201. DB::delete("delete from rubric_criterion where rubric_id ={$rubric->id}");
  202. DB::delete("delete from activity_criterion where activity_id = {$activity->id}");
  203. foreach ($criteria as $index => $criterion_id) {
  204. $result = DB::insert("insert into rubric_criterion (`rubric_id`, `criterion_id`) values ({$rubric->id}, {$criterion_id}) ");
  205. $result2 = DB::insert("insert into `activity_criterion` (`activity_id`,`criterion_id`, `weight`) values ({$activity->id}, {$criterion_id}, {$defaultWeight})");
  206. if (!($result || $result2)) {
  207. DB::rollBack();
  208. Session::flash('status', 'danger');
  209. Session::flash('message', 'Rubric could not be created.');
  210. return action('ActivitiesController@show', array($activity->id));
  211. }
  212. }
  213. foreach ($titles as $index => $id) {
  214. DB::insert("insert into rubric_title (`rubric_id`, `title_id`, `position`) values ({$rubric->id},{$id},{$index}) ");
  215. }
  216. Log::info('entré7???');
  217. DB::commit();
  218. Session::flash('status', 'success');
  219. Session::flash('message', 'Rubric updated.');
  220. return action('ActivitiesController@show', array($activity->id));
  221. }
  222. /**
  223. * Remove the specified resource from storage.
  224. *
  225. * @return Response
  226. */
  227. public function destroy()
  228. {
  229. $rubric = Rubric::find(Input::get('id'));
  230. if ($rubric->delete()) {
  231. Session::flash('status', 'success');
  232. Session::flash('message', 'Rubric deleted.');
  233. } else {
  234. Session::flash('status', 'danger');
  235. Session::flash('message', 'Error: The rubric could not be deleted. Try again later.');
  236. }
  237. return;
  238. }
  239. /**
  240. * Show a specific rubric
  241. *
  242. * @return Response
  243. */
  244. public function show($activity_id)
  245. {
  246. $activity = Activity::find($activity_id);
  247. // Get activity's course
  248. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  249. // If activity does not belong to the requesting user, display 403
  250. if ($course->user_id != Auth::id())
  251. App::abort('403', 'Access Forbidden');
  252. Log::info($activity->rubric[0]->id);
  253. $rubric = Rubric::where('id', '=', $activity->rubric[0]->id)->firstOrFail();
  254. $rubric_criterion = DB::table('criteria')
  255. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  256. ->where('rubric_criterion.rubric_id', '=', $activity->rubric[0]->id)
  257. ->get();
  258. Log::info($rubric_criterion);
  259. $rubric->titles = DB::table('titles')
  260. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  261. ->where('rubric_title.rubric_id', $rubric->id)
  262. ->lists('text');
  263. foreach ($rubric_criterion as $single_cr) {
  264. $single_cr->scales = json_encode(DB::table('scales')
  265. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  266. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  267. ->orderBy('position')
  268. ->lists('description'));
  269. $single_cr->outcomes = json_encode(DB::table('outcomes')
  270. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  271. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name'));
  272. }
  273. Log::info($rubric_criterion);
  274. $title = $activity->name . ': ' . $rubric->name;
  275. return View::make('local.professors.viewrubric', compact('rubric', 'activity', 'rubric_criterion', 'title', 'course'));
  276. }
  277. /**
  278. * Show a specific rubric without some course and user information
  279. *
  280. * @return Response
  281. */
  282. public function show_limited($rubric_id)
  283. {
  284. // If user is a professor, display 403.
  285. if (Auth::user()->role == 4)
  286. App::abort('403', 'Access Forbidden');
  287. $rubric = Rubric::where('id', '=', $rubric_id)->firstOrFail();
  288. $title = $rubric->name;
  289. $role = Auth::user()->role;
  290. return View::make('local.managers.shared.view_rubric_limited', compact('rubric', 'title', 'role'));
  291. }
  292. public function download($activity_id, $rubric_id)
  293. {
  294. $activity = Activity::find($activity_id);
  295. // Get activity's course
  296. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  297. // If activity does not belong to the requesting user, display 403
  298. if ($course->user_id != Auth::id())
  299. App::abort('403', 'Access Forbidden');
  300. $rubric = Rubric::where('id', '=', $activity->rubric_id)->firstOrFail();
  301. $rubric_criterion = DB::table('criteria')
  302. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  303. ->where('rubric_criterion.rubric_id', '=', $activity->rubric[0]->id)
  304. ->get();
  305. $rubric->titles = DB::table('titles')
  306. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  307. ->where('rubric_title.rubric_id', $rubric->id)
  308. ->orderBy('position')
  309. ->lists('text');
  310. foreach ($rubric_criterion as $single_cr) {
  311. $single_cr->scales = json_encode(DB::table('scales')
  312. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  313. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  314. ->orderBy('position')
  315. ->lists('description'));
  316. $single_cr->outcomes = json_encode(DB::table('outcomes')
  317. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  318. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name'));
  319. }
  320. $title = $activity->name . ': ' . $rubric->name;
  321. return View::make('local.professors.downloadrubric', compact('rubric', 'rubric_criterion', 'activity', 'title', 'course'));
  322. }
  323. public function printview($activity_id, $rubric_id)
  324. {
  325. $activity = Activity::find($activity_id);
  326. // Get activity's course
  327. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  328. // If activity does not belong to the requesting user, display 403
  329. if ($course->user_id != Auth::id())
  330. App::abort('403', 'Access Forbidden');
  331. $rubric = Rubric::where('id', '=', $activity->rubric[0]->id)->firstOrFail();
  332. $rubric_criterion = DB::table('criteria')
  333. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  334. ->where('rubric_criterion.rubric_id', '=', $activity->rubric[0]->id)
  335. ->get();
  336. $rubric->titles = DB::table('titles')
  337. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  338. ->where('rubric_title.rubric_id', $rubric->id)
  339. ->orderBy('position')
  340. ->lists('text');
  341. foreach ($rubric_criterion as $single_cr) {
  342. $single_cr->scales = json_encode(DB::table('scales')
  343. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  344. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  345. ->orderBy('position')
  346. ->lists('description'));
  347. $single_cr->outcomes = json_encode(DB::table('outcomes')
  348. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  349. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name'));
  350. }
  351. $title = $activity->name . ': ' . $rubric->name;
  352. return View::make('local.professors.printrubric', compact('rubric', 'rubric_criterion', 'activity', 'title', 'course'));
  353. }
  354. public function fetchRubricCriterion()
  355. {
  356. $rubric = DB::table("criterion_scale")
  357. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  358. ->where("criterion_id", '=', Input::get('criterion_id'))
  359. ->orderBy('position')
  360. ->get();
  361. Log::info($rubric);
  362. $rubric["criteria"] = DB::table("criteria")
  363. ->where("criteria.id", '=', Input::get('criterion_id'))
  364. ->select('name', 'notes')
  365. ->first();
  366. return json_encode($rubric);
  367. //$rubric_contents = json_decode($rubric->contents);
  368. //foreach ($rubric_contents as $key => $criterion) {
  369. // if ($criterion->id == $criterion_id) {
  370. // return json_encode($criterion);
  371. // }
  372. }
  373. }