123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace addons\exam\controller;
- use addons\exam\enum\CommonStatus;
- use addons\exam\enum\ExamMode;
- use addons\exam\library\ExamService;
- use addons\exam\model\PaperModel;
- use addons\exam\model\QuestionModel;
- use addons\exam\model\UserModel;
- use app\admin\model\exam\CateModel;
- use app\admin\model\exam\GradeModel;
- use think\Request;
- use think\Db;
- /**
- * 试卷接口
- */
- class Paper extends Base
- {
- protected $noNeedLogin = ['index'];
- protected $noNeedRight = ['*'];
- protected $user;
- /**
- * 查询出分类下的试卷
- */
- public function index()
- {
- $cate_id = input('cate_id/d', '0');
- $sort = input('sort/s', '');
- $now = time();
- $query = PaperModel::with([
- 'cates' => function ($query) {
- $query->withField('id, name');
- }
- ])
- ->where('status', CommonStatus::NORMAL)
- ->where('is_only_room', 0)// 过滤仅考场使用的试卷
- ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))");
- // 分类
- if ($cate_id) {
- $child_cate_ids = CateModel::getChildId($cate_id);
- array_push($child_cate_ids, $cate_id);
- $query->whereIn('cate_id', $child_cate_ids);
- }
- // 排序
- if ($sort && $sort != 'null') {
- $sort = explode('|', $sort);
- $field = $sort[0];
- $order_by = $sort[1];
- $field = in_array($field, ['join_count']) ? $field : 'join_count';
- $order_by = $order_by == 'desc' ? 'desc' : 'asc';
- $query->order("{$field} $order_by");
- }
- $list = $query->paginate();
- $this->success('', ['list' => $list]);
- }
- /**
- * 试卷取题接口
- */
- public function getExamQuestion()
- {
- $paper_id = input('paper_id/d', 0);
- $room_id = input('room_id/d', 0);
- // 验证是否需要绑定手机号
- // UserModel::isMustBindMobile($this->auth->getUser());
- // 预创建考场考试记录
- // $room_grade_id = ExamService::preRoomGrade($room_id, $this->auth->id);
- // 获取试卷题目
- $question_data = ExamService::getExamQuestion($paper_id, $room_id);
- // $this->success('', array_merge($question_data, ['room_grade_id' => $room_grade_id]));
- $this->success('', $question_data);
- }
- //开始考试接口
- public function startpaper(){
- $paper_id = input('paper_id/d', 0);
- $user_id = $this->auth->id;
- //检查考试状态
- $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find();
- if($check){
- $this->success('您有其他考试正在进行中,即将继续考试',0);//直接给成功,数据返回0,前端跳转
- }
- //检查试卷
- $paper = PaperModel::get($paper_id);
- switch (true) {
- case !$paper:
- $this->error('试卷信息不存在');
- case $paper->status != 'NORMAL':
- $this->error('试卷未开启');
- case $paper->mode == 'RANDOM' && !$paper->configs:
- $this->error('试卷未配置');
- }
- //时间限制
- if ($paper['start_time'] > 0 && $paper['start_time'] > time()) {
- $this->error('该试卷未开始,不能参与考试');
- }
- if ($paper['end_time'] > 0 && $paper['end_time'] < time()) {
- $this->error('该试卷已结束,不能参与考试');
- }
- //考试资格
- if(!in_array($user_id,explode(',',$paper['user_ids']))){
- $this->error('您不能参加该考试');
- }
- //次数限制
- if ($paper['limit_count'] > 0){
- $my_count = Db::name('exam_grade')->where('user_id', $user_id)->where('paper_id', $paper_id)->count();
- if($my_count >= $paper['limit_count']) {
- $this->error('该试卷您的考试次数已达上限');
- }
- }
- //记录为已开始,计划任务倒计时之后 自动结束
- $data = [
- 'cate_id' => $paper['cate_id'],
- 'user_id' => $this->auth->id,
- 'paper_id' => $paper_id,
- 'mode' => $paper['mode'],
- 'total_score' => $paper['total_score'],
- 'total_count' => $paper['quantity'],
- 'start_time' => time(),
- 'createtime' => time(),
- 'status' => 1,
- 'limit_time' => $paper['limit_time'], //限时N秒
- 'last_time' => $paper['limit_time'] > 0 ? (time() + $paper['limit_time']) : 0, //最后限制交卷时间,时间戳
- ];
- $grade_id = Db::name('exam_grade')->insertGetId($data);
- $this->success('',$grade_id);
- }
- //进行中考试
- public function half_examing(){
- $user_id = $this->auth->id;
- $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find();
- if(empty($check)){
- $this->error('您没有进行中的考试');
- }
- $paper_id = $check['paper_id'];
- // 获取试卷题目
- $question_data = ExamService::getExamQuestion($paper_id, 0);
- $question_data['paper']['limit_time'] = $check['last_time'] - time();//倒计时秒数
- $this->success('', $question_data);
- }
- /**
- * 交卷
- */
- public function submit()
- {
- $request = Request::instance();
- $user_id = $this->auth->id;
- $paper_id = $request->post('paper_id/d', 0);
- $questions = $request->post('questions/a', []);
- $start_time = $request->post('start_time/d', time());
- $room_id = 0;
- $room_grade_id = $request->post('room_grade_id/d', 0);
- if (!$user_id || !$paper_id || !$questions) {
- $this->error('提交数据有误');
- }
- $check = Db::name('exam_grade')->where('status',1)->where('user_id',$user_id)->where('paper_id',$paper_id)->find();
- if(!$check){
- $this->error('交卷有误,或者您已交卷');
- }
- $grade_id = $check['id'];
- $start_time = $check['start_time'];
- // 考场考试
- if ($room_id) {
- if (!$room_grade_id) {
- $this->error('提交数据不合法');
- }
- // 考场考试
- $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
- // 记录考场考试成绩
- $room_grade_log->allowField(true)->save(
- array_merge(
- $result,
- [
- // 'cate_id' => $paper['cate_id'],
- 'user_id' => $user_id,
- 'paper_id' => $paper_id,
- 'is_makeup' => $is_makeup,
- 'is_pre' => 0, // 提交成绩后不再为预创建标记
- ],
- [
- 'exam_mode' => ExamMode::ROOM,
- ]
- )
- );
- } else {
- $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
- // 记录考试成绩
- /*GradeModel::create(array_merge(
- $result,
- [
- 'cate_id' => $paper['cate_id'],
- 'user_id' => $user_id,
- 'paper_id' => $paper_id,
- ],
- [
- // 'exam_mode' => ExamMode::PAPER,
- 'date' => date('Y-m-d'),
- ]), true);*/
- $update = array_merge(
- $result,
- [
- 'cate_id' => $paper['cate_id'],
- 'updatetime' => time(),
- 'date' => date('Y-m-d'),
- 'status' => 2,
- 'finish_time' => time(),
- ]);
- unset($update['pass_score']);
- unset($update['start_time']);
- $rs = Db::name('exam_grade')->where('id',$grade_id)->update($update);
- if($rs === false){
- $this->error('交卷失败');
- }
- }
- $result['nickname'] = $this->auth->nickname;
- //删除本试卷分数最低的试卷
- $old_grade = Db::name('exam_grade')->where('user_id',$user_id)->where('paper_id',$paper_id)->where('id','NEQ',$grade_id)->find();
- if(!empty($old_grade)){
- if($old_grade['score'] <= $update['score']){
- $delete_id = $old_grade['id'];
- }else{
- $delete_id = $grade_id;
- }
- Db::name('exam_grade')->where('id',$delete_id)->delete();
- }
- //删除本试卷分数最低的试卷
- $this->success('',$result);
- // return json($result);
- }
- /*
- * 查看错题
- * Robin
- * @param $ids
- * */
- public function error_ids($ids)
- {
- $questions = QuestionModel::whereIn('id', ($ids))->select();
- $this->success('', $questions);
- }
- }
|