Нема описа

ActivitiesController.php 45KB

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