Question.php 7.0 KB

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