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

ActivitiesController.php 45KB

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