Browse Source

取题接口

lizhen_gitee 4 months ago
parent
commit
ca846e775b
1 changed files with 173 additions and 0 deletions
  1. 173 0
      application/api/controller/Question.php

+ 173 - 0
application/api/controller/Question.php

@@ -0,0 +1,173 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 答题
+ */
+class Question extends Api
+{
+
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+
+
+    public function get_question(){
+
+        //上次绑定选手的时间不是今天
+        if($this->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);
+    }
+
+
+    public 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 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;
+
+        $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);
+    }
+
+}