PassAnalysisController.class.php
9.66 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
<?php
/**
* 【考试中心-后台】试卷参加考试人员分布接口
* PassAnalysisController.class.php
* @author : wanghuan
* @date : 2017-07-17
* @version: $Id$
*/
namespace Apicp\Controller\Answer;
use Common\Common\User;
use Common\Common\Department;
use Common\Service\PaperService;
use Common\Service\RightService;
use Common\Service\AnswerService;
class PassAnalysisController extends AbstractController
{
// 岗位
const TYPE_JOY = 1;
// 角色
const TYPE_ROLE = 2;
// 标签
const TYPE_TAG = 3;
/** @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)) {
return false;
}
// 初始化User
$this->user = User::instance();
// 实例化试卷service
$this->paper_serv = new PaperService();
// 实例化权限service
$this->right_serv = new RightService();
// 实例化答卷service
$this->answer_serv = new AnswerService();
return true;
}
public function Index_post()
{
set_time_limit(0);
ini_set("memory_limit", "1128M");
// 接收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');
}
// 查询条件初始化
$join_conds = [
'ep_id' => $ep_id,
'my_time > ?' => 0
];
// 【部门】
if (isset($params['dpIds']) && !empty($params['dpIds']) && is_array($params['dpIds'])) {
$params_dpids = $params['dpIds'];
$dpServ = &Department::instance();
// 取子级部门ID
$child_ids = $dpServ->list_childrens_by_cdid($params_dpids);
// 合并部门ID
$dp_ids = array_merge($params_dpids, array_values($child_ids));
$dp_conds = [
'dpIdList' => $dp_ids
];
$dp_users = $this->user->listAll($dp_conds);
// 选择部门下的员工id集合
$d_uids = array_column($dp_users, 'memUid');
// 组装查询条件
$dp_uids = $this->answer_serv->get_uids($paper['ep_id'], $d_uids);
if (!empty($dp_uids)) {
$join_conds['uid'] = $dp_uids;
} else {
// 空查询所有 为了避免查询所有传入0
$join_conds['uid'] = [0];
}
}
// 总分
$score_total = 0;
// 部门下考试的员工考试信息
$join = [];
// 试卷类型
$paper_type = $paper['paper_type'];
// 分页
$page_option = null;
// 排序
$order_by = [];
// 查询字段
$fields = 'ea_id,uid,my_score,created';
// 模拟考试
if (PaperService::SIMULATION_PAPER_TYPE == $paper_type) {
// 部门下模拟考试的员工考试信息
$join = $this->answer_serv->get_mock_join_list($join_conds, $page_option, $order_by, $fields);
if (!empty($join)) {
// 取考分列
$my_max_score = array_column($join, 'my_max_score');
// 考分之和
$score_total = array_sum($my_max_score);
}
} elseif (PaperService::EVALUATION_PAPER_TYPE == $paper_type) { // 测评考试
// 只用最高分进行计算
$answer_conds['is_score_top'] = AnswerService::IS_SCORE_TOP_TRUE;
// 部门测评考试通过的员工考试信息
$join = $this->answer_serv->list_by_conds($join_conds, $page_option, $order_by, $fields);
if (!empty($join)) {
// 取考分列
$my_score = array_column($join, 'my_score');
// 考分之和
$score_total = array_sum($my_score);
}
}
// 总数
$count = 0;
// 平均分
$average = 0;
// 待返数据
$list = [];
// 部门下考试的员工列表不为空
if (!empty($join)) {
// 部门下考试员工id集合
$join_uids = array_column($join, 'uid');
// 部门下参与考试的员工列表
$user_list = $this->answer_serv->getUser($join_uids);
// 参加考试员工不为空,获取统计数据
if (!empty($user_list)) {
// 【部门下参加考试总人数】
$count = count($join);
// 【平均分】
$average = round(($score_total / $count), 2);
// 默认类型:岗位
$type = self::TYPE_JOY;
// 类型参数不为空,重新赋值
if (isset($params['type']) && $params['type']) {
$type = intval($params['type']);
}
// 参加考试人数不为空,获取返回数据
if ($count) {
$list = $this->get_list_type_data($user_list, $type, $join, $paper_type, $count);
}
}
}
// 返回结果
$this->_result = [
'count' => intval($count),
'average' => $average,
'list' => $list
];
}
/**
* 组装list数据
*
* @param $user_list array 部门下参加考试的员工列表
* @param $type int 请求数据类型:1=岗位,2=角色
* @param $join array 部门下有权限考试的员工的考试信息
* @param $paper_type int 试卷类型:0=测评考试,1=模拟考试
* @param $count int 总数
*
* @return array 返回list数据
*/
protected function get_list_type_data($user_list, $type, $join, $paper_type, $count)
{
// 初始化返回数据
$list = [];
// 将答题记录转换为以用户ID为主键的二维数组
$join = array_combine_by_key($join, 'uid');
// 循环用户列表
foreach ($user_list as $val) {
$answer_info = $join[$val['memUid']];
$score = 0;
if (!empty($answer_info)) {
// 模拟考试
if (PaperService::SIMULATION_PAPER_TYPE == $paper_type) {
// 累加模拟考试同一岗位/角色的考分
$score = $answer_info['my_max_score'];
} elseif (PaperService::EVALUATION_PAPER_TYPE == $paper_type) { // 测评考试
// 累加模测评考试同一岗位/角色的考分
$score = $answer_info['my_score'];
}
}
// 岗位
if (self::TYPE_JOY == $type) {
if(empty($val['memJob'])){
continue;
}
$arr = [];
if (array_key_exists($val['memJob'], $list)) {
$arr['name'] = $val['memJob'];
$arr['count'] = $list[$val['memJob']]['count'] +1;
$arr['score'] = $list[$val['memJob']]['score'] +$score;
$list[$val['memJob']] = $arr;
} else {
$arr['name'] = $val['memJob'];
$arr['count'] = 1;
$arr['score'] = $score;
$list[$val['memJob']] = $arr;
}
} elseif (self::TYPE_ROLE == $type) { // 角色
if(empty($val['memRole'])){
continue;
}
$arr = [];
if (array_key_exists($val['memRole'], $list)) {
$arr['name'] = $val['memRole'];
$arr['count'] = $list[$val['memRole']]['count'] +1;
$arr['score'] = $list[$val['memRole']]['score'] +1;
$list[$val['memRole']] = $arr;
} else {
$arr['name'] = $val['memRole'];
$arr['count'] = 1;
$arr['score'] = $score;
$list[$val['memRole']] = $arr;
}
} elseif (self::TYPE_TAG == $type) { // 标签
if(empty($val['memTag'])){
continue;
}
$arr = [];
if (array_key_exists($val['memTag'], $list)) {
$arr['name'] = $val['memTag'];
$arr['count'] = $list[$val['memTag']]['count'] +1;
$arr['score'] = $list[$val['memTag']]['score'] +1;
$list[$val['memTag']] = $arr;
} else {
$arr['name'] = $val['memTag'];
$arr['count'] = 1;
$arr['score'] = $score;
$list[$val['memTag']] = $arr;
}
}
}
// 循环处理数据
$return_list = [];
foreach ($list as $v) {
$res = [];
$res['name'] = $v['name'];
$res['count'] = $v['count'];
$res['average'] = round($v['score'] / $v['count'],2);
// 百分比
$res['percentage'] = round((($v['count'] / $count) * 100), 2) . '%';
$return_list[] = $res;
}
return $return_list;
}
}