123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Course;
- use App\Permission;
- use App\Semester;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Support\Facades\Redirect;
- use Illuminate\Support\Facades\Storage;
-
- class CourseController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
-
- $courses = Course::with('sections:id,course_id,semester_code')->select('courses.id', 'courses.code', 'courses.title', 'courses.syllabus');
- // Filter courses by faculty or id
- if ($request->session()->get('filter') === 'f') {
- $courses->join('departments', 'courses.dept_id', '=', 'departments.id')->where('faculty_id', '=', $request->session()->get('faculty'));
- } else if ($request->session()->get('filter') === 'd') {
- $courses->where('dept_id', '=', $request->session()->get('department'));
- }
- // dd($courses->toSql());
- // dd($courses->get());
- return view('courses.index', [
- 'courses' => $courses->get()->sort('cmpCourseCode'),
- 'semesters' => Semester::orderBy('code', 'asc')->get(),
- ]);
- }
-
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- }
-
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- // TODO: Validate syllabus is pdf
- $course = Course::create( $request->validate([
- 'code' => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
- 'title' => ['required','max:255', 'string'],
- 'dept_id' => ['required','exists:departments,id', 'integer'],
- ]));
-
- if ($request->file('syllabus')) {
- $path = $request->file('syllabus')->store('course_syllabus');
- $course->update(['syllabus' => '/storage/'.$path]);
- }
-
- return redirect('/course/' . $course->id);
- }
-
- /**
- * Display the specified resource.
- *
- * @param \App\Course $course
- * @return \Illuminate\Http\Response
- */
- public function show(Course $course)
- {
- $course->loadMissing(['semesters.sections.professors']);
- return view('courses.show', [
- 'course' => $course,
- ]);
- }
-
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Course $course
- * @return \Illuminate\Http\Response
- */
- public function edit(Course $course)
- {
- //
- }
-
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Course $course
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, Course $course)
- {
- $data = array_filter($request->validate([
- 'code' => ['nullable', 'unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
- 'title' => ['nullable', 'max:255', 'string'],
- 'dept_id' => ['nullable', 'exists:departments,id', 'integer'],
- ]));
-
- $course->update($data);
-
- if ($request->file('syllabus')) {
- $path = $request->file('syllabus')->store('course_syllabus');
- $course->update(['syllabus' => '/storage/'.$path]);
- }
- // dd($path);
- // dd(asset($path));
- // dd($course->syllabus);
- // return redirect($course->syllabus);
- return redirect()->back();
- }
-
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Course $course
- * @return \Illuminate\Http\Response
- */
- public function destroy(Course $course)
- {
- //
- }
- }
|