Geen omschrijving

CourseController.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Course;
  4. use App\Permission;
  5. use App\Semester;
  6. use App\User;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Gate;
  11. use Illuminate\Support\Facades\Redirect;
  12. use Illuminate\Support\Facades\Storage;
  13. class CourseController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index(Request $request)
  21. {
  22. $courses = Course::with('sections:id,course_id,semester_code')->select('courses.id', 'courses.code', 'courses.title', 'courses.syllabus');
  23. // Filter courses by faculty or id
  24. if ($request->session()->get('filter') === 'f') {
  25. $courses->join('departments', 'courses.dept_id', '=', 'departments.id')->where('faculty_id', '=', $request->session()->get('faculty'));
  26. } else if ($request->session()->get('filter') === 'd') {
  27. $courses->where('dept_id', '=', $request->session()->get('department'));
  28. }
  29. // dd($courses->toSql());
  30. // dd($courses->get());
  31. return view('courses.index', [
  32. 'courses' => $courses->get()->sort('cmpCourseCode'),
  33. 'semesters' => Semester::orderBy('code', 'asc')->get(),
  34. ]);
  35. }
  36. /**
  37. * Show the form for creating a new resource.
  38. *
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function create()
  42. {
  43. }
  44. /**
  45. * Store a newly created resource in storage.
  46. *
  47. * @param \Illuminate\Http\Request $request
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function store(Request $request)
  51. {
  52. // TODO: Validate syllabus is pdf
  53. $course = Course::create( $request->validate([
  54. 'code' => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
  55. 'title' => ['required','max:255', 'string'],
  56. 'dept_id' => ['required','exists:departments,id', 'integer'],
  57. ]));
  58. if ($request->file('syllabus')) {
  59. $path = $request->file('syllabus')->store('course_syllabus');
  60. $course->update(['syllabus' => '/storage/'.$path]);
  61. }
  62. return redirect('/course/' . $course->id);
  63. }
  64. /**
  65. * Display the specified resource.
  66. *
  67. * @param \App\Course $course
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function show(Course $course)
  71. {
  72. $course->loadMissing(['semesters.sections.professors']);
  73. return view('courses.show', [
  74. 'course' => $course,
  75. ]);
  76. }
  77. /**
  78. * Show the form for editing the specified resource.
  79. *
  80. * @param \App\Course $course
  81. * @return \Illuminate\Http\Response
  82. */
  83. public function edit(Course $course)
  84. {
  85. //
  86. }
  87. /**
  88. * Update the specified resource in storage.
  89. *
  90. * @param \Illuminate\Http\Request $request
  91. * @param \App\Course $course
  92. * @return \Illuminate\Http\Response
  93. */
  94. public function update(Request $request, Course $course)
  95. {
  96. $data = array_filter($request->validate([
  97. 'code' => ['nullable', 'unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
  98. 'title' => ['nullable', 'max:255', 'string'],
  99. 'dept_id' => ['nullable', 'exists:departments,id', 'integer'],
  100. ]));
  101. $course->update($data);
  102. if ($request->file('syllabus')) {
  103. $path = $request->file('syllabus')->store('course_syllabus');
  104. $course->update(['syllabus' => '/storage/'.$path]);
  105. }
  106. // dd($path);
  107. // dd(asset($path));
  108. // dd($course->syllabus);
  109. // return redirect($course->syllabus);
  110. return redirect()->back();
  111. }
  112. /**
  113. * Remove the specified resource from storage.
  114. *
  115. * @param \App\Course $course
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function destroy(Course $course)
  119. {
  120. //
  121. }
  122. }