Sin descripción

CoursesController.php 33KB

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