暫無描述

ActivitiesController.php 45KB

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