Нема описа

ActivitiesController.php 44KB

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