Everyday.php
6.5 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
<?php
namespace app\mobile\controller;
use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\Question;
use app\mobile\model\QuestionAnswer;
/**
* 每日一练接口
* @ApiWeigh (95)
*/
class Everyday extends Api
{
protected $noNeedLogin = ['index'];
protected $noNeedRight = ['*'];
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = model('app\mobile\model\Everyday');
}
/**
* @ApiTitle (首页)
* @ApiSummary (首页)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599131811",
"data": [{
"id": 1, //每日一练ID
"title": "测试每日一练", //标题
"uptime": "今天", //上架时间
"do_num": 77 //做过人数
}]
})
*/
public function index()
{
$list = $this->model->where('uptime','<=',time())
->order('uptime desc')
->select();
foreach ($list as &$v) {
$v['uptime'] = date('Y-m-d',$v['uptime']) == date('Y-m-d') ? '今天' : date('m月d日',$v['uptime']);
$v->visible(['id','title','uptime','do_num']);
}
$this->success('成功',$list);
}
/**
* @ApiTitle (每日一练-题目详情)
* @ApiSummary (每日一练-题目详情)
* @ApiMethod (POST)
*
* @ApiParams (name="everyday_id", type="int", required=true, description="每日一练ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"total": 1, //题目总数
"list": [{ //题目列表
"id": 1, //题目ID
"title": "测定混凝土立方体抗压强度时,标准试件的尺寸是( )㎜。", //题目
"option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
"type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
"answer": "A", //答案
"note": "45678910", //笔记
"is_collect": "1" //是否收藏:0=否,1=是
}]
}
})
*/
public function questionList()
{
$user_id = $this->auth->id;
$everyday_id = $this->request->param('everyday_id');
empty($everyday_id) && $this->error('缺少必要参数');
$info = $this->model->get($everyday_id);
empty($info) && $this->error('每日一练信息不存在');
$list = Question::alias('q')
->join('mobile_question_note qn','q.id = qn.question_id and qn.user_id='.$user_id,'left')
->join('mobile_question_collect qc','q.id = qc.question_id and qn.user_id='.$user_id,'left')
->where('q.target_id',$everyday_id)
->where('q.target_type','4')
->field('
q.id,
q.title,
q.option,
q.type,
q.answer,
qn.content note,
if(qc.id > 0,1,0) is_collect
')->select();
$total = count($list);
// 删除原来的做题记录
QuestionAnswer::where('question_id','in',array_column($list, 'id'))
->where('user_id',$user_id)
->delete();
$this->success('成功',compact('total','list'));
}
/**
* @ApiTitle (每日一练-答题卡)
* @ApiSummary (每日一练-答题卡)
* @ApiMethod (POST)
*
* @ApiParams (name="everyday_id", type="int", required=true, description="每日一练ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599132508",
"data": {
"total": 1, // 题目数
"right_rate": 0, //正确率
"right_count": 0, //正确题数
"wrong_count": 0, //错误题数
"no_count": 1, //未做题数
"list": [{
"id": 1, //题目ID
"title": "测定混凝土立方体抗压强度时,标准试件的尺寸是( )㎜。", //题目
"option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
"type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
"answer": "A", //答案
"status": 0, //状态:0=未答,1=答对,2=答错
"get_score": null //得分
}]
}
})
*/
public function answerSheet()
{
$user_id = $this->auth->id;
$everyday_id = $this->request->param('everyday_id');
empty($everyday_id) && $this->error('缺少必要参数');
$info = $this->model->get($everyday_id);
empty($info) && $this->error('每日一练信息不存在');
$list = Question::alias('q')
->join('mobile_question_answer qa','q.id = qa.question_id and user_id='.$user_id,'left')
->where('q.target_id',$everyday_id)
->where('q.target_type','4')
->field('
q.id,
q.title,
q.option,
q.type,
q.answer,
if(qa.id > 0,if(qa.is_wrong="0",1,2),0) status,
qa.get_score
')->select();
// 题目数
$total = count($list);
// 得分
$myscore = array_sum(array_column($list, 'get_score'));
// 正确题数
$right_count = 0;
// 错误题数
$wrong_count = 0;
// 未做题数
$no_count = 0;
foreach ($list as $v) {
switch ($v['status']) {
case '0':
$no_count++;
break;
case '1':
$right_count++;
break;
case '2':
$wrong_count++;
break;
}
}
// 正确率
$right_rate = !empty($right_count) ? bcdiv($right_count,$total,4) * 100 : 0;
$this->success('成功',compact('total','right_rate','right_count','wrong_count','no_count','list'));
}
}