123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
-
- class CoursesController extends \BaseController {
-
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return Response
- */
- public function show($id)
- {
-
- $course = Course::with('semester')->where('id', $id)->first();
- $title=$course->code.$course->number.'-'.$course->section.' <span class="small attention">('.$course->semester->code.')</span>';
-
- // If course does not exist, display 404
- if(!$course)
- App::abort('404');
-
- // If course does not belong to the person
- if ($course->user_id != Auth::id())
- App::abort('403', 'Access forbidden.');
-
- $activities = $course->activities;
- $students = $course->students;
-
- $outcomes = Outcome::orderBy('name', 'asc')->get();
- $outcomes_achieved = json_decode($course->outcomes_achieved, true);
- $outcomes_attempted = json_decode($course->outcomes_attempted, true);
-
- // Get active semesters
- $active_semesters= array();
- $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();
- foreach ($active_semesters_collection as $active_semester)
- {
- $active_semesters[]=$active_semester->id;
- }
-
- return View::make('local.professors.course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'active_semesters'));
- }
-
-
-
- /**
- * Display the specified course, but with limited information.
- *
- * @param int $id
- * @return Response
- */
- public function showLimited($id)
- {
- $course = Course::with('semester')->where('id', $id)->first();
- $students = $course->students;
- $title=$course->code.$course->number.'-'.$course->section.' <span class="small attention">('.$course->semester->code.')</span>';
-
- // If course does not exist, display 404
- if(!$course)
- App::abort('404');
-
- $activities = $course->activities;
- $students = $course->students;
-
- $outcomes = Outcome::orderBy('name', 'asc')->get();
- $outcomes_achieved = json_decode($course->outcomes_achieved, true);
- $outcomes_attempted = json_decode($course->outcomes_attempted, true);
-
- $schools = School::all();
-
- $rubrics = array();
- foreach ($activities as $activity )
- {
- if($activity->rubric_id!=NULL)
- {
- $rubrics[]=$activity->rubric;
- }
- }
-
- return View::make('local.managers.shared.limited-course', compact('title', 'course', 'activities', 'students', 'outcomes', 'outcomes_achieved', 'outcomes_attempted', 'schools', 'rubrics', 'students'));
- }
-
-
- /**
- * Show grouped sections of a course
- */
-
- public function showGrouped($code, $number, $semester_code)
- {
- $title=$code.$number.' ('.$semester_code.')';
- $semester = Semester::where('code', $semester_code)->first();
- $role = Auth::user()->role;
-
- $grouped_courses = Course::
- where('code', $code)
- ->where('number', $number)
- ->where('semester_id', $semester->id)
- ->groupBy(array('code', 'number'))->get();
-
- $outcomes = Outcome::orderBy('name', 'asc')->get();
- $outcomeCount = Outcome::all()->count();
-
- foreach ($grouped_courses as $index => $grouped_course)
- {
- // Blank outcomes for one course
- $outcomes_achieved = array_fill(1, $outcomeCount, 0);
- $outcomes_attempted = array_fill(1, $outcomeCount, 0);
-
- // Find sections for this course
- $sections = Course::
- where('code', $grouped_course->code)
- ->where('number', $grouped_course->number)
- ->where('semester_id', $grouped_course->semester_id)
- ->get();
-
- // For each of the sections, add the attempted and achieved criteria per outcome
- foreach ($sections as $section)
- {
- if($section->outcomes_achieved!=NULL)
- {
- $section_outcomes_achieved =json_decode($section->outcomes_achieved, true);
- $section_outcomes_attempted =json_decode($section->outcomes_attempted, true);
- for($i=1; $i<=count($outcomes_attempted); $i++)
- {
- $outcomes_achieved[$i]+=$section_outcomes_achieved[$i];
- $outcomes_attempted[$i]+=$section_outcomes_attempted[$i];
- }
- }
- }
- }
-
- $section_ids = Course::
- where('code', $code)
- ->where('number', $number)
- ->where('semester_id', $semester->id)
- ->lists('id');
-
- $activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
-
- return View::make('local.managers.shared.grouped_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'transforming_actions', 'activities'));
- }
-
- /**
- * Printable version for a course (grouped sections)
- */
-
- public function print_course($code, $number, $semester_code)
- {
- $title=$code.$number.' ('.$semester_code.')';
- $semester = Semester::where('code', $semester_code)->first();
- $role = Auth::user()->role;
-
- $grouped_courses = Course::
- where('code', $code)
- ->where('number', $number)
- ->where('semester_id', $semester->id)
- ->groupBy(array('code', 'number'))->get();
-
- $outcomes = Outcome::orderBy('name', 'asc')->get();
- $outcomeCount = Outcome::all()->count();
-
- foreach ($grouped_courses as $index => $grouped_course)
- {
- // Blank outcomes for one course
- $outcomes_achieved = array_fill(1, $outcomeCount, 0);
- $outcomes_attempted = array_fill(1, $outcomeCount, 0);
-
- // Find sections for this course
- $sections = Course::
- where('code', $grouped_course->code)
- ->where('number', $grouped_course->number)
- ->where('semester_id', $grouped_course->semester_id)
- ->get();
-
- // For each of the sections, add the attempted and achieved criteria per outcome
- foreach ($sections as $section)
- {
- if($section->outcomes_achieved!=NULL)
- {
- $section_outcomes_achieved =json_decode($section->outcomes_achieved, true);
- $section_outcomes_attempted =json_decode($section->outcomes_attempted, true);
- for($i=1; $i<=count($outcomes_attempted); $i++)
- {
- $outcomes_achieved[$i]+=$section_outcomes_achieved[$i];
- $outcomes_attempted[$i]+=$section_outcomes_attempted[$i];
- }
- }
- }
- }
-
- $section_ids = Course::
- where('code', $code)
- ->where('number', $number)
- ->where('semester_id', $semester->id)
- ->lists('id');
-
- // Activities with transforming actions
- $activities = Activity::with('course')->whereNotNull('transforming_actions')->whereIn('course_id', $section_ids)->orderBy('name')->groupBy('transforming_actions')->get();
-
- return View::make('local.managers.shared.print_course', compact('role', 'title', 'grouped_courses', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'sections', 'activities'));
- }
-
-
- private function excelConv(&$value, $key)
- {
- Log::debug('CoursesController@excelConv');
-
- $value = iconv('UTF-8', 'Windows-1252//IGNORE', $value);
-
- }
-
-
-
-
- public function exportGrades($id)
- {
- // Find course
- $course = Course::find($id);
-
- // Set file name and open file
- $filename = 'olas_student_grades_'.$course->code.$course->number.$course->section.$course->semester->code.'_'.time().'.csv';
- $file= fopen('exports/'.$filename, 'w');
-
- // To add an empty line to the csv
- $empty_line = array(' ');
-
- // Set section name at the top
- fputcsv($file, array($course->code.$course->number.$course->section.' ('.$course->semester->code.')'));
- fputcsv($file, $empty_line);
-
- // Individual activity tables -----------------------------------------
-
- // For each activity
- foreach($course->assessedActivities as $activity) {
- // Set name
-
- $activity_name = iconv('UTF-8', 'Windows-1252//IGNORE', $activity->name);
- fputcsv($file, array($activity_name));
-
- // Get assessments
- $assessments = DB::table('assessments')->join('students', 'assessments.student_id','=','students.id')->where('activity_id', '=', $activity->id)->orderBy('assessments.id', 'asc')->get();
-
- // Get rubric contents
- $rubric_contents = json_decode($activity->rubric->contents);
-
- $criterion_list = array('#', 'Name', 'School', 'Major');
-
- // Set criterion names
- foreach ($rubric_contents as $criterion) {
- $criterion_list[] = $criterion->name;
- }
- $criterion_list[] = 'Percentage';
- $criterion_list[] = 'Comments';
-
- // Change encoding to be compatible with Excel
- array_walk($criterion_list, array($this,'excelConv'));
-
- fputcsv($file, $criterion_list);
-
- // Set student info and scores for each criterion
- foreach ($assessments as $assessment) {
- $student = Student::find($assessment->student_id);
- $scores = json_decode($assessment->scores, true);
- fputcsv($file, array_merge(
- array($student->number, $student->name, $student->school_code, $student->conc_code),
- $scores,
- array($assessment->percentage, $assessment->comments)));
- }
- fputcsv($file, $empty_line);
- }
-
- // Table with all activities and grades
-
- // Set headers for table
- $activity_list = array('#', 'Name', 'School', 'Major');
- foreach($course->assessedActivities as $activity) {
- $activity_list[]=$activity->name;
- }
- $activity_list[] = 'Average';
-
- fputcsv($file, $empty_line);
- fputcsv($file, array('All Activities'));
-
- // Change encoding to be compatible with Excel
- array_walk($activity_list, array($this,'excelConv'));
-
- fputcsv($file, $activity_list);
-
- // For each student, set info
- foreach ($activity->course->students as $student) {
- $student_record= array($student->number, $student->name, $student->school_code, $student->conc_code);
-
- $assessed_activities = $course->assessedActivities;
- // Iterate over activities, get percentages and add them to the record array
- foreach ($assessed_activities as $activity) {
- $percentage = DB::table('assessments')
- ->select('percentage')
- ->where('student_id', '=', $student->id)
- ->where('activity_id', '=', $activity->id)->first();
-
- $student_record[] = $percentage->percentage;
- }
-
- // Average
- $student_record[] = array_sum(
- array_slice(
- $student_record,
- 4,
- count($assessed_activities)
- )
- )/count($assessed_activities);
-
- fputcsv($file, $student_record);
- }
-
- // Close the file
- fclose($file);
-
- // Return response for download
- return Response::download('exports/'.$filename);
- }
-
- public function reassign()
- {
- $title = 'Course Management';
- $programs = Program::with('school')->orderBy('name', 'asc')->get();
- $users = User::select(array('id', 'first_name', 'surnames'))->orderBy('surnames', 'asc')->orderBy('first_name', 'asc')->get();
- $semesters = Semester::where('is_visible', '1')->orderBy('start', 'asc')->get();
- return View::make('local.managers.admins.reassign', compact('title', 'programs', 'users', 'semesters'));
- }
-
- public function update()
- {
- if(Input::get('reassign_program'))
- {
- $code = strtoupper(trim(str_replace('*', '%', Input::get('code'))));
- $number = strtoupper(trim(str_replace('*', '%', Input::get('number'))));
- $section = strtoupper(trim(str_replace('*', '%', Input::get('section'))));
-
- if(!$code && !$number && !$section)
- {
- Session::flash('status','danger');
- Session::flash('message','At least one field is required.');
-
- return Redirect::back()->withInput();
- }
-
- $update_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
- $fetch_query = Course::orderBy('code', 'asc')->orderBy('number', 'asc')->orderBy('section', 'asc');
-
- if($code)
- {
- $update_query->where('code', 'LIKE', $code);
- $fetch_query->where('code', 'LIKE', $code);
- }
-
- if($number)
- {
- $update_query->where('number', 'LIKE', $number);
- $fetch_query->where('number', 'LIKE', $number);
- }
-
- if($section)
- {
- $update_query->where('section', 'LIKE', $section);
- $fetch_query->where('section', 'LIKE', $section);
- }
- // If section is blank, results can be grouped by code and number
- else
- {
- $fetch_query->groupBy(array('code', 'number'));
- }
-
- $affected_rows = $fetch_query->get();
-
- // If there are results
- if(!$affected_rows->isEmpty())
- {
- // Try updating and flash success message if successful
- if($update_query->update(array('program_id'=>Input::get('program'))))
- {
- Session::flash('courses', json_encode($affected_rows));
- Session::flash('status', 'success');
- Session::flash('message', 'Courses succesfully updated.');
-
- if($section)
- {
- Session::flash('show_sections', true);
- }
-
- }
- else
- {
- Session::flash('status', 'danger');
- Session::flash('message', 'Error reassigning courses to a program. Try again later.');
- }
- }
- else
- {
- Session::flash('status', 'warning');
- Session::flash('message', 'No courses matched your criteria. Try to broaden your search.');
- }
-
- return Redirect::back()->withInput();
- }
- elseif(Input::get('reassign_professor'))
- {
-
- $code = strtoupper(trim(str_replace('*', '%', Input::get('code_prof'))));
- $number = strtoupper(trim(str_replace('*', '%', Input::get('number_prof'))));
- $section = strtoupper(trim(str_replace('*', '%', Input::get('section_prof'))));
- $user = Input::get('user_prof');
- $semester = Input::get('semester_prof');
-
-
- if(!$code || !$number || !$section)
- {
- Session::flash('status','danger');
- Session::flash('message','Error assigning professor to a course. All fields are required.');
-
- return Redirect::back()->withInput();
- }
-
- $update_query = Course::
- where('code', '=', $code)
- ->where('number', '=', $number)
- ->where('section', '=', $section)
- ->where('semester_id', '=', $semester);
- $fetch_query = Course::
- where('code', '=', $code)
- ->where('number', '=', $number)
- ->where('section', '=', $section)
- ->orderBy('code', 'asc')
- ->orderBy('number', 'asc')
- ->orderBy('section', 'asc');
-
- $affected_rows = $fetch_query->get();
-
- // If there are results
- if(!$affected_rows->isEmpty())
- {
- try
- {
- // Try updating and flash success message if successful
- $update_query->update(array('user_id'=>$user));
-
- Session::flash('status', 'success');
- Session::flash('message', 'Successfully changed professor for '.$code.$number.'-'.$section);
- }
- catch(Exception $e)
- {
- Session::flash('status', 'danger');
- Session::flash('message', 'An error occurred when changing professor for '.$code.$number.'-'.$section).'.';
- }
- }
- else
- {
- Session::flash('status', 'warning');
- Session::flash('message', 'No course matches your criteria. Try again with different values.');
- }
-
- return Redirect::back()->withInput();
- }
- else
- {
-
- }
- }
-
-
- }
|