Brak opisu

CoursesController.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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')
  275. // ->join('students', 'assessments.student_id', '=', 'students.id')
  276. // ->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  277. $assessments = DB::table('assessments')
  278. ->join('students', 'assessments.student_id', '=', 'students.id')
  279. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  280. ->where('activity_criterion.activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  281. // Get rubric contents
  282. $rubric_contents = json_decode($activity->rubric->contents);
  283. $criterion_list = array('#', 'Name', 'School', 'Major');
  284. // Set criterion names
  285. foreach ($rubric_contents as $criterion) {
  286. $criterion_list[] = $criterion->name;
  287. }
  288. $criterion_list[] = 'Percentage';
  289. $criterion_list[] = 'Comments';
  290. // Change encoding to be compatible with Excel
  291. array_walk($criterion_list, array($this, 'excelConv'));
  292. fputcsv($file, $criterion_list);
  293. // Set student info and scores for each criterion
  294. foreach ($assessments as $assessment) {
  295. $student = Student::find($assessment->student_id);
  296. $scores = json_decode($assessment->scores, true);
  297. fputcsv($file, array_merge(
  298. array($student->number, $student->name, $student->school_code, $student->conc_code),
  299. $scores,
  300. array($assessment->percentage, $assessment->comments)
  301. ));
  302. }
  303. fputcsv($file, $empty_line);
  304. }
  305. // Table with all activities and grades
  306. // Set headers for table
  307. $activity_list = array('#', 'Name', 'School', 'Major');
  308. foreach ($course->assessedActivities as $activity) {
  309. $activity_list[] = $activity->name;
  310. }
  311. $activity_list[] = 'Average';
  312. fputcsv($file, $empty_line);
  313. fputcsv($file, array('All Activities'));
  314. // Change encoding to be compatible with Excel
  315. array_walk($activity_list, array($this, 'excelConv'));
  316. fputcsv($file, $activity_list);
  317. // For each student, set info
  318. foreach ($activity->course->students as $student) {
  319. $student_record = array($student->number, $student->name, $student->school_code, $student->conc_code);
  320. $assessed_activities = $course->assessedActivities;
  321. // Iterate over activities, get percentages and add them to the record array
  322. foreach ($assessed_activities as $activity) {
  323. $percentage = DB::table('assessments')
  324. ->select('percentage')
  325. ->where('student_id', '=', $student->id)
  326. ->where('activity_id', '=', $activity->id)->first();
  327. $student_record[] = $percentage->percentage;
  328. }
  329. // Average
  330. $student_record[] = array_sum(
  331. array_slice(
  332. $student_record,
  333. 4,
  334. count($assessed_activities)
  335. )
  336. ) / count($assessed_activities);
  337. fputcsv($file, $student_record);
  338. }
  339. // Close the file
  340. fclose($file);
  341. // Return response for download
  342. return Response::download('exports/' . $filename);
  343. }
  344. public function reassign()
  345. {
  346. $title = 'Course Management';
  347. $programs = Program::with('school')->orderBy('name', 'asc')->get();
  348. $users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  349. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  350. return View::make('local.managers.admins.reassign', compact('title', 'programs', 'users', 'semesters'));
  351. }
  352. public function update()
  353. {
  354. if (Input::get('reassign_program')) {
  355. $code = strtoupper(trim(str_replace('*', '%', Input::get('code'))));
  356. $number = strtoupper(trim(str_replace('*', '%', Input::get('number'))));
  357. $section = strtoupper(trim(str_replace('*', '%', Input::get('section'))));
  358. if (!$code && !$number && !$section) {
  359. Session::flash('status', 'danger');
  360. Session::flash('message', 'At least one field is required.');
  361. return Redirect::back()->withInput();
  362. }
  363. $update_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  364. $fetch_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  365. if ($code) {
  366. $update_query->where('code', 'LIKE', $code);
  367. $fetch_query->where('code', 'LIKE', $code);
  368. }
  369. if ($number) {
  370. $update_query->where('number', 'LIKE', $number);
  371. $fetch_query->where('number', 'LIKE', $number);
  372. }
  373. if ($section) {
  374. $update_query->where('section', 'LIKE', $section);
  375. $fetch_query->where('section', 'LIKE', $section);
  376. }
  377. // If section is blank, results can be grouped by code and number
  378. else {
  379. $fetch_query->groupBy(array('code', 'number'));
  380. }
  381. $affected_rows = $fetch_query->get();
  382. // If there are results
  383. if (!$affected_rows->isEmpty()) {
  384. // Try updating and flash success message if successful
  385. if ($update_query->update(array('program_id' => Input::get('program')))) {
  386. Session::flash('courses', json_encode($affected_rows));
  387. Session::flash('status', 'success');
  388. Session::flash('message', 'Courses succesfully updated.');
  389. if ($section) {
  390. Session::flash('show_sections', true);
  391. }
  392. } else {
  393. Session::flash('status', 'danger');
  394. Session::flash('message', 'Error reassigning courses to a program. Try again later.');
  395. }
  396. } else {
  397. Session::flash('status', 'warning');
  398. Session::flash('message', 'No courses matched your criteria. Try to broaden your search.');
  399. }
  400. return Redirect::back()->withInput();
  401. } elseif (Input::get('reassign_professor')) {
  402. $code = strtoupper(trim(str_replace('*', '%', Input::get('code_prof'))));
  403. $number = strtoupper(trim(str_replace('*', '%', Input::get('number_prof'))));
  404. $section = strtoupper(trim(str_replace('*', '%', Input::get('section_prof'))));
  405. $user = Input::get('user_prof');
  406. $semester = Input::get('semester_prof');
  407. if (!$code || !$number || !$section) {
  408. Session::flash('status', 'danger');
  409. Session::flash('message', 'Error assigning professor to a course. All fields are required.');
  410. return Redirect::back()->withInput();
  411. }
  412. $update_query = Course::where('code', '=', $code)
  413. ->where('number', '=', $number)
  414. ->where('section', '=', $section)
  415. ->where('semester_id', '=', $semester);
  416. $fetch_query = Course::where('code', '=', $code)
  417. ->where('number', '=', $number)
  418. ->where('section', '=', $section)
  419. ->orderBy('code', 'asc')
  420. ->orderBy('number', 'asc')
  421. ->orderBy('section', 'asc');
  422. $affected_rows = $fetch_query->get();
  423. // If there are results
  424. if (!$affected_rows->isEmpty()) {
  425. try {
  426. // Try updating and flash success message if successful
  427. $update_query->update(array('user_id' => $user));
  428. Session::flash('status', 'success');
  429. Session::flash('message', 'Successfully changed professor for ' . $code . $number . '-' . $section);
  430. } catch (Exception $e) {
  431. Session::flash('status', 'danger');
  432. Session::flash('message', 'An error occurred when changing professor for ' . $code . $number . '-' . $section) . '.';
  433. }
  434. } else {
  435. Session::flash('status', 'warning');
  436. Session::flash('message', 'No course matches your criteria. Try again with different values.');
  437. }
  438. return Redirect::back()->withInput();
  439. } else {
  440. }
  441. }
  442. }