UserListController.class.php
12.8 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/10/14
* Time: 14:32
*/
namespace Apicp\Controller\UserStudyData;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\CourseHelper;
use Common\Common\User;
use Common\Model\CourseArticleModel;
use Common\Model\CourseCompleteModel;
use Common\Model\CourseClassModel;
use Common\Model\CourseStudyTimeModel;
use Common\Model\CommentModel;
class UserListController extends \Apicp\Controller\AbstractController
{
/**
* UserList
* @author liyifei
* @desc 员工学习数据列表接口
* @param Int page:1 当前页
* @param Int limit:20 当前页条数
* @param Array dp_ids 组织ID
* @param Array role_ids 角色ID
* @param Array job_ids 岗位ID
* @param String username 姓名
* @param Int class_id 课程分类ID
* @param String article_title 课程名称
* @param Int is_complete 是否学习完成(1=否;2=是)
* @param Array uids 人员UID数组(当以上所有条件参数值{分页除外}无变化时,前端将此接口uids返回值回传过来;当uids有值时,接口会忽略以上{分页除外}所有筛选条件;此参数主要用在分页功能。)
* @return array
array(
'page' => 1, // 当前页
'limit' => 20, // 当前页条数
'total' => 100, // 总条数
'list' => array( // 列表数据
array(
'uid' => 'B4B3B9D17F00000173E870DA9A855AE7', // 人员UID
'username' => '李四', // 姓名
'dp_names' => array('开发组', '测试组'), // 组织
'job' => '苹果系统工程师', // 岗位
'role' => 'leader', // 角色
'article_title' => '课程001', // 课程名称
'latest_study_time' => '1507953600000', // 最近学习时间
'is_complete' => 1, // 是否完成学习(已学习+测评通过)
'complete_time' => '1507953600000', // 完成学习时间
'comment_total' => 10, // 评论数(需求变更:不返回)
),
),
'uids' => array( // 筛选条件下所有人员UID数组
'B4B3B9D17F00000173E870DA9A855AE7',
'5B1AFACC7F0000014D41E650287BAD6F',
),
)
*/
public function Index_post()
{
// 验证规则
$rules = [
'page' => 'integer|gt:0',
'limit' => 'integer|gt:0',
'uids' => 'array',
'dp_ids' => 'array',
'role_ids' => 'array',
'job_ids' => 'array',
'username' => 'max:64',
'class_id' => 'integer|gt:0',
'article_title' => 'max:64',
'is_complete' => 'integer|in:1,2',
];
// 验证请求数据
$postData = I('post.');
$validate = new PackageValidate();
$validate->postData = $postData;
$validate->validateParams($rules);
// 分页
$page = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
$limit = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
list($pageSql, $limitSql) = page_limit($page, $limit, 10000);
// 人员条件(当uids有值时,则忽略人员其他筛选条件)
$userConds = [];
if (!empty($postData['uids'])) {
$userConds['memUids'] = $postData['uids'];
} else {
if (isset($postData['dp_ids'])) {
$userConds['dpIdList'] = $postData['dp_ids'];
$userConds['departmentChildrenFlag'] = 1;
}
if (isset($postData['role_ids'])) {
$userConds['roleIdList'] = $postData['role_ids'];
}
if (isset($postData['job_ids'])) {
$userConds['jobIdList'] = $postData['job_ids'];
}
if (isset($postData['username'])) {
$userConds['memUsername'] = $postData['username'];
}
}
// 课程条件
$articleConds = [];
if (isset($postData['article_title'])) {
$articleConds['article_title like ?'] = '%' . $postData['article_title'] . '%';
}
if (isset($postData['class_id'])) {
$classModel = new CourseClassModel();
$classIds = $classModel->getChildClassIds($postData['class_id']);
$articleConds['class_id'] = $classIds;
}
$userList = [];
$articleList = [];
$courseHelper = &CourseHelper::instance();
// 仅搜索课程
if (!empty($articleConds) && empty($userConds)) {
$articleList = $courseHelper->userStudyListByArticle($articleConds);
if (empty($articleList)) {
$this->_result = [];
return ;
}
}
// 仅搜索人员
// if (!empty($userConds) && empty($articleConds)) {
$userList = $courseHelper->userStudyListByUser($userConds);
if (empty($userList)) {
$this->_result = [];
return ;
}
// }
// 搜索人员及课程
if (!empty($userConds) && !empty($articleConds)) {
list($userList, $articleList) = $courseHelper->userStudyListByUserArticle($userConds, $articleConds);
if (empty($userList) || empty($articleList)) {
$this->_result = [];
return ;
}
}
$uids = [];
if (!empty($userList)) {
$uids = array_keys($userList);
}
// 查询常规课程id集合
$course_article = new CourseArticleModel();
$course_article_list = $course_article->list_by_conds(['course_type' => Constant::COURSE_TYPE_NORMAL], null, [], 'article_id,data_id,article_type');
$articles = array_column($course_article_list,'article_id');
if (!empty($articleList)) {
$articleIds = array_keys($articleList);
// 对常规课程id 和 搜索课程所获取的课程id取交集
$articleIds = array_intersect($articles,$articleIds);
}else{
$articleIds = $articles;
}
// 获取数据列表
$datas = [];
$total = 0;
$completeModel = new CourseCompleteModel();
$studyTimeModel = new CourseStudyTimeModel();
if (!empty($postData['is_complete'])) {
switch ($postData['is_complete']) {
// 是否完成学习:已完成(course_complete)
case Constant::COURSE_IS_COMPLETE_TRUE:
$datas = $completeModel->list_by_conds(['uid' => $uids, 'article_id' => $articleIds], [$pageSql, $limitSql], ['created' => 'desc']);
$total = $completeModel->count_by_conds(['uid' => $uids, 'article_id' => $articleIds]);
// 最近学习情况
$studyList = $studyTimeModel->listUserStudy($datas);
break;
// 是否完成学习:未完成(在course_study_time,但不在course_complete)连表查询
case Constant::COURSE_IS_COMPLETE_FALSE:
$datas = $studyTimeModel->listUserUnCompleteCourse(['uids' => $uids, 'article_ids' => $articleIds], [$pageSql, $limitSql]);
$total = $studyTimeModel->countUserUnCompleteCourse(['uids' => $uids, 'article_ids' => $articleIds]);
break;
}
} else {
// 是否完成学习:全部,已完成+未完成(course_study_time)分表查询
$conds = [
'uid' => $uids,
'article_id' => $articleIds,
];
$datas = $studyTimeModel->listUserAllowStudy($conds, [$pageSql, $limitSql]);
$total = $studyTimeModel->totalUserAllowStudy($conds);
// 课程完成情况
$completeList = $completeModel->listUserComplete($datas);
}
// 获取评论列表
// $commentModel = new CommentModel();
// $commentList = $commentModel->listCommentTotalGroup($uids, Constant::APP_COURSE, $articleIds);
// 补全人员信息
$list = [];
if (!empty($datas)) {
// 获取课程信息
if (empty($articleList)) {
$articleIds = array_column($datas, 'article_id');
$articleList = $courseHelper->userStudyListByArticle(['article_id' => $articleIds]);
}
// 获取人员信息
if (empty($userList)) {
$uids = array_column($datas, 'uid');
$userList = $courseHelper->userStudyListByUser(['memUids' => $uids]);
}
foreach ($datas as $v) {
$uid = $v['uid'];
$user = $userList[$uid];
if(empty($user)){
// 如果用户被删除,需要重新获取
$user = User::instance()->getByUid($uid);
}
$articleId = $v['article_id'];
// 格式化"已完成"数据时,补全最近学习时间
$completeTime = 0;
$latestStudyTime = 0;
if (isset($studyList) && !empty($studyList)) {
// 当数据列表未课程完成列表时,创建时间即为课程完成时间;
$completeTime = $v['created'];
foreach ($studyList as $study) {
if ($articleId == $study['article_id'] && $uid == $study['uid']) {
$latestStudyTime = $study['latest_study_time'];
break;
}
}
}
// 格式化"全部"数据时,补全课程完成情况
if (isset($completeList) && !empty($completeList)) {
foreach ($completeList as $complete) {
if ($articleId == $complete['article_id'] && $uid == $complete['uid']) {
$completeTime = $complete['complete_time'];
break;
}
}
}
/*
// 评论数据
$commentTotal = 0;
if (isset($commentList) && !empty($commentList)) {
foreach ($commentList as $comment) {
if ($articleId == $comment['app_data_id'] && $uid == $comment['uid']) {
$commentTotal = $comment['total'];
break;
}
}
}
*/
// 学习时长
$times = $studyTimeModel->listStudyTime($uid, $articleId);
$study_time = $this->secToTime((int)$times[0]['study_time']);
$list[] = [
'uid' => $uid,
'article_id' => $articleId,
'username' => $user['memUsername'],
'dp_names' => isset($user['dpName']) && !empty($user['dpName']) ? array_column($user['dpName'], 'dpName') : [],
'job' => $user['memJob'],
'role' => $user['memRole'],
'article_title' => $articleList[$articleId]['article_title'],
'latest_study_time' => isset($v['latest_study_time']) ? $v['latest_study_time'] : $latestStudyTime,
'is_complete' => (int)$completeTime > 0 ? Constant::COURSE_IS_COMPLETE_TRUE : Constant::COURSE_IS_COMPLETE_FALSE,
'complete_time' => $completeTime,
'study_time' => $study_time
// 'comment_total' => (int)$commentTotal,
];
}
}
$this->_result = [
'page' => (int)$page,
'limit' => (int)$limit,
'total' => (int)$total,
'list' => $list,
'uids' => $uids,
];
}
/**
* 把秒数转换为时分秒的格式
* @author liyifei
* @param Int $times 时间,单位 秒
* @return String
*/
private function secToTime($times)
{
$result = '';
if ($times > 0) {
$hour = floor($times / 3600);
$minute = floor(($times - 3600 * $hour) / 60);
$second = floor((($times - 3600 * $hour) - 60 * $minute) % 60);
if ($hour > 0) {
$result .= "{$hour}时";
}
if ($minute > 0) {
$result .= "{$minute}分";
}
if ($second > 0) {
$result .= "{$second}秒";
}
}
return empty($result) ? 0 : $result;
}
}