Answer.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Session;
  5. /**
  6. * 答案接口
  7. */
  8. class Answer extends Api
  9. {
  10. protected $noNeedRight = "*";
  11. protected $config;
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->config = get_addon_config('rexam');
  16. $this->model = new \app\admin\model\rexam\Answer;
  17. }
  18. /**
  19. * 获取题目列表
  20. *
  21. * @return void
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public function question(){
  27. if ($this->request->isAjax()) {
  28. $param = input();
  29. $where = array();
  30. $where['user_id'] = $this->auth->id;
  31. $where['rexam_id'] = $param['rid'];
  32. $answer_one = $this->model
  33. ->where($where)
  34. ->find();
  35. $question_list = explode(',', $answer_one['rexamquestion_ids']);
  36. $qid = $question_list[$param['qnum'] -1 ];
  37. $rexam_mode = new \app\admin\model\rexam\Rexam;
  38. $rexam_one = $rexam_mode->get(['id' => $param['rid']]);
  39. if (empty($param['ana'])) {
  40. $random = $rexam_one['randomdata'];
  41. } else {
  42. $random = 0;
  43. }
  44. $question_mode = new \app\admin\model\rexam\Question;
  45. $quesion_one = $question_mode->load($qid, $random);
  46. $limit = array();
  47. if ($rexam_one['limitdata'] && empty($param['ana'])) {
  48. $question_limit = Session::get('rexam_' . $param['rid']);
  49. if ($rexam_one['limitall']) {
  50. if (empty($question_limit['limitall'])) {
  51. $question_limit['limitall'] = time() + $rexam_one['limitall'];
  52. $question_limit['allcount'] = $rexam_one['limitall'];
  53. } else {
  54. $question_limit['allcount'] = $question_limit['limitall'] - time();
  55. if ($question_limit['allcount'] < 0) {
  56. $question_limit['allcount'] = 0;
  57. }
  58. }
  59. }
  60. if ($rexam_one['limitone']) {
  61. if (empty($question_limit['limitone' . $qid])) {
  62. $question_limit['limitone'.$qid] = time() + $rexam_one['limitone'];
  63. $question_limit['onecount'.$qid] = $rexam_one['limitone'];
  64. } else {
  65. $question_limit['onecount'.$qid] = $question_limit['limitone'.$qid] - time();
  66. if($question_limit['onecount' . $qid] < 0){
  67. $question_limit['onecount' . $qid] = 0;
  68. }
  69. }
  70. }
  71. Session::set('rexam_'.$param['rid'], $question_limit);
  72. $limit = ['allcount' => $question_limit['allcount'], 'onecount' => $question_limit['onecount' . $qid], 'limitall' => $rexam_one['limitall'], 'limitone' => $rexam_one['limitone']];
  73. }
  74. $this->success('',array(
  75. "question" => $quesion_one,
  76. "limit" => $limit
  77. ));
  78. }
  79. }
  80. /**
  81. * 提交答案
  82. *
  83. * @return void
  84. * @throws \think\Exception
  85. * @throws \think\exception\DbException
  86. */
  87. public function answer(){
  88. if ($this->request->isPost()) {
  89. $param = input();
  90. $where = array();
  91. $data = array();
  92. $question_mode = new \app\admin\model\rexam\Question;
  93. $answer_one = $this->model->where(['rexam_id' => $param['aid'], 'user_id' => $this->auth->id])->find();
  94. $rexamquestion_ids_arr = explode(',', $answer_one['rexamquestion_ids']);
  95. foreach ($param as $key => $vo) {
  96. if(substr( $key,0,8) == 'question'){
  97. $data[substr($key, 8)] = $vo;
  98. }
  99. // if ($key == 'aid') {
  100. // $where['id'] = $vo;
  101. // }
  102. }
  103. $answerarr = array();
  104. $rightcount = 0;
  105. foreach ($data as $key => $vo) {
  106. if (in_array($key, $rexamquestion_ids_arr)) {
  107. $right = $question_mode->check($key, $vo);
  108. if ($right) {
  109. $rightcount++;
  110. }
  111. $questionone = [
  112. 'qid' => $key,
  113. 'ans' => $vo,
  114. 'res' => $right
  115. ];
  116. $answerarr[] = $questionone;
  117. }
  118. }
  119. $answerjson = json_encode($answerarr);
  120. $save = intval($param['save']);
  121. if ($save == 1) {
  122. $rexam_mode = new \app\admin\model\rexam\Rexam;
  123. $rexam_one = $rexam_mode->get(['id' => $answer_one['rexam_id']]);
  124. $points = 0;
  125. if ($rexam_one['pointsdata']) {
  126. $points = $rexam_one['pointslimit'] * $rightcount / $answer_one['question_count'];
  127. }
  128. $answer_one->allowField(true)->save(['answerjson' => $answerjson, 'right_count' => $rightcount, 'points' => $points, 'overtime' => time()]);
  129. if ($points) {
  130. $user_mode = new \app\admin\model\rexam\User;
  131. $user_mode->where(['user_id' => $this->auth->id])->setInc('points', $points);
  132. }
  133. // 删除对应的设备下线
  134. $ip = request()->ip();
  135. $monitor = \app\admin\model\rexam\Monitor::where(['ip' => $ip])->find();
  136. if ($monitor) {
  137. $monitor->delete();
  138. }
  139. } else {
  140. $answer_one->allowField(true)->save(['answerjson' => $answerjson]);
  141. }
  142. $this->success('OK');
  143. }
  144. }
  145. }