No Description

CoursesController.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. use Illuminate\Support\Facades\Log;
  3. class
  4. CoursesController extends \BaseController
  5. {
  6. /**
  7. * Display the specified resource.
  8. *
  9. * @param int $id
  10. * @return Response
  11. */
  12. public function show($id)
  13. {
  14. $course = Course::with('semester')->where('id', $id)->first();
  15. $title = $course->code . $course->number . '-' . $course->section . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  16. // If course does not exist, display 404
  17. if (!$course)
  18. App::abort('404');
  19. // If course does not belong to the person
  20. if ($course->user_id != Auth::id())
  21. App::abort('403', 'Access forbidden.');
  22. $activities = $course->activities;
  23. $students = $course->students;
  24. //$outcomes = Outcome::orderBy('name', 'asc')->get();
  25. $semesters = Session::get('semesters_ids');
  26. $semesters = DB::table('semesters')->where('id', $course->semester_id)->orderBy('start', 'ASC')->first();
  27. $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))
  28. ->whereNull('deleted_at')
  29. ->whereRaw("(deactivation_date IS NULL or deactivation_date >= '{$semesters->start}')")
  30. ->orderBy('name', 'ASC')->get();
  31. $outcomes_achieved = $course->outcomes_ach();
  32. // $outcomes_attempted = json_decode($course->outcomes_attempted, true);
  33. $outcomes_attempted = $course->outcomes_att();
  34. // Get active semesters
  35. $active_semesters = array();
  36. $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();
  37. foreach ($active_semesters_collection as $active_semester) {
  38. $active_semesters[] = $active_semester->id;
  39. }
  40. return View::make('local.professors.course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'active_semesters'));
  41. }
  42. public function editView()
  43. {
  44. $title = 'Create/Edit Course';
  45. switch (Auth::user()->role) {
  46. case 1:
  47. $programs = Program::with('school')->orderBy('name', 'asc')->get();
  48. $professors = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  49. break;
  50. case 2:
  51. $programs = Program::where("school_id", Auth::user()->school->id);
  52. $professors = User::fromSchool(Auth::user()->school->id)->select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  53. break;
  54. case 3:
  55. $programs = Auth::user()->programs;
  56. $program_ids = Program::join('program_user', 'program_user.program_id', '=', 'programs.id')
  57. ->where('program_user.user_id', Auth::user()->id)
  58. ->lists('programs.id');
  59. $professors = User::fromPrograms($program_ids)->select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  60. break;
  61. }
  62. //$users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  63. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'desc')->get();
  64. $modalities = DB::table("courses")
  65. ->groupBy("modality")
  66. ->lists("modality");
  67. return View::make('local.managers.shared.course_edit', compact('title', 'programs', 'professors', 'modalities', 'semesters'));
  68. }
  69. public function create()
  70. {
  71. $all_input = Input::all();
  72. $name = $all_input['name'];
  73. $code = $all_input['code'];
  74. $number = $all_input['number'];
  75. $section = $all_input['section'];
  76. $program = $all_input['program'];
  77. $professor_id = $all_input['professor_id'];
  78. $semester_id = $all_input['semester_id'];
  79. //Input::file('students')//->move('course_student/', 'test.txt');
  80. Log::info($all_input);
  81. /** Instantiate new Objective */
  82. $course = new Course;
  83. $course->name = $name;
  84. $course->code = $code;
  85. $course->number = $number;
  86. $course->section = $section;
  87. $course->program_id = $program;
  88. $course->user_id = $professor_id;
  89. $course->semester_id = $semester_id;
  90. /** If Objective is saved, send success message */
  91. if ($course->save()) {
  92. //$students->move('./course_student', 'test.txt');
  93. $student_nums = file(Input::file('students'));
  94. if ($student_nums) {
  95. foreach ($student_nums as $stu_num) {
  96. Log::info($stu_num);
  97. Log::info("Stunum");
  98. $student_id = DB::select("select id from students where number = " . $stu_num);
  99. /*DB::table("students")
  100. ->where('number', '=', $stu_num)
  101. ->first();
  102. */
  103. if ($student_id) {
  104. $student_id = $student_id[0]->id;
  105. DB::table("course_student")
  106. ->insert(array(
  107. 'course_id' => $course->id,
  108. 'student_id' => $student_id,
  109. 'semester_id' => $course->semester_id
  110. ));
  111. } else {
  112. $course->forceDelete();
  113. Session::flash('status', 'danger');
  114. Session::flash('message', '<p>Error Enrolling students. Please try again later.</p>');
  115. return Redirect::to('editCourses')->withInput();
  116. }
  117. }
  118. Session::flash('status', 'success');
  119. Session::flash('message', 'Course created: "' . $course->name . '".');
  120. return Redirect::to('editCourses');
  121. //fclose($student_nums);
  122. } else {
  123. $course->forceDelete();
  124. Session::flash('status', 'danger');
  125. Session::flash('message', '<p>Error creating course. Please try again later.</p>');
  126. return Redirect::to('editCourses')->withInput();
  127. }
  128. /*$objectiveId = $objective->id;
  129. Log::info($clean_input['outcome_id']);
  130. foreach ($clean_input['program_id'] as $program_id) {
  131. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  132. }
  133. foreach ($clean_input['outcome_id'] as $outcome_id) {
  134. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  135. //DB::raw("insert ignore into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  136. /*if (!($objectiveOutcome->save())) {
  137. Session::flash('status', 'danger');
  138. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  139. return Redirect::to('objective')->withInput();
  140. }
  141. }
  142. DB::table('criterion_objective_outcome')
  143. ->join('program_criterion', 'criterion_objective_outcome.criterion_id', "=", 'program_criterion.criterion_id')
  144. ->whereIn('program_id', $clean_input['program_id'])
  145. ->whereIn('outcome_id', $clean_input['outcome_id'])
  146. ->where('objective_id', '=', 0)
  147. ->update(array('objective_id' => $objectiveId));
  148. // update("update criterion_objective_outcome coo join program_criterion pc on coo.criterion_id=pc.criterion_id set
  149. // objective_id= {$objectiveId} where program_id in (".$clean_input['program_id'].") and objective_id=0 and outcome_id in (".$clean_input['outcome_id'].")");
  150. */
  151. /*$role = Auth::user()['role'];
  152. switch ($role) {
  153. case 1:
  154. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  155. case 2:
  156. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  157. case 3:
  158. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  159. }*/
  160. }
  161. /** If saving fails, send error message and old data */
  162. else {
  163. Session::flash('status', 'danger');
  164. Session::flash('message', '<p>Error creating course. Please try again later.</p>');
  165. return Redirect::to('editCourse')->withInput();
  166. /*$role = Auth::user()['role'];
  167. switch ($role) {
  168. case 1:
  169. return Redirect::to('objective')->withInput();
  170. case 2:
  171. return Redirect::to('school-objective')->withInput();
  172. case 3:
  173. return Redirect::to('program-objective')->withInput();
  174. }*/
  175. }
  176. }
  177. public function newShow($id)
  178. {
  179. $course = Course::findOrFail($id);
  180. $title = $course->name;
  181. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  182. $is_active_semester = $semesters->filter(function ($semester) {
  183. 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);
  184. });
  185. // $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];
  186. $activities = $course->activities;
  187. // var_dump($active_semesters);
  188. // var_dump($course->semester->id);
  189. return View::make('local.managers.admins.new-course-show', compact('title', 'course', 'semesters', 'activities', 'is_active_semester'));
  190. }
  191. /**
  192. * Display the specified course, but with limited information.
  193. *
  194. * @param int $id
  195. * @return Response
  196. */
  197. public function showLimited($id)
  198. {
  199. $course = Course::with('semester')->where('id', $id)->first();
  200. $students = $course->students;
  201. $title = $course->code . $course->number . '-' . $course->section . ' <span class="small attention">(' . $course->semester->code . ')</span>';
  202. // If course does not exist, display 404
  203. if (!$course)
  204. App::abort('404');
  205. $activities = $course->activities;
  206. $students = $course->students;
  207. $level = DB::table('courses')
  208. ->join('programs', 'programs.id', '=', 'courses.program_id')
  209. ->where('courses.id', $id)
  210. ->select('programs.is_graduate')
  211. ->first();
  212. $outcomes = Outcome::active_by_semesters(array($course->semester), $level->is_graduate);
  213. // $outcomeCount = count((array)$outcomes);
  214. $course = Course::where('id', $id)->first();
  215. foreach ($outcomes as $outcome) {
  216. $outcomes_attempted[$outcome->id] = $outcome->courses_attempted(array($course));
  217. $outcomes_achieved[$outcome->id] = $outcome->courses_achieved(array($course));
  218. }
  219. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  220. // $outcomes_achieved = json_decode($course->outcomes_achieved, true);
  221. // $outcomes_attempted = json_decode($course->outcomes_attempted, true);
  222. $schools = School::all();
  223. $rubrics = array();
  224. Log::info($activities);
  225. foreach ($activities as $activity) {
  226. if (!empty($activity->rubric)) {
  227. $rubrics[] = $activity->rubric[0];
  228. }
  229. }
  230. return View::make('local.managers.shared.limited-course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'schools', 'rubrics', 'students'));
  231. }
  232. /**
  233. * Show grouped sections of a course
  234. */
  235. public function showGrouped($code, $number, $semester_code)
  236. {
  237. $title = $code . $number . ' (' . $semester_code . ')';
  238. $semester = Semester::where('code', $semester_code)->first();
  239. // $selected_semesters = Semester::find(Session::get('semesters_ids'));
  240. $role = Auth::user()->role;
  241. $level = DB::table('courses')
  242. ->join('programs', 'programs.id', '=', 'courses.program_id')
  243. ->where('courses.code', $code)
  244. ->where('courses.number', $number)
  245. ->where('courses.semester_id', $semester->id)
  246. ->select('programs.is_graduate')
  247. ->first();
  248. // var_dump($level);
  249. // exit();
  250. $grouped_courses = Course::where('code', $code)
  251. ->where('number', $number)
  252. ->where('semester_id', $semester->id)
  253. ->groupBy(array('code', 'number'))->get();
  254. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  255. // $outcomeCount = Outcome::all()->count();
  256. $outcomes = Outcome::active_by_semesters(array($semester), $level->is_graduate);
  257. $outcomeCount = count((array)$outcomes);
  258. foreach ($outcomes as $outcome) {
  259. $outcomes_attempted[$outcome->id] = $outcome->courses_attempted($grouped_courses);
  260. $outcomes_achieved[$outcome->id] = $outcome->courses_achieved($grouped_courses);
  261. }
  262. // var_dump($outcomes_attempted);
  263. // print "<br>";
  264. // var_dump($outcomes_achieved);
  265. // print "<br>";
  266. foreach ($grouped_courses as $index => $grouped_course) {
  267. // // Blank outcomes for one course
  268. // $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  269. // $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  270. //
  271. // // Find sections for this course
  272. $sections = Course::where('code', $grouped_course->code)
  273. ->where('number', $grouped_course->number)
  274. ->where('semester_id', $grouped_course->semester_id)
  275. ->get();
  276. //
  277. // // For each of the sections, add the attempted and achieved criteria per outcome
  278. // foreach ($sections as $section)
  279. // {
  280. // if($section->outcomes_achieved!=NULL)
  281. // {
  282. // $section_outcomes_achieved =count($section->outcomes_attempted());
  283. // // var_dump($section_outcomes_achieved);
  284. // // exit();
  285. // $section_outcomes_attempted =count($section->outcomes_achieved());
  286. // // $section_outcomes_achieved =json_decode($section->outcomes_achieved, true);
  287. // // $section_outcomes_attempted =json_decode($section->outcomes_attempted, true);
  288. // foreach($outcomes as $outcome)
  289. // {
  290. // if(!isset($outcomes_achieved[$outcome->id]))$outcomes_achieved[$outcome->id]=0;
  291. // if(!isset($outcomes_attempted[$outcome->id]))$outcomes_attempted[$outcome->id]=0;
  292. // $outcomes_achieved[$outcome->id]+=$section_outcomes_achieved[$i];
  293. // $outcomes_attempted[$outcome->id]+=$section_outcomes_attempted[$i];
  294. //
  295. // }
  296. // // for($i=1; $i<=count($outcomes_attempted); $i++)
  297. // // {
  298. // // $outcomes_achieved[$i]+=$section_outcomes_achieved[$i];
  299. // // $outcomes_attempted[$i]+=$section_outcomes_attempted[$i];
  300. // // }
  301. // }
  302. // }
  303. }
  304. $section_ids = Course::where('code', $code)
  305. ->where('number', $number)
  306. ->where('semester_id', $semester->id)
  307. ->lists('id');
  308. //$activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  309. $activities = Activity::with('course')->whereIn('course_id', $section_ids)
  310. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  311. ->join('transformative_activity_criterion as tac', 'ac.id', '=', 'tac.activity_criterion_id')
  312. ->join('transformative_actions as ta', 'ta.id', '=', 'tac.trans_action_id')
  313. ->select('activities.*')
  314. ->addSelect('ta.description as transforming_actions')
  315. ->distinct()
  316. ->orderBy('name')->get();
  317. return View::make('local.managers.shared.grouped_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'transforming_actions', 'activities'));
  318. }
  319. /**
  320. * Printable version for a course (grouped sections)
  321. */
  322. public function print_course($code, $number, $semester_code)
  323. {
  324. $title = $code . $number . ' (' . $semester_code . ')';
  325. $semester = Semester::where('code', $semester_code)->first();
  326. $role = Auth::user()->role;
  327. $grouped_courses = Course::where('code', $code)
  328. ->where('number', $number)
  329. ->where('semester_id', $semester->id)
  330. ->groupBy(array('code', 'number'))->get();
  331. // Log::info($grouped_courses[0]->id);
  332. // exit();
  333. $level = DB::table('courses')
  334. ->join('programs', 'programs.id', '=', 'courses.program_id')
  335. ->where('courses.id', $grouped_courses[0]->id)
  336. ->select('programs.is_graduate')
  337. ->first();
  338. $outcomes = Outcome::active_by_semesters(array($semester), $level->is_graduate);
  339. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  340. $outcomeCount = Outcome::all()->count();
  341. foreach ($grouped_courses as $index => $grouped_course) {
  342. // Blank outcomes for one course
  343. $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  344. $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  345. // Find sections for this course
  346. $sections = Course::where('code', $grouped_course->code)
  347. ->where('number', $grouped_course->number)
  348. ->where('semester_id', $grouped_course->semester_id)
  349. ->get();
  350. // For each of the sections, add the attempted and achieved criteria per outcome
  351. foreach ($sections as &$section) {
  352. if ($section->outcomes_ach()) {
  353. $section->outcomes_attempted = true;
  354. $course_outcomes_achieved = $section->outcomes_ach();
  355. $course_outcomes_attempted = $section->outcomes_att();
  356. foreach ($course_outcomes_achieved as $outcome => $score) {
  357. if (array_key_exists($outcome, $outcomes_achieved))
  358. $outcomes_achieved[$outcome] += $score;
  359. else $outcomes_achieved[$outcome] = $score;
  360. }
  361. foreach ($course_outcomes_attempted as $outcome => $score) {
  362. if (array_key_exists($outcome, $outcomes_attempted))
  363. $outcomes_attempted[$outcome] += $score;
  364. else $outcomes_attempted[$outcome] = $score;
  365. }
  366. }
  367. // if ($section->outcomes_achieved != NULL) {
  368. // $section_outcomes_achieved = json_decode($section->outcomes_achieved, true);
  369. // $section_outcomes_attempted = json_decode($section->outcomes_attempted, true);
  370. // for ($i = 1; $i <= count($outcomes_attempted); $i++) {
  371. // $outcomes_achieved[$i] += $section_outcomes_achieved[$i];
  372. // $outcomes_attempted[$i] += $section_outcomes_attempted[$i];
  373. // }
  374. // }
  375. }
  376. }
  377. $section_ids = Course::where('code', $code)
  378. ->where('number', $number)
  379. ->where('semester_id', $semester->id)
  380. ->lists('id');
  381. // Activities with transforming actions
  382. // $activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  383. $activities = Activity::with('course')->whereIn('course_id', $section_ids)->orderBy('name')->get();
  384. return View::make('local.managers.shared.print_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'activities'));
  385. }
  386. private function excelConv(&$value, $key)
  387. {
  388. Log::debug('CoursesController@excelConv');
  389. $value = iconv('UTF-8', 'Windows-1252//IGNORE', $value);
  390. }
  391. public function exportGrades($id)
  392. {
  393. // Find course
  394. $course = Course::find($id);
  395. // Set file name and open file
  396. $filename = 'olas_student_grades_' . $course->code . $course->number . $course->section . $course->semester->code . '_' . time() . '.csv';
  397. $file = fopen('exports/' . $filename, 'w');
  398. // To add an empty line to the csv
  399. $empty_line = array(' ');
  400. // Set section name at the top
  401. fputcsv($file, array($course->code . $course->number . $course->section . ' (' . $course->semester->code . ')'));
  402. fputcsv($file, $empty_line);
  403. // Individual activity tables -----------------------------------------
  404. // For each activity
  405. foreach ($course->assessedActivities as $activity) {
  406. // Set name
  407. $activity_name = iconv('UTF-8', 'Windows-1252//IGNORE', $activity->name);
  408. fputcsv($file, array($activity_name));
  409. // Get assessments
  410. // $assessments = DB::table('assessments')
  411. // ->join('students', 'assessments.student_id', '=', 'students.id')
  412. // ->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  413. $assessments = DB::table('assessments')
  414. ->join('students', 'assessments.student_id', '=', 'students.id')
  415. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  416. ->where('activity_criterion.activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  417. // Get rubric contents
  418. $rubric_contents = json_decode($activity->rubric->contents);
  419. $criterion_list = array('#', 'Name', 'School', 'Major');
  420. // Set criterion names
  421. foreach ($rubric_contents as $criterion) {
  422. $criterion_list[] = $criterion->name;
  423. }
  424. $criterion_list[] = 'Percentage';
  425. $criterion_list[] = 'Comments';
  426. // Change encoding to be compatible with Excel
  427. array_walk($criterion_list, array($this, 'excelConv'));
  428. fputcsv($file, $criterion_list);
  429. // Set student info and scores for each criterion
  430. foreach ($assessments as $assessment) {
  431. $student = Student::find($assessment->student_id);
  432. $scores = json_decode($assessment->scores, true);
  433. fputcsv($file, array_merge(
  434. array($student->number, $student->name, $student->school_code, $student->conc_code),
  435. $scores,
  436. array($assessment->percentage, $assessment->comments)
  437. ));
  438. }
  439. fputcsv($file, $empty_line);
  440. }
  441. // Table with all activities and grades
  442. // Set headers for table
  443. $activity_list = array('#', 'Name', 'School', 'Major');
  444. foreach ($course->assessedActivities as $activity) {
  445. $activity_list[] = $activity->name;
  446. }
  447. $activity_list[] = 'Average';
  448. fputcsv($file, $empty_line);
  449. fputcsv($file, array('All Activities'));
  450. // Change encoding to be compatible with Excel
  451. array_walk($activity_list, array($this, 'excelConv'));
  452. fputcsv($file, $activity_list);
  453. // For each student, set info
  454. foreach ($activity->course->students as $student) {
  455. $student_record = array($student->number, $student->name, $student->school_code, $student->conc_code);
  456. $assessed_activities = $course->assessedActivities;
  457. // Iterate over activities, get percentages and add them to the record array
  458. foreach ($assessed_activities as $activity) {
  459. $percentage = DB::table('assessments')
  460. ->select('percentage')
  461. ->where('student_id', '=', $student->id)
  462. ->where('activity_id', '=', $activity->id)->first();
  463. $student_record[] = $percentage->percentage;
  464. }
  465. // Average
  466. $student_record[] = array_sum(
  467. array_slice(
  468. $student_record,
  469. 4,
  470. count($assessed_activities)
  471. )
  472. ) / count($assessed_activities);
  473. fputcsv($file, $student_record);
  474. }
  475. // Close the file
  476. fclose($file);
  477. // Return response for download
  478. return Response::download('exports/' . $filename);
  479. }
  480. public function reassign()
  481. {
  482. $title = 'Course Management';
  483. $programs = Program::with('school')->orderBy('name', 'asc')->get();
  484. $users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  485. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  486. return View::make('local.managers.admins.reassign', compact('title', 'programs', 'users', 'semesters'));
  487. }
  488. public function update()
  489. {
  490. if (Input::get('reassign_program')) {
  491. $code = strtoupper(trim(str_replace('*', '%', Input::get('code'))));
  492. $number = strtoupper(trim(str_replace('*', '%', Input::get('number'))));
  493. $section = strtoupper(trim(str_replace('*', '%', Input::get('section'))));
  494. if (!$code && !$number && !$section) {
  495. Session::flash('status', 'danger');
  496. Session::flash('message', 'At least one field is required.');
  497. return Redirect::back()->withInput();
  498. }
  499. $update_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  500. $fetch_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  501. if ($code) {
  502. $update_query->where('code', 'LIKE', $code);
  503. $fetch_query->where('code', 'LIKE', $code);
  504. }
  505. if ($number) {
  506. $update_query->where('number', 'LIKE', $number);
  507. $fetch_query->where('number', 'LIKE', $number);
  508. }
  509. if ($section) {
  510. $update_query->where('section', 'LIKE', $section);
  511. $fetch_query->where('section', 'LIKE', $section);
  512. }
  513. // If section is blank, results can be grouped by code and number
  514. else {
  515. $fetch_query->groupBy(array('code', 'number'));
  516. }
  517. $affected_rows = $fetch_query->get();
  518. // If there are results
  519. if (!$affected_rows->isEmpty()) {
  520. // Try updating and flash success message if successful
  521. if ($update_query->update(array('program_id' => Input::get('program')))) {
  522. Session::flash('courses', json_encode($affected_rows));
  523. Session::flash('status', 'success');
  524. Session::flash('message', 'Courses succesfully updated.');
  525. if ($section) {
  526. Session::flash('show_sections', true);
  527. }
  528. } else {
  529. Session::flash('status', 'danger');
  530. Session::flash('message', 'Error reassigning courses to a program. Try again later.');
  531. }
  532. } else {
  533. Session::flash('status', 'warning');
  534. Session::flash('message', 'No courses matched your criteria. Try to broaden your search.');
  535. }
  536. return Redirect::back()->withInput();
  537. } elseif (Input::get('reassign_professor')) {
  538. $code = strtoupper(trim(str_replace('*', '%', Input::get('code_prof'))));
  539. $number = strtoupper(trim(str_replace('*', '%', Input::get('number_prof'))));
  540. $section = strtoupper(trim(str_replace('*', '%', Input::get('section_prof'))));
  541. $user = Input::get('user_prof');
  542. $semester = Input::get('semester_prof');
  543. if (!$code || !$number || !$section) {
  544. Session::flash('status', 'danger');
  545. Session::flash('message', 'Error assigning professor to a course. All fields are required.');
  546. return Redirect::back()->withInput();
  547. }
  548. $update_query = Course::where('code', '=', $code)
  549. ->where('number', '=', $number)
  550. ->where('section', '=', $section)
  551. ->where('semester_id', '=', $semester);
  552. $fetch_query = Course::where('code', '=', $code)
  553. ->where('number', '=', $number)
  554. ->where('section', '=', $section)
  555. ->orderBy('code', 'asc')
  556. ->orderBy('number', 'asc')
  557. ->orderBy('section', 'asc');
  558. $affected_rows = $fetch_query->get();
  559. // If there are results
  560. if (!$affected_rows->isEmpty()) {
  561. try {
  562. // Try updating and flash success message if successful
  563. $update_query->update(array('user_id' => $user));
  564. Session::flash('status', 'success');
  565. Session::flash('message', 'Successfully changed professor for ' . $code . $number . '-' . $section);
  566. } catch (Exception $e) {
  567. Session::flash('status', 'danger');
  568. Session::flash('message', 'An error occurred when changing professor for ' . $code . $number . '-' . $section) . '.';
  569. }
  570. } else {
  571. Session::flash('status', 'warning');
  572. Session::flash('message', 'No course matches your criteria. Try again with different values.');
  573. }
  574. return Redirect::back()->withInput();
  575. } else {
  576. }
  577. }
  578. }