Geen omschrijving

RubricsController.php 20KB

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