Paper.php 4.6 KB

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