Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Course;
  4. use App\Department;
  5. use App\Professor;
  6. use Illuminate\Http\Request;
  7. use App\Semester;
  8. use App\User;
  9. use Exception;
  10. use phpDocumentor\Reflection\Types\Object_;
  11. class DashboardController extends Controller
  12. {
  13. /**
  14. * Create a new controller instance.
  15. *
  16. * @return void
  17. */
  18. public function __construct()
  19. {
  20. $this->middleware('auth');
  21. }
  22. /**
  23. * Show the application dashboard.
  24. *
  25. * @return \Illuminate\Contracts\Support\Renderable
  26. */
  27. public function index()
  28. {
  29. return view('dashboard.index', [
  30. 'professors' => Professor::orderBy('last_name')->get(),
  31. 'semesters' => Semester::orderBy('code')->get()
  32. ]);
  33. }
  34. /**
  35. * Download the schedule for the given semester.
  36. *
  37. * @param string $semester
  38. * @return View
  39. */
  40. public function schedule(Request $request, $semester_code) {
  41. $semester = Semester::findOrFail($semester_code);
  42. $data = (object)['alpha' => $semester->alpha];
  43. $data->dept = Department::find($request->session()->get('department'));
  44. $data->courses = $semester->courses()->where('dept_id', '=', $data->dept->id)->with([
  45. 'sections' => function ($query) use ($semester) {
  46. $query->where('semester_code', '=', $semester->code);
  47. },
  48. 'sections.schedules',
  49. 'sections.professors',
  50. ])->distinct()->get()->sort('cmpCourseCode')->values();
  51. // dump(json_encode($data, JSON_PRETTY_PRINT));
  52. return '<script type="text/javascript">var data='.json_encode($data, JSON_UNESCAPED_UNICODE).';</script><script type="text/javascript" src="/js/createPDF.js"></script>';
  53. }
  54. public function export($semester_code) {
  55. $semester = Semester::findOrFail($semester_code);
  56. $professors = Professor::with([
  57. 'sections' => function ($query) use ($semester_code) {
  58. $query->where('semester_code', '=', $semester_code);
  59. },
  60. 'semesters:code,admin_load,investigative_load,other'
  61. ])->whereIn('id', function($query) use ($semester_code) {
  62. $query->select('professor_id')
  63. ->from('professor_semester')
  64. ->where('semester_code', '=', $semester_code);
  65. })->orWhereIn('id', function($query) use ($semester_code) {
  66. $query->select('professor_id')
  67. ->from('sections')
  68. ->join('professor_section', 'sections.id', '=', 'professor_section.section_id')
  69. ->where('semester_code', '=', $semester_code);
  70. })->orderBy('last_name')->get();
  71. header('Content-Type: application/csv');
  72. header(`Content-Disposition: attachment; filename="${semester_code}.csv";`);
  73. if ($file = fopen('php://output', 'w+')) {
  74. fputcsv($file, ['PROFESOR', 'CARGA ACADEMICA', 'CARGA ADMINISTRATIVA', 'CARGA INVESTIGATIVA', 'OTRAS CARGAS', 'CARGA TOTAL']);
  75. foreach ($professors as $professor) {
  76. $professor->academic_load = $professor->getAcademicLoad($semester);
  77. $prof_loads = $professor->semesters->find($semester);
  78. fputcsv($file, [
  79. $professor->last_name . ', ' . $professor->first_name,
  80. $professor->academic_load,
  81. $prof_loads->admin_load ?? 0,
  82. $prof_loads->investigative_load ?? 0,
  83. $prof_loads->other ?? 0,
  84. $professor->academic_load + ($prof_loads->admin_load ?? 0) + ($prof_loads->investigative_load ?? 0) + ($prof_loads->other ?? 0),
  85. ]);
  86. }
  87. fclose($file);
  88. die();
  89. }
  90. }
  91. public function exportCourses(Request $request) {
  92. $courses = Course::where('dept_id', '=', $request->session()->get('department'))->with('sections:id,course_id,semester_code')->get()->sort('cmpCourseCode');
  93. $semesters = Semester::orderBy('code', 'asc')->get();
  94. header('Content-Type: application/csv');
  95. header(`Content-Disposition: attachment; filename="courses.csv";`);
  96. if ($file = fopen('php://output', 'w+')) {
  97. fputcsv($file, array_merge(['CURSO'], $semesters->pluck('alpha')->toArray()));
  98. foreach ($courses as $course) {
  99. fputcsv($file,
  100. array_merge([$course->code], $semesters->map(function ($semester) use ($course) {
  101. return $course->getSemesterSectionCount($semester->code);
  102. })->toArray())
  103. );
  104. }
  105. fclose($file);
  106. die();
  107. }
  108. }
  109. public function cloneSemester(Request $request, $semester_code) {
  110. $semester = Semester::findOrFail($semester_code);
  111. // dd($semester->sections);
  112. $data = $request->validate([
  113. 'new_semester' => 'required|regex:/^[a-zA-Z][0-9][123]$/',
  114. 'new_alpha' => 'nullable'
  115. ]);
  116. try {
  117. $semester->clone(strtoupper($data['new_semester']), strtoupper($data['new_alpha']), $request->session()->get('department'));
  118. } catch (Exception $e) {
  119. echo $e->getMessage();
  120. }
  121. return redirect()->back();
  122. }
  123. /**
  124. */
  125. public function addUser(Request $request) {
  126. return view('dashboard.user');
  127. }
  128. }