Bez popisu

CoursesController.php 35KB

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