Няма описание

ActivitiesController.php 40KB

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