middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('dashboard', [
'professors' => Professor::orderBy('last_name')->get(),
'semesters' => Semester::orderBy('code')->get()
]);
}
/**
* Download the schedule for the given semester.
*
* @param string $semester
* @return View
*/
public function schedule(Request $request, $semester_code) {
$semester = Semester::findOrFail($semester_code);
$data = (object)['alpha' => $semester->alpha];
$data->dept = Department::find($request->session()->get('department'));
$data->courses = $semester->courses()->where('dept_id', '=', $data->dept->id)->distinct()->get();
// dd($data);
// $data->loadMissing(['courses.sections' => function ($query) use ($request, $semester_code) {
// $query->where('semester_code', '=', $semester_code);
// }, 'courses.sections.professors:id,first_name,last_name', 'courses.sections.schedules']);
// return $data->toJson();
// $data->courses = $data->courses->sort('cmpCourseCode');
// dump($data->courses[0]->sections);
// foreach($data->courses as $course) {
// $course->sections = $course->sections;
// foreach($course->sections as $section) {
// $section->professors = $section->professors;
// }
// }
// dd($data);
dd(json_encode($data, JSON_PRETTY_PRINT));
// header('Content-Type: application/pdf');
// header(`Content-Disposition: attachment; filename="${semester_code}.pdf";`);
// return $data->toJson();
return '';
}
public function export($semester_code) {
$semester = Semester::findOrFail($semester_code);
$professors = Professor::with(['sections', 'semesters:code,admin_load,investigative_load'])->whereIn('id', function($query) use ($semester_code) {
$query->select('professor_id')
->from('professor_semester')
->where('semester_code', '=', $semester_code);
})->orWhereIn('id', function($query) use ($semester_code) {
$query->select('professor_id')
->from('sections')
->join('professor_section', 'sections.id', '=', 'professor_section.section_id')
->where('semester_code', '=', $semester_code);
})->orderBy('last_name')->get();
header('Content-Type: application/csv');
header(`Content-Disposition: attachment; filename="${semester_code}.csv";`);
if ($file = fopen('php://output', 'w+')) {
fputcsv($file, ['PROFESOR', 'CARGA ACADEMICA', 'CARGA ADMINISTRATIVA', 'CARGA INVESTIGATIVE', 'CARGA TOTAL']);
foreach ($professors as $professor) {
$professor->academic_load = $professor->getAcademicLoad($semester);
$prof_loads = $professor->semesters->find($semester);
fputcsv($file, [
$professor->last_name . ', ' . $professor->first_name,
$professor->academic_load,
$prof_loads->admin_load ?? 0,
$prof_loads->investigative_load ?? 0,
$professor->academic_load + ($prof_loads->admin_load ?? 0) + ($prof_loads->investigative_load ?? 0),
]);
}
fclose($file);
die();
}
}
public function exportCourses(Request $request) {
$courses = Course::where('dept_id', '=', $request->session()->get('department'))->with('sections:id,course_id,semester_code')->get()->sort('cmpCourseCode');
$semesters = Semester::orderBy('code', 'asc')->get();
header('Content-Type: application/csv');
header(`Content-Disposition: attachment; filename="courses.csv";`);
if ($file = fopen('php://output', 'w+')) {
fputcsv($file, array_merge(['CURSO'], $semesters->pluck('alpha')->toArray()));
foreach ($courses as $course) {
fputcsv($file,
array_merge([$course->code], $semesters->map(function ($semester) use ($course) {
return $course->getSemesterSectionCount($semester->code);
})->toArray())
);
}
fclose($file);
die();
}
}
public function cloneSemester(Request $request, $semester_code) {
$semester = Semester::findOrFail($semester_code);
// dd($semester->sections);
$data = $request->validate([
'new_semester' => 'required|unique:semesters,code|regex:/^[a-zA-Z][0-9][123]$/',
'new_alpha' => 'required|unique:semesters,alpha'
]);
try {
$semester->clone(strtoupper($data['new_semester']), strtoupper($data['new_alpha']));
} catch (Exception $e) {
echo $e->getMessage();
}
return redirect()->back();
}
/**
* TODO: Ability to add non-google users
*/
public function addUser(Request $request) {
$data = $request->validate(['email' => ['required', 'email']]);
User::create($data);
return redirect()->back();
}
}