설명 없음

ActivitiesController.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. use Illuminate\Database\Eloquent\Collection;
  3. class ActivitiesController extends \BaseController
  4. {
  5. /**
  6. * Save a new activity
  7. *
  8. * @param int $id The id of the parent course
  9. * @return Response Redirect to the parent course's page
  10. */
  11. public function create($id)
  12. {
  13. /** Validation rules */
  14. $validator = Validator::make(
  15. array(
  16. 'name' => Input::get('name'),
  17. 'description' => Input::get('description')
  18. ),
  19. array(
  20. 'name' => 'required|unique:activities,course_id,' . $id,
  21. 'description' => 'required|min:10'
  22. )
  23. );
  24. /** If validation fails */
  25. if ($validator->fails()) {
  26. /** Prepare error message */
  27. $message = 'Error(s) creating a new Activity<ul>';
  28. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  29. $message .= $validationError;
  30. }
  31. $message .= '</ul>';
  32. /** Send error message and old data */
  33. Session::flash('status', 'danger');
  34. Session::flash('message', $message);
  35. return Redirect::back()->withInput();
  36. } else {
  37. /** Instantiate new activity */
  38. $activity = new Activity;
  39. $activity->name = Input::get('name');
  40. $activity->description = Input::get('description');
  41. $activity->course_id = $id;
  42. $activity->date = date('Y-m-d');
  43. /** If activity is saved, send success message */
  44. if ($activity->save()) {
  45. Session::flash('status', 'success');
  46. Session::flash('message', 'Activity created.');
  47. return Redirect::action('ActivitiesController@show', array($activity->id));
  48. }
  49. /** If saving fails, send error message and old data */
  50. else {
  51. Session::flash('status', 'warning');
  52. Session::flash('message', 'Error adding Activity. Please try again later.');
  53. return Redirect::back()->withInput();
  54. }
  55. }
  56. }
  57. public function newCreate($course_id = null)
  58. {
  59. $title = 'Create Activity';
  60. $activity_types = [];
  61. $instruments = Rubric::all();
  62. $courses = Course::where('user_id', Auth::user()->id)->get();
  63. $outcomes = Outcome::with('objectives')->get();
  64. // var_dump($outcomes[0]->objectives);
  65. $objectives_by_outcome = Collection::make([]);
  66. $outcomes->each(function ($outcome) use (&$objectives_by_outcome) {
  67. // var_dump($outcome->objectives);
  68. $objectives_by_outcome->put($outcome->id, $outcome->objectives);
  69. // var_dump($objectives);
  70. });
  71. $criteria_by_objective = Collection::make([]);
  72. $objectives_by_outcome->each(function ($objectives) use (&$criteria_by_objective) {
  73. $objectives->each(function ($objective) use (&$criteria_by_objective) {
  74. $criteria_by_objective->put($objective->id, $objective->criteria);
  75. });
  76. });
  77. $transforming_actions = [];
  78. $course = Course::find($course_id);
  79. // var_dump($criteria_by_objective);
  80. // return $objectives->toJson();
  81. return View::make(
  82. 'local.managers.admins.new-activity-create',
  83. compact(
  84. 'title',
  85. 'course',
  86. 'activity_types',
  87. 'instruments',
  88. 'courses',
  89. 'outcomes',
  90. 'objectives_by_outcome',
  91. 'criteria_by_objective',
  92. 'transforming_actions'
  93. )
  94. );
  95. }
  96. /**
  97. *
  98. */
  99. public function show($id)
  100. {
  101. $activity = Activity::find($id);
  102. // If activity does not exist, display 404
  103. if (!$activity)
  104. App::abort('404');
  105. // Get activity's course
  106. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  107. // If activity does not belong to the requesting user, display 403
  108. if ($course->user_id != Auth::id() and Auth::user()->role==4)
  109. App::abort('403', 'Access Forbidden');
  110. // Get active semesters
  111. $active_semesters = array();
  112. $active_semesters_collection = Semester::select('id')->where('is_visible', 1)->where('start', '<=', date('Y-m-d H:i:s'))->where('end', '>=', date('Y-m-d H:i:s'))->get();
  113. foreach ($active_semesters_collection as $active_semester) {
  114. $active_semesters[] = $active_semester->id;
  115. }
  116. // Added the function htmlspecialchars to activity name string because it was corrupting Jquery code while using quotes on page rendering. - Carlos R Caraballo 1/18/2019
  117. $title = $course->code . $course->number . '-' . $course->section . ': ' . htmlspecialchars($activity->name, ENT_QUOTES) . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  118. $outcomes = Outcome::orderBy('name', 'asc')->get();
  119. $outcomes_achieved = json_decode($activity->outcomes_achieved, true);
  120. $outcomes_attempted = json_decode($activity->outcomes_attempted, true);
  121. return View::make('local.professors.activity', compact('activity', 'title', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'course', 'student_count', 'active_semesters'));
  122. }
  123. public function assess($id)
  124. {
  125. $activity = Activity::find($id);
  126. // If activity does not exist, display 404
  127. if (!$activity)
  128. App::abort('404');
  129. // Get activity's course
  130. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  131. // If activity does not belong to the requesting user, display 403
  132. if ($course->user_id != Auth::id())
  133. App::abort('403', 'Access Forbidden');
  134. $title = 'Assessment Sheet';
  135. $students = $course->students;
  136. // Get rubric contents
  137. $rubric = Rubric::find($activity->rubric_id);
  138. $rubric_contents = json_decode($rubric->contents);
  139. // Get results
  140. $assessments = DB::table('assessments')->join('students', 'assessments.student_id', '=', 'students.id')->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  141. // Decode the scores (blade workaround)
  142. $scores_array = array();
  143. foreach ($assessments as $index => $assessment) {
  144. $scores_array[$assessment->id] = json_decode($assessment->scores, true);
  145. }
  146. return View::make('local.professors.assessment', compact('activity', 'title', 'students', 'course', 'rubric_contents', 'assessments', 'scores_array', 'rubric'));
  147. }
  148. public function saveAssessment()
  149. {
  150. try {
  151. $exception = DB::transaction(function () {
  152. DB::transaction(function () {
  153. // Student assessment data
  154. $student_data = json_decode(Input::get('student_scores'));
  155. // Outcome count
  156. $outcomeCount = Outcome::all()->count();
  157. // Activity
  158. $activity = Activity::find(Input::get('activity_id'));
  159. // Create or update student scores
  160. if ($activity->outcomes_attempted == NULL) {
  161. // For each student, save her/his assessment in the db
  162. foreach ($student_data as $single_student_data) {
  163. // Find student by id
  164. $student = Student::find($single_student_data->student_id);
  165. $comments = trim($single_student_data->comments);
  166. if ($comments == '') {
  167. $comments = NULL;
  168. }
  169. // Add the assessment to the pivot table
  170. $student->assessed_activities()->attach($activity->id, array(
  171. 'scores' => json_encode($single_student_data->scores),
  172. 'percentage' => $single_student_data->percentage,
  173. 'comments' => $single_student_data->comments
  174. ));
  175. }
  176. } else {
  177. // For each student, save her/his assessment in the db
  178. foreach ($student_data as $single_student_data) {
  179. // Find student by id
  180. $student = Student::find($single_student_data->student_id);
  181. $comments = trim($single_student_data->comments);
  182. if ($comments == '') {
  183. $comments = NULL;
  184. }
  185. // Update the assessment in the pivot table
  186. $student->assessed_activities()->updateExistingPivot($activity->id, array(
  187. 'scores' => json_encode($single_student_data->scores),
  188. 'percentage' => $single_student_data->percentage,
  189. 'comments' => $single_student_data->comments
  190. ));
  191. }
  192. }
  193. // Prepare arrays for criteria achievement for this activity
  194. $criteria_achievement = json_decode(Input::get('criteria_achievement'));
  195. $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  196. $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  197. // Fetch parent course's criteria achievement by outcome, if it exists
  198. $course = $activity->course;
  199. $course_outcomes_attempted = NULL;
  200. $course_outcomes_achieved = NULL;
  201. if ($course->outcomes_attempted == NULL) {
  202. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  203. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  204. } else {
  205. // the second argument is necessary to convert it into an array
  206. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  207. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  208. }
  209. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  210. // Find corresponding learning outcome
  211. $criterion = Criterion::withTrashed()->find($criterion_id);
  212. $outcome = Outcome::find($criterion->outcome_id);
  213. // If criterion is achieved (1), add 1 to all arrays
  214. if ($criterion_achieved === 1) {
  215. $outcomes_attempted[$outcome->id] += 1;
  216. $outcomes_achieved[$outcome->id] += 1;
  217. $course_outcomes_attempted[$outcome->id] += 1;
  218. $course_outcomes_achieved[$outcome->id] += 1;
  219. }
  220. // Else if it's 0, only add to the attempted outcomes arrays
  221. elseif ($criterion_achieved === 0) {
  222. $outcomes_attempted[$outcome->id] += 1;
  223. $course_outcomes_attempted[$outcome->id] += 1;
  224. }
  225. }
  226. // If all values are 0, throw exception
  227. if (count(array_unique($outcomes_attempted)) == 1 && $outcomes_attempted[1] == 0)
  228. throw new Exception("Error Processing Request", 1);
  229. // Set activity fields
  230. $activity->criteria_achieved = Input::get('criteria_achievement');
  231. $activity->criteria_achieved_percentage = Input::get('criteria_achieved_percentage');
  232. $activity->outcomes_attempted = json_encode($outcomes_attempted);
  233. $activity->outcomes_achieved = json_encode($outcomes_achieved);
  234. // Publish results if not a draft. That is, update the activity's course.
  235. if (Input::get('draft') == false) {
  236. // Update course
  237. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  238. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  239. $course->save();
  240. $activity->draft = false;
  241. } else {
  242. // Set draft to true
  243. $activity->draft = true;
  244. }
  245. // Save activity
  246. $activity->save();
  247. // Recalculate course outcomes
  248. $activities = DB::table('activities')
  249. ->where('course_id', $activity->course->id)
  250. ->where('draft', 0)
  251. ->get();
  252. // Check if any assessed activities remain
  253. $remainingAssessed = false;
  254. foreach ($activities as $activity1) {
  255. if ($activity1->outcomes_attempted != NULL) {
  256. $remainingAssessed = true;
  257. break;
  258. }
  259. }
  260. //If there are still evaluated activities in the course, recalculate course outcomes
  261. if (count($activities) && $remainingAssessed) {
  262. $outcomeCount = Outcome::all()->count();
  263. // Variables to hold recalculated outcomes for the course
  264. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  265. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  266. // For each activity
  267. foreach ($activities as $activity2) {
  268. // If activity has been assessed
  269. if ($activity2->outcomes_attempted != NULL) {
  270. // Get the achieved criteria
  271. $criteria_achievement = json_decode($activity2->criteria_achieved, true);
  272. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  273. // Find corresponding learning outcome;
  274. $criterion = Criterion::withTrashed()->find($criterion_id);
  275. $outcome = Outcome::find($criterion->outcome_id);
  276. // If criterion is achieved (1), add 1 to both arrays
  277. if ($criterion_achieved === 1) {
  278. $course_outcomes_attempted[$outcome->id] += 1;
  279. $course_outcomes_achieved[$outcome->id] += 1;
  280. }
  281. // Else, only add to the attempted outcomes arrays
  282. elseif ($criterion_achieved === 0) {
  283. $course_outcomes_attempted[$outcome->id] += 1;
  284. }
  285. }
  286. }
  287. }
  288. // Update course
  289. DB::table('courses')
  290. ->where('id', $course->id)
  291. ->update(array(
  292. 'outcomes_attempted' => json_encode($course_outcomes_attempted),
  293. 'outcomes_achieved' => json_encode($course_outcomes_achieved),
  294. 'updated_at' => date('Y-m-d H:i:s')
  295. ));
  296. }
  297. // Otherwise, set them all to NULL
  298. else {
  299. DB::table('courses')
  300. ->where('id', $course->id)
  301. ->update(array(
  302. 'outcomes_attempted' => NULL,
  303. 'outcomes_achieved' => NULL,
  304. 'updated_at' => date('Y-m-d H:i:s')
  305. ));
  306. }
  307. });
  308. });
  309. if (is_null($exception)) {
  310. Session::flash('status', 'success');
  311. Session::flash('message', 'Assessment Saved. To add transforming actions click "Transforming Actions".');
  312. return action('ActivitiesController@show', array(Input::get('activity_id')));
  313. }
  314. } catch (Exception $e) {
  315. Log::info('e:' . $e);
  316. echo $e->getMessage();
  317. Session::flash('status', 'danger');
  318. Session::flash('message', 'Error saving assessment. Try again later.');
  319. return action('ActivitiesController@show', array(Input::get('activity_id')));
  320. }
  321. }
  322. public function deleteAssessment()
  323. {
  324. try {
  325. $exception = DB::transaction(function () {
  326. $activity = DB::table('activities')->where('id', Input::get('id'))->first();
  327. $course = DB::table('courses')->where('id', $activity->course_id)->first();
  328. // Reset results in activity
  329. DB::table('activities')
  330. ->where('id', Input::get('id'))
  331. ->update(array(
  332. 'draft' => 0,
  333. 'outcomes_attempted' => NULL,
  334. 'outcomes_achieved' => NULL,
  335. 'criteria_achieved' => NULL,
  336. 'transforming_actions' => NULL,
  337. 'assessment_comments' => NULL,
  338. 'criteria_achieved_percentage' => NULL,
  339. 'updated_at' => date('Y-m-d H:i:s')
  340. ));
  341. // Delete students score
  342. DB::table('assessments')->where('activity_id', $activity->id)->delete();
  343. // Recalculate course outcomes
  344. $activities = DB::table('activities')
  345. ->where('course_id', $course->id)
  346. ->where('draft', 0)
  347. ->get();
  348. // Check if any assessed activties remain
  349. $remainingAssessed = false;
  350. foreach ($activities as $activity) {
  351. if ($activity->outcomes_attempted != NULL) {
  352. $remainingAssessed = true;
  353. break;
  354. }
  355. }
  356. //If there are still evaluated activities in the course, recalculate course outcomes
  357. if (count($activities) && $remainingAssessed) {
  358. $outcomeCount = Outcome::all()->count();
  359. // Variables to hold recalculated outcomes for the course
  360. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  361. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  362. // For each activity
  363. foreach ($activities as $activity) {
  364. // If activity has been assessed
  365. if ($activity->outcomes_attempted != NULL) {
  366. // Get the achieved criteria
  367. $criteria_achievement = json_decode($activity->criteria_achieved, true);
  368. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  369. // Find corresponding learning outcome;
  370. $criterion = Criterion::withTrashed()->find($criterion_id);
  371. $outcome = Outcome::find($criterion->outcome_id);
  372. // If criterion is achieved (1), add 1 to both arrays
  373. if ($criterion_achieved === 1) {
  374. $course_outcomes_attempted[$outcome->id] += 1;
  375. $course_outcomes_achieved[$outcome->id] += 1;
  376. }
  377. // Else, only add to the attempted outcomes arrays
  378. elseif ($criterion_achieved === 0) {
  379. $course_outcomes_attempted[$outcome->id] += 1;
  380. }
  381. }
  382. }
  383. }
  384. // Update course
  385. DB::table('courses')
  386. ->where('id', $course->id)
  387. ->update(array(
  388. 'outcomes_attempted' => json_encode($course_outcomes_attempted),
  389. 'outcomes_achieved' => json_encode($course_outcomes_achieved),
  390. 'updated_at' => date('Y-m-d H:i:s')
  391. ));
  392. }
  393. // Otherwise, set them all to NULL
  394. else {
  395. DB::table('courses')
  396. ->where('id', $course->id)
  397. ->update(array(
  398. 'outcomes_attempted' => NULL,
  399. 'outcomes_achieved' => NULL,
  400. 'updated_at' => date('Y-m-d H:i:s')
  401. ));
  402. }
  403. });
  404. if (is_null($exception)) {
  405. Session::flash('status', 'success');
  406. Session::flash('message', 'Assessment deleted.');
  407. return Redirect::back();
  408. }
  409. } catch (Exception $e) {
  410. Session::flash('status', 'danger');
  411. Session::flash('message', 'Error saving assessment. Try again later.');
  412. return Redirect::back();
  413. }
  414. }
  415. public function destroy($id)
  416. {
  417. $course = Activity::find($id)->course;
  418. if (Activity::destroy($id)) {
  419. // Recalculate course outcomes
  420. $activities = $course->activities;
  421. // Check if any assessed activties remain
  422. $remainingAssessed = false;
  423. foreach ($course->activities as $activity) {
  424. if ($activity->outcomes_attempted != NULL) {
  425. $remainingAssessed = true;
  426. break;
  427. }
  428. }
  429. //If there are still evaluated activities in the course, recalculate course outcomes
  430. if (!$course->activities->isEmpty() && $remainingAssessed) {
  431. $outcomeCount = Outcome::all()->count();
  432. // Variables to hold recalculated outcomes for the course
  433. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  434. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  435. // For each activity
  436. foreach ($activities as $activity) {
  437. // If activity has been assessed
  438. if ($activity->outcomes_attempted != NULL) {
  439. // Get the achieved criteria
  440. $criteria_achievement = json_decode($activity->criteria_achieved, true);
  441. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  442. // Find corresponding learning outcome;
  443. $criterion = Criterion::withTrashed()->find($criterion_id);
  444. $outcome = Outcome::find($criterion->outcome_id);
  445. // If criterion is achieved (1), add 1 to both arrays
  446. if ($criterion_achieved === 1) {
  447. $course_outcomes_attempted[$outcome->id] += 1;
  448. $course_outcomes_achieved[$outcome->id] += 1;
  449. }
  450. // Else, only add to the attempted outcomes arrays
  451. elseif ($criterion_achieved === 0) {
  452. $course_outcomes_attempted[$outcome->id] += 1;
  453. }
  454. }
  455. }
  456. }
  457. // Update course
  458. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  459. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  460. } else {
  461. $course->outcomes_achieved = NULL;
  462. $course->outcomes_attempted = NULL;
  463. }
  464. if ($course->save()) {
  465. Session::flash('status', 'success');
  466. Session::flash('message', 'Activity deleted.');
  467. } else {
  468. Session::flash('status', 'danger');
  469. Session::flash('message', 'Error deleting activity. Try again later.');
  470. return Redirect::back();
  471. }
  472. return Redirect::action('CoursesController@show', array($course->id));
  473. } else {
  474. Session::flash('status', 'danger');
  475. Session::flash('message', 'Error deleting activity. Try again later.');
  476. return Redirect::back();
  477. }
  478. }
  479. public function update($id)
  480. {
  481. try {
  482. $activity = Activity::find($id);
  483. if (Input::has('update_activity_information')) {
  484. /** Validation rules */
  485. $validator = Validator::make(
  486. array(
  487. 'name' => Input::get('name'),
  488. 'description' => Input::get('description'),
  489. 'date' => Input::get('date'),
  490. ),
  491. array(
  492. 'name' => 'required|unique:activities,course_id,' . $id,
  493. 'description' => 'required|min:10',
  494. 'date' => 'required|dateFormat:Y-m-d'
  495. ),
  496. array(
  497. 'date.dateFormat' => 'The date does not match the correct format: yyyy-mm-dd.'
  498. )
  499. );
  500. /** If validation fails */
  501. if ($validator->fails()) {
  502. /** Prepare error message */
  503. $message = 'Error(s) updating the Activity<ul>';
  504. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  505. $message .= $validationError;
  506. }
  507. $message .= '</ul>';
  508. /** Send error message and old data */
  509. Session::flash('status', 'warning');
  510. Session::flash('message', $message);
  511. return Redirect::back()->withInput();
  512. }
  513. /** Update activity info */
  514. $activity->name = Input::get('name');
  515. $activity->description = Input::get('description');
  516. $activity->date = Input::get('date');
  517. } elseif (Input::has('update_transforming_actions')) {
  518. if (trim(Input::get('transforming_actions')) != "")
  519. $activity->transforming_actions = Input::get('transforming_actions');
  520. else
  521. $activity->transforming_actions = NULL;
  522. } elseif (Input::has('update_assessment_comments')) {
  523. if (trim(Input::get('assessment_comments')) != "")
  524. $activity->assessment_comments = Input::get('assessment_comments');
  525. else
  526. $activity->assessment_comments = NULL;
  527. } else {
  528. Session::flash('status', 'danger');
  529. Session::flash('message', 'Error updating Activity. Please try again later.');
  530. return Redirect::action('ActivitiesController@show', array($activity->id));
  531. }
  532. $activity->save();
  533. /** If activity is saved, send success message */
  534. Session::flash('status', 'success');
  535. Session::flash('message', 'Activity succesfully updated.');
  536. return Redirect::action('ActivitiesController@show', array($activity->id));
  537. } catch (Exception $e) {
  538. Session::flash('status', 'warning');
  539. Session::flash('message', 'Error updating Activity. Please try again later.');
  540. return Redirect::action('ActivitiesController@show', array($activity->id));
  541. }
  542. }
  543. //TODO the code in the next 2 functions is the same as the assess function except for the view returned. try to refactor this to avoid copying code.
  544. public function viewAssessment($id)
  545. {
  546. $activity = Activity::find($id);
  547. // If activity does not exist, display 404
  548. if (!$activity)
  549. App::abort('404');
  550. // Get activity's course
  551. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  552. // If activity does not belong to the requesting user, display 403
  553. if ($course->user_id != Auth::id())
  554. App::abort('403', 'Access Forbidden');
  555. $title = 'Assessment Sheet';
  556. $students = $course->students;
  557. // Get rubric contents
  558. $rubric_contents = Rubric::select('contents')->where('id', '=', $activity->rubric_id)->get();
  559. $rubric_contents = json_decode($rubric_contents['0']->contents);
  560. $rubric = Rubric::find($activity->rubric_id);
  561. // Get results
  562. $assessments = DB::table('assessments')->where('activity_id', '=', $activity->id)->orderBy('id', 'asc')->get();
  563. // Decode the scores (blade workaround)
  564. $scores_array = array();
  565. foreach ($assessments as $index => $assessment) {
  566. $scores_array[$assessment->id] = json_decode($assessment->scores, true);
  567. }
  568. return View::make('local.professors.view_assessment', compact('activity', 'title', 'students', 'course', 'rubric_contents', 'assessments', 'scores_array', 'rubric'));
  569. }
  570. public function printAssessment($id)
  571. {
  572. $activity = Activity::find($id);
  573. // If activity does not exist, display 404
  574. if (!$activity)
  575. App::abort('404');
  576. // Get activity's course
  577. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  578. // If activity does not belong to the requesting user, display 403
  579. if ($course->user_id != Auth::id())
  580. App::abort('403', 'Access Forbidden');
  581. $title = 'Assessment Sheet';
  582. $students = $course->students;
  583. // Get rubric contents
  584. $rubric_contents = Rubric::select('contents')->where('id', '=', $activity->rubric_id)->get();
  585. $rubric_contents = json_decode($rubric_contents['0']->contents);
  586. $rubric = Rubric::find($activity->rubric_id);
  587. // Get results
  588. $assessments = DB::table('assessments')->where('activity_id', '=', $activity->id)->orderBy('id', 'asc')->get();
  589. // Decode the scores (blade workaround)
  590. $scores_array = array();
  591. foreach ($assessments as $index => $assessment) {
  592. $scores_array[$assessment->id] = json_decode($assessment->scores, true);
  593. }
  594. return View::make('local.professors.print_assessment', compact('activity', 'title', 'students', 'course', 'rubric_contents', 'assessments', 'scores_array', 'rubric'));
  595. }
  596. }