説明なし

ProfessorController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Professor;
  4. use App\Semester;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Http\Request;
  7. class ProfessorController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index(Request $request)
  15. {
  16. // dd(Professor::where('dept_id', '=', 1)->with(['sections:semester_code,credits,student_count', 'semesters:semester_code,admin_load,investigative_load'])->orderBy('professors.last_name')->get());
  17. $professors = Professor::with(['sections:semester_code,credits,student_count', 'semesters:semester_code,admin_load,investigative_load,other'])->select('professors.id', 'first_name', 'last_name');
  18. if ($request->session()->get('filter') === 'f') {
  19. $professors->join('departments', 'professors.dept_id', '=', 'departments.id')->where('faculty_id', '=', $request->session()->get('faculty'));
  20. } else if ($request->session()->get('filter') === 'd') {
  21. $professors->where('dept_id', '=', $request->session()->get('department'));
  22. }
  23. return view('professors.index', [
  24. 'professors' => $professors->orderBy('professors.last_name')->get(),
  25. 'semesters' => Semester::orderBy('semesters.code', 'asc')->get(),
  26. ]);
  27. }
  28. /**
  29. * Show the form for creating a new resource.
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function create()
  34. {
  35. //
  36. }
  37. /**
  38. * Store a newly created resource in storage.
  39. *
  40. * @param \Illuminate\Http\Request $request
  41. * @return \Illuminate\Http\Response
  42. */
  43. public function store(Request $request)
  44. {
  45. //
  46. $professor = Professor::create( $request->validate([
  47. 'num_prof' => ['required', 'unique:professors', 'max:16', 'string'],
  48. 'first_name' => ['required', 'max:255', 'string', 'alpha'],
  49. 'last_name' => ['required', 'max:255', 'string', 'alpha'],
  50. 'email' => ['nullable', 'email'],
  51. 'type' => ['nullable', 'in:plantilla,contrato,ta'],
  52. 'dept_id' => ['required', 'exists:departments,id', 'integer'],
  53. ]));
  54. // dd($professor);
  55. return redirect('/professor/' . $professor->id);
  56. }
  57. /**
  58. * Display the specified resource.
  59. *
  60. * @param \App\Professor $professor
  61. * @return \Illuminate\Http\Response
  62. */
  63. public function show(Professor $professor)
  64. {
  65. $professor->loadMissing(['sections.course', 'semesters.sections.course', 'sections.schedules']);
  66. $loads = $professor->semesters()->get()->keyBy('code');
  67. $sections = $professor->sections()->with('course:id,code')->get()->groupBy('semester_code');
  68. $semesters = Semester::whereIn('code', $loads->keys())->orWhereIn('code', $sections->keys())->orderBy('code', 'desc')->get();
  69. return view('professors.show', compact([
  70. 'professor',
  71. 'semesters',
  72. 'sections',
  73. 'loads'
  74. ]));
  75. }
  76. /**
  77. * Show the form for editing the specified resource.
  78. *
  79. * @param \App\Professor $professor
  80. * @return \Illuminate\Http\Response
  81. */
  82. public function edit(Professor $professor)
  83. {
  84. //
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @param \App\Professor $professor
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function update(Request $request, Professor $professor)
  94. {
  95. // TODO: Update section percent,schedule and eval
  96. if ($request->isMethod('put')) {
  97. $data = array_filter($request->validate([
  98. 'semester_code' => ['required_with:admin_load,investigative_load,other', 'string','size:3'],
  99. 'admin_load' => ['nullable', 'numeric'],
  100. 'investigative_load' => ['nullable', 'numeric'],
  101. 'other' => ['nullable', 'numeric']
  102. ]));
  103. // dd($data);
  104. if (! $professor->semesters->contains($data['semester_code'])) {
  105. $professor->semesters()->attach($data['semester_code'], $data);
  106. } else {
  107. $professor->semesters()->updateExistingPivot($data['semester_code'], $data);
  108. }
  109. } else if ($request->isMethod('patch')) {
  110. $data = array_filter($request->validate([
  111. 'num_prof' => ['nullable', 'unique:professors', 'max:16', 'string'],
  112. 'first_name' => ['nullable', 'max:255', 'string', 'alpha'],
  113. 'last_name' => ['nullable', 'max:255', 'string', 'alpha'],
  114. 'email' => ['nullable', 'email'],
  115. 'type' => ['nullable', 'in:plantilla,contrato,ta'],
  116. 'dept_id' => ['nullable', 'exists:departments,id', 'integer'],
  117. ]));
  118. $professor->update($data);
  119. }
  120. return redirect()->back();
  121. }
  122. /**
  123. * Remove the specified resource from storage.
  124. *
  125. * @param \App\Professor $professor
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function destroy(Professor $professor)
  129. {
  130. //
  131. }
  132. }