Нема описа

ActivitiesController.php 32KB

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