Sin descripción

CoursesController.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. class
  3. CoursesController extends \BaseController
  4. {
  5. /**
  6. * Display the specified resource.
  7. *
  8. * @param int $id
  9. * @return Response
  10. */
  11. public function show($id)
  12. {
  13. $course = Course::with('semester')->where('id', $id)->first();
  14. $title = $course->code . $course->number . '-' . $course->section . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  15. // If course does not exist, display 404
  16. if (!$course)
  17. App::abort('404');
  18. // If course does not belong to the person
  19. if ($course->user_id != Auth::id())
  20. App::abort('403', 'Access forbidden.');
  21. $activities = $course->activities;
  22. $students = $course->students;
  23. //$outcomes = Outcome::orderBy('name', 'asc')->get();
  24. $semesters = Session::get('semesters_ids');
  25. $semesters = DB::table('semesters')->where('id', $course->semester_id)->orderBy('start', 'ASC')->first();
  26. $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))
  27. ->whereNull('deleted_at')
  28. ->whereRaw("(deactivation_date IS NULL or deactivation_date >= '{$semesters->start}')")
  29. ->orderBy('name', 'ASC')->get();
  30. $outcomes_achieved = $course->outcomes_ach();
  31. // $outcomes_attempted = json_decode($course->outcomes_attempted, true);
  32. $outcomes_attempted = $course->outcomes_att();
  33. // Get active semesters
  34. $active_semesters = array();
  35. $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();
  36. foreach ($active_semesters_collection as $active_semester) {
  37. $active_semesters[] = $active_semester->id;
  38. }
  39. return View::make('local.professors.course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'active_semesters'));
  40. }
  41. public function newShow($id)
  42. {
  43. $course = Course::findOrFail($id);
  44. $title = $course->name;
  45. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  46. $is_active_semester = $semesters->filter(function ($semester) {
  47. return Semester::where('is_visible', 1)->where('start', '<=', date('Y-m-d H:i:s'))->where('end', '>=', date('Y-m-d H:i:s'))->get()->has($semester->id);
  48. });
  49. // $active_semesters = 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()->toArray()[0];
  50. $activities = $course->activities;
  51. // var_dump($active_semesters);
  52. // var_dump($course->semester->id);
  53. return View::make('local.managers.admins.new-course-show', compact('title', 'course', 'semesters', 'activities', 'is_active_semester'));
  54. }
  55. /**
  56. * Display the specified course, but with limited information.
  57. *
  58. * @param int $id
  59. * @return Response
  60. */
  61. public function showLimited($id)
  62. {
  63. $course = Course::with('semester')->where('id', $id)->first();
  64. $students = $course->students;
  65. $title = $course->code . $course->number . '-' . $course->section . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  66. // If course does not exist, display 404
  67. if (!$course)
  68. App::abort('404');
  69. $activities = $course->activities;
  70. $students = $course->students;
  71. $level = DB::table('courses')
  72. ->join('programs', 'programs.id', '=', 'courses.program_id')
  73. ->where('courses.id', $id)
  74. ->select('programs.is_graduate')
  75. ->first();
  76. $outcomes = Outcome::active_by_semesters(array($course->semester), $level->is_graduate);
  77. // $outcomeCount = count((array)$outcomes);
  78. $course = Course::where('id', $id)->first();
  79. foreach ($outcomes as $outcome) {
  80. $outcomes_attempted[$outcome->id] = $outcome->courses_attempted(array($course));
  81. $outcomes_achieved[$outcome->id] = $outcome->courses_achieved(array($course));
  82. }
  83. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  84. // $outcomes_achieved = json_decode($course->outcomes_achieved, true);
  85. // $outcomes_attempted = json_decode($course->outcomes_attempted, true);
  86. $schools = School::all();
  87. $rubrics = array();
  88. Log::info($activities);
  89. foreach ($activities as $activity) {
  90. if (!empty($activity->rubric)) {
  91. $rubrics[] = $activity->rubric[0];
  92. }
  93. }
  94. return View::make('local.managers.shared.limited-course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'schools', 'rubrics', 'students'));
  95. }
  96. /**
  97. * Show grouped sections of a course
  98. */
  99. public function showGrouped($code, $number, $semester_code)
  100. {
  101. $title = $code . $number . ' (' . $semester_code . ')';
  102. $semester = Semester::where('code', $semester_code)->first();
  103. // $selected_semesters = Semester::find(Session::get('semesters_ids'));
  104. $role = Auth::user()->role;
  105. $level = DB::table('courses')
  106. ->join('programs', 'programs.id', '=', 'courses.program_id')
  107. ->where('courses.code', $code)
  108. ->where('courses.number', $number)
  109. ->where('courses.semester_id', $semester->id)
  110. ->select('programs.is_graduate')
  111. ->first();
  112. // var_dump($level);
  113. // exit();
  114. $grouped_courses = Course::where('code', $code)
  115. ->where('number', $number)
  116. ->where('semester_id', $semester->id)
  117. ->groupBy(array('code', 'number'))->get();
  118. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  119. // $outcomeCount = Outcome::all()->count();
  120. $outcomes = Outcome::active_by_semesters(array($semester), $level->is_graduate);
  121. $outcomeCount = count((array)$outcomes);
  122. foreach ($outcomes as $outcome) {
  123. $outcomes_attempted[$outcome->id] = $outcome->courses_attempted($grouped_courses);
  124. $outcomes_achieved[$outcome->id] = $outcome->courses_achieved($grouped_courses);
  125. }
  126. // var_dump($outcomes_attempted);
  127. // print "<br>";
  128. // var_dump($outcomes_achieved);
  129. // print "<br>";
  130. foreach ($grouped_courses as $index => $grouped_course) {
  131. // // Blank outcomes for one course
  132. // $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  133. // $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  134. //
  135. // // Find sections for this course
  136. $sections = Course::where('code', $grouped_course->code)
  137. ->where('number', $grouped_course->number)
  138. ->where('semester_id', $grouped_course->semester_id)
  139. ->get();
  140. //
  141. // // For each of the sections, add the attempted and achieved criteria per outcome
  142. // foreach ($sections as $section)
  143. // {
  144. // if($section->outcomes_achieved!=NULL)
  145. // {
  146. // $section_outcomes_achieved =count($section->outcomes_attempted());
  147. // // var_dump($section_outcomes_achieved);
  148. // // exit();
  149. // $section_outcomes_attempted =count($section->outcomes_achieved());
  150. // // $section_outcomes_achieved =json_decode($section->outcomes_achieved, true);
  151. // // $section_outcomes_attempted =json_decode($section->outcomes_attempted, true);
  152. // foreach($outcomes as $outcome)
  153. // {
  154. // if(!isset($outcomes_achieved[$outcome->id]))$outcomes_achieved[$outcome->id]=0;
  155. // if(!isset($outcomes_attempted[$outcome->id]))$outcomes_attempted[$outcome->id]=0;
  156. // $outcomes_achieved[$outcome->id]+=$section_outcomes_achieved[$i];
  157. // $outcomes_attempted[$outcome->id]+=$section_outcomes_attempted[$i];
  158. //
  159. // }
  160. // // for($i=1; $i<=count($outcomes_attempted); $i++)
  161. // // {
  162. // // $outcomes_achieved[$i]+=$section_outcomes_achieved[$i];
  163. // // $outcomes_attempted[$i]+=$section_outcomes_attempted[$i];
  164. // // }
  165. // }
  166. // }
  167. }
  168. $section_ids = Course::where('code', $code)
  169. ->where('number', $number)
  170. ->where('semester_id', $semester->id)
  171. ->lists('id');
  172. //$activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  173. $activities = Activity::with('course')->whereIn('course_id', $section_ids)
  174. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  175. ->join('transformative_activity_criterion as tac', 'ac.id', '=', 'tac.activity_criterion_id')
  176. ->join('transformative_actions as ta', 'ta.id', '=', 'tac.trans_action_id')
  177. ->select('activities.*')
  178. ->addSelect('ta.description as transforming_actions')
  179. ->distinct()
  180. ->orderBy('name')->get();
  181. return View::make('local.managers.shared.grouped_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'transforming_actions', 'activities'));
  182. }
  183. /**
  184. * Printable version for a course (grouped sections)
  185. */
  186. public function print_course($code, $number, $semester_code)
  187. {
  188. $title = $code . $number . ' (' . $semester_code . ')';
  189. $semester = Semester::where('code', $semester_code)->first();
  190. $role = Auth::user()->role;
  191. $grouped_courses = Course::where('code', $code)
  192. ->where('number', $number)
  193. ->where('semester_id', $semester->id)
  194. ->groupBy(array('code', 'number'))->get();
  195. // Log::info($grouped_courses[0]->id);
  196. // exit();
  197. $level = DB::table('courses')
  198. ->join('programs', 'programs.id', '=', 'courses.program_id')
  199. ->where('courses.id', $grouped_courses[0]->id)
  200. ->select('programs.is_graduate')
  201. ->first();
  202. $outcomes = Outcome::active_by_semesters(array($semester), $level->is_graduate);
  203. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  204. $outcomeCount = Outcome::all()->count();
  205. foreach ($grouped_courses as $index => $grouped_course) {
  206. // Blank outcomes for one course
  207. $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  208. $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  209. // Find sections for this course
  210. $sections = Course::where('code', $grouped_course->code)
  211. ->where('number', $grouped_course->number)
  212. ->where('semester_id', $grouped_course->semester_id)
  213. ->get();
  214. // For each of the sections, add the attempted and achieved criteria per outcome
  215. foreach ($sections as &$section) {
  216. if ($section->outcomes_ach()) {
  217. $section->outcomes_attempted=true;
  218. $course_outcomes_achieved = $section->outcomes_ach();
  219. $course_outcomes_attempted = $section->outcomes_att();
  220. foreach ($course_outcomes_achieved as $outcome => $score) {
  221. if (array_key_exists($outcome, $outcomes_achieved))
  222. $outcomes_achieved[$outcome] += $score;
  223. else $outcomes_achieved[$outcome] = $score;
  224. }
  225. foreach ($course_outcomes_attempted as $outcome => $score) {
  226. if (array_key_exists($outcome, $outcomes_attempted))
  227. $outcomes_attempted[$outcome] += $score;
  228. else $outcomes_attempted[$outcome] = $score;
  229. }
  230. }
  231. // if ($section->outcomes_achieved != NULL) {
  232. // $section_outcomes_achieved = json_decode($section->outcomes_achieved, true);
  233. // $section_outcomes_attempted = json_decode($section->outcomes_attempted, true);
  234. // for ($i = 1; $i <= count($outcomes_attempted); $i++) {
  235. // $outcomes_achieved[$i] += $section_outcomes_achieved[$i];
  236. // $outcomes_attempted[$i] += $section_outcomes_attempted[$i];
  237. // }
  238. // }
  239. }
  240. }
  241. $section_ids = Course::where('code', $code)
  242. ->where('number', $number)
  243. ->where('semester_id', $semester->id)
  244. ->lists('id');
  245. // Activities with transforming actions
  246. // $activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  247. $activities = Activity::with('course')->whereIn('course_id', $section_ids)->orderBy('name')->get();
  248. return View::make('local.managers.shared.print_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'activities'));
  249. }
  250. private function excelConv(&$value, $key)
  251. {
  252. Log::debug('CoursesController@excelConv');
  253. $value = iconv('UTF-8', 'Windows-1252//IGNORE', $value);
  254. }
  255. public function exportGrades($id)
  256. {
  257. // Find course
  258. $course = Course::find($id);
  259. // Set file name and open file
  260. $filename = 'olas_student_grades_' . $course->code . $course->number . $course->section . $course->semester->code . '_' . time() . '.csv';
  261. $file = fopen('exports/' . $filename, 'w');
  262. // To add an empty line to the csv
  263. $empty_line = array(' ');
  264. // Set section name at the top
  265. fputcsv($file, array($course->code . $course->number . $course->section . ' (' . $course->semester->code . ')'));
  266. fputcsv($file, $empty_line);
  267. // Individual activity tables -----------------------------------------
  268. // For each activity
  269. foreach ($course->assessedActivities as $activity) {
  270. // Set name
  271. $activity_name = iconv('UTF-8', 'Windows-1252//IGNORE', $activity->name);
  272. fputcsv($file, array($activity_name));
  273. // Get assessments
  274. $assessments = DB::table('assessments')->join('students', 'assessments.student_id', '=', 'students.id')->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  275. // Get rubric contents
  276. $rubric_contents = json_decode($activity->rubric->contents);
  277. $criterion_list = array('#', 'Name', 'School', 'Major');
  278. // Set criterion names
  279. foreach ($rubric_contents as $criterion) {
  280. $criterion_list[] = $criterion->name;
  281. }
  282. $criterion_list[] = 'Percentage';
  283. $criterion_list[] = 'Comments';
  284. // Change encoding to be compatible with Excel
  285. array_walk($criterion_list, array($this, 'excelConv'));
  286. fputcsv($file, $criterion_list);
  287. // Set student info and scores for each criterion
  288. foreach ($assessments as $assessment) {
  289. $student = Student::find($assessment->student_id);
  290. $scores = json_decode($assessment->scores, true);
  291. fputcsv($file, array_merge(
  292. array($student->number, $student->name, $student->school_code, $student->conc_code),
  293. $scores,
  294. array($assessment->percentage, $assessment->comments)
  295. ));
  296. }
  297. fputcsv($file, $empty_line);
  298. }
  299. // Table with all activities and grades
  300. // Set headers for table
  301. $activity_list = array('#', 'Name', 'School', 'Major');
  302. foreach ($course->assessedActivities as $activity) {
  303. $activity_list[] = $activity->name;
  304. }
  305. $activity_list[] = 'Average';
  306. fputcsv($file, $empty_line);
  307. fputcsv($file, array('All Activities'));
  308. // Change encoding to be compatible with Excel
  309. array_walk($activity_list, array($this, 'excelConv'));
  310. fputcsv($file, $activity_list);
  311. // For each student, set info
  312. foreach ($activity->course->students as $student) {
  313. $student_record = array($student->number, $student->name, $student->school_code, $student->conc_code);
  314. $assessed_activities = $course->assessedActivities;
  315. // Iterate over activities, get percentages and add them to the record array
  316. foreach ($assessed_activities as $activity) {
  317. $percentage = DB::table('assessments')
  318. ->select('percentage')
  319. ->where('student_id', '=', $student->id)
  320. ->where('activity_id', '=', $activity->id)->first();
  321. $student_record[] = $percentage->percentage;
  322. }
  323. // Average
  324. $student_record[] = array_sum(
  325. array_slice(
  326. $student_record,
  327. 4,
  328. count($assessed_activities)
  329. )
  330. ) / count($assessed_activities);
  331. fputcsv($file, $student_record);
  332. }
  333. // Close the file
  334. fclose($file);
  335. // Return response for download
  336. return Response::download('exports/' . $filename);
  337. }
  338. public function reassign()
  339. {
  340. $title = 'Course Management';
  341. $programs = Program::with('school')->orderBy('name', 'asc')->get();
  342. $users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  343. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  344. return View::make('local.managers.admins.reassign', compact('title', 'programs', 'users', 'semesters'));
  345. }
  346. public function update()
  347. {
  348. if (Input::get('reassign_program')) {
  349. $code = strtoupper(trim(str_replace('*', '%', Input::get('code'))));
  350. $number = strtoupper(trim(str_replace('*', '%', Input::get('number'))));
  351. $section = strtoupper(trim(str_replace('*', '%', Input::get('section'))));
  352. if (!$code && !$number && !$section) {
  353. Session::flash('status', 'danger');
  354. Session::flash('message', 'At least one field is required.');
  355. return Redirect::back()->withInput();
  356. }
  357. $update_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  358. $fetch_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  359. if ($code) {
  360. $update_query->where('code', 'LIKE', $code);
  361. $fetch_query->where('code', 'LIKE', $code);
  362. }
  363. if ($number) {
  364. $update_query->where('number', 'LIKE', $number);
  365. $fetch_query->where('number', 'LIKE', $number);
  366. }
  367. if ($section) {
  368. $update_query->where('section', 'LIKE', $section);
  369. $fetch_query->where('section', 'LIKE', $section);
  370. }
  371. // If section is blank, results can be grouped by code and number
  372. else {
  373. $fetch_query->groupBy(array('code', 'number'));
  374. }
  375. $affected_rows = $fetch_query->get();
  376. // If there are results
  377. if (!$affected_rows->isEmpty()) {
  378. // Try updating and flash success message if successful
  379. if ($update_query->update(array('program_id' => Input::get('program')))) {
  380. Session::flash('courses', json_encode($affected_rows));
  381. Session::flash('status', 'success');
  382. Session::flash('message', 'Courses succesfully updated.');
  383. if ($section) {
  384. Session::flash('show_sections', true);
  385. }
  386. } else {
  387. Session::flash('status', 'danger');
  388. Session::flash('message', 'Error reassigning courses to a program. Try again later.');
  389. }
  390. } else {
  391. Session::flash('status', 'warning');
  392. Session::flash('message', 'No courses matched your criteria. Try to broaden your search.');
  393. }
  394. return Redirect::back()->withInput();
  395. } elseif (Input::get('reassign_professor')) {
  396. $code = strtoupper(trim(str_replace('*', '%', Input::get('code_prof'))));
  397. $number = strtoupper(trim(str_replace('*', '%', Input::get('number_prof'))));
  398. $section = strtoupper(trim(str_replace('*', '%', Input::get('section_prof'))));
  399. $user = Input::get('user_prof');
  400. $semester = Input::get('semester_prof');
  401. if (!$code || !$number || !$section) {
  402. Session::flash('status', 'danger');
  403. Session::flash('message', 'Error assigning professor to a course. All fields are required.');
  404. return Redirect::back()->withInput();
  405. }
  406. $update_query = Course::where('code', '=', $code)
  407. ->where('number', '=', $number)
  408. ->where('section', '=', $section)
  409. ->where('semester_id', '=', $semester);
  410. $fetch_query = Course::where('code', '=', $code)
  411. ->where('number', '=', $number)
  412. ->where('section', '=', $section)
  413. ->orderBy('code', 'asc')
  414. ->orderBy('number', 'asc')
  415. ->orderBy('section', 'asc');
  416. $affected_rows = $fetch_query->get();
  417. // If there are results
  418. if (!$affected_rows->isEmpty()) {
  419. try {
  420. // Try updating and flash success message if successful
  421. $update_query->update(array('user_id' => $user));
  422. Session::flash('status', 'success');
  423. Session::flash('message', 'Successfully changed professor for ' . $code . $number . '-' . $section);
  424. } catch (Exception $e) {
  425. Session::flash('status', 'danger');
  426. Session::flash('message', 'An error occurred when changing professor for ' . $code . $number . '-' . $section) . '.';
  427. }
  428. } else {
  429. Session::flash('status', 'warning');
  430. Session::flash('message', 'No course matches your criteria. Try again with different values.');
  431. }
  432. return Redirect::back()->withInput();
  433. } else {
  434. }
  435. }
  436. }