123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Professor;
- use App\Section;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
-
- class SectionController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- }
-
- /**
- * 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)
- {
- $data = $request->validate([
- 'course_id' => ['required', 'integer', 'exists:courses,id'],
- 'semester_code' => ['required', 'size:3', 'exists:semesters,code'],
- 'section_count' => ['required', 'integer', 'max:50'],
- ]);
-
- // TODO: Check if section code is taken
- for ($i=1; $i <= $request['section_count']; $i++) {
- Section::create([
- 'course_id' => $request->course_id,
- 'semester_code' => $request->semester_code,
- 'code' => str_pad($i, 3, '0', STR_PAD_LEFT)
- ]);
- }
- return redirect()->back();
- }
-
- /**
- * Display the specified resource.
- *
- * @param \App\Section $section
- * @return \Illuminate\Http\Response
- */
- public function show(Section $section)
- {
- //
- }
-
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Section $section
- * @return \Illuminate\Http\Response
- */
- public function edit(Section $section)
- {
- //
- }
-
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Section $section
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, Section $section)
- {
- // dd($request);
- $data = array_filter($request->validate([
- 'code' => ['nullable', 'size:3', 'string'],
- 'professor_id' => ['nullable', 'array'],
- 'professor_id.*' => ['exists:professors,id'],
- 'credits' => ['nullable', 'numeric'],
- 'student_count' => ['nullable', 'integer'],
- ]));
- $section->update($data);
-
- // TODO: Insert syllabus
- if ($request->file('syllabus')) {
- $path = $request->file('syllabus')->store('section_syllabus');
- $section->update(['syllabus' => '/storage/'.$path]);
- }
-
- if (isset($data['professor_id'])) {
- $section->professors()->detach();
- foreach($data['professor_id'] as $professor_id) {
- $section->professors()->attach($professor_id);
- }
- }
- return redirect()->back();
- }
-
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Section $section
- * @return \Illuminate\Http\Response
- */
- public function destroy(Section $section)
- {
- //
- }
- }
|