Nessuna descrizione

ActivitiesController.php 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. $activity->diagnostic = 0;
  45. //
  46. $activity->date = date('Y-m-d');
  47. /** If activity is saved, send success message */
  48. if ($activity->save()) {
  49. Session::flash('status', 'success');
  50. Session::flash('message', 'Activity created.');
  51. return Redirect::action('ActivitiesController@show', array($activity->id));
  52. }
  53. /** If saving fails, send error message and old data */
  54. else {
  55. Session::flash('status', 'warning');
  56. Session::flash('message', 'Error adding Activity. Please try again later.');
  57. return Redirect::back()->withInput();
  58. }
  59. }
  60. }
  61. public function newCreate($course_id = null)
  62. {
  63. $title = 'Create Activity';
  64. $activity_types = [];
  65. $instruments = Rubric::all();
  66. $courses = Course::where('user_id', Auth::user()->id)->get();
  67. $outcomes = Outcome::with('objectives')->get();
  68. // var_dump($outcomes[0]->objectives);
  69. $objectives_by_outcome = Collection::make([]);
  70. $outcomes->each(function ($outcome) use (&$objectives_by_outcome) {
  71. // var_dump($outcome->objectives);
  72. $objectives_by_outcome->put($outcome->id, $outcome->objectives);
  73. // var_dump($objectives);
  74. });
  75. $criteria_by_objective = Collection::make([]);
  76. $objectives_by_outcome->each(function ($objectives) use (&$criteria_by_objective) {
  77. $objectives->each(function ($objective) use (&$criteria_by_objective) {
  78. $criteria_by_objective->put($objective->id, $objective->criteria);
  79. });
  80. });
  81. $transforming_actions = [];
  82. $course = Course::find($course_id);
  83. // var_dump($criteria_by_objective);
  84. // return $objectives->toJson();
  85. return View::make(
  86. 'local.managers.admins.new-activity-create',
  87. compact(
  88. 'title',
  89. 'course',
  90. 'activity_types',
  91. 'instruments',
  92. 'courses',
  93. 'outcomes',
  94. 'objectives_by_outcome',
  95. 'criteria_by_objective',
  96. 'transforming_actions'
  97. )
  98. );
  99. }
  100. /**
  101. *
  102. */
  103. public function show($id)
  104. {
  105. $activity = Activity::find($id);
  106. // If activity does not exist, display 404
  107. if (!$activity)
  108. App::abort('404');
  109. // Get activity's course
  110. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  111. // If activity does not belong to the requesting user, display 403
  112. if ($course->user_id != Auth::id() and Auth::user()->role == 4)
  113. App::abort('403', 'Access Forbidden');
  114. // Get active semesters
  115. $active_semesters = array();
  116. $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();
  117. foreach ($active_semesters_collection as $active_semester) {
  118. $active_semesters[] = $active_semester->id;
  119. }
  120. Log::info($active_semesters);
  121. // 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
  122. $title = $course->code . $course->number . '-' . $course->section . ': ' . htmlspecialchars($activity->name, ENT_QUOTES) . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  123. $semesters = DB::table('semesters')->where('id', $course->semester_id)->orderBy('start', 'ASC')->first();
  124. /*$outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))
  125. ->whereNull('deleted_at')
  126. ->whereRaw("(deactivation_date IS NULL or deactivation_date >= '{$semesters->start}')")
  127. ->orderBy('name', 'ASC')->get();*/
  128. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  129. $level = DB::table('courses')
  130. ->join('programs', 'programs.id', '=', 'courses.program_id')
  131. ->where('courses.id', $activity->course_id)
  132. // ->where('courses.number', $number)
  133. // ->where('courses.semester_id', $semester->id)
  134. ->select('programs.is_graduate')
  135. ->first();
  136. $outcomes = Outcome::active_by_semesters(array($course->semester), $level->is_graduate);
  137. //$outcomes = $activity->getOutcomeReport();
  138. $assessment = DB::table('assessments')
  139. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  140. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  141. ->where('activity_id', $activity->id)
  142. ->get();
  143. //if ($assessment) {
  144. // $outcomes_achieved = $activity->o_ach_array;
  145. // $outcomes_attempted = $activity->o_att_array;
  146. //} else {
  147. // $outcomes_achieved = [];
  148. // $outcomes_attempted = [];
  149. //}
  150. //Log::info($outcomes_achieved);
  151. // Log::info(json_encode($activity));
  152. $outcomes_activity = $activity->getOutcomeReport();
  153. $activity_criterion = DB::table('criteria')
  154. ->join('activity_criterion', 'criteria.id', '=', 'activity_criterion.criterion_id')
  155. ->where('activity_id', $activity->id)
  156. ->select('activity_criterion.id', 'activity_criterion.criterion_id')
  157. ->addSelect('criteria.name')
  158. ->get();
  159. $transformative_actions = DB::table('transformative_activity_criterion')
  160. ->join('activity_criterion', 'transformative_activity_criterion.activity_criterion_id', '=', 'activity_criterion.id')
  161. ->join('transformative_actions', 'transformative_activity_criterion.trans_action_id', '=', 'transformative_actions.id')
  162. ->where('activity_criterion.activity_id', $id)
  163. ->get();
  164. return View::make('local.professors.activity', compact('activity', 'outcomes_activity', 'transformative_actions', 'activity_criterion', 'title', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'course', 'student_count', 'active_semesters'));
  165. }
  166. public function assess($id)
  167. {
  168. $activity = Activity::find($id);
  169. // If activity does not exist, display 404
  170. if (!$activity)
  171. App::abort('404');
  172. // Get activity's course
  173. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  174. // If activity does not belong to the requesting user, display 403
  175. if ($course->user_id != Auth::id())
  176. App::abort('403', 'Access Forbidden');
  177. $title = 'Assessment Sheet';
  178. $students = $course->students;
  179. // Get rubric contents
  180. $rubric = Rubric::find($activity->rubric[0]->id);
  181. $rubric->titles = DB::table('titles')
  182. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  183. ->where('rubric_title.rubric_id', '=', $rubric->id)
  184. ->lists('text');
  185. $rubric_criterion = DB::table('criteria')
  186. ->join("rubric_criterion", "rubric_criterion.criterion_id", "=", "criteria.id")
  187. ->join("activity_criterion", "criteria.id", '=', 'activity_criterion.criterion_id')
  188. ->where("activity_criterion.activity_id", '=', $activity->id)
  189. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  190. ->select('criteria.name', 'criteria.id as criterion_id', 'criteria.subcriteria')
  191. ->addSelect('activity_criterion.activity_id', 'activity_criterion.weight', 'activity_criterion.id as activity_criterion_id')
  192. ->addSelect('rubric_criterion.rubric_id', 'rubric_criterion.id as rubric_criterion_id')
  193. ->get();
  194. $total_weights = 0;
  195. foreach ($rubric_criterion as $index => $singleCR) {
  196. $singleCR->scales = json_encode(DB::table('scales')
  197. ->join('criterion_scale', 'criterion_scale.scale_id', '=', 'scales.id')
  198. ->where('criterion_scale.criterion_id', '=', $singleCR->criterion_id)
  199. ->orderBy('position')
  200. ->lists('description'));
  201. $total_weights += $singleCR->weight;
  202. }
  203. $rubric_criterion_ids = DB::table('rubric_criterion')->where('rubric_id', '=', $rubric->id)->lists('id');
  204. // Get results
  205. $activity_criterion_ids = DB::table('activity_criterion')->where("activity_id", '=', $activity->id)->lists('id');
  206. $assessments = DB::table('assessments')
  207. ->join('students', 'assessments.student_id', '=', 'students.id')
  208. ->whereIn('activity_criterion_id', $activity_criterion_ids)
  209. ->orderBy('assessments.id', 'asc')->get();
  210. // Decode the scores (blade workaround)
  211. $scores_array = array();
  212. foreach ($assessments as $index => $assessment) {
  213. $scores_array[$assessment->student_id][$assessment->activity_criterion_id] = $assessment->score;
  214. $scores_array[$assessment->student_id]['comments'] = DB::table('activity_student')->where('student_id', '=', $assessment->student_id)
  215. ->where("activity_id", '=', $activity->id)
  216. ->select('comments')->first()->comments;
  217. }
  218. return View::make('local.professors.assessment', compact('total_weights', 'activity', 'title', 'students', 'course', 'rubric_criterion', 'assessments', 'scores_array', 'rubric'));
  219. }
  220. public function saveAssessment()
  221. {
  222. try {
  223. $exception = DB::transaction(function () {
  224. DB::transaction(function () {
  225. // Student assessment data
  226. $activity_id = Input::get('activity_id');
  227. $student_data = json_decode(Input::get('student_info'));
  228. $weights = json_decode(Input::get('weights'));
  229. Log::info(json_encode($weights));
  230. Log::info(json_encode($student_data));
  231. foreach ($student_data as $index => $student_dict) {
  232. $student_id = $student_dict->studentId;
  233. foreach ($student_dict->activity_crit_id as $act_crit_id => $score) {
  234. Log::info(json_encode($score));
  235. if (DB::table('assessments')->where('student_id', '=', $student_id)
  236. ->where('activity_criterion_id', '=', $act_crit_id)
  237. ->first()
  238. ) {
  239. DB::table('assessments')->where('student_id', '=', $student_id)
  240. ->where('activity_criterion_id', '=', $act_crit_id)
  241. ->update(array('score' => $score));
  242. } else {
  243. if($score==null)
  244. {
  245. DB::insert("insert into assessments (`activity_criterion_id`, `student_id`) values ({$act_crit_id}, {$student_id})");
  246. }
  247. else
  248. {
  249. DB::insert("insert into assessments (`activity_criterion_id`, `student_id`, `score`) values ({$act_crit_id}, {$student_id}, {$score})");
  250. }
  251. }
  252. }
  253. if (DB::table('activity_student')
  254. ->where('student_id', '=', $student_id)->where('activity_id', '=', $activity_id)
  255. ->first()
  256. ) {
  257. DB::table('activity_student')
  258. ->where('student_id', '=', $student_id)->where('activity_id', '=', $activity_id)
  259. ->update(array('comments' => $student_dict->comments));
  260. } else {
  261. DB::insert("insert into activity_student (`activity_id`, `student_id`, `comments`) values ({$activity_id}, {$student_id}, '{$student_dict->comments}')");
  262. }
  263. }
  264. $activity_draft = Input::get('draft');
  265. $activity_diagnostic = Input::get('diagnostic');
  266. Log::info(json_encode($weights));
  267. foreach ($weights as $act_crit => $weigh) {
  268. DB::update("update activity_criterion set weight = {$weigh} where id = {$act_crit}");
  269. }
  270. DB::update("update activities set draft = {$activity_draft}, diagnostic = {$activity_diagnostic} where id = {$activity_id}");
  271. // Outcome count
  272. Session::flash('status', 'success');
  273. Session::flash('message', 'Assessment Saved. To add Formative actions click "Formative Actions".');
  274. return action('ActivitiesController@show', array(Input::get('activity_id')));
  275. $outcomeCount = Outcome::all()->count();
  276. // Activity
  277. $activity = Activity::find(Input::get('activity_id'));
  278. // Create or update student scores
  279. if ($activity->outcomes_attempted == NULL) {
  280. // For each student, save her/his assessment in the db
  281. foreach ($student_data as $single_student_data) {
  282. // Find student by id
  283. $student = Student::find($single_student_data->student_id);
  284. $comments = trim($single_student_data->comments);
  285. if ($comments == '') {
  286. $comments = NULL;
  287. }
  288. // Add the assessment to the pivot table
  289. $student->assessed_activities()->attach($activity->id, array(
  290. 'scores' => json_encode($single_student_data->scores),
  291. 'comments' => $single_student_data->comments
  292. ));
  293. }
  294. } else {
  295. // For each student, save her/his assessment in the db
  296. foreach ($student_data as $single_student_data) {
  297. // Find student by id
  298. $student = Student::find($single_student_data->student_id);
  299. $comments = trim($single_student_data->comments);
  300. if ($comments == '') {
  301. $comments = NULL;
  302. }
  303. // Update the assessment in the pivot table
  304. $student->assessed_activities()->updateExistingPivot($activity->id, array(
  305. 'scores' => json_encode($single_student_data->scores),
  306. 'percentage' => $single_student_data->percentage,
  307. 'comments' => $single_student_data->comments
  308. ));
  309. }
  310. }
  311. // Prepare arrays for criteria achievement for this activity
  312. $criteria_achievement = json_decode(Input::get('criteria_achievement'));
  313. $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  314. $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  315. // Fetch parent course's criteria achievement by outcome, if it exists
  316. $course = $activity->course;
  317. $course_outcomes_attempted = NULL;
  318. $course_outcomes_achieved = NULL;
  319. if ($course->outcomes_attempted == NULL) {
  320. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  321. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  322. } else {
  323. // the second argument is necessary to convert it into an array
  324. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  325. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  326. }
  327. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  328. // Find corresponding learning outcome
  329. $criterion = Criterion::withTrashed()->find($criterion_id);
  330. $outcome = Outcome::find($criterion->outcome_id);
  331. // If criterion is achieved (1), add 1 to all arrays
  332. if ($criterion_achieved === 1) {
  333. $outcomes_attempted[$outcome->id] += 1;
  334. $outcomes_achieved[$outcome->id] += 1;
  335. $course_outcomes_attempted[$outcome->id] += 1;
  336. $course_outcomes_achieved[$outcome->id] += 1;
  337. }
  338. // Else if it's 0, only add to the attempted outcomes arrays
  339. elseif ($criterion_achieved === 0) {
  340. $outcomes_attempted[$outcome->id] += 1;
  341. $course_outcomes_attempted[$outcome->id] += 1;
  342. }
  343. }
  344. // If all values are 0, throw exception
  345. if (count(array_unique($outcomes_attempted)) == 1 && $outcomes_attempted[1] == 0)
  346. throw new Exception("Error Processing Request", 1);
  347. // Set activity fields
  348. $activity->criteria_achieved = Input::get('criteria_achievement');
  349. $activity->criteria_achieved_percentage = Input::get('criteria_achieved_percentage');
  350. $activity->outcomes_attempted = json_encode($outcomes_attempted);
  351. $activity->outcomes_achieved = json_encode($outcomes_achieved);
  352. // Publish results if not a draft. That is, update the activity's course.
  353. if (Input::get('draft') == false) {
  354. // Update course
  355. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  356. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  357. $course->save();
  358. $activity->draft = false;
  359. } else {
  360. // Set draft to true
  361. $activity->draft = true;
  362. }
  363. // Save activity
  364. $activity->save();
  365. // Recalculate course outcomes
  366. $activities = DB::table('activities')
  367. ->where('course_id', $activity->course->id)
  368. ->where('draft', 0)
  369. ->get();
  370. // Check if any assessed activities remain
  371. $remainingAssessed = false;
  372. foreach ($activities as $activity1) {
  373. if ($activity1->outcomes_attempted != NULL) {
  374. $remainingAssessed = true;
  375. break;
  376. }
  377. }
  378. //If there are still evaluated activities in the course, recalculate course outcomes
  379. if (count($activities) && $remainingAssessed) {
  380. $outcomeCount = Outcome::all()->count();
  381. // Variables to hold recalculated outcomes for the course
  382. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  383. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  384. // For each activity
  385. foreach ($activities as $activity2) {
  386. // If activity has been assessed
  387. if ($activity2->outcomes_attempted != NULL) {
  388. // Get the achieved criteria
  389. $criteria_achievement = json_decode($activity2->criteria_achieved, true);
  390. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  391. // Find corresponding learning outcome;
  392. $criterion = Criterion::withTrashed()->find($criterion_id);
  393. $outcome = Outcome::find($criterion->outcome_id);
  394. // If criterion is achieved (1), add 1 to both arrays
  395. if ($criterion_achieved === 1) {
  396. $course_outcomes_attempted[$outcome->id] += 1;
  397. $course_outcomes_achieved[$outcome->id] += 1;
  398. }
  399. // Else, only add to the attempted outcomes arrays
  400. elseif ($criterion_achieved === 0) {
  401. $course_outcomes_attempted[$outcome->id] += 1;
  402. }
  403. }
  404. }
  405. }
  406. // Update course
  407. DB::table('courses')
  408. ->where('id', $course->id)
  409. ->update(array(
  410. 'outcomes_attempted' => json_encode($course_outcomes_attempted),
  411. 'outcomes_achieved' => json_encode($course_outcomes_achieved),
  412. 'updated_at' => date('Y-m-d H:i:s')
  413. ));
  414. }
  415. // Otherwise, set them all to NULL
  416. else {
  417. DB::table('courses')
  418. ->where('id', $course->id)
  419. ->update(array(
  420. 'outcomes_attempted' => NULL,
  421. 'outcomes_achieved' => NULL,
  422. 'updated_at' => date('Y-m-d H:i:s')
  423. ));
  424. }
  425. });
  426. });
  427. if (is_null($exception)) {
  428. Session::flash('status', 'success');
  429. Session::flash('message', 'Assessment Saved. To add Formative actions click "Formative Actions".');
  430. return action('ActivitiesController@show', array(Input::get('activity_id')));
  431. }
  432. } catch (Exception $e) {
  433. Log::info('e:' . $e);
  434. echo $e->getMessage();
  435. Session::flash('status', 'danger');
  436. Session::flash('message', 'Error saving assessment. Try again later.');
  437. return action('ActivitiesController@show', array(Input::get('activity_id')));
  438. }
  439. }
  440. public function deleteAssessment()
  441. {
  442. try {
  443. $exception = DB::transaction(function () {
  444. $activity = DB::table('activities')->where('id', Input::get('id'))->first();
  445. $course = DB::table('courses')->where('id', $activity->course_id)->first();
  446. // Reset results in activity
  447. DB::table('activities')
  448. ->where('id', Input::get('id'))
  449. ->update(array(
  450. 'draft' => 1,
  451. 'assessment_comments' => NULL,
  452. 'updated_at' => date('Y-m-d H:i:s')
  453. ));
  454. // Delete students score
  455. DB::table('assessments')
  456. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  457. ->where('activity_id', $activity->id)->delete();
  458. // Recalculate course outcomes
  459. /*$activities = DB::table('activities')
  460. ->where('course_id', $course->id)
  461. ->where('draft', 0)
  462. ->get();
  463. // Check if any assessed activties remain
  464. $remainingAssessed = false;
  465. foreach ($activities as $activity) {
  466. if ($activity->outcomes_attempted != NULL) {
  467. $remainingAssessed = true;
  468. break;
  469. }
  470. }
  471. //If there are still evaluated activities in the course, recalculate course outcomes
  472. if (count($activities) && $remainingAssessed) {
  473. $outcomeCount = Outcome::all()->count();
  474. // Variables to hold recalculated outcomes for the course
  475. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  476. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  477. // For each activity
  478. foreach ($activities as $activity) {
  479. // If activity has been assessed
  480. if ($activity->outcomes_attempted != NULL) {
  481. // Get the achieved criteria
  482. $criteria_achievement = json_decode($activity->criteria_achieved, true);
  483. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  484. // Find corresponding learning outcome;
  485. $criterion = Criterion::withTrashed()->find($criterion_id);
  486. $outcome = Outcome::find($criterion->outcome_id);
  487. // If criterion is achieved (1), add 1 to both arrays
  488. if ($criterion_achieved === 1) {
  489. $course_outcomes_attempted[$outcome->id] += 1;
  490. $course_outcomes_achieved[$outcome->id] += 1;
  491. }
  492. // Else, only add to the attempted outcomes arrays
  493. elseif ($criterion_achieved === 0) {
  494. $course_outcomes_attempted[$outcome->id] += 1;
  495. }
  496. }
  497. }
  498. }
  499. // Update course
  500. DB::table('courses')
  501. ->where('id', $course->id)
  502. ->update(array(
  503. 'outcomes_attempted' => json_encode($course_outcomes_attempted),
  504. 'outcomes_achieved' => json_encode($course_outcomes_achieved),
  505. 'updated_at' => date('Y-m-d H:i:s')
  506. ));
  507. }
  508. // Otherwise, set them all to NULL
  509. else {
  510. DB::table('courses')
  511. ->where('id', $course->id)
  512. ->update(array(
  513. 'outcomes_attempted' => NULL,
  514. 'outcomes_achieved' => NULL,
  515. 'updated_at' => date('Y-m-d H:i:s')
  516. ));
  517. }
  518. });*/
  519. });
  520. if (is_null($exception)) {
  521. Session::flash('status', 'success');
  522. Session::flash('message', 'Assessment deleted.');
  523. return Redirect::back();
  524. }
  525. } catch (Exception $e) {
  526. Session::flash('status', 'danger');
  527. Session::flash('message', 'Error saving assessment. Try again later.');
  528. return Redirect::back();
  529. }
  530. }
  531. public function destroy($id)
  532. {
  533. $course = Activity::find($id)->course;
  534. if (Activity::destroy($id)) {
  535. // Recalculate course outcomes
  536. /*$activities = $course->activities;
  537. // Check if any assessed activties remain
  538. $remainingAssessed = false;
  539. foreach ($course->activities as $activity) {
  540. if ($activity->outcomes_attempted != NULL) {
  541. $remainingAssessed = true;
  542. break;
  543. }
  544. }
  545. //If there are still evaluated activities in the course, recalculate course outcomes
  546. if (!$course->activities->isEmpty() && $remainingAssessed) {
  547. $outcomeCount = Outcome::all()->count();
  548. // Variables to hold recalculated outcomes for the course
  549. $course_outcomes_attempted = array_fill(1, $outcomeCount, 0);
  550. $course_outcomes_achieved = array_fill(1, $outcomeCount, 0);
  551. // For each activity
  552. foreach ($activities as $activity) {
  553. // If activity has been assessed
  554. if ($activity->outcomes_attempted != NULL) {
  555. // Get the achieved criteria
  556. $criteria_achievement = json_decode($activity->criteria_achieved, true);
  557. foreach ($criteria_achievement as $criterion_id => $criterion_achieved) {
  558. // Find corresponding learning outcome;
  559. $criterion = Criterion::withTrashed()->find($criterion_id);
  560. $outcome = Outcome::find($criterion->outcome_id);
  561. // If criterion is achieved (1), add 1 to both arrays
  562. if ($criterion_achieved === 1) {
  563. $course_outcomes_attempted[$outcome->id] += 1;
  564. $course_outcomes_achieved[$outcome->id] += 1;
  565. }
  566. // Else, only add to the attempted outcomes arrays
  567. elseif ($criterion_achieved === 0) {
  568. $course_outcomes_attempted[$outcome->id] += 1;
  569. }
  570. }
  571. }
  572. }
  573. // Update course
  574. $course->outcomes_achieved = json_encode($course_outcomes_achieved);
  575. $course->outcomes_attempted = json_encode($course_outcomes_attempted);
  576. } else {
  577. $course->outcomes_achieved = NULL;
  578. $course->outcomes_attempted = NULL;
  579. } */
  580. /*if ($course->save()) {
  581. Session::flash('status', 'success');
  582. Session::flash('message', 'Activity deleted.');
  583. } else {
  584. Session::flash('status', 'danger');
  585. Session::flash('message', 'Error deleting activity. Try again later.');
  586. return Redirect::back();
  587. }*/
  588. Session::flash('status', 'success');
  589. Session::flash('message', 'Activity deleted.');
  590. return Redirect::action('CoursesController@show', array($course->id));
  591. } else {
  592. Session::flash('status', 'danger');
  593. Session::flash('message', 'Error deleting activity. Try again later.');
  594. return Redirect::back();
  595. }
  596. }
  597. public function update($id)
  598. {
  599. try {
  600. $activity = Activity::find($id);
  601. if (Input::has('update_activity_information')) {
  602. /** Validation rules */
  603. $validator = Validator::make(
  604. array(
  605. 'name' => Input::get('name'),
  606. 'description' => Input::get('description'),
  607. 'date' => Input::get('date'),
  608. ),
  609. array(
  610. 'name' => 'required|unique:activities,course_id,' . $id,
  611. 'description' => 'required|min:10',
  612. 'date' => 'required|dateFormat:Y-m-d'
  613. ),
  614. array(
  615. 'date.dateFormat' => 'The date does not match the correct format: yyyy-mm-dd.'
  616. )
  617. );
  618. /** If validation fails */
  619. if ($validator->fails()) {
  620. /** Prepare error message */
  621. $message = 'Error(s) updating the Activity<ul>';
  622. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  623. $message .= $validationError;
  624. }
  625. $message .= '</ul>';
  626. /** Send error message and old data */
  627. Session::flash('status', 'warning');
  628. Session::flash('message', $message);
  629. return Redirect::back()->withInput();
  630. }
  631. /** Update activity info */
  632. $activity->name = Input::get('name');
  633. $activity->description = Input::get('description');
  634. $activity->date = Input::get('date');
  635. } /*elseif (Input::has('update_transforming_actions')) {
  636. if (trim(Input::get('transforming_actions')) != "")
  637. $activity->transforming_actions = Input::get('transforming_actions');
  638. else
  639. $activity->transforming_actions = NULL;
  640. }*/ elseif (Input::has('update_assessment_comments')) {
  641. if (trim(Input::get('assessment_comments')) != "")
  642. $activity->assessment_comments = Input::get('assessment_comments');
  643. else
  644. $activity->assessment_comments = NULL;
  645. } else {
  646. Session::flash('status', 'danger');
  647. Session::flash('message', 'Error updating Activity. Please try again later.');
  648. return Redirect::action('ActivitiesController@show', array($activity->id));
  649. }
  650. $activity->save();
  651. /** If activity is saved, send success message */
  652. Session::flash('status', 'success');
  653. Session::flash('message', 'Activity succesfully updated.');
  654. return Redirect::action('ActivitiesController@show', array($activity->id));
  655. } catch (Exception $e) {
  656. Session::flash('status', 'warning');
  657. Session::flash('message', 'Error updating Activity. Please try again later.');
  658. return Redirect::action('ActivitiesController@show', array($activity->id));
  659. }
  660. }
  661. //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.
  662. public function viewAssessment($id)
  663. {
  664. $activity = Activity::find($id);
  665. // If activity does not exist, display 404
  666. if (!$activity)
  667. App::abort('404');
  668. // Get activity's course
  669. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  670. // If activity does not belong to the requesting user, display 403
  671. if ($course->user_id != Auth::id())
  672. App::abort('403', 'Access Forbidden');
  673. $students = $course->students;
  674. // Get rubric contents
  675. $rubric = Rubric::find($activity->rubric[0]->id);
  676. $rubric->titles = DB::table('titles')
  677. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  678. ->where('rubric_id', $rubric->id)
  679. ->orderBy("position", 'ASC')
  680. ->lists('text');
  681. //$rubric_contents = Rubric::select('contents')->where('id', '=', $activity->rubric_id)->get();
  682. //$rubric_contents = json_decode($rubric_contents['0']->contents);
  683. $rubric_criterion = DB::table('criteria')
  684. ->join("rubric_criterion", "rubric_criterion.criterion_id", "=", "criteria.id")
  685. ->join("activity_criterion", "criteria.id", '=', 'activity_criterion.criterion_id')
  686. ->where("activity_criterion.activity_id", '=', $activity->id)
  687. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  688. ->select('criteria.name', 'criteria.id as criterion_id', 'criteria.subcriteria')
  689. ->addSelect('activity_criterion.activity_id', 'activity_criterion.weight', 'activity_criterion.id as activity_criterion_id')
  690. ->addSelect('rubric_criterion.rubric_id', 'rubric_criterion.id as rubric_criterion_id')
  691. ->get();
  692. foreach ($rubric_criterion as $index => $crit) {
  693. $crit->scales = DB::table('scales')
  694. ->join('criterion_scale', 'scales.id', '=', 'criterion_scale.scale_id')
  695. ->where('criterion_id', $crit->criterion_id)
  696. ->get();
  697. }
  698. // Get results
  699. $activity_criterion_ids = DB::table('activity_criterion')->where("activity_id", '=', $activity->id)->lists('id');
  700. Log::info($activity_criterion_ids);
  701. $assessments = DB::table('assessments')
  702. ->join('students', 'assessments.student_id', '=', 'students.id')
  703. ->whereIn('activity_criterion_id', $activity_criterion_ids)
  704. ->orderBy('assessments.id', 'asc')->get();
  705. Log::info($assessments);
  706. // Decode the scores (blade workaround)
  707. $scores_array = array();
  708. foreach ($assessments as $index => $assessment) {
  709. $scores_array[$assessment->student_id][$assessment->activity_criterion_id] = $assessment->score;
  710. $scores_array[$assessment->student_id]['comments'] = DB::table('activity_student')->where('student_id', '=', $assessment->student_id)
  711. ->where("activity_id", '=', $activity->id)
  712. ->select('comments')->first()->comments;
  713. }
  714. $title = 'Assessment Sheet';
  715. return View::make('local.professors.view_assessment', compact('activity', 'title', 'students', 'course', 'rubric_criterion', 'assessments', 'scores_array', 'rubric'));
  716. }
  717. public function printAssessment($id)
  718. {
  719. $activity = Activity::find($id);
  720. // If activity does not exist, display 404
  721. if (!$activity)
  722. App::abort('404');
  723. // Get activity's course
  724. $course = Course::where('id', '=', $activity->course_id)->firstOrFail();
  725. // If activity does not belong to the requesting user, display 403
  726. if ($course->user_id != Auth::id())
  727. App::abort('403', 'Access Forbidden');
  728. $title = 'Assessment Sheet';
  729. $students = $course->students;
  730. // Get rubric contents
  731. $rubric = Rubric::find($activity->rubric[0]->id);
  732. $rubric_contents = DB::table('criteria')
  733. ->join("rubric_criterion", "rubric_criterion.criterion_id", "=", "criteria.id")
  734. ->join("activity_criterion", "criteria.id", '=', 'activity_criterion.criterion_id')
  735. ->where("activity_criterion.activity_id", '=', $activity->id)
  736. ->where('rubric_criterion.rubric_id', '=', $rubric->id)
  737. ->select('criteria.name', 'criteria.id as criterion_id', 'criteria.subcriteria')
  738. ->addSelect('activity_criterion.activity_id', 'activity_criterion.weight', 'activity_criterion.id as activity_criterion_id')
  739. ->addSelect('rubric_criterion.rubric_id', 'rubric_criterion.id as rubric_criterion_id')
  740. ->get();
  741. $rubric->titles = DB::table('titles')
  742. ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
  743. ->where('rubric_id', $rubric->id)
  744. ->orderBy("position", 'ASC')
  745. ->lists('text');
  746. foreach ($rubric_contents as $index => $crit) {
  747. $crit->scales = DB::table('scales')
  748. ->join('criterion_scale', 'scales.id', '=', 'criterion_scale.scale_id')
  749. ->where('criterion_id', $crit->criterion_id)
  750. ->get();
  751. }
  752. // Get results
  753. /*$assessments = DB::table('assessments')->where('activity_id', '=', $activity->id)->orderBy('id', 'asc')->get();
  754. // Decode the scores (blade workaround)
  755. $scores_array = array();
  756. foreach ($assessments as $index => $assessment) {
  757. $scores_array[$assessment->id] = json_decode($assessment->scores, true);
  758. }*/
  759. // Get results
  760. $activity_criterion_ids = DB::table('activity_criterion')->where("activity_id", '=', $activity->id)->lists('id');
  761. Log::info($activity_criterion_ids);
  762. $assessments = DB::table('assessments')
  763. ->join('students', 'assessments.student_id', '=', 'students.id')
  764. ->whereIn('activity_criterion_id', $activity_criterion_ids)
  765. ->orderBy('assessments.id', 'asc')->get();
  766. Log::info($assessments);
  767. // Decode the scores (blade workaround)
  768. $scores_array = array();
  769. foreach ($assessments as $index => $assessment) {
  770. $scores_array[$assessment->student_id][$assessment->activity_criterion_id] = $assessment->score;
  771. $scores_array[$assessment->student_id]['comments'] = DB::table('activity_student')->where('student_id', '=', $assessment->student_id)
  772. ->where("activity_id", '=', $activity->id)
  773. ->select('comments')->first()->comments;
  774. }
  775. return View::make('local.professors.print_assessment', compact('activity', 'title', 'students', 'course', 'rubric_contents', 'assessments', 'scores_array', 'rubric'));
  776. }
  777. public function compareActivities($activity_1, $activity_2)
  778. {
  779. $activity_1 = Activity::find($activity_1);
  780. $activity_2 = Activity::find($activity_2);
  781. // If activity does not exist, display 404
  782. if (!$activity_1 || !$activity_2)
  783. App::abort('404');
  784. // Get activity's course
  785. $course = Course::where('id', '=', $activity_1->course_id)->firstOrFail();
  786. // If activity does not belong to the requesting user, display 403
  787. if ($course->user_id != Auth::id() and Auth::user()->role == 4)
  788. App::abort('403', 'Access Forbidden');
  789. // Get active semesters
  790. $active_semesters = array();
  791. $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();
  792. foreach ($active_semesters_collection as $active_semester) {
  793. $active_semesters[] = $active_semester->id;
  794. }
  795. $semesters = DB::table('semesters')->where('id', $course->semester_id)->orderBy('start', 'ASC')->first();
  796. Log::info($active_semesters);
  797. // 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
  798. $title = $course->code . $course->number . '-' . $course->section . ': ' . htmlspecialchars($activity_1->name, ENT_QUOTES) . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  799. $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))
  800. ->whereNull('deleted_at')
  801. ->whereRaw("(deactivation_date IS NULL or deactivation_date >= '{$semesters->start}')")
  802. ->orderBy('name', 'ASC')->get();
  803. $assessment_1 = DB::table('assessments')
  804. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  805. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  806. ->where('activity_id', $activity_1->id)
  807. ->get();
  808. //results
  809. if ($assessment_1) {
  810. $results_1 = $activity_1->getOutcomeReport();
  811. $outcomes_achieved_1 = $activity_1->o_ach_array;
  812. $outcomes_attempted_1 = $activity_1->o_att_array;
  813. } else {
  814. $outcomes_achieved_1 = [];
  815. $outcomes_attempted_1 = [];
  816. }
  817. $assessment_2 = DB::table('assessments')
  818. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  819. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  820. ->where('activity_id', $activity_2->id)
  821. ->get();
  822. if ($assessment_2) {
  823. $results_2 = $activity_1->getOutcomeReport();
  824. $outcomes_achieved_2 = $activity_2->o_ach_array;
  825. $outcomes_attempted_2 = $activity_2->o_att_array;
  826. } else {
  827. $outcomes_achieved_2 = [];
  828. $outcomes_attempted_2 = [];
  829. }
  830. $activity_criterion_2 = DB::table('criteria')
  831. ->join('activity_criterion', 'criteria.id', '=', 'activity_criterion.criterion_id')
  832. ->where('activity_id', $activity_2->id)
  833. ->select('activity_criterion.id', 'activity_criterion.criterion_id')
  834. ->addSelect('criteria.name')
  835. ->get();
  836. return View::make('local.professors.compare_activities', compact('results_1', 'results_2', 'activity_1', 'activity_2', 'activity_criterion_1', 'activity_criterion_2', 'title', 'outcomes', 'outcomes_achieved_1', 'outcomes_attempted_1', 'outcomes_achieved_2', 'outcomes_attempted_2', 'course', 'student_count', 'active_semesters'));
  837. }
  838. }