Question.php 5.2 KB

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