123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace app\api\controller;
- use app\admin\model\rexam\Time;
- use app\common\controller\Api;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Session;
- /**
- * 考试接口
- */
- class Exam extends Api
- {
- protected $noNeedRight = '*';
- protected $noNeedLogin = [''];
- public $one = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\rexam\Rexam;
- }
- /**
- * 考试id
- */
- protected function check_id(){
- $param = input();
- if(empty($param['id'])){
- $this->error('参数错误');
- }
- $this->one = $this->model->get(['id' => $param['id']]);
- if(empty($this->one)){
- $this->error('未查询到对应考试');
- }
- return $param['id'];
- }
- protected function build(){
- $answer_model = new \app\admin\model\rexam\Answer;
- $question_model = new \app\admin\model\rexam\Question;
- $where = array();
- if ($this->one['areadata'] == 2) {
- $where['id'] = array('in', $this->one['rexam_question_ids']);
- } elseif ($this->one['areadata'] == 1) {
- $where['rexam_subject_id'] = array('in', $this->one['rexam_subject_ids']);
- } else {
- $where['id'] = array('gt', 0);
- }
- $temp_question_list = $question_model->field('id')->where($where)->select();
- $question_count = count($temp_question_list); //获取总记录数
- if ($question_count < $this->one['question_count']) {
- $this->error('题目数量不足,请联系管理员');
- }
- $i = 0;
- $question_list = array();
- while ($i < $this->one['question_count']) {
- $rund_question = $temp_question_list[rand(0, $question_count - 1)]['id'];
- if (!in_array($rund_question, $question_list)) {
- $question_list[] = $rund_question;
- $i++;
- }
- }
- Db::startTrans();
- try {
- $data = array();
- $data['user_id'] = $this->auth->id;
- $data['rexam_id'] = $this->one['id'];
- $data['rexamquestion_ids'] = implode(',', $question_list);
- $data['question_count'] = $this->one['question_count'];
- $data['begintime'] = time();
- $end_time = Time::where([
- 'rexam_id' => $this->one['id'],
- 'user_id' => $this->auth->id,
- ])->value('end_time');
- if (empty($end_time)) {
- $end_time = date('Y-m-d H:i:s', $data['begintime'] + $this->one['limitall']);
- $time_model = new Time();
- $time_model->rexam_id = $this->one['id'];
- $time_model->user_id = $this->auth->id;
- $time_model->end_time = $end_time;
- $time_model->save();
- }
- $user_answer = Db::name('rexam_answer')->where(['user_id' => $this->auth->id, 'rexam_id' => $this->one['id']])->find();
- if (empty($user_answer)) {
- $answer_model->create($data);
- }
- // 获取题目
- $question = $this->question($this->one['id'], $data['rexamquestion_ids']);
- if ($question) {
- Db::commit();
- $data['question'] = $question;
- // 计算结束时间
- if ($this->one['limitdata']) {
- $data['endtime'] = $end_time;
- }
- }
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- return $data;
- }
- /**
- * 答题
- */
- public function go(){
- $rid = $this->check_id();
- $current_time = time();
- if ($this->one['starttime'] >= $current_time) {
- $this->error('开始时间是:' . datetime($this->one['starttime'], 'Y-m-d H:i:s'));
- } elseif ($this->one['expiretime'] <= $current_time) {
- $this->error('考试已截止');
- }
- $answer_model = new \app\admin\model\rexam\Answer;
- $answer = $answer_model->where(['user_id' => $this->auth->id, 'rexam_id' => $rid])->find();
- if ($answer) {
- if ($answer['overtime']) {
- $this->error('您已参与过考试');
- }
- }
- $answer_one = array();
- $answer = $this->build();
- $question = explode(',', $answer['rexamquestion_ids']);
- if (empty($answer['answerjson'])) {
- foreach ($question as $vo){
- $answer_one[$vo] = ['css' => '', 'value' => ''];
- }
- } else {
- $answerarr = json_decode($answer['answerjson'], true);
- foreach ($answerarr as $vo) {
- $answer_one[$vo['qid']] = ['css' => 'done', 'value' => $vo['ans']];
- }
- }
- $this->success('', $answer);
- }
- /**
- * 获取题目
- *
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function question($ana = '', $rexamquestion_ids)
- {
- $rexamquestion_ids = explode(',', $rexamquestion_ids);
- $question_model = new \app\admin\model\rexam\Question();
- // $questions = \app\admin\model\rexam\Question::where(['rexam_subject_id' => $this->one['rexam_subject_ids']])->select();
- $questions = \app\admin\model\rexam\Question::where(['rexam_subject_id' => $this->one['rexam_subject_ids'], 'id' => ['in', $rexamquestion_ids]])->select();
- shuffle($questions);
- $new_questions = array_rand($questions, 10);
- $new_questions_arr = [];
- foreach ($new_questions as $key => $question) {
- $new_questions_arr[$key] = $questions[$question];
- }
- $data = [];
- foreach ($new_questions_arr as $key => $question) {
- $question_one = $question_model->load($question['id'], $this->one['randomdata']);
- $limit = array();
- // if ($this->one['limitdata']) {
- // $question_limit = Session::get('rexam_' . $this->one['id']);
- // if ($this->one['limitall']) {
- // if (empty($question_limit['limitall'])) {
- // $question_limit['limitall'] = time() + $this->one['limitall'];
- // $question_limit['allcount'] = $this->one['limitall'];
- // } else {
- // $question_limit['allcount'] = $question_limit['limitall'] - time();
- // if ($question_limit['allcount'] < 0) {
- // $question_limit['allcount'] = 0;
- // }
- // }
- // }
- //
- // if ($this->one['limitone']) {
- // if (empty($question_limit['limitone' . $question['id']])) {
- // $question_limit['limitone' . $question['id']] = time() + $this->one['limitone'];
- // $question_limit['onecount' . $question['id']] = $this->one['limitone'];
- // } else {
- // $question_limit['onecount' . $question['id']] = $question_limit['limitone' . $question['id']] - time();
- // if($question_limit['onecount' . $question['id']] < 0){
- // $question_limit['onecount' . $question['id']] = 0;
- // }
- // }
- // }
- //
- // Session::set('rexam_' . $this->one['id'], $question_limit);
- // $limit = ['allcount' => $question_limit['allcount'], 'onecount' => $question_limit['onecount' . $question['id']], 'limitall' => $this->one['limitall'], 'limitone' => $this->one['limitone']];
- // }
- $data[$key]['question'] = $question_one;
- $data[$key]['limit'] = $limit;
- }
- return $data;
- }
- }
|