Question.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\utils\RedisKeyEnum;
  6. use app\utils\RedisUtil;
  7. /**
  8. * 答题
  9. */
  10. class Question extends Api
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = ['*'];
  14. //获取10个题
  15. public function get_question(){
  16. //上次绑定选手的时间不是今天
  17. if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
  18. $this->error('先绑定单位再答题',null,2);
  19. }
  20. //今天,总答题次数
  21. $gift_question = config('site.exam_times_user_eday');
  22. //今天,已答题次数
  23. $submit_question = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->get();
  24. //今日剩余答题次数
  25. $today_my_question = $gift_question - $submit_question;
  26. if($today_my_question < 0){
  27. $today_my_question = 0;
  28. }
  29. if($today_my_question == 0){
  30. $this->error('今日答题次数用完了,明天再来吧');
  31. }
  32. //获取10个随机题目
  33. $lists = Db::name('exam_question')
  34. ->field('id,kind,title,options_json')
  35. ->where('is_material_child', 0)// 材料题子题不显示
  36. ->where('status', 'NORMAL')// 正常
  37. ->where('deletetime', NULL)
  38. ->orderRaw('rand()')->limit($today_my_question)->select();
  39. foreach($lists as $key => $val){
  40. $val['options_json'] = $this->getOptionsJsonAttr($val['options_json']);
  41. $lists[$key] = $val;
  42. }
  43. $this->success(1,$lists);
  44. }
  45. private function getOptionsJsonAttr($value)
  46. {
  47. if ($value = json_decode($value, true)) {
  48. $data = [];
  49. foreach ($value as $key => $row) {
  50. $arr['key'] = $key;
  51. $arr['value'] = $row;
  52. array_push($data, $arr);
  53. }
  54. return $data;
  55. }
  56. return [];
  57. }
  58. /**
  59. * 答题
  60. */
  61. public function submit()
  62. {
  63. // $this->error('接口作废');
  64. if(!$this->apiLimit('操作太快了,休息一下吧'));
  65. //上次绑定选手的时间不是今天
  66. if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
  67. $this->error('先绑定单位再答题',null,2);
  68. }
  69. //检查今日答题次数
  70. $exam_times_user_eday = config('site.exam_times_user_eday');
  71. $count = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->get();
  72. if($count >= $exam_times_user_eday){
  73. $this->error('今日答题次数用完了,明天再来吧');
  74. }
  75. //答题
  76. $user_questions = input('questions','','htmlspecialchars_decode');
  77. $user_questions = json_decode($user_questions,true);
  78. if (!$user_questions) {
  79. $this->error('提交数据有误');
  80. }
  81. //避免超次数
  82. if(count($user_questions) + $count > $exam_times_user_eday){
  83. $this->error('今日答题次数用完了,明天再来吧!');
  84. }
  85. $questions_ids = array_column($user_questions, 'id'); // 答题id
  86. $answers = array_column($user_questions, 'answer'); // 用户答案
  87. $is_right_number = 0;
  88. $log_data = [];
  89. $questions = Db::name('exam_question')->field('id,kind,answer')->where('id','IN', $questions_ids)->orderRaw('field(id,'. implode(',', $questions_ids) .')')->select();
  90. foreach($questions as $key => $question){
  91. $is_right = $this->paperExam($question,$answers[$key]);
  92. if($is_right){
  93. $is_right_number ++;
  94. }
  95. $log_data[] = [
  96. 'user_id' => $this->auth->id,
  97. 'question_id' => $question['id'],
  98. 'is_right' => $is_right ? 1 : 0,
  99. 'player_id' => $this->auth->bind_player_id,
  100. 'createtime' => time(),
  101. 'createdate' => strtotime(date('Y-m-d')),
  102. ];
  103. }
  104. Db::startTrans();
  105. //答题日志
  106. $log_id = Db::name('user_question_log')->insertAll($log_data);
  107. if(!$log_id){
  108. Db::rollback();
  109. $this->error('答题失败');
  110. }
  111. if($is_right_number > 0){
  112. //给选手加分
  113. $rs = Db::name('vote_player')->where('id',$this->auth->bind_player_id)->setInc('score',$is_right_number);
  114. if($rs === false){
  115. Db::rollback();
  116. $this->error('答题失败');
  117. }
  118. }
  119. Db::commit();
  120. //今日答题次数,自增一次
  121. $count = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->incrby_expire(count($user_questions),86400);
  122. if($is_right_number > 0){
  123. //今日答对次数,自增一次
  124. RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->incrby_expire($is_right_number,86400);
  125. }
  126. //返回正确了几道题,剩余答题次数
  127. $result = [
  128. 'submit_number'=> count($user_questions),
  129. 'right_number' => $is_right_number,
  130. 'remain' => $exam_times_user_eday - $count,
  131. ];
  132. $this->success('答题完毕',$result);
  133. }
  134. /**
  135. * 试题
  136. * @param $question_id
  137. * @param $answer
  138. * @return bool
  139. */
  140. private function paperExam($question,$answer)
  141. {
  142. $is_right = false;
  143. if(empty($question)){
  144. return false;
  145. }
  146. switch ($question['kind']) {
  147. case 'JUDGE': // 判断题
  148. case 'SINGLE': // 单选题
  149. case 'MULTI': // 多选题
  150. // 答题正确
  151. if (strtoupper($answer) == $question['answer']) {
  152. $is_right = true;
  153. } else {
  154. $is_right = false;
  155. }
  156. break;
  157. case 'SHORT': // 简答题
  158. // 答案得分配置
  159. $answer_config = is_string($question['answer']) ? json_decode($question['answer'], true) : $question['answer'];
  160. $user_answers = $answer;
  161. foreach ($answer_config['config'] as $answer_item) {
  162. // 匹配答案关键词
  163. if (strpos($user_answers, $answer_item['answer']) !== false) {
  164. $is_right = true;
  165. break;
  166. }
  167. }
  168. break;
  169. }
  170. return $is_right;
  171. }
  172. }