Nessuna descrizione

RubricsController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. $activity_id = Input::get("activity_id");
  84. $rubricId = $rubric->id;
  85. foreach ($criteria as $index => $criterion_id) {
  86. DB::insert("insert into rubric_criterion (`rubric_id`,`criterion_id`, `position`) values ({$rubricId},{$criterion_id}, {$index})");
  87. DB::commit();
  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. DB::delete("delete from rubric_title where rubric_id = {$rubric->id}");
  204. foreach ($criteria as $index => $criterion_id) {
  205. $result = DB::insert("insert into rubric_criterion (`rubric_id`, `criterion_id`, `position`) values ({$rubric->id}, {$criterion_id}, {$index}) ");
  206. $result2 = DB::insert("insert into `activity_criterion` (`activity_id`,`criterion_id`, `weight`) values ({$activity->id}, {$criterion_id}, {$defaultWeight})");
  207. if (!($result || $result2)) {
  208. DB::rollBack();
  209. Session::flash('status', 'danger');
  210. Session::flash('message', 'Rubric could not be created.');
  211. return action('ActivitiesController@show', array($activity->id));
  212. }
  213. }
  214. foreach ($titles as $index => $id) {
  215. DB::insert("insert into rubric_title (`rubric_id`, `title_id`, `position`) values ({$rubric->id},{$id},{$index}) ");
  216. }
  217. Log::info('entré7???');
  218. DB::commit();
  219. Session::flash('status', 'success');
  220. Session::flash('message', 'Rubric updated.');
  221. return action('ActivitiesController@show', array($activity->id));
  222. }
  223. /**
  224. * Remove the specified resource from storage.
  225. *
  226. * @return Response
  227. */
  228. public function destroy()
  229. {
  230. $rubric = Rubric::find(Input::get('id'));
  231. if ($rubric->delete()) {
  232. Session::flash('status', 'success');
  233. Session::flash('message', 'Rubric deleted.');
  234. } else {
  235. Session::flash('status', 'danger');
  236. Session::flash('message', 'Error: The rubric could not be deleted. Try again later.');
  237. }
  238. return;
  239. }
  240. /**
  241. * Show a specific rubric
  242. *
  243. * @return Response
  244. */
  245. public function show($activity_id)
  246. {
  247. $activity = Activity::find($activity_id);
  248. // Get activity's course
  249. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  250. // If activity does not belong to the requesting user, display 403
  251. if ($course->user_id != Auth::id())
  252. App::abort('403', 'Access Forbidden');
  253. Log::info($activity->rubric[0]->id);
  254. $rubric = Rubric::where('id', '=', $activity->rubric[0]->id)->firstOrFail();
  255. $rubric_criterion = DB::table('criteria')
  256. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  257. ->where('rubric_criterion.rubric_id', '=', $activity->rubric[0]->id)
  258. ->get();
  259. Log::info($rubric_criterion);
  260. $rubric->titles = DB::table('titles')
  261. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  262. ->where('rubric_title.rubric_id', $rubric->id)
  263. ->lists('text');
  264. foreach ($rubric_criterion as $single_cr) {
  265. $single_cr->scales = json_encode(DB::table('scales')
  266. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  267. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  268. ->orderBy('position')
  269. ->lists('description'));
  270. $single_cr->outcomes = json_encode(DB::table('outcomes')
  271. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  272. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name'));
  273. }
  274. Log::info($rubric_criterion);
  275. $title = $activity->name . ': ' . $rubric->name;
  276. return View::make('local.professors.viewrubric', compact('rubric', 'activity', 'rubric_criterion', 'title', 'course'));
  277. }
  278. /**
  279. * Show a specific rubric without some course and user information
  280. *
  281. * @return Response
  282. */
  283. public function show_limited($rubric_id)
  284. {
  285. // If user is a professor, display 403.
  286. if (Auth::user()->role == 4)
  287. App::abort('403', 'Access Forbidden');
  288. $rubric = Rubric::where('id', '=', $rubric_id)->firstOrFail();
  289. $rubric_criterion = DB::table('criteria')
  290. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  291. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  292. ->get();
  293. $rubric->titles = DB::table('titles')
  294. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  295. ->where('rubric_title.rubric_id', $rubric->id)
  296. ->orderBy('position')
  297. ->lists('text');
  298. foreach ($rubric_criterion as $single_cr) {
  299. $single_cr->scales = DB::table('scales')
  300. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  301. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  302. ->orderBy('position')
  303. ->lists('description');
  304. $single_cr->outcomes = DB::table('outcomes')
  305. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  306. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name');
  307. }
  308. $title = $rubric->name;
  309. $role = Auth::user()->role;
  310. return View::make('local.managers.shared.view_rubric_limited', compact('rubric', 'rubric_criterion', 'title', 'role'));
  311. }
  312. public function download($activity_id, $rubric_id)
  313. {
  314. $activity = Activity::find($activity_id);
  315. // Get activity's course
  316. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  317. // If activity does not belong to the requesting user, display 403
  318. if ($course->user_id != Auth::id())
  319. App::abort('403', 'Access Forbidden');
  320. $rubric = Rubric::where('id', '=', $activity->rubric_id)->firstOrFail();
  321. $rubric_criterion = DB::table('criteria')
  322. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  323. ->where('rubric_criterion.rubric_id', '=', $activity->rubric[0]->id)
  324. ->get();
  325. $rubric->titles = DB::table('titles')
  326. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  327. ->where('rubric_title.rubric_id', $rubric->id)
  328. ->orderBy('position')
  329. ->lists('text');
  330. foreach ($rubric_criterion as $single_cr) {
  331. $single_cr->scales = json_encode(DB::table('scales')
  332. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  333. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  334. ->orderBy('position')
  335. ->lists('description'));
  336. $single_cr->outcomes = json_encode(DB::table('outcomes')
  337. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  338. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name'));
  339. }
  340. $title = $activity->name . ': ' . $rubric->name;
  341. return View::make('local.professors.downloadrubric', compact('rubric', 'rubric_criterion', '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[0]->id)->firstOrFail();
  352. $rubric_criterion = DB::table('criteria')
  353. ->join('rubric_criterion', 'rubric_criterion.criterion_id', '=', 'criteria.id')
  354. ->where('rubric_criterion.rubric_id', '=', $activity->rubric[0]->id)
  355. ->get();
  356. $rubric->titles = DB::table('titles')
  357. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  358. ->where('rubric_title.rubric_id', $rubric->id)
  359. ->orderBy('position')
  360. ->lists('text');
  361. foreach ($rubric_criterion as $single_cr) {
  362. $single_cr->scales = json_encode(DB::table('scales')
  363. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  364. ->where('criterion_scale.criterion_id', '=', $single_cr->criterion_id)
  365. ->orderBy('position')
  366. ->lists('description'));
  367. $single_cr->outcomes = json_encode(DB::table('outcomes')
  368. ->join('criterion_objective_outcome', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  369. ->where('criterion_objective_outcome.criterion_id', '=', $single_cr->criterion_id)->lists('name'));
  370. }
  371. $title = $activity->name . ': ' . $rubric->name;
  372. return View::make('local.professors.printrubric', compact('rubric', 'rubric_criterion', 'activity', 'title', 'course'));
  373. }
  374. public function fetchRubricCriterion()
  375. {
  376. Log::info(Input::all());
  377. $rubric = DB::table("criterion_scale")
  378. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  379. ->where("criterion_id", '=', Input::get('criterion_id'))
  380. ->orderBy('position')
  381. ->get();
  382. Log::info($rubric);
  383. $rubric["criteria"] = DB::table("criteria")
  384. ->where("criteria.id", '=', Input::get('criterion_id'))
  385. ->select('name', 'notes')
  386. ->first();
  387. return json_encode($rubric);
  388. //$rubric_contents = json_decode($rubric->contents);
  389. //foreach ($rubric_contents as $key => $criterion) {
  390. // if ($criterion->id == $criterion_id) {
  391. // return json_encode($criterion);
  392. // }
  393. }
  394. }