Question.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $count = config('site.exam_times_user_eday');
  34. $lists = Db::name('exam_question')
  35. ->field('id,kind,title,options_json')
  36. ->where('is_material_child', 0)// 材料题子题不显示
  37. ->where('status', 'NORMAL')// 正常
  38. ->where('deletetime', NULL)
  39. ->orderRaw('rand()')->limit($today_my_question)->select();
  40. foreach($lists as $key => $val){
  41. $val['options_json'] = $this->getOptionsJsonAttr($val['options_json']);
  42. $lists[$key] = $val;
  43. }
  44. $this->success(1,$lists);
  45. }
  46. private function getOptionsJsonAttr($value)
  47. {
  48. if ($value = json_decode($value, true)) {
  49. $data = [];
  50. foreach ($value as $key => $row) {
  51. $arr['key'] = $key;
  52. $arr['value'] = $row;
  53. array_push($data, $arr);
  54. }
  55. return $data;
  56. }
  57. return [];
  58. }
  59. /**
  60. * 答题
  61. */
  62. public function submit()
  63. {
  64. $this->error('接口作废');
  65. if(!$this->apiLimit('操作太快了,休息一下吧'));
  66. //上次绑定选手的时间不是今天
  67. if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
  68. $this->error('先绑定单位再答题',null,2);
  69. }
  70. //检查今日答题次数
  71. $exam_times_user_eday = config('site.exam_times_user_eday');
  72. // $count = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$this->auth->id)->count();
  73. $count = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->get();
  74. if($count >= $exam_times_user_eday){
  75. $this->error('今日答题次数用完了,明天再来吧');
  76. }
  77. $question_id = input('question_id');
  78. $answer = input('answer');
  79. if (!$question_id || !$answer) {
  80. $this->error('提交数据有误');
  81. }
  82. $is_right = $this->paperExam($question_id,$answer);
  83. Db::startTrans();
  84. //答题日志
  85. $log = [
  86. 'user_id' => $this->auth->id,
  87. 'question_id' => $question_id,
  88. 'is_right' => $is_right ? 1 : 0,
  89. 'player_id' => $this->auth->bind_player_id,
  90. 'createtime' => time(),
  91. 'createdate' => strtotime(date('Y-m-d')),
  92. ];
  93. $log_id = Db::name('user_question_log')->insertGetId($log);
  94. if(!$log_id){
  95. Db::rollback();
  96. $this->error('答题失败');
  97. }
  98. if($is_right){
  99. //给选手加分
  100. $rs = Db::name('vote_player')->where('id',$this->auth->bind_player_id)->setInc('score');
  101. if($rs === false){
  102. Db::rollback();
  103. $this->error('答题失败');
  104. }
  105. }
  106. Db::commit();
  107. //今日答题次数,自增一次
  108. RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->incr_expire(86400);
  109. if($is_right){
  110. //今日答对次数,自增一次
  111. RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->incr_expire(86400);
  112. $msg = '回答正确';
  113. }else{
  114. //
  115. $msg = '回答错误';
  116. }
  117. $this->success($msg);
  118. }
  119. /**
  120. * 试题
  121. * @param $question_id
  122. * @param $answer
  123. * @return bool
  124. */
  125. private function paperExam($question_id,$answer)
  126. {
  127. $is_right = false;
  128. $question = Db::name('exam_question')->where('id', $question_id)->find();
  129. if(empty($question)){
  130. return false;
  131. }
  132. switch ($question['kind']) {
  133. case 'JUDGE': // 判断题
  134. case 'SINGLE': // 单选题
  135. case 'MULTI': // 多选题
  136. // 答题正确
  137. if (strtoupper($answer) == $question['answer']) {
  138. $is_right = true;
  139. } else {
  140. $is_right = false;
  141. }
  142. break;
  143. case 'SHORT': // 简答题
  144. // 答案得分配置
  145. $answer_config = is_string($question['answer']) ? json_decode($question['answer'], true) : $question['answer'];
  146. $user_answers = $answer;
  147. foreach ($answer_config['config'] as $answer_item) {
  148. // 匹配答案关键词
  149. if (strpos($user_answers, $answer_item['answer']) !== false) {
  150. $is_right = true;
  151. break;
  152. }
  153. }
  154. break;
  155. }
  156. return $is_right;
  157. }
  158. }