Nenhuma descrição

CoursesController.php 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. $results = $course->student_report_for_outcome;
  328. foreach ($outcomes as $outcome) {
  329. $outcomes_attempted[$outcome->id] = isset($results[$outcome->id]) ? $results[$outcome->id]['calculations']['student_attempted'] : 0;
  330. $outcomes_achieved[$outcome->id] = isset($results[$outcome->id]) ? $results[$outcome->id]['calculations']['student_achieved'] : 0;
  331. }
  332. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  333. // $outcomes_achieved = json_decode($course->outcomes_achieved, true);
  334. // $outcomes_attempted = json_decode($course->outcomes_attempted, true);
  335. $schools = School::all();
  336. $rubrics = array();
  337. Log::info($activities);
  338. foreach ($activities as $activity) {
  339. if (!empty($activity->rubric)) {
  340. $rubrics[] = $activity->rubric[0];
  341. }
  342. }
  343. return View::make('local.managers.shared.limited-course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'schools', 'rubrics', 'students'));
  344. }
  345. /**
  346. * Show grouped sections of a course
  347. */
  348. public function showGrouped($code, $number, $semester_code)
  349. {
  350. $title = $code . $number . ' (' . $semester_code . ')';
  351. $semester = Semester::where('code', $semester_code)->first();
  352. // $selected_semesters = Semester::find(Session::get('semesters_ids'));
  353. $role = Auth::user()->role;
  354. $level = DB::table('courses')
  355. ->join('programs', 'programs.id', '=', 'courses.program_id')
  356. ->where('courses.code', $code)
  357. ->where('courses.number', $number)
  358. ->where('courses.semester_id', $semester->id)
  359. ->select('programs.is_graduate')
  360. ->first();
  361. // var_dump($level);
  362. // exit();
  363. $grouped_courses = Course::where('code', $code)
  364. ->where('number', $number)
  365. ->where('semester_id', $semester->id)
  366. ->select("courses.*", DB::raw("1 as grouped"))
  367. ->groupBy(array('code', 'number'))->get();
  368. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  369. // $outcomeCount = Outcome::all()->count();
  370. $outcomes = Outcome::active_by_semesters(array($semester), $level->is_graduate);
  371. $outcomeCount = count((array)$outcomes);
  372. foreach ($outcomes as $outcome) {
  373. $outcomes_attempted[$outcome->id] = 0;
  374. $outcomes_achieved[$outcome->id] = 0;
  375. }
  376. //foreach ($outcomes as $outcome) {
  377. //$outcomes_attempted[$outcome->id] = $outcome->courses_attempted($grouped_courses);
  378. //$outcomes_achieved[$outcome->id] = $outcome->courses_achieved($grouped_courses);
  379. // $outcomes_achieved[$outcome->id] = $results[$outcome->id]['calculations']
  380. // $outcomes_attempted[$outcome->id] = $results[$outcome->id]['calculations']
  381. //}
  382. // var_dump($outcomes_attempted);
  383. // print "<br>";
  384. // var_dump($outcomes_achieved);
  385. // print "<br>";
  386. foreach ($grouped_courses as $index => $grouped_course) {
  387. // // Blank outcomes for one course
  388. // $outcomes_achieved = array_fill(1, $outcomeCount, 0);
  389. // $outcomes_attempted = array_fill(1, $outcomeCount, 0);
  390. //
  391. // // Find sections for this course
  392. $sections = Course::where('code', $grouped_course->code)
  393. ->where('number', $grouped_course->number)
  394. ->where('semester_id', $grouped_course->semester_id)
  395. ->get();
  396. $results = $grouped_course->student_report_for_outcome;
  397. Log::info($results);
  398. foreach ($outcomes as $outcome) {
  399. $outcomes_attempted[$outcome->id] += isset($results[$outcome->id]) ? $results[$outcome->id]['calculations']['student_attempted'] : 0;
  400. $outcomes_achieved[$outcome->id] += isset($results[$outcome->id]) ? $results[$outcome->id]['calculations']['student_achieved'] : 0;
  401. }
  402. //
  403. // // For each of the sections, add the attempted and achieved criteria per outcome
  404. // foreach ($sections as $section)
  405. // {
  406. // if($section->outcomes_achieved!=NULL)
  407. // {
  408. // $section_outcomes_achieved =count($section->outcomes_attempted());
  409. // // var_dump($section_outcomes_achieved);
  410. // // exit();
  411. // $section_outcomes_attempted =count($section->outcomes_achieved());
  412. // // $section_outcomes_achieved =json_decode($section->outcomes_achieved, true);
  413. // // $section_outcomes_attempted =json_decode($section->outcomes_attempted, true);
  414. // foreach($outcomes as $outcome)
  415. // {
  416. // if(!isset($outcomes_achieved[$outcome->id]))$outcomes_achieved[$outcome->id]=0;
  417. // if(!isset($outcomes_attempted[$outcome->id]))$outcomes_attempted[$outcome->id]=0;
  418. // $outcomes_achieved[$outcome->id]+=$section_outcomes_achieved[$i];
  419. // $outcomes_attempted[$outcome->id]+=$section_outcomes_attempted[$i];
  420. //
  421. // }
  422. // // for($i=1; $i<=count($outcomes_attempted); $i++)
  423. // // {
  424. // // $outcomes_achieved[$i]+=$section_outcomes_achieved[$i];
  425. // // $outcomes_attempted[$i]+=$section_outcomes_attempted[$i];
  426. // // }
  427. // }
  428. // }
  429. }
  430. $section_ids = Course::where('code', $code)
  431. ->where('number', $number)
  432. ->where('semester_id', $semester->id)
  433. ->lists('id');
  434. //$activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  435. $activities = Activity::with('course')->whereIn('course_id', $section_ids)
  436. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  437. //->leftJoin('transformative_activity_criterion as tac', 'ac.id', '=', 'tac.activity_criterion_id')
  438. //->join('transformative_actions as ta', 'ta.id', '=', 'tac.trans_action_id')
  439. ->select('activities.*')
  440. //->addSelect('ta.description as transforming_actions')
  441. ->distinct()
  442. ->orderBy('name')->get();
  443. return View::make('local.managers.shared.grouped_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'transforming_actions', 'activities'));
  444. }
  445. /**
  446. * Printable version for a course (grouped sections)
  447. */
  448. public function print_course($code, $number, $semester_code)
  449. {
  450. $title = $code . $number . ' (' . $semester_code . ')';
  451. $semester = Semester::where('code', $semester_code)->first();
  452. $role = Auth::user()->role;
  453. $grouped_courses = Course::where('code', $code)
  454. ->where('number', $number)
  455. ->where('semester_id', $semester->id)
  456. ->select("courses.*", DB::raw("1 as grouped"))
  457. ->groupBy(array('code', 'number'))->get();
  458. // Log::info($grouped_courses[0]->id);
  459. // exit();
  460. $level = DB::table('courses')
  461. ->join('programs', 'programs.id', '=', 'courses.program_id')
  462. ->where('courses.id', $grouped_courses[0]->id)
  463. ->select('programs.is_graduate')
  464. ->first();
  465. $outcomes = Outcome::active_by_semesters(array($semester), $level->is_graduate);
  466. $outcomeCount = count((array)$outcomes);
  467. foreach ($outcomes as $outcome) {
  468. $outcomes_attempted[$outcome->id] = 0;
  469. $outcomes_achieved[$outcome->id] = 0;
  470. }
  471. // $outcomes = Outcome::orderBy('name', 'asc')->get();
  472. foreach ($grouped_courses as $index => $grouped_course) {
  473. // Find sections for this course
  474. $sections = Course::where('code', $grouped_course->code)
  475. ->where('number', $grouped_course->number)
  476. ->where('semester_id', $grouped_course->semester_id)
  477. ->get();
  478. $results = $grouped_course->student_report_for_outcome;
  479. // Log::info($results);
  480. foreach ($outcomes as $outcome) {
  481. $outcomes_attempted[$outcome->id] += isset($results[$outcome->id]) ? $results[$outcome->id]['calculations']['student_attempted'] : 0;
  482. $outcomes_achieved[$outcome->id] += isset($results[$outcome->id]) ? $results[$outcome->id]['calculations']['student_achieved'] : 0;
  483. }
  484. /*
  485. // For each of the sections, add the attempted and achieved criteria per outcome
  486. foreach ($sections as &$section) {
  487. if ($section->outcomes_ach()) {
  488. $section->outcomes_attempted = true;
  489. $course_outcomes_achieved = $section->outcomes_ach();
  490. $course_outcomes_attempted = $section->outcomes_att();
  491. foreach ($course_outcomes_achieved as $outcome => $score) {
  492. if (array_key_exists($outcome, $outcomes_achieved))
  493. $outcomes_achieved[$outcome] += $score;
  494. else $outcomes_achieved[$outcome] = $score;
  495. }
  496. foreach ($course_outcomes_attempted as $outcome => $score) {
  497. if (array_key_exists($outcome, $outcomes_attempted))
  498. $outcomes_attempted[$outcome] += $score;
  499. else $outcomes_attempted[$outcome] = $score;
  500. }
  501. }
  502. // if ($section->outcomes_achieved != NULL) {
  503. // $section_outcomes_achieved = json_decode($section->outcomes_achieved, true);
  504. // $section_outcomes_attempted = json_decode($section->outcomes_attempted, true);
  505. // for ($i = 1; $i <= count($outcomes_attempted); $i++) {
  506. // $outcomes_achieved[$i] += $section_outcomes_achieved[$i];
  507. // $outcomes_attempted[$i] += $section_outcomes_attempted[$i];
  508. // }
  509. // }
  510. }
  511. }
  512. */
  513. }
  514. $section_ids = Course::where('code', $code)
  515. ->where('number', $number)
  516. ->where('semester_id', $semester->id)
  517. ->lists('id');
  518. // Activities with transforming actions
  519. // $activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
  520. $activities = Activity::with('course')->whereIn('course_id', $section_ids)->orderBy('name')->get();
  521. return View::make('local.managers.shared.print_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'activities'));
  522. }
  523. private function excelConv(&$value, $key)
  524. {
  525. Log::debug('CoursesController@excelConv');
  526. $value = iconv('UTF-8', 'Windows-1252//IGNORE', $value);
  527. }
  528. public function exportGrades($id)
  529. {
  530. // Find course
  531. $course = Course::find($id);
  532. // Set file name and open file
  533. $filename = 'olas_student_grades_' . $course->code . $course->number . $course->section . $course->semester->code . '_' . time() . '.csv';
  534. $file = fopen('exports/' . $filename, 'w');
  535. // To add an empty line to the csv
  536. $empty_line = array(' ');
  537. // Set section name at the top
  538. fputcsv($file, array($course->code . $course->number . $course->section . ' (' . $course->semester->code . ')'));
  539. fputcsv($file, $empty_line);
  540. // Individual activity tables -----------------------------------------
  541. // For each activity
  542. foreach ($course->assessedActivities as $activity) {
  543. // Set name
  544. $activity_name = iconv('UTF-8', 'Windows-1252//IGNORE', $activity->name);
  545. fputcsv($file, array($activity_name));
  546. // Get assessments
  547. // $assessments = DB::table('assessments')
  548. // ->join('students', 'assessments.student_id', '=', 'students.id')
  549. // ->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  550. $assessments = DB::table('assessments')
  551. ->join('students', 'assessments.student_id', '=', 'students.id')
  552. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  553. ->where('activity_criterion.activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
  554. // Get rubric contents
  555. $rubric_contents = json_decode($activity->rubric->contents);
  556. $criterion_list = array('#', 'Name', 'School', 'Major');
  557. // Set criterion names
  558. foreach ($rubric_contents as $criterion) {
  559. $criterion_list[] = $criterion->name;
  560. }
  561. $criterion_list[] = 'Percentage';
  562. $criterion_list[] = 'Comments';
  563. // Change encoding to be compatible with Excel
  564. array_walk($criterion_list, array($this, 'excelConv'));
  565. fputcsv($file, $criterion_list);
  566. // Set student info and scores for each criterion
  567. foreach ($assessments as $assessment) {
  568. $student = Student::find($assessment->student_id);
  569. $scores = json_decode($assessment->scores, true);
  570. fputcsv($file, array_merge(
  571. array($student->number, $student->name, $student->school_code, $student->conc_code),
  572. $scores,
  573. array($assessment->percentage, $assessment->comments)
  574. ));
  575. }
  576. fputcsv($file, $empty_line);
  577. }
  578. // Table with all activities and grades
  579. // Set headers for table
  580. $activity_list = array('#', 'Name', 'School', 'Major');
  581. foreach ($course->assessedActivities as $activity) {
  582. $activity_list[] = $activity->name;
  583. }
  584. $activity_list[] = 'Average';
  585. fputcsv($file, $empty_line);
  586. fputcsv($file, array('All Activities'));
  587. // Change encoding to be compatible with Excel
  588. array_walk($activity_list, array($this, 'excelConv'));
  589. fputcsv($file, $activity_list);
  590. // For each student, set info
  591. foreach ($activity->course->students as $student) {
  592. $student_record = array($student->number, $student->name, $student->school_code, $student->conc_code);
  593. $assessed_activities = $course->assessedActivities;
  594. // Iterate over activities, get percentages and add them to the record array
  595. foreach ($assessed_activities as $activity) {
  596. $percentage = DB::table('assessments')
  597. ->select('percentage')
  598. ->where('student_id', '=', $student->id)
  599. ->where('activity_id', '=', $activity->id)->first();
  600. $student_record[] = $percentage->percentage;
  601. }
  602. // Average
  603. $student_record[] = array_sum(
  604. array_slice(
  605. $student_record,
  606. 4,
  607. count($assessed_activities)
  608. )
  609. ) / count($assessed_activities);
  610. fputcsv($file, $student_record);
  611. }
  612. // Close the file
  613. fclose($file);
  614. // Return response for download
  615. return Response::download('exports/' . $filename);
  616. }
  617. public function reassign()
  618. {
  619. $title = 'Course Management';
  620. $programs = Program::with('school')->orderBy('name', 'asc')->get();
  621. $users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
  622. $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
  623. return View::make('local.managers.admins.reassign', compact('title', 'programs', 'users', 'semesters'));
  624. }
  625. public function update()
  626. {
  627. if (Input::get('reassign_program')) {
  628. $code = strtoupper(trim(str_replace('*', '%', Input::get('code'))));
  629. $number = strtoupper(trim(str_replace('*', '%', Input::get('number'))));
  630. $section = strtoupper(trim(str_replace('*', '%', Input::get('section'))));
  631. if (!$code && !$number && !$section) {
  632. Session::flash('status', 'danger');
  633. Session::flash('message', 'At least one field is required.');
  634. return Redirect::back()->withInput();
  635. }
  636. $update_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  637. $fetch_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
  638. if ($code) {
  639. $update_query->where('code', 'LIKE', $code);
  640. $fetch_query->where('code', 'LIKE', $code);
  641. }
  642. if ($number) {
  643. $update_query->where('number', 'LIKE', $number);
  644. $fetch_query->where('number', 'LIKE', $number);
  645. }
  646. if ($section) {
  647. $update_query->where('section', 'LIKE', $section);
  648. $fetch_query->where('section', 'LIKE', $section);
  649. }
  650. // If section is blank, results can be grouped by code and number
  651. else {
  652. $fetch_query->groupBy(array('code', 'number'));
  653. }
  654. $affected_rows = $fetch_query->get();
  655. // If there are results
  656. if (!$affected_rows->isEmpty()) {
  657. // Try updating and flash success message if successful
  658. if ($update_query->update(array('program_id' => Input::get('program')))) {
  659. Session::flash('courses', json_encode($affected_rows));
  660. Session::flash('status', 'success');
  661. Session::flash('message', 'Courses succesfully updated.');
  662. if ($section) {
  663. Session::flash('show_sections', true);
  664. }
  665. } else {
  666. Session::flash('status', 'danger');
  667. Session::flash('message', 'Error reassigning courses to a program. Try again later.');
  668. }
  669. } else {
  670. Session::flash('status', 'warning');
  671. Session::flash('message', 'No courses matched your criteria. Try to broaden your search.');
  672. }
  673. return Redirect::back()->withInput();
  674. } elseif (Input::get('reassign_professor')) {
  675. $code = strtoupper(trim(str_replace('*', '%', Input::get('code_prof'))));
  676. $number = strtoupper(trim(str_replace('*', '%', Input::get('number_prof'))));
  677. $section = strtoupper(trim(str_replace('*', '%', Input::get('section_prof'))));
  678. $user = Input::get('user_prof');
  679. $semester = Input::get('semester_prof');
  680. if (!$code || !$number || !$section) {
  681. Session::flash('status', 'danger');
  682. Session::flash('message', 'Error assigning professor to a course. All fields are required.');
  683. return Redirect::back()->withInput();
  684. }
  685. $update_query = Course::where('code', '=', $code)
  686. ->where('number', '=', $number)
  687. ->where('section', '=', $section)
  688. ->where('semester_id', '=', $semester);
  689. $fetch_query = Course::where('code', '=', $code)
  690. ->where('number', '=', $number)
  691. ->where('section', '=', $section)
  692. ->orderBy('code', 'asc')
  693. ->orderBy('number', 'asc')
  694. ->orderBy('section', 'asc');
  695. $affected_rows = $fetch_query->get();
  696. // If there are results
  697. if (!$affected_rows->isEmpty()) {
  698. try {
  699. // Try updating and flash success message if successful
  700. $update_query->update(array('user_id' => $user));
  701. Session::flash('status', 'success');
  702. Session::flash('message', 'Successfully changed professor for ' . $code . $number . '-' . $section);
  703. } catch (Exception $e) {
  704. Session::flash('status', 'danger');
  705. Session::flash('message', 'An error occurred when changing professor for ' . $code . $number . '-' . $section) . '.';
  706. }
  707. } else {
  708. Session::flash('status', 'warning');
  709. Session::flash('message', 'No course matches your criteria. Try again with different values.');
  710. }
  711. return Redirect::back()->withInput();
  712. } else {
  713. }
  714. }
  715. }