Нет описания

ActivitiesController.php 45KB

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