Paper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  13. * 试卷接口
  14. */
  15. class Paper extends Base
  16. {
  17. protected $noNeedLogin = ['index'];
  18. protected $noNeedRight = ['*'];
  19. protected $user;
  20. /**
  21. * 查询出分类下的试卷
  22. */
  23. public function index()
  24. {
  25. $cate_id = input('cate_id/d', '0');
  26. $sort = input('sort/s', '');
  27. $now = time();
  28. $query = PaperModel::with([
  29. 'cates' => function ($query) {
  30. $query->withField('id, name');
  31. },
  32. ])
  33. ->where('status', CommonStatus::NORMAL)
  34. ->where('is_only_room', 0)// 过滤仅考场使用的试卷
  35. ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))");
  36. // 分类
  37. if ($cate_id) {
  38. $child_cate_ids = CateModel::getChildId($cate_id);
  39. array_push($child_cate_ids, $cate_id);
  40. $query->whereIn('cate_id', $child_cate_ids);
  41. }
  42. // 排序
  43. if ($sort && $sort != 'null') {
  44. $sort = explode('|', $sort);
  45. $field = $sort[0];
  46. $order_by = $sort[1];
  47. $field = in_array($field, ['join_count']) ? $field : 'join_count';
  48. $order_by = $order_by == 'desc' ? 'desc' : 'asc';
  49. $query->order("{$field} $order_by");
  50. }
  51. $list = $query->paginate();
  52. $this->success('', ['list' => $list]);
  53. }
  54. /**
  55. * 试卷取题接口
  56. */
  57. public function getExamQuestion()
  58. {
  59. $paper_id = input('paper_id/d', 0);
  60. $room_id = input('room_id/d', 0);
  61. // 验证是否需要绑定手机号
  62. UserModel::isMustBindMobile($this->auth->getUser());
  63. // 预创建考场考试记录
  64. $room_grade_id = ExamService::preRoomGrade($room_id, $this->auth->id);
  65. // 获取试卷题目
  66. $question_data = ExamService::getExamQuestion($paper_id, $room_id);
  67. // 标记题目是否已收藏
  68. $question_data['questions'] = QuestionModel::isCollected($this->auth->id, $question_data['questions']);
  69. $this->success('', array_merge($question_data, ['room_grade_id' => $room_grade_id]));
  70. }
  71. /**
  72. * 交卷
  73. */
  74. public function submit()
  75. {
  76. $request = Request::instance();
  77. $user_id = $this->auth->id;
  78. $paper_id = $request->post('paper_id/d', 0);
  79. $questions = $request->post('questions/a', []);
  80. $start_time = $request->post('start_time/d', time());
  81. $room_id = $request->post('room_id/d', 0);
  82. $room_grade_id = $request->post('room_grade_id/d', 0);
  83. if (!$user_id || !$paper_id || !$questions) {
  84. $this->error('提交数据有误' . $user_id);
  85. }
  86. // 考场考试
  87. if ($room_id) {
  88. if (!$room_grade_id) {
  89. $this->error('提交数据不合法');
  90. }
  91. // 考场考试
  92. $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
  93. // 记录考场考试成绩
  94. $room_grade_log->allowField(true)->save(
  95. array_merge(
  96. $result,
  97. [
  98. // 'cate_id' => $paper['cate_id'],
  99. 'user_id' => $user_id,
  100. 'paper_id' => $paper_id,
  101. 'is_makeup' => $is_makeup,
  102. 'is_pre' => 0, // 提交成绩后不再为预创建标记
  103. ],
  104. [
  105. 'exam_mode' => ExamMode::ROOM,
  106. ]
  107. )
  108. );
  109. } else {
  110. $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
  111. // 记录考试成绩
  112. GradeModel::create(array_merge(
  113. $result,
  114. [
  115. 'cate_id' => $paper['cate_id'],
  116. 'user_id' => $user_id,
  117. 'paper_id' => $paper_id,
  118. ],
  119. [
  120. // 'exam_mode' => ExamMode::PAPER,
  121. 'date' => date('Y-m-d'),
  122. ]), true);
  123. }
  124. return json($result);
  125. }
  126. /*
  127. * 查看错题
  128. * Robin
  129. * @param $ids
  130. * */
  131. public function error_ids($ids)
  132. {
  133. $questions = QuestionModel::whereIn('id', ($ids))->select();
  134. $this->success('', $questions);
  135. }
  136. }