Keine Beschreibung

CoursesController.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. $outcomes = Outcome::orderBy('name', 'asc')->get();
  196. $outcomeCount = Outcome::all()->count();
  197. foreach ($grouped_courses as $index => $grouped_course) {
  198. // Blank outcomes for one course
  199. $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  200. $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  201. // Find sections for this course
  202. $sections = Course::where('code', $grouped_course->code)
  203. ->where('number', $grouped_course->number)
  204. ->where('semester_id', $grouped_course->semester_id)
  205. ->get();
  206. // For each of the sections, add the attempted and achieved criteria per outcome
  207. foreach ($sections as $section) {
  208. if ($section->outcomes_achieved != NULL) {
  209. $section_outcomes_achieved = json_decode($section->outcomes_achieved, true);
  210. $section_outcomes_attempted = json_decode($section->outcomes_attempted, true);
  211. for ($i = 1; $i <= count($outcomes_attempted); $i++) {
  212. $outcomes_achieved[$i] += $section_outcomes_achieved[$i];
  213. $outcomes_attempted[$i] += $section_outcomes_attempted[$i];
  214. }
  215. }
  216. }
  217. }
  218. $section_ids = Course::where('code', $code)
  219. ->where('number', $number)
  220. ->where('semester_id', $semester->id)
  221. ->lists('id');
  222. // Activities with transforming actions
  223. $activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  224. return View::make('local.managers.shared.print_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'activities'));
  225. }
  226. private function excelConv(&$value, $key)
  227. {
  228. Log::debug('CoursesController@excelConv');
  229. $value = iconv('UTF-8', 'Windows-1252//IGNORE', $value);
  230. }
  231. public function exportGrades($id)
  232. {
  233. // Find course
  234. $course = Course::find($id);
  235. // Set file name and open file
  236. $filename = 'olas_student_grades_' . $course->code . $course->number . $course->section . $course->semester->code . '_' . time() . '.csv';
  237. $file = fopen('exports/' . $filename, 'w');
  238. // To add an empty line to the csv
  239. $empty_line = array(' ');
  240. // Set section name at the top
  241. fputcsv($file, array($course->code . $course->number . $course->section . ' (' . $course->semester->code . ')'));
  242. fputcsv($file, $empty_line);
  243. // Individual activity tables -----------------------------------------
  244. // For each activity
  245. foreach ($course->assessedActivities as $activity) {
  246. // Set name
  247. $activity_name = iconv('UTF-8', 'Windows-1252//IGNORE', $activity->name);
  248. fputcsv($file, array($activity_name));
  249. // Get assessments
  250. $assessments = DB::table('assessments')->join('students', 'assessments.student_id', '=', 'students.id')->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  251. // Get rubric contents
  252. $rubric_contents = json_decode($activity->rubric->contents);
  253. $criterion_list = array('#', 'Name', 'School', 'Major');
  254. // Set criterion names
  255. foreach ($rubric_contents as $criterion) {
  256. $criterion_list[] = $criterion->name;
  257. }
  258. $criterion_list[] = 'Percentage';
  259. $criterion_list[] = 'Comments';
  260. // Change encoding to be compatible with Excel
  261. array_walk($criterion_list, array($this, 'excelConv'));
  262. fputcsv($file, $criterion_list);
  263. // Set student info and scores for each criterion
  264. foreach ($assessments as $assessment) {
  265. $student = Student::find($assessment->student_id);
  266. $scores = json_decode($assessment->scores, true);
  267. fputcsv($file, array_merge(
  268. array($student->number, $student->name, $student->school_code, $student->conc_code),
  269. $scores,
  270. array($assessment->percentage, $assessment->comments)
  271. ));
  272. }
  273. fputcsv($file, $empty_line);
  274. }
  275. // Table with all activities and grades
  276. // Set headers for table
  277. $activity_list = array('#', 'Name', 'School', 'Major');
  278. foreach ($course->assessedActivities as $activity) {
  279. $activity_list[] = $activity->name;
  280. }
  281. $activity_list[] = 'Average';
  282. fputcsv($file, $empty_line);
  283. fputcsv($file, array('All Activities'));
  284. // Change encoding to be compatible with Excel
  285. array_walk($activity_list, array($this, 'excelConv'));
  286. fputcsv($file, $activity_list);
  287. // For each student, set info
  288. foreach ($activity->course->students as $student) {
  289. $student_record = array($student->number, $student->name, $student->school_code, $student->conc_code);
  290. $assessed_activities = $course->assessedActivities;
  291. // Iterate over activities, get percentages and add them to the record array
  292. foreach ($assessed_activities as $activity) {
  293. $percentage = DB::table('assessments')
  294. ->select('percentage')
  295. ->where('student_id', '=', $student->id)
  296. ->where('activity_id', '=', $activity->id)->first();
  297. $student_record[] = $percentage->percentage;
  298. }
  299. // Average
  300. $student_record[] = array_sum(
  301. array_slice(
  302. $student_record,
  303. 4,
  304. count($assessed_activities)
  305. )
  306. ) / count($assessed_activities);
  307. fputcsv($file, $student_record);
  308. }
  309. // Close the file
  310. fclose($file);
  311. // Return response for download
  312. return Response::download('exports/' . $filename);
  313. }
  314. public function reassign()
  315. {
  316. $title = 'Course Management';
  317. $programs = Program::with('school')->orderBy('name', 'asc')->get();
  318. $users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  319. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  320. return View::make('local.managers.admins.reassign', compact('title', 'programs', 'users', 'semesters'));
  321. }
  322. public function update()
  323. {
  324. if (Input::get('reassign_program')) {
  325. $code = strtoupper(trim(str_replace('*', '%', Input::get('code'))));
  326. $number = strtoupper(trim(str_replace('*', '%', Input::get('number'))));
  327. $section = strtoupper(trim(str_replace('*', '%', Input::get('section'))));
  328. if (!$code && !$number && !$section) {
  329. Session::flash('status', 'danger');
  330. Session::flash('message', 'At least one field is required.');
  331. return Redirect::back()->withInput();
  332. }
  333. $update_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  334. $fetch_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  335. if ($code) {
  336. $update_query->where('code', 'LIKE', $code);
  337. $fetch_query->where('code', 'LIKE', $code);
  338. }
  339. if ($number) {
  340. $update_query->where('number', 'LIKE', $number);
  341. $fetch_query->where('number', 'LIKE', $number);
  342. }
  343. if ($section) {
  344. $update_query->where('section', 'LIKE', $section);
  345. $fetch_query->where('section', 'LIKE', $section);
  346. }
  347. // If section is blank, results can be grouped by code and number
  348. else {
  349. $fetch_query->groupBy(array('code', 'number'));
  350. }
  351. $affected_rows = $fetch_query->get();
  352. // If there are results
  353. if (!$affected_rows->isEmpty()) {
  354. // Try updating and flash success message if successful
  355. if ($update_query->update(array('program_id' => Input::get('program')))) {
  356. Session::flash('courses', json_encode($affected_rows));
  357. Session::flash('status', 'success');
  358. Session::flash('message', 'Courses succesfully updated.');
  359. if ($section) {
  360. Session::flash('show_sections', true);
  361. }
  362. } else {
  363. Session::flash('status', 'danger');
  364. Session::flash('message', 'Error reassigning courses to a program. Try again later.');
  365. }
  366. } else {
  367. Session::flash('status', 'warning');
  368. Session::flash('message', 'No courses matched your criteria. Try to broaden your search.');
  369. }
  370. return Redirect::back()->withInput();
  371. } elseif (Input::get('reassign_professor')) {
  372. $code = strtoupper(trim(str_replace('*', '%', Input::get('code_prof'))));
  373. $number = strtoupper(trim(str_replace('*', '%', Input::get('number_prof'))));
  374. $section = strtoupper(trim(str_replace('*', '%', Input::get('section_prof'))));
  375. $user = Input::get('user_prof');
  376. $semester = Input::get('semester_prof');
  377. if (!$code || !$number || !$section) {
  378. Session::flash('status', 'danger');
  379. Session::flash('message', 'Error assigning professor to a course. All fields are required.');
  380. return Redirect::back()->withInput();
  381. }
  382. $update_query = Course::where('code', '=', $code)
  383. ->where('number', '=', $number)
  384. ->where('section', '=', $section)
  385. ->where('semester_id', '=', $semester);
  386. $fetch_query = Course::where('code', '=', $code)
  387. ->where('number', '=', $number)
  388. ->where('section', '=', $section)
  389. ->orderBy('code', 'asc')
  390. ->orderBy('number', 'asc')
  391. ->orderBy('section', 'asc');
  392. $affected_rows = $fetch_query->get();
  393. // If there are results
  394. if (!$affected_rows->isEmpty()) {
  395. try {
  396. // Try updating and flash success message if successful
  397. $update_query->update(array('user_id' => $user));
  398. Session::flash('status', 'success');
  399. Session::flash('message', 'Successfully changed professor for ' . $code . $number . '-' . $section);
  400. } catch (Exception $e) {
  401. Session::flash('status', 'danger');
  402. Session::flash('message', 'An error occurred when changing professor for ' . $code . $number . '-' . $section) . '.';
  403. }
  404. } else {
  405. Session::flash('status', 'warning');
  406. Session::flash('message', 'No course matches your criteria. Try again with different values.');
  407. }
  408. return Redirect::back()->withInput();
  409. } else {
  410. }
  411. }
  412. }