Browse Source

提交试题

lizhen_gitee 4 months ago
parent
commit
b9ee532dea
1 changed files with 88 additions and 97 deletions
  1. 88 97
      application/api/controller/Question.php

+ 88 - 97
application/api/controller/Question.php

@@ -14,7 +14,7 @@ class Question extends Api
     protected $noNeedRight = ['*'];
 
 
-
+    //获取10个题
     public function get_question(){
 
         //上次绑定选手的时间不是今天
@@ -40,8 +40,7 @@ class Question extends Api
         $this->success(1,$lists);
     }
 
-
-    public function getOptionsJsonAttr($value)
+    private function getOptionsJsonAttr($value)
     {
         if ($value = json_decode($value, true)) {
             $data = [];
@@ -55,119 +54,111 @@ class Question extends Api
         return [];
     }
 
-    public function getAnswerAttr($value, $data)
-    {
-        if (is_array($value)) {
-            return $value;
-        }
-        if (in_array($data['kind'], ['FILL', 'SHORT'])) {
-            return json_decode($value, true);
-        }
-        return $value;
-    }
-
     /**
      * 交卷
      */
     public function submit()
     {
-        $request       = Request::instance();
-        $user_id       = $this->auth->id;
+        //上次绑定选手的时间不是今天
+        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('今日答题次数用完了,明天再来吧');
+        }
 
-        $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);
+        $question_id  = input('question_id');
+        $answer       = input('answer');
 
-        if (!$user_id || !$paper_id || !$questions) {
+        if (!$question_id || !$answer) {
             $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('交卷有误,或者您已交卷');
+        $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('答题失败');
         }
-        $grade_id = $check['id'];
-        $start_time = $check['start_time'];
 
 
-        // 考场考试
-        if ($room_id) {
-            if (!$room_grade_id) {
-                $this->error('提交数据不合法');
-            }
+        if($is_right){
+            $msg = '回答正确';
 
-            // 考场考试
-            $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('交卷失败');
-            }
+            //给选手加分
+            
+
+        }else{
+            $msg = '回答错误';
         }
 
-        $result['nickname'] = $this->auth->nickname;
+        Db::commit();
 
-        //删除本试卷分数最低的试卷
-        $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($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;
         }
-        //删除本试卷分数最低的试卷
 
-        $this->success('',$result);
-//        return json($result);
+        return $is_right;
     }
 
 }