Ei kuvausta

ActivitiesController.php 32KB

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