Exam.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\rexam\Time;
  4. use app\common\controller\Api;
  5. use think\Db;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use think\Session;
  10. /**
  11. * 考试接口
  12. */
  13. class Exam extends Api
  14. {
  15. protected $noNeedRight = '*';
  16. protected $noNeedLogin = [''];
  17. public $one = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\rexam\Rexam;
  22. }
  23. /**
  24. * 考试id
  25. */
  26. protected function check_id(){
  27. $param = input();
  28. if(empty($param['id'])){
  29. $this->error('参数错误');
  30. }
  31. $this->one = $this->model->get(['id' => $param['id']]);
  32. if(empty($this->one)){
  33. $this->error('未查询到对应考试');
  34. }
  35. return $param['id'];
  36. }
  37. protected function build(){
  38. $answer_model = new \app\admin\model\rexam\Answer;
  39. $question_model = new \app\admin\model\rexam\Question;
  40. $where = array();
  41. if ($this->one['areadata'] == 2) {
  42. $where['id'] = array('in', $this->one['rexam_question_ids']);
  43. } elseif ($this->one['areadata'] == 1) {
  44. $where['rexam_subject_id'] = array('in', $this->one['rexam_subject_ids']);
  45. } else {
  46. $where['id'] = array('gt', 0);
  47. }
  48. $temp_question_list = $question_model->field('id')->where($where)->select();
  49. $question_count = count($temp_question_list); //获取总记录数
  50. if ($question_count < $this->one['question_count']) {
  51. $this->error('题目数量不足,请联系管理员');
  52. }
  53. $i = 0;
  54. $question_list = array();
  55. while ($i < $this->one['question_count']) {
  56. $rund_question = $temp_question_list[rand(0, $question_count - 1)]['id'];
  57. if (!in_array($rund_question, $question_list)) {
  58. $question_list[] = $rund_question;
  59. $i++;
  60. }
  61. }
  62. Db::startTrans();
  63. try {
  64. $data = array();
  65. $data['user_id'] = $this->auth->id;
  66. $data['rexam_id'] = $this->one['id'];
  67. $data['rexamquestion_ids'] = implode(',', $question_list);
  68. $data['question_count'] = $this->one['question_count'];
  69. $data['begintime'] = time();
  70. $end_time = Time::where([
  71. 'rexam_id' => $this->one['id'],
  72. 'user_id' => $this->auth->id,
  73. ])->value('end_time');
  74. if (empty($end_time)) {
  75. $end_time = date('Y-m-d H:i:s', $data['begintime'] + $this->one['limitall']);
  76. $time_model = new Time();
  77. $time_model->rexam_id = $this->one['id'];
  78. $time_model->user_id = $this->auth->id;
  79. $time_model->end_time = $end_time;
  80. $time_model->save();
  81. }
  82. $user_answer = Db::name('rexam_answer')->where(['user_id' => $this->auth->id, 'rexam_id' => $this->one['id']])->find();
  83. if (empty($user_answer)) {
  84. $answer_model->create($data);
  85. }
  86. // 获取题目
  87. $question = $this->question($this->one['id'], $data['rexamquestion_ids']);
  88. if ($question) {
  89. Db::commit();
  90. $data['question'] = $question;
  91. // 计算结束时间
  92. if ($this->one['limitdata']) {
  93. $data['endtime'] = $end_time;
  94. }
  95. }
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. return $data;
  101. }
  102. /**
  103. * 答题
  104. */
  105. public function go(){
  106. $rid = $this->check_id();
  107. $current_time = time();
  108. if ($this->one['starttime'] >= $current_time) {
  109. $this->error('开始时间是:' . datetime($this->one['starttime'], 'Y-m-d H:i:s'));
  110. } elseif ($this->one['expiretime'] <= $current_time) {
  111. $this->error('考试已截止');
  112. }
  113. $answer_model = new \app\admin\model\rexam\Answer;
  114. $answer = $answer_model->where(['user_id' => $this->auth->id, 'rexam_id' => $rid])->find();
  115. if ($answer) {
  116. if ($answer['overtime']) {
  117. $this->error('您已参与过考试');
  118. }
  119. }
  120. $answer_one = array();
  121. $answer = $this->build();
  122. $question = explode(',', $answer['rexamquestion_ids']);
  123. if (empty($answer['answerjson'])) {
  124. foreach ($question as $vo){
  125. $answer_one[$vo] = ['css' => '', 'value' => ''];
  126. }
  127. } else {
  128. $answerarr = json_decode($answer['answerjson'], true);
  129. foreach ($answerarr as $vo) {
  130. $answer_one[$vo['qid']] = ['css' => 'done', 'value' => $vo['ans']];
  131. }
  132. }
  133. $this->success('', $answer);
  134. }
  135. /**
  136. * 获取题目
  137. *
  138. * @return void
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. * @throws \think\exception\DbException
  142. */
  143. public function question($ana = '', $rexamquestion_ids)
  144. {
  145. $rexamquestion_ids = explode(',', $rexamquestion_ids);
  146. $question_model = new \app\admin\model\rexam\Question();
  147. // $questions = \app\admin\model\rexam\Question::where(['rexam_subject_id' => $this->one['rexam_subject_ids']])->select();
  148. $questions = \app\admin\model\rexam\Question::where(['rexam_subject_id' => $this->one['rexam_subject_ids'], 'id' => ['in', $rexamquestion_ids]])->select();
  149. shuffle($questions);
  150. $new_questions = array_rand($questions, 10);
  151. $new_questions_arr = [];
  152. foreach ($new_questions as $key => $question) {
  153. $new_questions_arr[$key] = $questions[$question];
  154. }
  155. $data = [];
  156. foreach ($new_questions_arr as $key => $question) {
  157. $question_one = $question_model->load($question['id'], $this->one['randomdata']);
  158. $limit = array();
  159. // if ($this->one['limitdata']) {
  160. // $question_limit = Session::get('rexam_' . $this->one['id']);
  161. // if ($this->one['limitall']) {
  162. // if (empty($question_limit['limitall'])) {
  163. // $question_limit['limitall'] = time() + $this->one['limitall'];
  164. // $question_limit['allcount'] = $this->one['limitall'];
  165. // } else {
  166. // $question_limit['allcount'] = $question_limit['limitall'] - time();
  167. // if ($question_limit['allcount'] < 0) {
  168. // $question_limit['allcount'] = 0;
  169. // }
  170. // }
  171. // }
  172. //
  173. // if ($this->one['limitone']) {
  174. // if (empty($question_limit['limitone' . $question['id']])) {
  175. // $question_limit['limitone' . $question['id']] = time() + $this->one['limitone'];
  176. // $question_limit['onecount' . $question['id']] = $this->one['limitone'];
  177. // } else {
  178. // $question_limit['onecount' . $question['id']] = $question_limit['limitone' . $question['id']] - time();
  179. // if($question_limit['onecount' . $question['id']] < 0){
  180. // $question_limit['onecount' . $question['id']] = 0;
  181. // }
  182. // }
  183. // }
  184. //
  185. // Session::set('rexam_' . $this->one['id'], $question_limit);
  186. // $limit = ['allcount' => $question_limit['allcount'], 'onecount' => $question_limit['onecount' . $question['id']], 'limitall' => $this->one['limitall'], 'limitone' => $this->one['limitone']];
  187. // }
  188. $data[$key]['question'] = $question_one;
  189. $data[$key]['limit'] = $limit;
  190. }
  191. return $data;
  192. }
  193. }