暫無描述

ActivitiesController.php 32KB

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