Geen omschrijving

ActivitiesController.php 32KB

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