Geen omschrijving

CoursesController.php 17KB

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