123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Session;
- /**
- * 答案接口
- */
- class Answer extends Api
- {
- protected $noNeedRight = "*";
- protected $config;
- public function _initialize()
- {
- parent::_initialize();
- $this->config = get_addon_config('rexam');
- $this->model = new \app\admin\model\rexam\Answer;
- }
- /**
- * 获取题目列表
- *
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function question(){
- if ($this->request->isAjax()) {
- $param = input();
- $where = array();
- $where['user_id'] = $this->auth->id;
- $where['rexam_id'] = $param['rid'];
- $answer_one = $this->model
- ->where($where)
- ->find();
- $question_list = explode(',', $answer_one['rexamquestion_ids']);
- $qid = $question_list[$param['qnum'] -1 ];
- $rexam_mode = new \app\admin\model\rexam\Rexam;
- $rexam_one = $rexam_mode->get(['id' => $param['rid']]);
- if (empty($param['ana'])) {
- $random = $rexam_one['randomdata'];
- } else {
- $random = 0;
- }
- $question_mode = new \app\admin\model\rexam\Question;
- $quesion_one = $question_mode->load($qid, $random);
- $limit = array();
- if ($rexam_one['limitdata'] && empty($param['ana'])) {
- $question_limit = Session::get('rexam_' . $param['rid']);
- if ($rexam_one['limitall']) {
- if (empty($question_limit['limitall'])) {
- $question_limit['limitall'] = time() + $rexam_one['limitall'];
- $question_limit['allcount'] = $rexam_one['limitall'];
- } else {
- $question_limit['allcount'] = $question_limit['limitall'] - time();
- if ($question_limit['allcount'] < 0) {
- $question_limit['allcount'] = 0;
- }
- }
- }
- if ($rexam_one['limitone']) {
- if (empty($question_limit['limitone' . $qid])) {
- $question_limit['limitone'.$qid] = time() + $rexam_one['limitone'];
- $question_limit['onecount'.$qid] = $rexam_one['limitone'];
- } else {
- $question_limit['onecount'.$qid] = $question_limit['limitone'.$qid] - time();
- if($question_limit['onecount' . $qid] < 0){
- $question_limit['onecount' . $qid] = 0;
- }
- }
- }
- Session::set('rexam_'.$param['rid'], $question_limit);
- $limit = ['allcount' => $question_limit['allcount'], 'onecount' => $question_limit['onecount' . $qid], 'limitall' => $rexam_one['limitall'], 'limitone' => $rexam_one['limitone']];
- }
- $this->success('',array(
- "question" => $quesion_one,
- "limit" => $limit
- ));
- }
- }
- /**
- * 提交答案
- *
- * @return void
- * @throws \think\Exception
- * @throws \think\exception\DbException
- */
- public function answer(){
- if ($this->request->isPost()) {
- $param = input();
- $where = array();
- $data = array();
- $question_mode = new \app\admin\model\rexam\Question;
- $answer_one = $this->model->where(['rexam_id' => $param['aid'], 'user_id' => $this->auth->id])->find();
- $rexamquestion_ids_arr = explode(',', $answer_one['rexamquestion_ids']);
- foreach ($param as $key => $vo) {
- if(substr( $key,0,8) == 'question'){
- $data[substr($key, 8)] = $vo;
- }
- // if ($key == 'aid') {
- // $where['id'] = $vo;
- // }
- }
- $answerarr = array();
- $rightcount = 0;
- foreach ($data as $key => $vo) {
- if (in_array($key, $rexamquestion_ids_arr)) {
- $right = $question_mode->check($key, $vo);
- if ($right) {
- $rightcount++;
- }
- $questionone = [
- 'qid' => $key,
- 'ans' => $vo,
- 'res' => $right
- ];
- $answerarr[] = $questionone;
- }
- }
- $answerjson = json_encode($answerarr);
- $save = intval($param['save']);
- if ($save == 1) {
- $rexam_mode = new \app\admin\model\rexam\Rexam;
- $rexam_one = $rexam_mode->get(['id' => $answer_one['rexam_id']]);
- $points = 0;
- if ($rexam_one['pointsdata']) {
- $points = $rexam_one['pointslimit'] * $rightcount / $answer_one['question_count'];
- }
- $answer_one->allowField(true)->save(['answerjson' => $answerjson, 'right_count' => $rightcount, 'points' => $points, 'overtime' => time()]);
- if ($points) {
- $user_mode = new \app\admin\model\rexam\User;
- $user_mode->where(['user_id' => $this->auth->id])->setInc('points', $points);
- }
- // 删除对应的设备下线
- $ip = request()->ip();
- $monitor = \app\admin\model\rexam\Monitor::where(['ip' => $ip])->find();
- if ($monitor) {
- $monitor->delete();
- }
- } else {
- $answer_one->allowField(true)->save(['answerjson' => $answerjson]);
- }
- $this->success('OK');
- }
- }
- }
|