説明なし

CoursesController.php 18KB

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