123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Course;
- use App\Semester;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- 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()
- {
- return view('courses.index', [
- 'courses' => Course::where('dept_id', '=', 1)->with('sections:id,course_id,semester_code')->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)
- {
- // dd($request->validate(['code' => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string']]));
- $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'],
- 'syllabus' => ['nullable','file', 'mimes:pdf']
- ]));
-
- if ($request->file('syllabus')) {
- $path = $request->file('syllabus')->store('course_syllabus', 'public');
- $course->update(['syllabus' => asset($path)]);
- }
-
- return redirect('/course/' . $course->id);
- }
-
- /**
- * Display the specified resource.
- *
- * @param \App\Course $course
- * @return \Illuminate\Http\Response
- */
- public function show(Course $course)
- {
- // dd($course->toJson());
- // dump(1);
- // Auth::logout();
- // $semesters = $course->semesters->unique()->loadMissing('sections');
- // $course->loadMissing('semesters.sections');
- // dd($semesters);
- $course->loadMissing(['semesters.sections.professors']);
- // dd($course->semesters->unique());
- // $semesters = $course->sections->groupBy('semester_code')->map( function ($sections) {
- // return collect(['alpha' => $sections[0]->semester->alpha, 'sections' => $sections->sortBy('code')]);
- // })->sort();
- // dd($semesters);
- // dd($course->loadMissing(['sections.semester']));
- // dd($course->sections->groupBy('semester_code'));
- return view('courses.show', [
- 'course' => $course,
- // 'semesters' => $semesters
- ]);
- }
-
- /**
- * 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)
- {
-
- $course->update( $request->validate([
- 'code' => ['unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
- 'title' => ['max:255', 'string'],
- 'dept_id' => ['exists:departments,id', 'integer'],
- 'syllabus' => ['nullable', 'file', 'mimetypes:application/pdf'],
- ]));
-
- if ($request->file('syllabus')) {
- $path = $request->file('syllabus')->store('course_syllabus');
- $course->update(['syllabus' => asset($path)]);
- }
- dump(asset($path));
- // dd($course->syllabus);
- return redirect($course->syllabus);
- return redirect('/course');
- }
-
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Course $course
- * @return \Illuminate\Http\Response
- */
- public function destroy(Course $course)
- {
- //
- }
- }
|