/** * 保存所选课程 * @author FuRongxin * @date 2016-06-29 * @version 2.1.1 * @param \Illuminate\Http\Request $request 保存请求 * @return \Illuminate\Http\Response 选课列表 */ public function store(Request $request) { if ('pubsport' == $request->input('type')) { if (config('constants.status.disable') == Setting::find('XK_GT')->value) { abort(403, '现在未开放公体选课,不允许公体选课'); } } elseif (config('constants.status.disable') == Setting::find('XK_KG')->value) { abort(403, '现在未开放选课,不允许选课'); } if (Unpaid::whereXh(Auth::user()->xh)->exists()) { abort(403, '请交清费用再进行选课'); } if ('pubsport' != $request->input('type')) { if (config('constants.status.enable') == Setting::find('XK_SJXZ')->value) { $profile = Profile::whereXh(Auth::user()->xh)->select('nj', 'xz')->firstOrFail(); // 未在时间限制表中配置,默认不允许选课 $now = Carbon::now(); $exists = Lmttime::whereNj($profile->nj)->whereXz($profile->xz)->where('kssj', '<', $now)->where('jssj', '>', $now)->exists(); if (!$exists) { abort(403, '现在未到选课时间,不允许选课'); } } } if ($request->isMethod('post')) { $this->validate($request, ['kcxh' => 'required|alpha_num|size:12']); $inputs = $request->all(); if (in_array($inputs['type'], array_keys(config('constants.course.general')))) { if (config('constants.stauts.disable') == Setting::find('XK_TS')->value) { abort(403, '现在未开放通识素质课选课,不允许选课'); } if (config('constants.status.enable') == Setting::find('XK_TSXZ')->value) { $profile = Profile::whereXh(Auth::user()->xh)->select('nj', 'xz')->firstOrFail(); // 未在时间限制表中配置,默认不允许选通识素质课 $now = Carbon::now(); $limit = Lmtgeneral::whereNj($profile->nj)->whereXz($profile->xz)->where('kssj', '<', $now)->where('jssj', '>', $now)->orderBy('kssj', 'desc')->first(); if (!$limit) { abort(403, '现在未到通识素质课选课时间,不允许选课'); } $limit_course = $limit->ms; $limit_ratio = 0 < $limit->bl ? $limit->bl / 100 : $limit->bl; } } else { $limit_ratio = 1; } $course = Mjcourse::ofType($inputs['type'])->whereNd(session('year'))->whereXq(session('term'))->whereZsjj(session('season'))->whereKcxh($inputs['kcxh'])->firstOrFail(); $ms = isset($limit_course) ? $limit_course : -1; $rs = isset($limit_ratio) ? $limit_ratio * $course->rs : -1; $limits = $this->checkcourse($inputs['type'], $course->kcxh, $course->zy, $ms, $rs); if ($limits['ms']) { $request->session()->flash('forbidden', '通识素质课选课门数已达上限' . $ms . '门,请选其他课程'); return back()->withInput(); } if ($limits['rs']) { $request->session()->flash('forbidden', '选课人数已满,请选其他课程'); return back()->withInput(); } if (Prior::failed(Helper::getCno($course->kcxh), Auth::user())->exists()) { $request->session()->flash('forbidden', '前修课未修读'); return back()->withInput(); } $selcourse = new Selcourse(); $selcourse->xh = Auth::user()->xh; $selcourse->xm = Auth::user()->profile->xm; $selcourse->nd = $course->nd; $selcourse->xq = $course->xq; $selcourse->kcxh = $inputs['kcxh']; $selcourse->kch = Helper::getCno($inputs['kcxh']); $selcourse->pt = $course->pt; $selcourse->xz = $course->xz; $selcourse->xl = $course->xl; $selcourse->jsgh = $course->task->jsgh; $selcourse->xf = $course->plan->zxf; $selcourse->sf = config('constants.status.enable'); $selcourse->zg = $course->bz; $selcourse->cx = config('constants.status.disable'); $selcourse->bz = config('constants.status.disable'); $selcourse->sj = Carbon::now(); $selcourse->kkxy = $course->kkxy; $selcourse->qz = 0; $selcourse->tdkch = ''; $selcourse->tdyy = ''; $selcourse->zy = $course->zy; if ($selcourse->save()) { return redirect()->route('selcourse.show', $inputs['type'])->withStatus('选课成功'); } else { return back()->withInput()->withStatus('选课失败'); } } }