Question.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 答题
  7. */
  8. class Question extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //获取10个题
  13. public function get_question(){
  14. //上次绑定选手的时间不是今天
  15. if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
  16. $this->error('先绑定单位再答题',null,2);
  17. }
  18. //获取10个随机题目
  19. $count = config('site.exam_times_user_eday');
  20. $lists = Db::name('exam_question')
  21. ->field('id,kind,title,options_json')
  22. ->where('is_material_child', 0)// 材料题子题不显示
  23. ->where('status', 'NORMAL')// 正常
  24. ->where('deletetime', NULL)
  25. ->orderRaw('rand()')->limit($count)->select();
  26. foreach($lists as $key => $val){
  27. $val['options_json'] = $this->getOptionsJsonAttr($val['options_json']);
  28. $lists[$key] = $val;
  29. }
  30. $this->success(1,$lists);
  31. }
  32. private function getOptionsJsonAttr($value)
  33. {
  34. if ($value = json_decode($value, true)) {
  35. $data = [];
  36. foreach ($value as $key => $row) {
  37. $arr['key'] = $key;
  38. $arr['value'] = $row;
  39. array_push($data, $arr);
  40. }
  41. return $data;
  42. }
  43. return [];
  44. }
  45. /**
  46. * 交卷
  47. */
  48. public function submit()
  49. {
  50. //上次绑定选手的时间不是今天
  51. if($this->auth->bind_player_date != strtotime(date('Y-m-d'))){
  52. $this->error('先绑定单位再答题',null,2);
  53. }
  54. //检查今日答题次数
  55. $exam_times_user_eday = config('site.exam_times_user_eday');
  56. $count = Db::name('user_question_log')->where('user_id',$this->auth->id)->where('createdate',strtotime(date('Y-m-d')))->count();
  57. if($count >= $exam_times_user_eday){
  58. $this->error('今日答题次数用完了,明天再来吧');
  59. }
  60. $question_id = input('question_id');
  61. $answer = input('answer');
  62. if (!$question_id || !$answer) {
  63. $this->error('提交数据有误');
  64. }
  65. $is_right = $this->paperExam($question_id,$answer);
  66. Db::startTrans();
  67. //答题日志
  68. $log = [
  69. 'user_id' => $this->auth->id,
  70. 'question_id' => $question_id,
  71. 'is_right' => $is_right ? 1 : 0,
  72. 'player_id' => $this->auth->bind_player_id,
  73. 'createtime' => time(),
  74. 'createdate' => strtotime(date('Y-m-d')),
  75. ];
  76. $log_id = Db::name('user_question_log')->insertGetId($log);
  77. if(!$log_id){
  78. Db::rollback();
  79. $this->error('答题失败');
  80. }
  81. if($is_right){
  82. $msg = '回答正确';
  83. //给选手加分
  84. }else{
  85. $msg = '回答错误';
  86. }
  87. Db::commit();
  88. $this->success($msg);
  89. }
  90. /**
  91. * 试题
  92. * @param $question_id
  93. * @param $answer
  94. * @return bool
  95. */
  96. private function paperExam($question_id,$answer)
  97. {
  98. $is_right = false;
  99. $question = Db::name('exam_question')->where('id', $question_id)->find();
  100. if(empty($question)){
  101. return false;
  102. }
  103. switch ($question['kind']) {
  104. case 'JUDGE': // 判断题
  105. case 'SINGLE': // 单选题
  106. case 'MULTI': // 多选题
  107. // 答题正确
  108. if (strtoupper($answer) == $question['answer']) {
  109. $is_right = true;
  110. } else {
  111. $is_right = false;
  112. }
  113. break;
  114. case 'SHORT': // 简答题
  115. // 答案得分配置
  116. $answer_config = is_string($question['answer']) ? json_decode($question['answer'], true) : $question['answer'];
  117. $user_answers = $answer;
  118. foreach ($answer_config['config'] as $answer_item) {
  119. // 匹配答案关键词
  120. if (strpos($user_answers, $answer_item['answer']) !== false) {
  121. $is_right = true;
  122. }
  123. }
  124. break;
  125. }
  126. return $is_right;
  127. }
  128. }