Ei kuvausta

DashboardController.php 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Course;
  4. use App\Professor;
  5. use Illuminate\Http\Request;
  6. use App\Semester;
  7. class DashboardController extends Controller
  8. {
  9. /**
  10. * Create a new controller instance.
  11. *
  12. * @return void
  13. */
  14. public function __construct()
  15. {
  16. $this->middleware('auth');
  17. }
  18. /**
  19. * Show the application dashboard.
  20. *
  21. * @return \Illuminate\Contracts\Support\Renderable
  22. */
  23. public function index()
  24. {
  25. return view('dashboard', [
  26. 'professors' => Professor::orderBy('last_name')->get(),
  27. 'semesters' => Semester::orderBy('code')->get()
  28. ]);
  29. }
  30. /**
  31. * Download the schedule for the given semester.
  32. *
  33. * @param string $semester
  34. * @return View
  35. */
  36. public function schedule($semester_code) {
  37. $data = Semester::findOrFail($semester_code);
  38. $data->courses = $data->courses->sort('cmpCourseCode');
  39. foreach($data->courses as $course) {
  40. $course->sections = $course->sections->where('semester_code', '=', $semester_code);
  41. foreach($course->sections as $section) {
  42. $section->professors = $section->professors;
  43. }
  44. }
  45. // header('Content-Type: application/pdf');
  46. // header(`Content-Disposition: attachment; filename="${semester_code}.pdf";`);
  47. // return $data->toJson();
  48. return '<script type="text/javascript">var data='.$data->toJson().'</script><script type="text/javascript" src="/js/createPDF.js"></script>';
  49. }
  50. public function export($semester_code) {
  51. $semester = Semester::findOrFail($semester_code);
  52. $professors = Professor::with(['sections', 'semesters:code,admin_load,investigative_load'])->whereIn('id', function($query) use ($semester_code) {
  53. $query->select('professor_id')
  54. ->from('professor_semester')
  55. ->where('semester_code', '=', $semester_code);
  56. })->orWhereIn('id', function($query) use ($semester_code) {
  57. $query->select('professor_id')
  58. ->from('sections')
  59. ->join('professor_section', 'sections.id', '=', 'professor_section.section_id')
  60. ->where('semester_code', '=', $semester_code);
  61. })->orderBy('last_name')->get();
  62. header('Content-Type: application/csv');
  63. header(`Content-Disposition: attachment; filename="${semester_code}.csv";`);
  64. if ($file = fopen('php://output', 'w+')) {
  65. fputcsv($file, ['PROFESOR', 'CARGA ACADEMICA', 'CARGA ADMINISTRATIVA', 'CARGA INVESTIGATIVE', 'CARGA TOTAL']);
  66. foreach ($professors as $professor) {
  67. $professor->academic_load = $professor->getAcademicLoad($semester);
  68. $prof_loads = $professor->semesters->find($semester);
  69. fputcsv($file, [
  70. $professor->last_name . ', ' . $professor->first_name,
  71. $professor->academic_load,
  72. $prof_loads->admin_load ?? 0,
  73. $prof_loads->investigative_load ?? 0,
  74. $professor->academic_load + ($prof_loads->admin_load ?? 0) + ($prof_loads->investigative_load ?? 0),
  75. ]);
  76. }
  77. fclose($file);
  78. die();
  79. }
  80. }
  81. }