Ingen beskrivning

ActivitiesController.php 41KB

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