Ingen beskrivning

ActivitiesController.php 45KB

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