설명 없음

ActivitiesController.php 41KB

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