Exam.php
20.3 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Session;
/**
* 考试接口
*/
class Exam extends Api
{
protected $noNeedLogin = ['index','examtext'];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (考试题型分值接口)
* @ApiSummary (考试题型分值接口)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/index)
*
* @ApiParams (name="study_id", type="int", required=true, description="学习系统id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"pass_mark": //及格分数,
"dan_num": //单选题数量,
"dan_score": //单选题分值,
"duo_num": //多选题数量,
"duo_score": //多选题分值,
"pan_num": //判断题数量,
"pan_score": //判断题分值
"examdurationtime"://考试时长
}
})
*/
public function index()
{
$study_id = $this->request->param('study_id');
if(empty($study_id)){
$this->error('缺少必要参数');
}
$data = Db::name('study')
->where('id',$study_id)
->field('pass_mark,dan_num,dan_score,duo_num,duo_score,pan_num,pan_score,examdurationtime')
->find();
$data['zong'] = bcmul($data['dan_num'],$data['dan_score']) + bcmul($data['duo_num'],$data['duo_score']) + bcmul($data['pan_num'],$data['pan_score']);
$this->success('success',$data);
}
/**
* @ApiTitle (开始考试题型)
* @ApiSummary (开始考试题型)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/question)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="study_id", type="int", required=true, description="学习系统id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"exam_num"://第几次考试
"third_exam_id"://考试记录id
"dan": [
{
"id": //单选题id,
"content": //题目,
"type": //类型1文字选项2图片选项,
"option": [
{
"name": //选项名称,
"gender": //选项内容
},
],
"answer": "D"
}
],
"duo": [
{
"id": 1,
"content": "<p>1+1等于几</p>",
"type": 1,
"option": [
{
"name": "A",
"gender": "2"
},
],
"answer": "A,D"
}
],
"pan": [
{
"id": 1,
"content": "<p>1+1是等于2吗</p>",
"answer": "A",
"option": [
{
"name": "A",
"gender": "正确"
},
{
"name": "B",
"gender": "错误"
}
]
}
]
}
})
*/
public function question()
{
$user_id = $this->auth->id;
$qiniu = get_addon_config('qiniu')['cdnurl'];
$study_id = $this->request->param('study_id');
if(empty($study_id)){
$this->error('缺少必要参数');
}
//查看用户该科目是否已经考过3次了
$third_exam = Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->order('exam_num desc')
->find();
if(empty($third_exam)){
$exam['user_id'] = $user_id;
$exam['study_id'] = $study_id;
$exam['exam_num'] = 1;
$exam['createtime'] = time();
$exam['updatetime'] = time();
$info['third_exam_id'] = Db::name('third_exam')->insertGetId($exam);
}else{
$exam_times = Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->whereTime('createtime','month')
->count();
if($exam_times >= 3 && $user_id != 673){
// $this->error('您已经考过三次了');
$this->error('您本月已经考过三次了');
}
$study = Db::name('study')->where('id',$study_id)->find();
$where_exam = [
'user_id' => $user_id,
'study_id' => $study_id,
'score' => ['egt',$study['pass_mark']]
];
$third_exam_pass = Db::name('third_exam')
->where($where_exam)
->whereTime('createtime','year')
->find();
// if($third_exam_pass) $this->error('已通过该考试');
if($third_exam_pass) $this->error('今年已通过该考试');
$exam['user_id'] = $user_id;
$exam['study_id'] = $study_id;
$exam['exam_num'] = $third_exam['exam_num'] + 1;
$exam['createtime'] = time();
$exam['updatetime'] = time();
$info['third_exam_id'] =Db::name('third_exam')->insertGetId($exam);
}
//该学习科目的题型信息 单选题数量 多选题数量 判断题数量
$data = Db::name('study')
->where('id',$study_id)
->field('pass_mark,dan_num,dan_score,duo_num,duo_score,pan_num,pan_score')
->find();
//判断第几次考试
$is_have = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->order('id desc')
->find();
//添加用户考试记录详情表中
$answer['user_id'] = $user_id;
$answer['study_id'] = $study_id;
$answer['createtime'] = time();
$answer['updatetime'] = time();
if(empty($is_have)){
$info['exam_num'] = 1;
}else{
$info['exam_num'] = $is_have['study_exam_num'] + 1;
}
//单选题库
$info['dan'] = Db::name('dan')
->field('id,content,type,option,answer')
->where('study_id',$study_id)
->orderRaw('rand()')
->limit($data['dan_num'])
->select();
foreach ($info['dan'] as &$v){
$answer['type'] = 1;
$answer['question_id'] = $v['id'];
if(empty($is_have)){
$answer['study_exam_num'] = 1;
}else{
$answer['study_exam_num'] = $is_have['study_exam_num'] + 1;
}
Db::name('answer_detail')->insertGetId($answer);
$v['option'] = json_decode($v['option'],true);
if($v['type'] == 2){
foreach ($v['option'] as &$val){
$val['gender'] = $qiniu.$val['gender'];
}
}
}
//多选题库
$info['duo'] = Db::name('duo')
->field('id,content,type,option,answer')
->where('study_id',$study_id)
->orderRaw('rand()')
->limit($data['duo_num'])
->select();
foreach ($info['duo'] as &$v){
$answer['type'] = 2;
$answer['question_id'] = $v['id'];
if(empty($is_have)){
$answer['study_exam_num'] = 1;
}else{
$answer['study_exam_num'] = $is_have['study_exam_num'] + 1;
}
Db::name('answer_detail')->insertGetId($answer);
$v['option'] = json_decode($v['option'],true);
if($v['type'] == 2){
foreach ($v['option'] as &$val){
$val['gender'] = $qiniu.$val['gender'];
}
}
}
//判断题库
$info['pan'] = Db::name('pan')
->field('id,content,answer')
->where('study_id',$study_id)
->orderRaw('rand()')
->limit($data['pan_num'])
->select();
foreach ($info['pan'] as &$v){
$answer['type'] = 3;
$answer['question_id'] = $v['id'];
if(empty($is_have)){
$answer['study_exam_num'] = 1;
}else{
$answer['study_exam_num'] = $is_have['study_exam_num'] + 1;
}
Db::name('answer_detail')->insertGetId($answer);
$v['option'] = array([
'name'=>'A',
'gender'=>'正确'
],
[
'name'=>'B',
'gender'=>'错误'
]
);
}
$this->success('success',$info);
}
/**
* @ApiTitle (考试次数)
* @ApiSummary (考试次数)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/examnum)
*
* @ApiHeaders (name="token", type=string, required=true, description="请求的Token")
* @ApiParams (name="study_id", type="int", required=true, description="学习系统id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
}
})
*/
public function examnum()
{
$user_id = $this->auth->id;
$study_id = $this->request->param('study_id');
if(empty($study_id)){
$this->error('缺少必要参数');
}
//查看用户该科目是否已经考过3次了
$exam_times = Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->whereTime('createtime','month')
->count();
if($exam_times >= 3 && $user_id != 673){
// $this->error('您已经考过三次了');
$this->error('您本月已经考过三次了');
}else{
$this->success('success');
}
}
/**
* @ApiTitle (考试须知)
* @ApiSummary (考试须知)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/examtext)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"content"://内容
}
})
*/
public function examtext()
{
$data = Db::name('examtext')->where('id',1)->find();
$this->success('success',$data);
}
/**
* @ApiTitle (考试记录)
* @ApiSummary (考试记录)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/exam_record)
*
* @ApiHeaders (name="token", type=string, required=true, description="请求的Token")
* @ApiParams (name="study_id", type="int", required=true, description="学习系统id")
* @ApiParams (name="month", type="string", required=false, description="月份")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"examname": //考试名称,
"record": [
{
"id": //id,
"exam_num": //第几次考试,
"score": //考试成绩,
"createtime": //考试时间
"is_pass"://是否已过 1通过2未过
}
]
}
})
*/
public function exam_record()
{
$user_id = $this->auth->id;
$study_id = $this->request->param('study_id');
$month = $this->request->param('month');
if(empty($study_id)){
$this->error('缺少必要参数');
}
if(empty($month)){
$starttime = strtotime(date('Y-m'));
$time = [
$starttime,
strtotime('+1 month',$starttime)
];
}else{
$starttime = strtotime($month);
$time = [
$starttime,
strtotime('+1 month',$starttime)
];
}
$study = Db::name('study')
->where('id',$study_id)
->field('id,examname,pass_mark')
->find();
$data['examname'] = $study['examname'];
$data['record'] = Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('createtime','between',$time)
->order('exam_num')
->field('id,exam_num,score,createtime')
->select();
foreach ($data['record'] as &$v){
if($v['score'] >= $study['pass_mark']){
$v['is_pass'] = 1;
}else{
$v['is_pass'] = 2;
}
$v['createtime'] = date('Y-m-d',$v['createtime']);
}
$this->success('success',$data);
}
/**
* @ApiTitle (提交答案)
* @ApiSummary (提交答案)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/exam_answer)
*
* @ApiHeaders (name="token", type=string, required=true, description="请求的Token")
* @ApiParams (name="study_id", type="int", required=true, description="学习系统id")
* @ApiParams (name="question_id", type="int", required=true, description="题目id")
* @ApiParams (name="type", type="int", required=true, description="类型1单选2多选3判断")
* @ApiParams (name="answer", type="string", required=true, description="用户答案")
* @ApiParams (name="study_exam_num", type="string", required=true, description="用户第几次考试")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
}
]
}
})
*/
public function exam_answer()
{
$user_id = $this->auth->id;
$study_id = $this->request->param('study_id');
$question_id = $this->request->param('question_id/a');
$type = $this->request->param('type');
$answer = $this->request->param('answer');
// 题目ID过滤
$question_id = implode(',',array_filter($question_id));
$study_exam_num = $this->request->param('study_exam_num');
if(empty($study_id) || empty($question_id) || empty($type) || empty($study_exam_num)){
$this->error('缺少必要参数');
}
//单选题
if($type == 1){
$this->judge($user_id,$study_id,1,$question_id,$study_exam_num,$answer);
}elseif ($type == 2){
$this->judge($user_id,$study_id,2,$question_id,$study_exam_num,$answer);
}elseif ($type == 3){
$this->judge($user_id,$study_id,3,$question_id,$study_exam_num,$answer);
}else{
$this->error('类型错误');
}
}
//判断用户回答是否正确以及得多少分
public function judge($user_id,$study_id,$type,$question_id,$study_exam_num,$answer){
$study = Db::name('study')->where('id',$study_id)->find();
if($type == 1){
//查询出该条题目详情
$data = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('type',1)
->where('question_id',$question_id)
->where('study_exam_num',$study_exam_num)
->find();
if(empty($data)){
$this->error('参数有误');
}else{
//查询该题的正确答案
$correct = Db::name('dan')
->where('id',$question_id)
->value('answer');
//判断用户是否回答正确
if($correct == $answer){
Db::name('answer_detail')->where('id',$data['id'])->update(['answer'=>$answer,'score'=>$study['dan_score']]);
}else{
Db::name('answer_detail')->where('id',$data['id'])->update(['answer'=>$answer,'score'=>0]);
}
//算出总得分
$score = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('study_exam_num',$study_exam_num)
->field('sum(score) as num')
->find();
Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('exam_num',$study_exam_num)
->update(['score'=>$score['num']]);
}
}elseif ($type == 2){
//查询出该条题目详情
$data = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('type',2)
->where('question_id',$question_id)
->where('study_exam_num',$study_exam_num)
->find();
if(empty($data)){
$this->error('参数有误');
}else{
//查询该题的正确答案
$correct = Db::name('duo')
->where('id',$question_id)
->value('answer');
if(!empty($correct)){
$correct = str_split(str_replace([',',',',' '],'',$correct));
}
$answer1 = [];
if(!empty($answer)){
$answer1 = explode(',',$answer);
}
$result = count($answer1) > count($correct) ? array_diff($answer1,$correct) : array_diff($correct,$answer1);
//判断用户是否回答正确
if(empty($result)){
//回答正确
Db::name('answer_detail')->where('id',$data['id'])->update(['answer'=>$answer,'score'=>$study['duo_score']]);
}else{
//回答错误
Db::name('answer_detail')->where('id',$data['id'])->update(['answer'=>$answer,'score'=>0]);
}
//算出总得分
$score = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('study_exam_num',$study_exam_num)
->field('sum(score) as num')
->find();
Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('exam_num',$study_exam_num)
->update(['score'=>$score['num']]);
}
}elseif ($type == 3){
//查询出该条题目详情
$data = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('type',3)
->where('question_id',$question_id)
->where('study_exam_num',$study_exam_num)
->find();
if(empty($data)){
$this->error('参数有误');
}else{
//查询该题的正确答案
$correct = Db::name('pan')
->where('id',$question_id)
->value('answer');
//判断用户是否回答正确
if($correct == $answer){
Db::name('answer_detail')->where('id',$data['id'])->update(['answer'=>$answer,'score'=>$study['pan_score']]);
}else{
Db::name('answer_detail')->where('id',$data['id'])->update(['answer'=>$answer,'score'=>0]);
}
//算出总得分
$score = Db::name('answer_detail')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('study_exam_num',$study_exam_num)
->field('sum(score) as num')
->find();
Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$study_id)
->where('exam_num',$study_exam_num)
->update(['score'=>$score['num']]);
}
}
}
/**
* @ApiTitle (交卷)
* @ApiSummary (交卷)
* @ApiMethod (POST)
* @ApiRoute (/api/exam/tijiao)
*
* @ApiHeaders (name="token", type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="inter", required=true, description="考试记录id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"num"://考试机会
"score"://分数
}
]
}
})
*/
public function tijiao()
{
$id = $this->request->param('id');
if(empty($id)){
$this->error('缺少必要参数');
}
$data = Db::name('third_exam')
->where('id',$id)
->find();
$exam_times = Db::name('third_exam')
->where('user_id',$data['user_id'])
->where('study_id',$data['study_id'])
->whereTime('createtime','month')
->count();
$info['num'] = 3 - $exam_times;
$info['score'] = $data['score'];
$this->success('success',$info);
}
}