Browse Source

考试接口

lizhen_gitee 8 months ago
parent
commit
921c9ac701
2 changed files with 19 additions and 18 deletions
  1. 17 17
      addons/exam/controller/Paper.php
  2. 2 1
      application/index/controller/Plantask.php

+ 17 - 17
addons/exam/controller/Paper.php

@@ -94,35 +94,35 @@ class Paper extends Base
         switch (true) {
             case !$paper:
                 fail('试卷信息不存在');
-            case $paper->status != CommonStatus::NORMAL:
+            case $paper->status != 'NORMAL':
                 fail('试卷未开启');
-            case $paper->mode == PaperMode::RANDOM && !$paper->configs:
+            case $paper->mode == 'RANDOM' && !$paper->configs:
                 fail('试卷未配置');
         }
 
+        //时间限制
+        if ($paper['start_time'] > 0 && $paper['start_time'] > time()) {
+            $this->error('该试卷未开始,不能参与考试');
+        }
+        if ($paper['end_time'] > 0 && $paper['end_time'] < time()) {
+            $this->error('该试卷已结束,不能参与考试');
+        }
+
         //检查考试状态
-        $check = Db::name('exam_grade')->where('user_id', $user_id)->where('paper_id', $paper_id)->where('status',1)->find();
+        $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find();
         if($check){
-            $this->error('当前考试进行中');//或许这里不应该报错,直接给成功
+            $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();
+            $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('该试卷您的考试次数已达上限');
             }
         }
 
-        //时间限制
-        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,
@@ -130,7 +130,7 @@ class Paper extends Base
             'start_time' => time(),
             'status' => 1,
             'limit_time' => $paper['limit_time'],  //限时N秒
-            'last_time' => time() + $paper['limit_time'], //最后限制交卷时间,时间戳
+            'last_time' => $paper['limit_time'] > 0 ? (time() + $paper['limit_time']) : 0, //最后限制交卷时间,时间戳
         ];
 
         $grade_id = Db::name('exam_grade')->insertGetId($data);
@@ -147,21 +147,21 @@ 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 (!$grade_id ||!$user_id || !$paper_id || !$questions) {
+        if (!$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();
+        $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'];
 
 
         // 考场考试

+ 2 - 1
application/index/controller/Plantask.php

@@ -8,12 +8,13 @@ use think\Db;
 class Plantask extends Controller
 {
 
-    //考试到时间了,自动结束,0分
+    //考试限时 且交卷时间了,自动结束,0分
     public function auto_grade_finish(){
 
         $nowtime = time();
         $map = [
             'status' => 1,
+            'limit_time' => ['gt',0],
             'last_time' => ['lt',time()]
         ];
         $update = [