ListController.class.php
6.17 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
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 17/10/17
* Time: 11:21
*/
namespace Apicp\Controller\CourseData;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\DataHelper;
use Common\Model\CourseClassModel;
use Common\Model\CourseArticleModel;
use Common\Model\CourseCompleteModel;
class ListController extends \Apicp\Controller\AbstractController
{
/**
* List
* @author zhonglei
* @desc 课程数据列表接口
* @param Int class_id 分类ID
* @param String article_title 课程名称
* @param Int created_start 发布时间戳
* @param Int created_end 发布时间戳
* @param Int page:1 当前页
* @param Int limit:20 每页数据总数
* @return array
array(
'list' => array( // 列表数据
array(
'article_id' => 188, // 课程ID
'article_title' => '热烈庆祝十九大胜利召开', // 课程名称
'class_id' => 188, // 分类ID
'class_name' => '分类A', // 分类名称
'user_total' => 1000, // 课程总人数
'study_total' => 800, // 学习人数
'complete_total' => 800, // 完成学习人数
'complete_rate' => 80, // 完成率
'like_total' => 100, // 点赞数
'comment_total' => 200, // 评论数
'created' => 1509292800000, // 发布时间
),
),
'page' => 100, // 当前页
'limit' => 100, // 每页数据总数
'total' => 100, // 数据总数
)
*/
public function Index_post()
{
// 请求数据
$post_data = I('post.');
// 验证规则
$rules = [
'class_id' => 'integer',
'article_title' => 'max:64',
'created_start' => 'integer',
'created_end' => 'integer',
'page' => 'integer',
'limit' => 'integer',
];
// 验证请求数据
$validate = new PackageValidate();
$validate->postData = $post_data;
$validate->validateParams($rules);
// 返回值
$this->_result = [
'list' => [],
'page' => isset($post_data['page']) ? $post_data['page'] : Constant::PAGING_DEFAULT_PAGE,
'limit' => isset($post_data['limit']) ? $post_data['limit'] : Constant::PAGING_DEFAULT_LIMIT,
'total' => 0,
];
// 获取分类数据
$classModel = new CourseClassModel();
$class_list = $classModel->list_all();
if (empty($class_list)) {
return;
}
$class_data = array_combine_by_key($class_list, 'class_id');
// 格式化分类数据
foreach ($class_data as $class_id => $class) {
$class_data[$class['parent_id']]['children'][$class_id] = &$class_data[$class_id];
}
if (isset($post_data['class_id'])) {
$class_id = $post_data['class_id'];
$ids = [];
// 未找到分类
if (!isset($class_data[$class_id])) {
return;
}
// 获取分类自身ID和所有子分类ID
$this->_getSelfChildId($class_data[$class_id], $ids);
$post_data['class_id'] = $ids;
}
$dataHelper = &DataHelper::instance();
// 搜索条件
$conds = ['course_type' => Constant::COURSE_TYPE_NORMAL];
if (isset($post_data['class_id'])) {
$conds['class_id'] = $post_data['class_id'];
}
if (isset($post_data['article_title'])) {
$conds['article_title like ?'] = "%{$post_data['article_title']}%";
}
if (isset($post_data['created_start'])) {
$conds['created >= ?'] = $dataHelper->formatStarttime($post_data['created_start']);
}
if (isset($post_data['created_end'])) {
$conds['created <= ?'] = $dataHelper->formatEndtime($post_data['created_end']);
}
// 获取课程总数
$articleModel = new CourseArticleModel();
$this->_result['total'] = $articleModel->count_by_conds($conds);
if ($this->_result['total'] == 0) {
return;
}
// 获取课程列表
list($start, $perpage) = page_limit($this->_result['page'], $this->_result['limit']);
$article_list = $articleModel->list_by_conds($conds, [$start, $perpage], ['created' => 'desc']);
$keys = ['article_id', 'class_id', 'article_title', 'user_total', 'study_total', 'comment_total', 'like_total', 'created'];
$articles = array_combine_by_key(array_intersect_key_reserved($article_list, $keys), 'article_id');
// 获取已完成人数
$completeModel = new CourseCompleteModel();
$complete_list = $completeModel->listArticleRank(['article_id' => array_keys($articles)]);
$completes = array_combine_by_key($complete_list, 'article_id');
foreach ($articles as $k => $v) {
$class_id = $v['class_id'];
$article_id = $v['article_id'];
$articles[$k]['class_name'] = isset($class_data[$class_id]) ? $class_data[$class_id]['class_name'] : '';
$articles[$k]['complete_total'] = isset($completes[$article_id]) ? $completes[$article_id]['total'] : 0;
$completeRate = round($articles[$k]['complete_total'] / $v['user_total'] * 100, 2);
$articles[$k]['complete_rate'] = $completeRate > 100 ? 100 : $completeRate;
}
$this->_result['list'] = array_values($articles);
}
/**
* 获取分类自身ID和所有子分类ID
* @author zhonglei
* @param array $class 分类数据
* @param array $ids 分类自身ID和所有子分类ID数组(引用)
* @return void
*/
private function _getSelfChildId($class, &$ids)
{
$ids[] = $class['class_id'];
// 包含子级分类
if (isset($class['children'])) {
foreach ($class['children'] as $child) {
$this->_getSelfChildId($child, $ids);
}
}
}
}