暂无描述

CoursesController.php 35KB

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