Açıklama Yok

ActivitiesController.php 36KB

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