Нет описания

ActivitiesController.php 31KB

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