설명 없음

CoursesController.php 20KB

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