auth->bind_player_date != strtotime(date('Y-m-d'))){ $this->error('先绑定单位再答题',null,2); } //获取10个随机题目 $count = config('site.exam_times_user_eday'); $lists = Db::name('exam_question') ->field('id,kind,title,options_json') ->where('is_material_child', 0)// 材料题子题不显示 ->where('status', 'NORMAL')// 正常 ->where('deletetime', NULL) ->orderRaw('rand()')->limit($count)->select(); foreach($lists as $key => $val){ $val['options_json'] = $this->getOptionsJsonAttr($val['options_json']); $lists[$key] = $val; } $this->success(1,$lists); } private function getOptionsJsonAttr($value) { if ($value = json_decode($value, true)) { $data = []; foreach ($value as $key => $row) { $arr['key'] = $key; $arr['value'] = $row; array_push($data, $arr); } return $data; } return []; } /** * 交卷 */ public function submit() { //上次绑定选手的时间不是今天 if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){ $this->error('先绑定单位再答题',null,2); } //检查今日答题次数 $exam_times_user_eday = config('site.exam_times_user_eday'); $count = Db::name('user_question_log')->where('user_id',$this->auth->id)->where('createdate',strtotime(date('Y-m-d')))->count(); if($count >= $exam_times_user_eday){ $this->error('今日答题次数用完了,明天再来吧'); } $question_id = input('question_id'); $answer = input('answer'); if (!$question_id || !$answer) { $this->error('提交数据有误'); } $is_right = $this->paperExam($question_id,$answer); Db::startTrans(); //答题日志 $log = [ 'user_id' => $this->auth->id, 'question_id' => $question_id, 'is_right' => $is_right ? 1 : 0, 'player_id' => $this->auth->bind_player_id, 'createtime' => time(), 'createdate' => strtotime(date('Y-m-d')), ]; $log_id = Db::name('user_question_log')->insertGetId($log); if(!$log_id){ Db::rollback(); $this->error('答题失败'); } if($is_right){ $msg = '回答正确'; //给选手加分 }else{ $msg = '回答错误'; } Db::commit(); $this->success($msg); } /** * 试题 * @param $question_id * @param $answer * @return bool */ private function paperExam($question_id,$answer) { $is_right = false; $question = Db::name('exam_question')->where('id', $question_id)->find(); if(empty($question)){ return false; } switch ($question['kind']) { case 'JUDGE': // 判断题 case 'SINGLE': // 单选题 case 'MULTI': // 多选题 // 答题正确 if (strtoupper($answer) == $question['answer']) { $is_right = true; } else { $is_right = false; } break; case 'SHORT': // 简答题 // 答案得分配置 $answer_config = is_string($question['answer']) ? json_decode($question['answer'], true) : $question['answer']; $user_answers = $answer; foreach ($answer_config['config'] as $answer_item) { // 匹配答案关键词 if (strpos($user_answers, $answer_item['answer']) !== false) { $is_right = true; } } break; } return $is_right; } }