No Description

ActivitiesController.php 39KB

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