Nav apraksta

ActivitiesController.php 36KB

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