123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Course;
- use App\Department;
- use App\Professor;
- use Illuminate\Http\Request;
- use App\Semester;
- use App\User;
- use Exception;
- use phpDocumentor\Reflection\Types\Object_;
-
- class DashboardController extends Controller
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->middleware('auth');
- }
-
- /**
- * Show the application dashboard.
- *
- * @return \Illuminate\Contracts\Support\Renderable
- */
- public function index()
- {
- return view('dashboard.index', [
- '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)->with([
- 'sections' => function ($query) use ($semester) {
- $query->where('semester_code', '=', $semester->code);
- },
- 'sections.schedules',
- 'sections.professors',
- ])->distinct()->get()->sort('cmpCourseCode')->values();
- // dump(json_encode($data, JSON_PRETTY_PRINT));
- return '<script type="text/javascript">var data='.json_encode($data, JSON_UNESCAPED_UNICODE).';</script><script type="text/javascript" src="/js/createPDF.js"></script>';
- }
-
- public function export($semester_code) {
- $semester = Semester::findOrFail($semester_code);
- $professors = Professor::with([
- 'sections' => function ($query) use ($semester_code) {
- $query->where('semester_code', '=', $semester_code);
- },
- 'semesters:code,admin_load,investigative_load,other'
- ])->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 INVESTIGATIVA', 'OTRAS CARGAS', '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,
- $prof_loads->other ?? 0,
- $professor->academic_load + ($prof_loads->admin_load ?? 0) + ($prof_loads->investigative_load ?? 0) + ($prof_loads->other ?? 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|regex:/^[a-zA-Z][0-9][123]$/',
- 'new_alpha' => 'nullable'
- ]);
- try {
- $semester->clone(strtoupper($data['new_semester']), strtoupper($data['new_alpha']), $request->session()->get('department'));
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- return redirect()->back();
- }
-
- /**
- */
- public function addUser(Request $request) {
- return view('dashboard.user');
- }
- }
|