Paper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // $this->success('', array_merge($question_data, ['room_grade_id' => $room_grade_id]));
  68. $this->success('', $question_data);
  69. }
  70. /**
  71. * 交卷
  72. */
  73. public function submit()
  74. {
  75. $request = Request::instance();
  76. $user_id = $this->auth->id;
  77. $paper_id = $request->post('paper_id/d', 0);
  78. $questions = $request->post('questions/a', []);
  79. $start_time = $request->post('start_time/d', time());
  80. $room_id = $request->post('room_id/d', 0);
  81. $room_grade_id = $request->post('room_grade_id/d', 0);
  82. if (!$user_id || !$paper_id || !$questions) {
  83. $this->error('提交数据有误' . $user_id);
  84. }
  85. // 考场考试
  86. if ($room_id) {
  87. if (!$room_grade_id) {
  88. $this->error('提交数据不合法');
  89. }
  90. // 考场考试
  91. $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
  92. // 记录考场考试成绩
  93. $room_grade_log->allowField(true)->save(
  94. array_merge(
  95. $result,
  96. [
  97. // 'cate_id' => $paper['cate_id'],
  98. 'user_id' => $user_id,
  99. 'paper_id' => $paper_id,
  100. 'is_makeup' => $is_makeup,
  101. 'is_pre' => 0, // 提交成绩后不再为预创建标记
  102. ],
  103. [
  104. 'exam_mode' => ExamMode::ROOM,
  105. ]
  106. )
  107. );
  108. } else {
  109. $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
  110. // 记录考试成绩
  111. GradeModel::create(array_merge(
  112. $result,
  113. [
  114. 'cate_id' => $paper['cate_id'],
  115. 'user_id' => $user_id,
  116. 'paper_id' => $paper_id,
  117. ],
  118. [
  119. // 'exam_mode' => ExamMode::PAPER,
  120. 'date' => date('Y-m-d'),
  121. ]), true);
  122. }
  123. return json($result);
  124. }
  125. /*
  126. * 查看错题
  127. * Robin
  128. * @param $ids
  129. * */
  130. public function error_ids($ids)
  131. {
  132. $questions = QuestionModel::whereIn('id', ($ids))->select();
  133. $this->success('', $questions);
  134. }
  135. }