Sin descripción

RubricsController.php 17KB

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