Keine Beschreibung

ActivitiesController.php 41KB

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