123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace addons\exam\controller;
- use addons\exam\enum\CommonStatus;
- use addons\exam\enum\ExamMode;
- use addons\exam\library\ExamService;
- use addons\exam\model\PaperModel;
- use addons\exam\model\QuestionModel;
- use addons\exam\model\UserModel;
- use app\admin\model\exam\CateModel;
- use app\admin\model\exam\GradeModel;
- use think\Request;
- /**
- * 试卷接口
- */
- class Paper extends Base
- {
- protected $noNeedLogin = ['index'];
- protected $noNeedRight = ['*'];
- protected $user;
- /**
- * 查询出分类下的试卷
- */
- public function index()
- {
- $cate_id = input('cate_id/d', '0');
- $sort = input('sort/s', '');
- $now = time();
- $query = PaperModel::with([
- 'cates' => function ($query) {
- $query->withField('id, name');
- }
- ])
- ->where('status', CommonStatus::NORMAL)
- ->where('is_only_room', 0)// 过滤仅考场使用的试卷
- ->whereRaw("((start_time = 0 and end_time = 0) or (start_time < {$now} and end_time > {$now}))");
- // 分类
- if ($cate_id) {
- $child_cate_ids = CateModel::getChildId($cate_id);
- array_push($child_cate_ids, $cate_id);
- $query->whereIn('cate_id', $child_cate_ids);
- }
- // 排序
- if ($sort && $sort != 'null') {
- $sort = explode('|', $sort);
- $field = $sort[0];
- $order_by = $sort[1];
- $field = in_array($field, ['join_count']) ? $field : 'join_count';
- $order_by = $order_by == 'desc' ? 'desc' : 'asc';
- $query->order("{$field} $order_by");
- }
- $list = $query->paginate();
- $this->success('', ['list' => $list]);
- }
- /**
- * 试卷取题接口
- */
- public function getExamQuestion()
- {
- $paper_id = input('paper_id/d', 0);
- $room_id = input('room_id/d', 0);
- // 验证是否需要绑定手机号
- // UserModel::isMustBindMobile($this->auth->getUser());
- // 预创建考场考试记录
- // $room_grade_id = ExamService::preRoomGrade($room_id, $this->auth->id);
- // 获取试卷题目
- $question_data = ExamService::getExamQuestion($paper_id, $room_id);
- // $this->success('', array_merge($question_data, ['room_grade_id' => $room_grade_id]));
- $this->success('', $question_data);
- }
- /**
- * 交卷
- */
- public function submit()
- {
- $request = Request::instance();
- $user_id = $this->auth->id;
- $paper_id = $request->post('paper_id/d', 0);
- $questions = $request->post('questions/a', []);
- $start_time = $request->post('start_time/d', time());
- $room_id = $request->post('room_id/d', 0);
- $room_grade_id = $request->post('room_grade_id/d', 0);
- if (!$user_id || !$paper_id || !$questions) {
- $this->error('提交数据有误' . $user_id);
- }
- // 考场考试
- if ($room_id) {
- if (!$room_grade_id) {
- $this->error('提交数据不合法');
- }
- // 考场考试
- $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);
- // 记录考场考试成绩
- $room_grade_log->allowField(true)->save(
- array_merge(
- $result,
- [
- // 'cate_id' => $paper['cate_id'],
- 'user_id' => $user_id,
- 'paper_id' => $paper_id,
- 'is_makeup' => $is_makeup,
- 'is_pre' => 0, // 提交成绩后不再为预创建标记
- ],
- [
- 'exam_mode' => ExamMode::ROOM,
- ]
- )
- );
- } else {
- $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);
- // 记录考试成绩
- GradeModel::create(array_merge(
- $result,
- [
- 'cate_id' => $paper['cate_id'],
- 'user_id' => $user_id,
- 'paper_id' => $paper_id,
- ],
- [
- // 'exam_mode' => ExamMode::PAPER,
- 'date' => date('Y-m-d'),
- ]), true);
- }
- return json($result);
- }
- /*
- * 查看错题
- * Robin
- * @param $ids
- * */
- public function error_ids($ids)
- {
- $questions = QuestionModel::whereIn('id', ($ids))->select();
- $this->success('', $questions);
- }
- }
|