No Description

CoursesController.php 37KB

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