Nav apraksta

ActivitiesController.php 46KB

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