Нет описания

RubricsController.php 18KB

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