Geen omschrijving

ActivitiesController.php 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. 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. //results
  801. if ($assessment_1) {
  802. $results_1 = $activity_1->getOutcomeReport();
  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. $results_2 = $activity_1->getOutcomeReport();
  816. $outcomes_achieved_2 = $activity_2->o_ach_array;
  817. $outcomes_attempted_2 = $activity_2->o_att_array;
  818. } else {
  819. $outcomes_achieved_2 = [];
  820. $outcomes_attempted_2 = [];
  821. }
  822. $activity_criterion_2 = DB::table('criteria')
  823. ->join('activity_criterion', 'criteria.id', '=', 'activity_criterion.criterion_id')
  824. ->where('activity_id', $activity_2->id)
  825. ->select('activity_criterion.id', 'activity_criterion.criterion_id')
  826. ->addSelect('criteria.name')
  827. ->get();
  828. 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'));
  829. }
  830. }