Blame view

addons/exam/controller/Paper.php 8.49 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  <?php

  

  namespace addons\exam\controller;

  

  use addons\exam\enum\CommonStatus;

  use addons\exam\enum\ExamMode;

  use addons\exam\enum\UserScoreType;

  use addons\exam\library\CertService;

  use addons\exam\library\ExamService;

  use addons\exam\library\ScoreService;

  use addons\exam\model\CateModel;

  use addons\exam\model\GradeModel;

  use addons\exam\model\PaperModel;

  use addons\exam\model\PaperOrderModel;

  use addons\exam\model\QuestionModel;

  use addons\exam\model\RoomSignupModel;

  use addons\exam\model\UserModel;

  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(15, true);

          $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]));

      }

  

      /**

       * 交卷

       */

      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) {

              $result = ExamService::roomExam($user_id, $room_id, $room_grade_id, $questions, $start_time, $paper, $room, $is_makeup, $room_grade_log);

  

              // 不记录用户答案

              // if (getConfig('system_config', 'record_exam_open', 0) == 0) {

              //     unset($result['user_answers']);

              // }

  

              $sign_up   = RoomSignupModel::where('room_id', $room_id)->where('user_id', $user_id)->find();

              $user_name = $sign_up['name'] ?? '';

              // 成绩写入报名时的学校、班级

              $result['school_id']  = $sign_up['school_id'] ?? 0;

              $result['class_name'] = $sign_up['class_name'] ?? '';

  

              if (!empty($room['cert_config_id'])) {

                  // 生成证书

                  $cert = CertService::createCert($room['cert_config_id'], $room_id, $paper_id, $user_id, $user_name, $result['score']);

                  if ($cert) {

                      $result['cert'] = $cert;

                  } else {

                      $result['cert'] = '';

                  }

              }

  

              // 记录考场考试成绩

              $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,

                      ]

                  )

              );

  

              $result['grade_id'] = $room_grade_log['id'];

  

              // 试卷考试得积分

              $result['point'] = [

                  'get_point' => ScoreService::getScore($this->auth->id, UserScoreType::ROOM, $room_grade_log),

                  'type'      => UserScoreType::getDescription(UserScoreType::ROOM)

              ];

          } else {

              $result = ExamService::paperExam($user_id, $paper_id, $questions, $start_time, $paper);

  

              // 记录考试成绩

              $paper_grade_log = 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);

  

              $result['grade_id'] = $paper_grade_log['id'];

  

              // 试卷考试得积分

              $result['point'] = [

                  'get_point' => ScoreService::getScore($this->auth->id, UserScoreType::PAPER, $paper_grade_log),

                  'type'      => UserScoreType::getDescription(UserScoreType::PAPER)

              ];

          }

          return json($result);

      }

  

      /*

       * 查看错题

       * Robin

       * @param $ids

       */

      public function error_ids($ids)

      {

          $questions = QuestionModel::whereIn('id', ($ids))->select();

          $this->success('', $questions);

      }

  

      /**

       * 检查是否需要支付

       */

      public function checkPay()

      {

          $paper_id = input('paper_id/d', 0);

          $room_id  = input('room_id/d', 0);

  

          if (!$paper_id) {

              $this->error('考试试卷参数有误');

          }

          if (!$paper = PaperModel::get($paper_id)) {

              $this->error('考试试卷数据不存在');

          }

          if ($room_id) {

              $this->success('', ['status' => 1]);

          }

  

          // 检测题库是否需要付费

          $result = CateModel::checkPay($paper['cate_id'], $this->auth->id);

          if ($result['status'] == 2) {

              // 需要支付直接返回

              $this->success('', $result);

          }

          // dd($result);

  

          // 检测试卷是否需要付费

          $result = PaperModel::checkPay($paper, $this->auth->id, false, $room_id);

          $this->success('', $result);

      }

  

      /**

       * 创建考试订单

       */

      public function createOrder()

      {

          $paper_id = input('paper_id/d', 0);

          $room_id  = input('room_id/d', 0);

  

          if (!$paper_id) {

              $this->error('考试试卷参数有误');

          }

          if (!$paper = PaperModel::get($paper_id)) {

              $this->error('考试试卷数据不存在');

          }

  

          $result = PaperModel::checkPay($paper, $this->auth->id, false, $room_id);

          if ($result['status'] != 2) {

              $this->error('该考试试卷无须付费,请尝试重新进入');

          }

  

          if (PaperOrderModel::hasUsableOrder($paper_id, $this->auth->id)) {

              $this->error('您已付费,无须重复支付,请尝试重新进入');

          }

  

          $order   = PaperOrderModel::createOrder($paper['id'], $this->auth->id, $result['is_member'] ? $paper['member_price'] : $paper['price']);

          $payment = PaperOrderModel::createPayment($this->auth->id, $order['order_no'], $order['amount']);

  

          $this->success('', [

              'order'   => $order,

              'payment' => $payment

          ]);

      }

  }