Açıklama Yok

RubricsController.php 19KB

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