AnalysisListController.class.php
8.14 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
<?php
/**
* 【考试中心-后台】试题分析列表接口
* AnalysisListController.class.php
* @author : wanghuan
* @date : 2017-07-17
* @version: $Id$
*/
namespace Apicp\Controller\Topic;
use Common\Service\PaperService;
use Common\Service\SnapshotService;
use Common\Service\AnswerDetailService;
use Common\Service\RandomSnapshotService;
class AnalysisListController extends AbstractController
{
// 试卷顺序
const SORT_PAPER = 1;
// 正确率升序
const SORT_ASC = 2;
// 正确率降序
const SORT_DESC = 3;
/** @var PaperService */
protected $paper_serv;
/** @var SnapshotService */
protected $snapshot_serv;
/** @var AnswerDetailService */
protected $answer_detail_serv;
/** @var RandomSnapshotService */
protected $random_snapshot_serv;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化试题service
$this->paper_serv = new PaperService();
// 实例化试题快照service
$this->snapshot_serv = new SnapshotService();
// 实例化答卷详情service
$this->answer_detail_serv = new AnswerDetailService();
// 实例化随机试题快照service
$this->random_snapshot_serv = new RandomSnapshotService();
return true;
}
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 参数为空,返回提示"参数不能为空"
if (empty($params)) {
E('_ERR_PARAMS_NOT_NULL');
}
// 试卷id
$ep_id = intval($params['ep_id']);
// 试卷id为空,返回提示"试卷ID不能为空"
if (!$ep_id) {
E('_EMPTY_EP_ID');
}
// 根据试卷id获取试卷详情
$paper = $this->paper_serv->get($ep_id);
if (empty($paper)) {
E('_ERR_PAPER_NOT_FOUND');
}
// 试卷类型:1=自主,2=规则,3=随机
$ep_type = $paper['ep_type'];
// 判断分页参数是否为空,为空赋默认值
$page = !empty($params['page']) ? intval($params['page']) : PaperService::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : PaperService::DEFAULT_LIMIT;
// 分页
list($start, $limit) = page_limit($page, $limit);
// 查询条件:试卷id
$conds['a.ep_id'] = $ep_id;
// 分页
$page_option = array(
$start,
$limit
);
// 排序默认:试卷顺序
$sort = self::SORT_PAPER;
// 排序参数不为空,重新赋值
if (isset($params['sort']) && !empty($params['sort'])) {
$sort = $params['sort'];
}
$order_option = [];
// 试卷顺序
if (self::SORT_PAPER == $sort) {
// 随机抽题
if (PaperService::TOPIC_RANDOM == $ep_type) {
$order_option['order_defualt'] = 'ASC';
} else { // 自主或规则选题
$order_option['order_num'] = 'ASC';
}
} elseif (self::SORT_ASC == $sort) { // 正确率升序
$order_option = [
'ea_sort' => 'ASC',
'probability' => 'ASC'
];
} elseif (self::SORT_DESC == $sort) { // 正确率降序
$order_option = [
'ea_sort' => 'ASC',
'probability' => 'DESC'
];
}
// 随机抽题
if (PaperService::TOPIC_RANDOM == $ep_type) {
$res = $this->random_snapshot($ep_id, $conds, $page_option, $order_option);
} else { // 自主或规则选题
$res = $this->snapshot($conds, $page_option, $order_option);
}
$topics = $res['topics'];
$this->_result = [
'title' => $paper['ep_name'],
'count' => intval($paper['join_count']),
'total' => intval($res['total']),
'limit' => intval($limit),
'page' => intval($page),
'list' => $topics
];
}
/**
* 获取随机抽题数据
*
* @param $ep_id int 试卷id
* @param $conds array 查询条件
* @param $page_option array 分页参数
* @param $order_option array 排序参数
*
* @return array|bool array 试题
*/
protected function random_snapshot($ep_id, $conds, $page_option, $order_option)
{
// 初始化试卷题目列表
$topics = [];
// 查询条件
$detail_conds['a.ep_id'] = $ep_id;
// 分页
$detail_page_option = null;
// 排序
$detail_order_option['ead_id'] = 'ASC';
// 返回字段
$detail_fields = 'a.esr_id';
// 查询参加考试的题
$answer_data = $this->answer_detail_serv->count_answer_detail_list($detail_conds, $detail_page_option,
$detail_order_option, $detail_fields);
$er_ids = array_unique(array_column($answer_data, 'esr_id'));
// 如果没有参加开始返回空
if (empty($er_ids)) {
return [
'total' => 0,
'topics' => $topics
];
}
$conds['a.er_id'] = $er_ids;
// 获取记录总数
$total = $this->random_snapshot_serv->conds_random_snapshot_count($conds);
if ($total) {
// 题目返回字段
$fields = ' a.er_id,a.order_num,a.et_id,a.title,a.et_type';
// 获取试卷题目列表
$topics = $this->random_snapshot_serv->conds_random_snapshot_list($conds, $page_option, $order_option,
$fields);
foreach ($topics as $k => $topic) {
// 题目id
$topics[$k]['esr_id'] = $topic['er_id'];
// 序号
$topics[$k]['order_num'] = intval($topic['order_num']);
// 非主观判断题,计算正确率
if (!in_array($topic['et_type'],
[SnapshotService::TOPIC_TYPE_QUESTION, SnapshotService::TOPIC_TYPE_VOICE])
) {
$topics[$k]['rate'] = (round($topic['probability'] * 100, 2)) . '%';
} else { // 非主观判断题,不计算正确率
$topics[$k]['rate'] = '';
}
unset($topics[$k]['er_id'], $topics[$k]['et_id'], $topics[$k]['et_type']);
}
}
return [
'total' => $total,
'topics' => $topics
];
}
/**
* 获取自主/规则抽题数据
*
* @param $conds array 查询条件
* @param $page_option array 分页参数
* @param $order_option array 排序参数
*
* @return array|bool array 试题
*/
protected function snapshot($conds, $page_option, $order_option)
{
// 初始化试卷题目列表
$topics = [];
// 获取记录总数
$total = $this->snapshot_serv->conds_snapshot_count($conds);
if ($total) {
// 题目返回字段
$fields = 'a.es_id,a.order_num,a.et_id,a.title,a.et_type';
// 获取试卷题目列表
$topics = $this->snapshot_serv->conds_snapshot_list($conds, $page_option, $order_option, $fields);
foreach ($topics as $k => $topic) {
// 题目id
$topics[$k]['esr_id'] = $topic['es_id'];
// 序号
$topics[$k]['order_num'] = intval($topic['order_num']);
// 非主观判断题,计算正确率
if (!in_array($topic['et_type'],
[SnapshotService::TOPIC_TYPE_QUESTION, SnapshotService::TOPIC_TYPE_VOICE])
&& $topic['probability']
) {
$topics[$k]['rate'] = (round($topic['probability'] * 100, 2)) . '%';
} else { // 非主观判断题,不计算正确率
$topics[$k]['rate'] = '';
}
unset($topics[$k]['es_id'], $topics[$k]['et_id'], $topics[$k]['et_type']);
}
}
return [
'total' => $total,
'topics' => $topics
];
}
}