Quellcode durchsuchen

先开始,再交卷,计划任务强制交卷

lizhen_gitee vor 8 Monaten
Ursprung
Commit
789d56c31c

+ 84 - 4
addons/exam/controller/Paper.php

@@ -11,6 +11,7 @@ use addons\exam\model\UserModel;
 use app\admin\model\exam\CateModel;
 use app\admin\model\exam\GradeModel;
 use think\Request;
+use think\Db;
 
 
 /**
@@ -83,6 +84,61 @@ class Paper extends Base
         $this->success('', $question_data);
     }
 
+    public function startpaper(){
+        $paper_id      = input('paper_id/d', 0);
+        $user_id = $this->auth->id;
+
+        //检查试卷
+        $paper   = PaperModel::get($paper_id);
+
+        switch (true) {
+            case !$paper:
+                fail('试卷信息不存在');
+            case $paper->status != CommonStatus::NORMAL:
+                fail('试卷未开启');
+            case $paper->mode == PaperMode::RANDOM && !$paper->configs:
+                fail('试卷未配置');
+        }
+
+        //检查考试状态
+        $check = Db::name('exam_grade')->where('user_id', $user_id)->where('paper_id', $paper_id)->where('status',1)->find();
+        if($check){
+            $this->error('当前考试进行中');//或许这里不应该报错,直接给成功
+        }
+
+        //次数限制
+        if ($paper['limit_count'] > 0){
+
+            $my_count = Db::name('exam_grade')->where('user_id', $user_id)->where('paper_id', $paper_id)->where('status',2)->count();
+            if($my_count >= $paper['limit_count']) {
+                $this->error('该试卷您的考试次数已达上限');
+            }
+        }
+
+        //时间限制
+        if ($paper['start_time'] > 0 && $paper['start_time'] > time()) {
+            $this->error('该试卷未开始,不能参与考试');
+        }
+        if ($paper['end_time'] > 0 && $paper['end_time'] < time()) {
+            $this->error('该试卷已结束,不能参与考试');
+        }
+
+        //记录为已开始,计划任务倒计时之后 自动结束
+        $data = [
+            'user_id'  => $this->auth->id,
+            'paper_id' => $paper_id,
+            'start_time' => time(),
+            'status' => 1,
+            'limit_time' => $paper['limit_time'],  //限时N秒
+            'last_time' => time() + $paper['limit_time'], //最后限制交卷时间,时间戳
+        ];
+
+        $grade_id = Db::name('exam_grade')->insertGetId($data);
+        $this->success('',$grade_id);
+
+    }
+
+
     /**
      * 交卷
      */
@@ -90,16 +146,24 @@ class Paper extends Base
     {
         $request       = Request::instance();
         $user_id       = $this->auth->id;
+
+        $grade_id      = $request->post('grade_id/d', 0);
         $paper_id      = $request->post('paper_id/d', 0);
         $questions     = $request->post('questions/a', []);
         $start_time    = $request->post('start_time/d', time());
         $room_id       = $request->post('room_id/d', 0);
         $room_grade_id = $request->post('room_grade_id/d', 0);
 
-        if (!$user_id || !$paper_id || !$questions) {
-            $this->error('提交数据有误' . $user_id);
+        if (!$grade_id ||!$user_id || !$paper_id || !$questions) {
+            $this->error('提交数据有误');
         }
 
+        $check = Db::name('exam_grade')->where('id',$grade_id)->where('user_id',$user_id)->where('paper_id',$paper_id)->find();
+        if(!$check){
+            $this->error('提交数据有误');
+        }
+
+
         // 考场考试
         if ($room_id) {
             if (!$room_grade_id) {
@@ -129,7 +193,7 @@ class Paper extends Base
             $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
 
             // 记录考试成绩
-            GradeModel::create(array_merge(
+            /*GradeModel::create(array_merge(
                 $result,
                 [
                     'cate_id'  => $paper['cate_id'],
@@ -139,7 +203,23 @@ class Paper extends Base
                 [
                     // 'exam_mode' => ExamMode::PAPER,
                     'date' => date('Y-m-d'),
-                ]), true);
+                ]), true);*/
+
+            $update = array_merge(
+                $result,
+                [
+                    'cate_id'  => $paper['cate_id'],
+                    'updatetime' => time(),
+                    'date' => date('Y-m-d'),
+
+                    'status' => 2,
+                    'finish_time' => time(),
+                ]);
+
+            $rs = Db::name('exam_grade')->where('id',$grade_id)->update($update);
+            if($rs === false){
+                $this->error('交卷失败');
+            }
         }
         return json($result);
     }

+ 1 - 1
addons/exam/model/GradeModel.php

@@ -36,6 +36,6 @@ class GradeModel extends \app\admin\model\exam\GradeModel
         }
 
         $date = $date ?: date('Y-m-d');
-        return self::where('user_id', $user_id)->where('date', $date)->count();
+        return self::where('user_id', $user_id)->where('paper_id', $paper_id)->where('date', $date)->count();
     }
 }

+ 9 - 0
application/api/controller/Index.php

@@ -23,6 +23,7 @@ class Index extends Api
         $papers = Db::name('exam_paper')->field('id,image,title,start_time,end_time,total_score,limit_time')
             ->where('status', 'NORMAL')
             ->where('index_status', 1)
+            ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
             ->limit(3)
             ->select();
 
@@ -34,4 +35,12 @@ class Index extends Api
         $this->success('', $papers);
     }
 
+    //我的考试
+    //可参加考试
+    //我的试卷
+
+    //进行中考试
+    //历史考试
+
+
 }

+ 35 - 0
application/index/controller/Plantask.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace app\index\controller;
+
+use think\Controller;
+use think\Db;
+
+class Plantask extends Controller
+{
+
+    //考试到时间了,自动结束,0分
+    public function auto_grade_finish(){
+
+        $nowtime = time();
+        $map = [
+            'status' => 1,
+            'last_time' => ['lt',time()]
+        ];
+        $update = [
+            'score' => 0,
+            'is_pass' => 0,
+//            'grade_time' => 'limit_time',
+            'date' => date('Y-m-d'),
+
+            'updatetime' => $nowtime,
+            'status' => 2,
+            'finish_time' => $nowtime,
+        ];
+        $rs = Db::name('exam_grade')->where($map)->update($update);
+    }
+
+
+
+
+}