No Description

ActivitiesController.php 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. // Gabriel añadió aquí
  43. $activity->draft = 1;
  44. //
  45. $activity->date = date('Y-m-d');
  46. /** If activity is saved, send success message */
  47. if ($activity->save()) {
  48. Session::flash('status', 'success');
  49. Session::flash('message', 'Activity created.');
  50. return Redirect::action('ActivitiesController@show', array($activity->id));
  51. }
  52. /** If saving fails, send error message and old data */
  53. else {
  54. Session::flash('status', 'warning');
  55. Session::flash('message', 'Error adding Activity. Please try again later.');
  56. return Redirect::back()->withInput();
  57. }
  58. }
  59. }
  60. public function newCreate($course_id = null)
  61. {
  62. $title = 'Create Activity';
  63. $activity_types = [];
  64. $instruments = Rubric::all();
  65. $courses = Course::where('user_id', Auth::user()->id)->get();
  66. $outcomes = Outcome::with('objectives')->get();
  67. // var_dump($outcomes[0]->objectives);
  68. $objectives_by_outcome = Collection::make([]);
  69. $outcomes->each(function ($outcome) use (&$objectives_by_outcome) {
  70. // var_dump($outcome->objectives);
  71. $objectives_by_outcome->put($outcome->id, $outcome->objectives);
  72. // var_dump($objectives);
  73. });
  74. $criteria_by_objective = Collection::make([]);
  75. $objectives_by_outcome->each(function ($objectives) use (&$criteria_by_objective) {
  76. $objectives->each(function ($objective) use (&$criteria_by_objective) {
  77. $criteria_by_objective->put($objective->id, $objective->criteria);
  78. });
  79. });
  80. $transforming_actions = [];
  81. $course = Course::find($course_id);
  82. // var_dump($criteria_by_objective);
  83. // return $objectives->toJson();
  84. return View::make(
  85. 'local.managers.admins.new-activity-create',
  86. compact(
  87. 'title',
  88. 'course',
  89. 'activity_types',
  90. 'instruments',
  91. 'courses',
  92. 'outcomes',
  93. 'objectives_by_outcome',
  94. 'criteria_by_objective',
  95. 'transforming_actions'
  96. )
  97. );
  98. }
  99. /**
  100. *
  101. */
  102. public function show($id)
  103. {
  104. $activity = Activity::find($id);
  105. // If activity does not exist, display 404
  106. if (!$activity)
  107. App::abort('404');
  108. // Get activity's course
  109. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  110. // If activity does not belong to the requesting user, display 403
  111. if ($course->user_id != Auth::id() and Auth::user()->role == 4)
  112. App::abort('403', 'Access Forbidden');
  113. // Get active semesters
  114. $active_semesters = array();
  115. $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();
  116. foreach ($active_semesters_collection as $active_semester) {
  117. $active_semesters[] = $active_semester->id;
  118. }
  119. Log::info($active_semesters);
  120. // 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
  121. $title = $course->code . $course->number . '-' . $course->section . ': ' . htmlspecialchars($activity->name, ENT_QUOTES) . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  122. $outcomes = Outcome::orderBy('name', 'asc')->get();
  123. $assessment = DB::table('assessments')
  124. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  125. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  126. ->where('activity_id', $activity->id)
  127. ->get();
  128. if ($assessment) {
  129. $outcomes_achieved = $activity->o_ach_array;
  130. $outcomes_attempted = $activity->o_att_array;
  131. } else {
  132. $outcomes_achieved = [];
  133. $outcomes_attempted = [];
  134. }
  135. Log::info($outcomes_achieved);
  136. Log::info($outcomes_achieved);
  137. $activity_criterion = DB::table('criteria')
  138. ->join('activity_criterion', 'criteria.id', '=', 'activity_criterion.criterion_id')
  139. ->where('activity_id', $activity->id)
  140. ->select('activity_criterion.id', 'activity_criterion.criterion_id')
  141. ->addSelect('criteria.name')
  142. ->get();
  143. $transformative_actions = DB::table('transformative_activity_criterion')
  144. ->join('activity_criterion', 'transformative_activity_criterion.activity_criterion_id', '=', 'activity_criterion.id')
  145. ->join('transformative_actions', 'transformative_activity_criterion.trans_action_id', '=', 'transformative_actions.id')
  146. ->where('activity_criterion.activity_id', $id)
  147. ->get();
  148. return View::make('local.professors.activity', compact('activity', 'transformative_actions', 'activity_criterion', 'title', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'course', 'student_count', 'active_semesters'));
  149. }
  150. public function assess($id)
  151. {
  152. $activity = Activity::find($id);
  153. // If activity does not exist, display 404
  154. if (!$activity)
  155. App::abort('404');
  156. // Get activity's course
  157. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  158. // If activity does not belong to the requesting user, display 403
  159. if ($course->user_id != Auth::id())
  160. App::abort('403', 'Access Forbidden');
  161. $title = 'Assessment Sheet';
  162. $students = $course->students;
  163. // Get rubric contents
  164. $rubric = Rubric::find($activity->rubric[0]->id);
  165. $rubric->titles = DB::table('titles')
  166. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  167. ->where('rubric_title.rubric_id', '=', $rubric->id)
  168. ->lists('text');
  169. Log::info($rubric);
  170. Log::info($activity);
  171. $rubric_criterion = DB::table('criteria')
  172. ->join("rubric_criterion", "rubric_criterion.criterion_id", "=", "criteria.id")
  173. ->join("activity_criterion", "criteria.id", '=', 'activity_criterion.criterion_id')
  174. ->where("activity_criterion.activity_id", '=', $activity->id)
  175. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  176. ->select('criteria.name', 'criteria.id as criterion_id', 'criteria.subcriteria')
  177. ->addSelect('activity_criterion.activity_id', 'activity_criterion.weight', 'activity_criterion.id as activity_criterion_id')
  178. ->addSelect('rubric_criterion.rubric_id', 'rubric_criterion.id as rubric_criterion_id')
  179. ->get();
  180. Log::info("EN mi cuarto o o o");
  181. Log::info($rubric_criterion);
  182. foreach ($rubric_criterion as $index => $singleCR) {
  183. $singleCR->scales = json_encode(DB::table('scales')
  184. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  185. ->where('criterion_scale.criterion_id', '=', $singleCR->criterion_id)
  186. ->orderBy('position')
  187. ->lists('description'));
  188. }
  189. $rubric_criterion_ids = DB::table('rubric_criterion')->where('rubric_id', '=', $rubric->id)->lists('id');
  190. Log::info($rubric);
  191. Log::info($rubric_criterion);
  192. // Get results
  193. $activity_criterion_ids = DB::table('activity_criterion')->where("activity_id", '=', $activity->id)->lists('id');
  194. Log::info($activity_criterion_ids);
  195. $assessments = DB::table('assessments')
  196. ->join('students', 'assessments.student_id', '=', 'students.id')
  197. ->whereIn('activity_criterion_id', $activity_criterion_ids)
  198. ->orderBy('assessments.id', 'asc')->get();
  199. Log::info($assessments);
  200. // Decode the scores (blade workaround)
  201. $scores_array = array();
  202. foreach ($assessments as $index => $assessment) {
  203. $scores_array[$assessment->student_id][$assessment->activity_criterion_id] = $assessment->score;
  204. $scores_array[$assessment->student_id]['comments'] = DB::table('activity_student')->where('student_id', '=', $assessment->student_id)
  205. ->where("activity_id", '=', $activity->id)
  206. ->select('comments')->first()->comments;
  207. }
  208. Log::info($assessments);
  209. Log::info($scores_array);
  210. return View::make('local.professors.assessment', compact('activity', 'title', 'students', 'course', 'rubric_criterion', 'assessments', 'scores_array', 'rubric'));
  211. }
  212. public function saveAssessment()
  213. {
  214. try {
  215. $exception = DB::transaction(function () {
  216. DB::transaction(function () {
  217. // Student assessment data
  218. $activity_id = Input::get('activity_id');
  219. $student_data = json_decode(Input::get('student_info'));
  220. $weights = json_decode(Input::get('weights'));
  221. Log::info(json_encode($weights));
  222. Log::info(json_encode($student_data));
  223. foreach ($student_data as $index => $student_dict) {
  224. $student_id = $student_dict->studentId;
  225. foreach ($student_dict->activity_crit_id as $act_crit_id => $score) {
  226. if (DB::table('assessments')->where('student_id', '=', $student_id)
  227. ->where('activity_criterion_id', '=', $act_crit_id)
  228. ->first()
  229. ) {
  230. DB::table('assessments')->where('student_id', '=', $student_id)
  231. ->where('activity_criterion_id', '=', $act_crit_id)
  232. ->update(array('score' => $score));
  233. } else {
  234. DB::insert("insert into assessments (`activity_criterion_id`, `student_id`, `score`) values ({$act_crit_id}, {$student_id}, {$score})");
  235. }
  236. }
  237. if (DB::table('activity_student')
  238. ->where('student_id', '=', $student_id)->where('activity_id', '=', $activity_id)
  239. ->first()
  240. ) {
  241. DB::table('activity_student')
  242. ->where('student_id', '=', $student_id)->where('activity_id', '=', $activity_id)
  243. ->update(array('comments' => $student_dict->comments));
  244. } else {
  245. DB::insert("insert into activity_student (`activity_id`, `student_id`, `comments`) values ({$activity_id}, {$student_id}, '{$student_dict->comments}')");
  246. }
  247. }
  248. $activity_draft = Input::get('draft');
  249. Log::info(json_encode($weights));
  250. foreach ($weights as $act_crit => $weigh) {
  251. DB::update("update activity_criterion set weight = {$weigh} where id = {$act_crit}");
  252. }
  253. DB::update("update activities set draft = {$activity_draft} where id = {$activity_id}");
  254. // Outcome count
  255. Session::flash('status', 'success');
  256. Session::flash('message', 'Assessment Saved. To add transforming actions click "Transforming Actions".');
  257. return action('ActivitiesController@show', array(Input::get('activity_id')));
  258. $outcomeCount = Outcome::all()->count();
  259. // Activity
  260. $activity = Activity::find(Input::get('activity_id'));
  261. // Create or update student scores
  262. if ($activity->outcomes_attempted == NULL) {
  263. // For each student, save her/his assessment in the db
  264. foreach ($student_data as $single_student_data) {
  265. // Find student by id
  266. $student = Student::find($single_student_data->student_id);
  267. $comments = trim($single_student_data->comments);
  268. if ($comments == '') {
  269. $comments = NULL;
  270. }
  271. // Add the assessment to the pivot table
  272. $student->assessed_activities()->attach($activity->id, array(
  273. 'scores' => json_encode($single_student_data->scores),
  274. 'comments' => $single_student_data->comments
  275. ));
  276. }
  277. } else {
  278. // For each student, save her/his assessment in the db
  279. foreach ($student_data as $single_student_data) {
  280. // Find student by id
  281. $student = Student::find($single_student_data->student_id);
  282. $comments = trim($single_student_data->comments);
  283. if ($comments == '') {
  284. $comments = NULL;
  285. }
  286. // Update the assessment in the pivot table
  287. $student->assessed_activities()->updateExistingPivot($activity->id, array(
  288. 'scores' => json_encode($single_student_data->scores),
  289. 'percentage' => $single_student_data->percentage,
  290. 'comments' => $single_student_data->comments
  291. ));
  292. }
  293. }
  294. // Prepare arrays for criteria achievement for this activity
  295. $criteria_achievement = json_decode(Input::get('criteria_achievement'));
  296. $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  297. $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  298. // Fetch parent course's criteria achievement by outcome, if it exists
  299. $course = $activity->course;
  300. $course_outcomes_attempted = NULL;
  301. $course_outcomes_achieved = NULL;
  302. if ($course->outcomes_attempted == NULL) {
  303. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  304. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  305. } else {
  306. // the second argument is necessary to convert it into an array
  307. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  308. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  309. }
  310. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  311. // Find corresponding learning outcome
  312. $criterion = Criterion::withTrashed()->find($criterion_id);
  313. $outcome = Outcome::find($criterion->outcome_id);
  314. // If criterion is achieved (1), add 1 to all arrays
  315. if ($criterion_achieved === 1) {
  316. $outcomes_attempted[$outcome->id] += 1;
  317. $outcomes_achieved[$outcome->id] += 1;
  318. $course_outcomes_attempted[$outcome->id] += 1;
  319. $course_outcomes_achieved[$outcome->id] += 1;
  320. }
  321. // Else if it's 0, only add to the attempted outcomes arrays
  322. elseif ($criterion_achieved === 0) {
  323. $outcomes_attempted[$outcome->id] += 1;
  324. $course_outcomes_attempted[$outcome->id] += 1;
  325. }
  326. }
  327. // If all values are 0, throw exception
  328. if (count(array_unique($outcomes_attempted)) == 1 && $outcomes_attempted[1] == 0)
  329. throw new Exception("Error Processing Request", 1);
  330. // Set activity fields
  331. $activity->criteria_achieved = Input::get('criteria_achievement');
  332. $activity->criteria_achieved_percentage = Input::get('criteria_achieved_percentage');
  333. $activity->outcomes_attempted = json_encode($outcomes_attempted);
  334. $activity->outcomes_achieved = json_encode($outcomes_achieved);
  335. // Publish results if not a draft. That is, update the activity's course.
  336. if (Input::get('draft') == false) {
  337. // Update course
  338. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  339. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  340. $course->save();
  341. $activity->draft = false;
  342. } else {
  343. // Set draft to true
  344. $activity->draft = true;
  345. }
  346. // Save activity
  347. $activity->save();
  348. // Recalculate course outcomes
  349. $activities = DB::table('activities')
  350. ->where('course_id', $activity->course->id)
  351. ->where('draft', 0)
  352. ->get();
  353. // Check if any assessed activities remain
  354. $remainingAssessed = false;
  355. foreach ($activities as $activity1) {
  356. if ($activity1->outcomes_attempted != NULL) {
  357. $remainingAssessed = true;
  358. break;
  359. }
  360. }
  361. //If there are still evaluated activities in the course, recalculate course outcomes
  362. if (count($activities) && $remainingAssessed) {
  363. $outcomeCount = Outcome::all()->count();
  364. // Variables to hold recalculated outcomes for the course
  365. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  366. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  367. // For each activity
  368. foreach ($activities as $activity2) {
  369. // If activity has been assessed
  370. if ($activity2->outcomes_attempted != NULL) {
  371. // Get the achieved criteria
  372. $criteria_achievement = json_decode($activity2->criteria_achieved, true);
  373. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  374. // Find corresponding learning outcome;
  375. $criterion = Criterion::withTrashed()->find($criterion_id);
  376. $outcome = Outcome::find($criterion->outcome_id);
  377. // If criterion is achieved (1), add 1 to both arrays
  378. if ($criterion_achieved === 1) {
  379. $course_outcomes_attempted[$outcome->id] += 1;
  380. $course_outcomes_achieved[$outcome->id] += 1;
  381. }
  382. // Else, only add to the attempted outcomes arrays
  383. elseif ($criterion_achieved === 0) {
  384. $course_outcomes_attempted[$outcome->id] += 1;
  385. }
  386. }
  387. }
  388. }
  389. // Update course
  390. DB::table('courses')
  391. ->where('id', $course->id)
  392. ->update(array(
  393. 'outcomes_attempted' => json_encode($course_outcomes_attempted),
  394. 'outcomes_achieved' => json_encode($course_outcomes_achieved),
  395. 'updated_at' => date('Y-m-d H:i:s')
  396. ));
  397. }
  398. // Otherwise, set them all to NULL
  399. else {
  400. DB::table('courses')
  401. ->where('id', $course->id)
  402. ->update(array(
  403. 'outcomes_attempted' => NULL,
  404. 'outcomes_achieved' => NULL,
  405. 'updated_at' => date('Y-m-d H:i:s')
  406. ));
  407. }
  408. });
  409. });
  410. if (is_null($exception)) {
  411. Session::flash('status', 'success');
  412. Session::flash('message', 'Assessment Saved. To add transforming actions click "Transforming Actions".');
  413. return action('ActivitiesController@show', array(Input::get('activity_id')));
  414. }
  415. } catch (Exception $e) {
  416. Log::info('e:' . $e);
  417. echo $e->getMessage();
  418. Session::flash('status', 'danger');
  419. Session::flash('message', 'Error saving assessment. Try again later.');
  420. return action('ActivitiesController@show', array(Input::get('activity_id')));
  421. }
  422. }
  423. public function deleteAssessment()
  424. {
  425. try {
  426. $exception = DB::transaction(function () {
  427. $activity = DB::table('activities')->where('id', Input::get('id'))->first();
  428. $course = DB::table('courses')->where('id', $activity->course_id)->first();
  429. // Reset results in activity
  430. DB::table('activities')
  431. ->where('id', Input::get('id'))
  432. ->update(array(
  433. 'draft' => 1,
  434. 'assessment_comments' => NULL,
  435. 'updated_at' => date('Y-m-d H:i:s')
  436. ));
  437. // Delete students score
  438. DB::table('assessments')
  439. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  440. ->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. Session::flash('status', 'success');
  572. Session::flash('message', 'Activity deleted.');
  573. return Redirect::action('CoursesController@show', array($course->id));
  574. } else {
  575. Session::flash('status', 'danger');
  576. Session::flash('message', 'Error deleting activity. Try again later.');
  577. return Redirect::back();
  578. }
  579. }
  580. public function update($id)
  581. {
  582. try {
  583. $activity = Activity::find($id);
  584. if (Input::has('update_activity_information')) {
  585. /** Validation rules */
  586. $validator = Validator::make(
  587. array(
  588. 'name' => Input::get('name'),
  589. 'description' => Input::get('description'),
  590. 'date' => Input::get('date'),
  591. ),
  592. array(
  593. 'name' => 'required|unique:activities,course_id,' . $id,
  594. 'description' => 'required|min:10',
  595. 'date' => 'required|dateFormat:Y-m-d'
  596. ),
  597. array(
  598. 'date.dateFormat' => 'The date does not match the correct format: yyyy-mm-dd.'
  599. )
  600. );
  601. /** If validation fails */
  602. if ($validator->fails()) {
  603. /** Prepare error message */
  604. $message = 'Error(s) updating the Activity<ul>';
  605. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  606. $message .= $validationError;
  607. }
  608. $message .= '</ul>';
  609. /** Send error message and old data */
  610. Session::flash('status', 'warning');
  611. Session::flash('message', $message);
  612. return Redirect::back()->withInput();
  613. }
  614. /** Update activity info */
  615. $activity->name = Input::get('name');
  616. $activity->description = Input::get('description');
  617. $activity->date = Input::get('date');
  618. } /*elseif (Input::has('update_transforming_actions')) {
  619. if (trim(Input::get('transforming_actions')) != "")
  620. $activity->transforming_actions = Input::get('transforming_actions');
  621. else
  622. $activity->transforming_actions = NULL;
  623. }*/ elseif (Input::has('update_assessment_comments')) {
  624. if (trim(Input::get('assessment_comments')) != "")
  625. $activity->assessment_comments = Input::get('assessment_comments');
  626. else
  627. $activity->assessment_comments = NULL;
  628. } else {
  629. Session::flash('status', 'danger');
  630. Session::flash('message', 'Error updating Activity. Please try again later.');
  631. return Redirect::action('ActivitiesController@show', array($activity->id));
  632. }
  633. $activity->save();
  634. /** If activity is saved, send success message */
  635. Session::flash('status', 'success');
  636. Session::flash('message', 'Activity succesfully updated.');
  637. return Redirect::action('ActivitiesController@show', array($activity->id));
  638. } catch (Exception $e) {
  639. Session::flash('status', 'warning');
  640. Session::flash('message', 'Error updating Activity. Please try again later.');
  641. return Redirect::action('ActivitiesController@show', array($activity->id));
  642. }
  643. }
  644. //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.
  645. public function viewAssessment($id)
  646. {
  647. $activity = Activity::find($id);
  648. // If activity does not exist, display 404
  649. if (!$activity)
  650. App::abort('404');
  651. // Get activity's course
  652. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  653. // If activity does not belong to the requesting user, display 403
  654. if ($course->user_id != Auth::id())
  655. App::abort('403', 'Access Forbidden');
  656. $title = 'Assessment Sheet';
  657. $students = $course->students;
  658. // Get rubric contents
  659. $rubric = Rubric::find($activity->rubric[0]->id);
  660. $rubric->titles = DB::table('titles')
  661. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  662. ->where('rubric_id', $rubric->id)
  663. ->orderBy("position", 'ASC')
  664. ->lists('text');
  665. //$rubric_contents = Rubric::select('contents')->where('id', '=', $activity->rubric_id)->get();
  666. //$rubric_contents = json_decode($rubric_contents['0']->contents);
  667. $rubric_criterion = DB::table('criteria')
  668. ->join("rubric_criterion", "rubric_criterion.criterion_id", "=", "criteria.id")
  669. ->join("activity_criterion", "criteria.id", '=', 'activity_criterion.criterion_id')
  670. ->where("activity_criterion.activity_id", '=', $activity->id)
  671. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  672. ->select('criteria.name', 'criteria.id as criterion_id', 'criteria.subcriteria')
  673. ->addSelect('activity_criterion.activity_id', 'activity_criterion.weight', 'activity_criterion.id as activity_criterion_id')
  674. ->addSelect('rubric_criterion.rubric_id', 'rubric_criterion.id as rubric_criterion_id')
  675. ->get();
  676. Log::info("EN mi cuarto o o o");
  677. Log::info($rubric_criterion);
  678. foreach ($rubric_criterion as $index => $crit) {
  679. $crit->scales = DB::table('scales')
  680. ->join('criterion_scale', 'scales.id', '=', 'criterion_scale.scale_id')
  681. ->where('criterion_id', $crit->criterion_id)
  682. ->get();
  683. }
  684. // Get results
  685. $activity_criterion_ids = DB::table('activity_criterion')->where("activity_id", '=', $activity->id)->lists('id');
  686. Log::info($activity_criterion_ids);
  687. $assessments = DB::table('assessments')
  688. ->join('students', 'assessments.student_id', '=', 'students.id')
  689. ->whereIn('activity_criterion_id', $activity_criterion_ids)
  690. ->orderBy('assessments.id', 'asc')->get();
  691. Log::info($assessments);
  692. // Decode the scores (blade workaround)
  693. $scores_array = array();
  694. foreach ($assessments as $index => $assessment) {
  695. $scores_array[$assessment->student_id][$assessment->activity_criterion_id] = $assessment->score;
  696. $scores_array[$assessment->student_id]['comments'] = DB::table('activity_student')->where('student_id', '=', $assessment->student_id)
  697. ->where("activity_id", '=', $activity->id)
  698. ->select('comments')->first()->comments;
  699. }
  700. return View::make('local.professors.view_assessment', compact('activity', 'title', 'students', 'course', 'rubric_criterion', 'assessments', 'scores_array', 'rubric'));
  701. }
  702. public function printAssessment($id)
  703. {
  704. $activity = Activity::find($id);
  705. // If activity does not exist, display 404
  706. if (!$activity)
  707. App::abort('404');
  708. // Get activity's course
  709. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  710. // If activity does not belong to the requesting user, display 403
  711. if ($course->user_id != Auth::id())
  712. App::abort('403', 'Access Forbidden');
  713. $title = 'Assessment Sheet';
  714. $students = $course->students;
  715. // Get rubric contents
  716. $rubric = Rubric::find($activity->rubric[0]->id);
  717. $rubric_contents = DB::table('criteria')
  718. ->join("rubric_criterion", "rubric_criterion.criterion_id", "=", "criteria.id")
  719. ->join("activity_criterion", "criteria.id", '=', 'activity_criterion.criterion_id')
  720. ->where("activity_criterion.activity_id", '=', $activity->id)
  721. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  722. ->select('criteria.name', 'criteria.id as criterion_id', 'criteria.subcriteria')
  723. ->addSelect('activity_criterion.activity_id', 'activity_criterion.weight', 'activity_criterion.id as activity_criterion_id')
  724. ->addSelect('rubric_criterion.rubric_id', 'rubric_criterion.id as rubric_criterion_id')
  725. ->get();
  726. $rubric->titles = DB::table('titles')
  727. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  728. ->where('rubric_id', $rubric->id)
  729. ->orderBy("position", 'ASC')
  730. ->lists('text');
  731. foreach ($rubric_contents as $index => $crit) {
  732. $crit->scales = DB::table('scales')
  733. ->join('criterion_scale', 'scales.id', '=', 'criterion_scale.scale_id')
  734. ->where('criterion_id', $crit->criterion_id)
  735. ->get();
  736. }
  737. // Get results
  738. /*$assessments = DB::table('assessments')->where('activity_id', '=', $activity->id)->orderBy('id', 'asc')->get();
  739. // Decode the scores (blade workaround)
  740. $scores_array = array();
  741. foreach ($assessments as $index => $assessment) {
  742. $scores_array[$assessment->id] = json_decode($assessment->scores, true);
  743. }*/
  744. // Get results
  745. $activity_criterion_ids = DB::table('activity_criterion')->where("activity_id", '=', $activity->id)->lists('id');
  746. Log::info($activity_criterion_ids);
  747. $assessments = DB::table('assessments')
  748. ->join('students', 'assessments.student_id', '=', 'students.id')
  749. ->whereIn('activity_criterion_id', $activity_criterion_ids)
  750. ->orderBy('assessments.id', 'asc')->get();
  751. Log::info($assessments);
  752. // Decode the scores (blade workaround)
  753. $scores_array = array();
  754. foreach ($assessments as $index => $assessment) {
  755. $scores_array[$assessment->student_id][$assessment->activity_criterion_id] = $assessment->score;
  756. $scores_array[$assessment->student_id]['comments'] = DB::table('activity_student')->where('student_id', '=', $assessment->student_id)
  757. ->where("activity_id", '=', $activity->id)
  758. ->select('comments')->first()->comments;
  759. }
  760. return View::make('local.professors.print_assessment', compact('activity', 'title', 'students', 'course', 'rubric_contents', 'assessments', 'scores_array', 'rubric'));
  761. }
  762. }