Paper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace addons\exam\controller;
  3. use addons\exam\enum\CommonStatus;
  4. use addons\exam\enum\ExamMode;
  5. use addons\exam\library\ExamService;
  6. use addons\exam\model\PaperModel;
  7. use addons\exam\model\QuestionModel;
  8. use addons\exam\model\UserModel;
  9. use app\admin\model\exam\CateModel;
  10. use app\admin\model\exam\GradeModel;
  11. use think\Request;
  12. use think\Db;
  13. /**
  14. * 试卷接口
  15. */
  16. class Paper extends Base
  17. {
  18. protected $noNeedLogin = ['index'];
  19. protected $noNeedRight = ['*'];
  20. protected $user;
  21. /**
  22. * 查询出分类下的试卷
  23. */
  24. public function index()
  25. {
  26. $cate_id = input('cate_id/d', '0');
  27. $sort = input('sort/s', '');
  28. $now = time();
  29. $query = PaperModel::with([
  30. 'cates' => function ($query) {
  31. $query->withField('id, name');
  32. }
  33. ])
  34. ->where('status', CommonStatus::NORMAL)
  35. ->where('is_only_room', 0)// 过滤仅考场使用的试卷
  36. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))");
  37. // 分类
  38. if ($cate_id) {
  39. $child_cate_ids = CateModel::getChildId($cate_id);
  40. array_push($child_cate_ids, $cate_id);
  41. $query->whereIn('cate_id', $child_cate_ids);
  42. }
  43. // 排序
  44. if ($sort && $sort != 'null') {
  45. $sort = explode('|', $sort);
  46. $field = $sort[0];
  47. $order_by = $sort[1];
  48. $field = in_array($field, ['join_count']) ? $field : 'join_count';
  49. $order_by = $order_by == 'desc' ? 'desc' : 'asc';
  50. $query->order("{$field} $order_by");
  51. }
  52. $list = $query->paginate();
  53. $this->success('', ['list' => $list]);
  54. }
  55. /**
  56. * 试卷取题接口
  57. */
  58. public function getExamQuestion()
  59. {
  60. $paper_id = input('paper_id/d', 0);
  61. $room_id = input('room_id/d', 0);
  62. // 验证是否需要绑定手机号
  63. // UserModel::isMustBindMobile($this->auth->getUser());
  64. // 预创建考场考试记录
  65. // $room_grade_id = ExamService::preRoomGrade($room_id, $this->auth->id);
  66. // 获取试卷题目
  67. $question_data = ExamService::getExamQuestion($paper_id, $room_id);
  68. // $this->success('', array_merge($question_data, ['room_grade_id' => $room_grade_id]));
  69. $this->success('', $question_data);
  70. }
  71. //开始考试接口
  72. public function startpaper(){
  73. $paper_id = input('paper_id/d', 0);
  74. $user_id = $this->auth->id;
  75. //检查考试状态
  76. $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find();
  77. if($check){
  78. $this->success('您有其他考试正在进行中,即将继续考试',0);//直接给成功,数据返回0,前端跳转
  79. }
  80. //检查试卷
  81. $paper = PaperModel::get($paper_id);
  82. switch (true) {
  83. case !$paper:
  84. $this->error('试卷信息不存在');
  85. case $paper->status != 'NORMAL':
  86. $this->error('试卷未开启');
  87. case $paper->mode == 'RANDOM' && !$paper->configs:
  88. $this->error('试卷未配置');
  89. }
  90. //时间限制
  91. if ($paper['start_time'] > 0 && $paper['start_time'] > time()) {
  92. $this->error('该试卷未开始,不能参与考试');
  93. }
  94. if ($paper['end_time'] > 0 && $paper['end_time'] < time()) {
  95. $this->error('该试卷已结束,不能参与考试');
  96. }
  97. //考试资格
  98. if(!in_array($user_id,$paper['user_ids'])){
  99. $this->error('您不能参加该考试');
  100. }
  101. //次数限制
  102. if ($paper['limit_count'] > 0){
  103. $my_count = Db::name('exam_grade')->where('user_id', $user_id)->where('paper_id', $paper_id)->count();
  104. if($my_count >= $paper['limit_count']) {
  105. $this->error('该试卷您的考试次数已达上限');
  106. }
  107. }
  108. //记录为已开始,计划任务倒计时之后 自动结束
  109. $data = [
  110. 'user_id' => $this->auth->id,
  111. 'paper_id' => $paper_id,
  112. 'start_time' => time(),
  113. 'status' => 1,
  114. 'limit_time' => $paper['limit_time'], //限时N秒
  115. 'last_time' => $paper['limit_time'] > 0 ? (time() + $paper['limit_time']) : 0, //最后限制交卷时间,时间戳
  116. ];
  117. $grade_id = Db::name('exam_grade')->insertGetId($data);
  118. $this->success('',$grade_id);
  119. }
  120. //进行中考试
  121. public function half_examing(){
  122. $user_id = $this->auth->id;
  123. $check = Db::name('exam_grade')->where('user_id', $user_id)->where('status',1)->find();
  124. if(empty($check)){
  125. $this->error('您没有进行中的考试');
  126. }
  127. $paper_id = $check['paper_id'];
  128. // 获取试卷题目
  129. $question_data = ExamService::getExamQuestion($paper_id, 0);
  130. $question_data['paper']['limit_time'] = $check['last_time'] - time();//倒计时秒数
  131. $this->success('', $question_data);
  132. }
  133. /**
  134. * 交卷
  135. */
  136. public function submit()
  137. {
  138. $request = Request::instance();
  139. $user_id = $this->auth->id;
  140. $paper_id = $request->post('paper_id/d', 0);
  141. $questions = $request->post('questions/a', []);
  142. $start_time = $request->post('start_time/d', time());
  143. $room_id = $request->post('room_id/d', 0);
  144. $room_grade_id = $request->post('room_grade_id/d', 0);
  145. if (!$user_id || !$paper_id || !$questions) {
  146. $this->error('提交数据有误');
  147. }
  148. $check = Db::name('exam_grade')->where('status',1)->where('user_id',$user_id)->where('paper_id',$paper_id)->find();
  149. if(!$check){
  150. $this->error('提交数据有误');
  151. }
  152. $grade_id = $check['id'];
  153. $start_time = $check['start_time'];
  154. // 考场考试
  155. if ($room_id) {
  156. if (!$room_grade_id) {
  157. $this->error('提交数据不合法');
  158. }
  159. // 考场考试
  160. $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
  161. // 记录考场考试成绩
  162. $room_grade_log->allowField(true)->save(
  163. array_merge(
  164. $result,
  165. [
  166. // 'cate_id' => $paper['cate_id'],
  167. 'user_id' => $user_id,
  168. 'paper_id' => $paper_id,
  169. 'is_makeup' => $is_makeup,
  170. 'is_pre' => 0, // 提交成绩后不再为预创建标记
  171. ],
  172. [
  173. 'exam_mode' => ExamMode::ROOM,
  174. ]
  175. )
  176. );
  177. } else {
  178. $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
  179. // 记录考试成绩
  180. /*GradeModel::create(array_merge(
  181. $result,
  182. [
  183. 'cate_id' => $paper['cate_id'],
  184. 'user_id' => $user_id,
  185. 'paper_id' => $paper_id,
  186. ],
  187. [
  188. // 'exam_mode' => ExamMode::PAPER,
  189. 'date' => date('Y-m-d'),
  190. ]), true);*/
  191. $update = array_merge(
  192. $result,
  193. [
  194. 'cate_id' => $paper['cate_id'],
  195. 'updatetime' => time(),
  196. 'date' => date('Y-m-d'),
  197. 'status' => 2,
  198. 'finish_time' => time(),
  199. ]);
  200. unset($update['pass_score']);
  201. unset($update['start_time']);
  202. $rs = Db::name('exam_grade')->where('id',$grade_id)->update($update);
  203. if($rs === false){
  204. $this->error('交卷失败');
  205. }
  206. }
  207. $result['nickname'] = $this->auth->nickname;
  208. $this->success('',$result);
  209. // return json($result);
  210. }
  211. /*
  212. * 查看错题
  213. * Robin
  214. * @param $ids
  215. * */
  216. public function error_ids($ids)
  217. {
  218. $questions = QuestionModel::whereIn('id', ($ids))->select();
  219. $this->success('', $questions);
  220. }
  221. }