暫無描述

ActivitiesController.php 31KB

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