Browse Source

考试接口

lizhen_gitee 8 months ago
parent
commit
bbc9bbd056

+ 19 - 3
addons/exam/controller/Paper.php

@@ -84,6 +84,7 @@ class Paper extends Base
         $this->success('', $question_data);
     }
 
+    //开始考试接口
     public function startpaper(){
         $paper_id      = input('paper_id/d', 0);
         $user_id = $this->auth->id;
@@ -93,11 +94,11 @@ class Paper extends Base
 
         switch (true) {
             case !$paper:
-                fail('试卷信息不存在');
+                $this->error('试卷信息不存在');
             case $paper->status != 'NORMAL':
-                fail('试卷未开启');
+                $this->error('试卷未开启');
             case $paper->mode == 'RANDOM' && !$paper->configs:
-                fail('试卷未配置');
+                $this->error('试卷未配置');
         }
 
         //时间限制
@@ -138,6 +139,21 @@ class Paper extends Base
 
     }
 
+    //进行中考试
+    public function half_examing(){
+        $user_id = $this->auth->id;
+
+        $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find();
+        if(empty($check)){
+            $this->error('您没有进行中的考试');
+        }
+        $paper_id = $check['paper_id'];
+
+        // 获取试卷题目
+        $question_data = ExamService::getExamQuestion($paper_id, 0);
+        $this->success('', $question_data);
+    }
+
 
     /**
      * 交卷

+ 6 - 4
addons/exam/library/ExamService.php

@@ -35,7 +35,7 @@ class ExamService
             fail('缺少试卷ID');
         }
 
-        $paper = self::validPaper($paper_id, $room_id);
+        $paper = self::validPaper($paper_id, $room_id,true);
         switch ($paper['mode']) {
             case PaperMode::RANDOM:
                 $questions = self::getRandomQuestions($paper);
@@ -135,7 +135,7 @@ class ExamService
     public static function paperExam($user_id, $paper_id, $user_questions, $start_time, &$paper, $from_room = false)
     {
         // 验证试卷
-        $paper = self::validPaper($paper_id, $from_room ? 1 : 0);
+        $paper = self::validPaper($paper_id, $from_room ? 1 : 0,false);
         if (!$questions_ids = array_column($user_questions, 'id')) {
             fail('提交的题目数据有误');
         }
@@ -357,7 +357,7 @@ class ExamService
      * @param int $room_id  考场ID
      * @return PaperModel|null
      */
-    private static function validPaper($paper_id, $room_id = 0)
+    private static function validPaper($paper_id, $room_id = 0,$checktime = false)
     {
         $paper   = PaperModel::get($paper_id);
         $user_id = getUserId();
@@ -377,9 +377,11 @@ class ExamService
                 fail('当前试卷考试次数已达今日上限,明天再来吧~');
             }*/
 
+        if($checktime){
             if ($paper['end_time'] > 0 && $paper['end_time'] < time()) {
-                fail('该试卷已失效,不能参与考试了');
+                fail('该试卷已截止,不能参与考试了');
             }
+        }
         /*}*/
 
         return $paper;

+ 34 - 1
application/api/controller/Index.php

@@ -39,9 +39,42 @@ class Index extends Api
     //我的考试
     //可参加考试
     //我的试卷
+    public function my_paper_list(){
+        $now    = time();
+        $papers = Db::name('exam_paper')->field('id,title,start_time,end_time,total_score,limit_time')
+            ->where('status', 'NORMAL')
+            ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))")
+            ->autopage()
+            ->select();
+
+        foreach($papers as $key => &$val){
+            $val['limit_time'] = $val['limit_time']/60; //秒转换分种
+            $val['is_start'] = 1;
+        }
+
+        $this->success('', $papers);
+    }
 
-    //进行中考试
     //历史考试
+    public function my_grade_list(){
+        $field = [
+            'grade.score','grade.is_pass','grade.total_score',
+            'grade.total_count','grade.right_count','grade.error_count',
+            'grade.grade_time','grade.start_time',
+            'paper.title'
+        ];
+        $list = Db::name('exam_grade')->alias('grade')
+            ->join('exam_paper paper','grade.paper_id = paper.id','LEFT')
+            ->field($field)
+            ->where('grade.user_id',$this->auth->id)
+            ->autopage()
+            ->select();
+        foreach($list as $key => &$val){
+            $val['grade_time_text'] = Sec2Time($val['grade_time']);
+        }
+
+        $this->success('', $list);
+    }
 
 
 }

+ 3 - 3
application/common.php

@@ -936,14 +936,14 @@ if(!function_exists('Sec2Time')) {
                 $value['years'] = floor($time/31556926);
                 $time = ($time%31556926);
             }*/
-            if($time >= 86400){
+            /*if($time >= 86400){
                 $value['days'] = floor($time/86400);
                 $time = ($time%86400);
             }
             if($time >= 3600){
                 $value['hours'] = floor($time/3600);
                 $time = ($time%3600);
-            }
+            }*/
             if($time >= 60){
                 $value['minutes'] = floor($time/60);
                 $time = ($time%60);
@@ -951,7 +951,7 @@ if(!function_exists('Sec2Time')) {
             $value['seconds'] = floor($time);
             //return (array) $value;
             //$t=$value['years'] .'年'. $value['days'] .'天'.' '. $value['hours'] .'小时'. $value['minutes'] .'分'.$value['seconds'].'秒';
-            $t = $value['days'] .'天' . $value['hours'] .'小时'. $value['minutes'] .'分';
+            $t = $value['minutes'] .'分'.$value['seconds'].'秒';
             return $t;
 
         }else{