TestJoinListController.class.php
6.94 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
<?php
/**
* 【考试中心-后台】获取测评考试统计已参与详情列表接口
* TestJoinListController.class.php
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-05-23 16:31:25
* @version $Id$
*/
namespace Apicp\Controller\Answer;
use Common\Common\User;
use Common\Service\PaperService;
use Common\Service\RightService;
use Common\Service\AnswerService;
class TestJoinListController extends AbstractController
{
// 成绩升序
const SORT_ASC = 2;
/** @var PaperService */
protected $paper_serv;
/** @var RightService */
protected $right_serv;
/** @var AnswerService */
protected $answer_serv;
public function before_action($action = '')
{
if (parent::before_action($action) === false) {
return false;
}
// 实例化试卷service
$this->paper_serv = new PaperService();
// 实例化权限service
$this->right_serv = new RightService();
// 实例化答卷service
$this->answer_serv = new AnswerService();
// 用户信息初始化
$this->user = User::instance();
return true;
}
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 参数为空,返回提示"参数不能为空"
if (empty($params)) {
E('_ERR_PARAMS_NOT_NULL');
}
// 获取试卷ID
$ep_id = rintval($params['ep_id']);
if (empty($ep_id)) {
E('_EMPTY_EP_ID');
}
// 试卷详情
$paper = $this->paper_serv->get($ep_id);
// 试卷不存在
if (empty($paper)) {
E('_ERR_PAPER_NOT_FOUND');
}
// 试卷类型不是测评试卷
if ($paper['paper_type'] != PaperService::EVALUATION_PAPER_TYPE) {
E('_ERR_PAPER_TYPE_TEST');
}
// 获取未参加人数
$un_total = $this->answer_serv->get_un_total($ep_id, $paper, $params);
// 默认值
$page = !empty($params['page']) ? intval($params['page']) : PaperService::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : PaperService::DEFAULT_LIMIT_ADMIN;
// 分页
list($start, $limit) = page_limit($page, $limit);
$page_option = [$start, $limit];
// 【排序】
$sort = $params['sort'];
// 成绩升序
if (self::SORT_ASC == $sort) {
$order_by = [
'my_score' => 'ASC',
'my_time' => 'ASC',
'created' => 'ASC'
];
} else { // 成绩降序
$order_by = array(
'my_score' => 'DESC',
'my_time' => 'ASC',
'created' => 'ASC'
);
}
// 组装查询条件
$conds = $this->answer_serv->get_conditions($paper, $params);
// 【考试完成时间】
if (isset($params['end_time']) && !empty($params['end_time']) && is_array($params['end_time'])) {
$end_time = $params['end_time'];
$end_time1 = $end_time[0];
$end_time2 = $end_time[1];
if ($end_time1 && $end_time2) {
$conds['my_end_time>=?'] = $end_time1;
$conds['my_end_time<=?'] = $end_time2;
}
}
// 根据最高分记录去查
$conds['is_score_top'] = AnswerService::IS_SCORE_TOP_TRUE;
// 统计参与的人数
$total = $this->answer_serv->count_by_where($conds);
$join = [];
if ($total) {
// 参与这场考试的人员的考试信息
$join = $this->answer_serv->list_by_where($conds, $page_option, $order_by);
if (!empty($join)) {
// 参与考试的所有人的UID
$uids = array_column($join, 'uid');
// 参与考试的人员的详细信息列表
$userlist = $this->answer_serv->getUser($uids);
// 格式化返回字段信息
foreach ($join as $key => &$val) {
// 获取用户所在的所有部门
$dpNames = '';
if ($userlist[$val['uid']]['dpName']) {
$res_dpNames = array_column($userlist[$val['uid']]['dpName'], 'dpName');
if (!empty($res_dpNames)) {
$dpNames = implode(';', $res_dpNames);
}
}
$val['ranking'] = intval($key + 1); // 排序
$val['username'] = $userlist[$val['uid']]['memUsername']; // 用户名
$val['dpName'] = $dpNames; // 部门
$val['memJob'] = $userlist[$val['uid']]['memJob'];
$val['memRole'] = $userlist[$val['uid']]['memRole'];
$val['memTag'] = $userlist[$val['uid']]['memTag'];
$val['begin_time'] = $val['my_begin_time']; // 开始时间
// 查询补考次数条件
$join_answer_conds = [
'uid' => $val['uid'],
'ep_id' => $ep_id,
'is_makeup' => 1, // 是补考
];
// 补考次数
$val['makeup_num'] = $this->answer_serv->count_by_conds($join_answer_conds);;
$val['end_time'] = $val['my_begin_time'] + $val['my_time']; // 结束时间
$val['my_is_pass'] = intval($val['my_is_pass']); // 是否通过
$val['answer_status'] = intval($val['answer_status']); // 阅卷状态
unset(
$val['ep_id'],
$val['my_begin_time'],
$val['my_error_num'],
$val['paper_info'],
$val['domain'],
$val['status'],
$val['created'],
$val['updated'],
$val['deleted']
);
}
}
}
// 去除条件
unset(
$params['page'],
$params['limit'],
$params['ep_id'],
$params['sort']
);
// 实时统计的未参与人数和已参与人数与缓存的人数不符
if (count(array_filter($params)) == 0 && isset($paper['unjoin_count']) && ($un_total != $paper['unjoin_count'] || $total['join_count'] != $paper['join_count'])) {
// 更新试卷表中缓存的未参与人数
$this->paper_serv->update_by_conds(['ep_id' => $ep_id],
[
'unjoin_count' => $un_total,
'join_count' => $total
]
);
}
$this->_result = [
'join_total' => (int)$total,
'un_total' => (int)$un_total,
'limit' => (int)$limit,
'page' => (int)$page,
'list' => $join
];
}
}