Nessuna descrizione

RubricsController.php 19KB

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