public function submitResponse(Request $request) { $validator = Validator::make($request->all(), ['courseCode' => 'required', 'credits' => 'required|numeric', 'courseSlot' => 'required']); $user = \Auth::user(); $existingCredits = 0; $slotsToBeAddedtemp = str_replace("+", "-", $request->input('courseSlot')); $slotsToBeAddedtemp = explode("-", $slotsToBeAddedtemp); if (count($slotsToBeAddedtemp) == 2) { $slotsToBeAdded = TimeTableSlot::where('name', $slotsToBeAddedtemp[0])->orWhere('name', $slotsToBeAddedtemp[1])->get(); } else { $slotsToBeAdded = TimeTableSlot::where('name', $slotsToBeAddedtemp[0])->get(); } $existingSlots = $user->usertimeslots; $this->isClashing = 0; $this->creditsMore = 0; //All the time slots which the user currently has $existingSlotsTiming = collect(); foreach ($existingSlots as $onecoursetimeslots) { $existingCredits += $onecoursetimeslots->credits; foreach ($onecoursetimeslots->timeslots as $onecoursetimeslot) { $temp = array('name' => $onecoursetimeslot->name, 'start' => $onecoursetimeslot->start, 'end' => $onecoursetimeslot->end, 'day' => $onecoursetimeslot->day); $existingSlotsTiming->push($temp); } } foreach ($slotsToBeAdded as $oneTimeSlotWhichWouldBeAdded) { foreach ($existingSlotsTiming as $existingSlotTiming) { if ($oneTimeSlotWhichWouldBeAdded->day == $existingSlotTiming['day']) { if ($oneTimeSlotWhichWouldBeAdded->start >= $existingSlotTiming['start'] and $oneTimeSlotWhichWouldBeAdded->end <= $existingSlotTiming['end']) { $this->isClashing = 1; $this->clashesWith = $existingSlotTiming['name']; break; } } } } if ($existingCredits + $request->input('credits') > 27) { $this->creditsMore = 1; } $validator->after(function ($validator) { if ($this->isClashing) { $validator->errors()->add('courseSlot', 'Time slot clashes with ' . $this->clashesWith); } if ($this->creditsMore) { $validator->errors()->add('credits', 'Total Credits exceed 27'); } }); if ($validator->fails()) { return redirect('/home')->withErrors($validator)->withInput(); } $credits = $request->input('credits'); foreach ($slotsToBeAddedtemp as $value) { $userTimeSlot = new userTimeSlot(); $userTimeSlot->userid = $user->id; $userTimeSlot->credits = $credits; $credits = 0; $userTimeSlot->slotid = $value; $userTimeSlot->courseCode = $request->input('courseCode'); $userTimeSlot->save(); } return redirect('home'); }
public function delete(Request $request) { $slotid = $request->input('slotid'); $userId = \Auth::id(); $slotsToBeAddedtemp = str_replace("+", "-", $request->input('slotid')); $slotsToBeAddedtemp = explode("-", $slotsToBeAddedtemp); if (count($slotsToBeAddedtemp) == 2) { $model = userTimeSlot::where('userid', $userId)->where('slotid', $slotsToBeAddedtemp[0])->delete(); $model = userTimeSlot::where('userid', $userId)->where('slotid', $slotsToBeAddedtemp[1])->delete(); } else { $model = userTimeSlot::where('userid', $userId)->where('slotid', $slotsToBeAddedtemp[0])->delete(); } return redirect('home'); }