CourseListController.class.php
9.41 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/10/12
* Time: 10:10
*/
namespace Apicp\Controller\UserData;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\User;
use Common\Common\CourseHelper;
use Common\Model\CourseArticleModel;
use Common\Model\CourseCompleteModel;
use Common\Model\CourseStudyTimeModel;
use Common\Model\CourseClassModel;
use Common\Model\CommentModel;
use Common\Model\LikeModel;
class CourseListController extends \Apicp\Controller\AbstractController
{protected $_require_login = false;
/**
* CourseList
* @author liyifei
* @desc 员工数据总览,员工课程明细接口
* @param Int page:1 当前页
* @param Int limit:20 当前页条数
* @param String uid 人员UID
* @return array
array(
'user' => array( // 人员数据
'username' => '李四', // 姓名
'userface' => 'http://p.qlogo.cn/bizmail/FZaQ9jwGLMdFlI84NwMVWCCcZsnxPwXrdkcWWWZTrWS8BAwYMW7yQQ/0', // 头像
'dp_names' => array('开发组', '测试组'), // 组织
'job' => '苹果系统工程师', // 岗位
'role' => 'leader', // 角色
),
'page' => 1, // 当前页
'limit' => 20, // 当前页条数
'total' => 100, // 总条数
'list' => array( // 列表数据
array(
'order_id' => 1, // 序号
'article_title' => '课程1', // 课程名称
'class_name' => '分类1', // 课程所属分类
'first_study_time' => '1501827237983', // 最近学习时间(原为首次学习时间,后产品修改需求)
'is_complete' => 1, // 是否完成(1=否;2=是)
'complete_time' => '1501827237983', // 完成时间(毫秒级时间戳)
'study_total' => 5, // 学习次数
'study_time' => 10, // 学习时长(单位为秒)
'is_like' => 1, // 是否点赞(1=否;2=是)
'comment_total' => 1, // 评论数
),
),
)
*/
public function Index_post()
{
// 验证规则
$rules = [
'uid' => 'max:32',
'page' => 'integer|gt:0',
'limit' => 'integer|gt:0',
];
// 验证请求数据
$postData = I('post.');
$validate = new PackageValidate();
$validate->postData = $postData;
$validate->validateParams($rules);
// 人员信息
$userServ = &User::instance();
$user = $userServ->getByUid($postData['uid']);
if (empty($user)) {
E('_ERR_USERDATA_USER_IS_EMPTY');
}
// 分页
$page = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
$limit = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
list($sqlPage, $sqlLimit) = page_limit($page, $limit);
// 查询常规课程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', ['withOutStatus' => true]);
$articles = array_column($course_article_list,'article_id');
$list = [];
// 用户已参与的课程ID
$articleIds = [];
$studyTimeModel = new CourseStudyTimeModel();
$studyTimeList = $studyTimeModel->list_by_conds(['uid' => $user['memUid'],'article_id'=>$articles]);
if (!empty($studyTimeList)) {
$articleIds = array_values(array_unique(array_column($studyTimeList, 'article_id')));
}
// 用户可参与的课程ID
// $courseHelper = &CourseHelper::instance();
// $articleIds = $courseHelper->listArticleIdByRight($user, 'article_id');
if (!empty($articleIds)) {
// 获取已完成课程ID,并以完成时间倒序
$completeModel = new CourseCompleteModel();
$completeList = $completeModel->list_by_conds(['uid' => $user['memUid'], 'article_id' => $articleIds], null, ['created' => 'DESC']);
$completeIds = [];
if (!empty($completeList)) {
$completeList = array_combine_by_key($completeList, 'article_id');
$completeIds = array_values(array_unique(array_column($completeList, 'article_id')));
}
// 将已完成课程ID、可参与课程ID合并去重,获得按课程完成时间倒序排列的课程ID
$orderArticleIds = array_unique(array_merge($completeIds, $articleIds));
// 课程总数
$articleModel = new CourseArticleModel();
$articleTotal = $articleModel->count_by_conds(['article_id' => $orderArticleIds], '*', false, true);
// 获取课程列表
$articleList = $articleModel->list_by_conds(['article_id' => $orderArticleIds], [$sqlPage, $sqlLimit], [], '*', ['withOutStatus' => true]);
if (!empty($articleList)) {
// 分页筛选后的课程ID数组
$ids = array_column($articleList, 'article_id');
// 分类名称
$classIds = array_values(array_unique(array_column($articleList, 'class_id')));
$classModel = new CourseClassModel();
$classList = $classModel->list_by_conds(['class_id' => $classIds]);
if (!empty($classList)) {
$classList = array_combine_by_key($classList, 'class_id');
}
// 学习次数
$studyTimeModel = new CourseStudyTimeModel();
$courseStudyList = $studyTimeModel->listCourseStudyTotal($user['memUid'], $ids);
if (!empty($courseStudyList)) {
$courseStudyList = array_combine_by_key($courseStudyList, 'article_id');
}
// 学习时长(单位为秒)
$timeTotals = $studyTimeModel->listStudyTime($user['memUid'], $ids);
if (!empty($timeTotals)) {
$timeTotals = array_combine_by_key($timeTotals, 'article_id');
}
// 最近学习时间(原为首次学习时间,后产品修改需求)
$firstTimeList = $studyTimeModel->listFirstStudyTime($user['memUid'], $ids);
if (!empty($firstTimeList)) {
$firstTimeList = array_combine_by_key($firstTimeList, 'article_id');
}
// 点赞(埋点数据)
$likeModel = new LikeModel();
$conds = [
'uid' => $user['memUid'],
'app' => Constant::APP_COURSE,
'app_data_id' => $ids,
];
$likeList = $likeModel->listLikeRank($conds);
if (!empty($likeList)) {
$likeList = array_combine_by_key($likeList, 'app_data_id');
}
// 评论数(埋点数据)
$commentModel = new CommentModel();
$commentList = $commentModel->listCommentTotalGroup([$user['memUid']], Constant::APP_COURSE, $ids);
if (!empty($commentList)) {
$commentList = array_combine_by_key($commentList, 'app_data_id');
}
// 组合数据
foreach ($articleList as $k => $article) {
$articleId = $article['article_id'];
$list[] = [
'order_id' => $k,
'article_id' => $articleId,
'article_title' => $article['article_title'],
'class_name' => trim($classList[$article['class_id']]['class_name']),
'first_study_time' => (int)$firstTimeList[$articleId]['created'],
'is_complete' => in_array($articleId, $completeIds) ? Constant::COURSE_IS_COMPLETE_TRUE : Constant::COURSE_IS_COMPLETE_FALSE,
'complete_time' => (int)$completeList[$articleId]['created'],
'study_total' => (int)$courseStudyList[$articleId]['total'],
'study_time' => (int)$timeTotals[$articleId]['study_time'],
'is_like' => isset($likeList[$articleId]) ? Constant::COURSE_IS_LIKE_TRUE: Constant::COURSE_IS_LIKEE_FALSE,
'comment_total' => (int)$commentList[$articleId]['total'],
'data_status' => (int)$article['status']
];
}
}
}
$this->_result = [
'user' => [
'uid' => $user['memUid'],
'username' => $user['memUsername'],
'userface' => $user['memFace'],
'dp_names' => isset($user['dpName']) && !empty($user['dpName']) ? array_column($user['dpName'], 'dpName') : [],
'job' => $user['memJob'],
'role' => $user['memRole'],
],
'page' => (int)$page,
'limit' => (int)$limit,
'total' => isset($articleTotal) ? (int)$articleTotal : 0,
'list' => $list,
];
}
}