PublishController.class.php
8.98 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
<?php
/**
* 【后台】立即发布培训计划接口
* PublishController.class.php
* User: yingcai
* Date: 2017/9/1
* Time: 下午3:12
*/
namespace Apicp\Controller\Education;
use Common\Common\Constant;
use Common\Common\Teacher;
use Common\Service\CourseArticleService;
use Common\Service\CronService;
use Common\Service\EducationService;
use Common\Service\PlanService;
use Common\Service\RightService;
use Common\Service\RightUsersService;
class PublishController extends \Apicp\Controller\AbstractController
{
/** @var EducationService 实例化权限表对象 */
protected $education_service;
/** @var RightService 实例化权限表对象 */
protected $right_service;
/** @var RightUsersService 班级人员表对象 */
protected $right_user_service;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
$this->education_service = new EducationService();
$this->right_service = new RightService();
$this->right_user_service = new RightUsersService();
return true;
}
public function Index_post()
{
$ed_id = I('ed_id', 0, 'intval');
// 培训ID是否为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 判断培训是否存在
$education = $this->education_service->get($ed_id);
if (empty($education)) {
E('_ERR_EDUCATION_NOT_EXIST');
}
if ($education['ed_begin_time'] < MILLI_TIME) {
// 培训开始时间不能小于当前时间
E('_ERR_BEGIN_TIME_LT_NOWTIME');
}
// 报名截止时间小于当前时间
if (EducationService::EDUCATION_SIGN_OPEN == $education['ed_is_sign_up']) {
if ($education['ed_sign_deadline'] > $education['ed_begin_time']) {
// 报名时间截止不能大于培训开始时间
E('_ERR_SIGN_TIME_DEADLINE_START_NOWTIME');
}
if ($education['ed_sign_deadline'] < MILLI_TIME) {
// 报名时间截止不能大于培训开始时间
E('_ERR_SIGN_TIME_DEADLINE_LT_NOWTIME');
}
}
try {
// 开始事务
$this->education_service->start_trans();
// 更改培训状态
$update_data = ['ed_status' => EducationService::EDUCATION_PUBLISH_STATUS_PUBLISHED];
$rel = $this->education_service->update($ed_id, $update_data);
if ($rel) {
// 发布成功更新状态
$education['ed_status'] = EducationService::EDUCATION_PUBLISH_STATUS_PUBLISHED;
// 操作已启用的线下课程类培训安排
$this->plan_course($ed_id);
}
// 将权限范围内的所有人员添加到班级人员表
$uids = $this->right_user_service->save_right_users(
$ed_id,
$education['ed_is_sign_up']
);
// 提交事务
$this->education_service->commit();
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->education_service->rollback();
return false;
}
$education['uids'] = $uids;
// 推送消息和首页feed流推荐
$this->sen_msg_and_recommend($ed_id, $education);
return true;
}
/**
* 批量处理线下课程类的培训安排(创建计划任务、发送讲师提醒、同步授课任务至讲师中心)
* @param int $ed_id 培训id
* @return bool
*/
protected function plan_course($ed_id)
{
// 已启用、线下课程类的培训安排
$conds = [
'ed_id' => $ed_id,
'plan_type' => Constant::PLAN_TYPE_UNDER_LINE_COURSE,
'plan_use_status' => Constant::PLAN_USE_STATUS_IS_TRUE,
];
$planServ = new PlanService();
$plan_list = $planServ->list_by_conds($conds);
if (!empty($plan_list)) {
// 线下课程列表
$article_ids = array_column($plan_list, 'plan_obj_id');
$articleServ = new CourseArticleService();
$article_list = $articleServ->list_by_pks($article_ids);
// 批量查询讲师信息
$teacherServ = &Teacher::instance();
$teacher_ids = array_unique(array_filter(array_column($article_list, 'teacher_id')));
if (!empty($teacher_ids)) {
// 正常状态的内部讲师
$teacher_list = $teacherServ->teacherList(['teacher_id' => $teacher_ids, 'teacher_type' => 1, 'teacher_status' => 1]);
$teacher_list = array_combine_by_key($teacher_list, 'teacher_id');
}
$cronServ = new CronService();
foreach ($article_list as $article) {
// 通知讲师中心,记录讲师授课任务、课时
$teacherServ->saveOfflineCourse($article);
// 开启签到、有学分,保存计划任务
if ($article['is_sign'] == Constant::COURSE_IS_SIGN_IS_TRUE && $article['credit'] > 0) {
$cronServ->saveCron($article);
}
// 开启消息推送时,给讲师推送课程提醒
if ($article['is_notice'] == Constant::ARTICLE_IS_NOTICE_TRUE && !empty($article['teacher_id'])) {
$teacher_id = $article['teacher_id'];
$uid = isset($teacher_list[$teacher_id]) ? $teacher_list[$teacher_id]['uid'] : '';
if ($uid) {
$articleServ->sendNotice($uid, $article);
}
}
}
}
return true;
}
/**
* 发送推送消息和首页feed流推荐
*
* @author houyingcai
* @param int $ed_id 培训ID
* @param array $education 培训数据
*/
protected function sen_msg_and_recommend($ed_id, $education)
{
// 如果是发布状态
if (EducationService::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education['ed_status']) {
// 开启消息推送
if (EducationService::PUSH_MSG_OPEN == $education['ed_is_push_msg']) {
$msg_params = [
'ed_id' => $ed_id,
'name' => $education['ed_name'],
'begin_time' => $education['ed_begin_time'],
'end_time' => $education['ed_end_time'],
'img_id' => $education['ed_cover_id'],
'uids' => $education['uids'],
];
// 发送提醒消息
$this->education_service->send_msg($msg_params, EducationService::MSG_EDUCATION_PUBLISH);
}
// 开启首页推荐
if (EducationService::RECOMMEND_INDEX_TRUE == $education['ed_is_recommend']) {
$url = rpcUrl('/Public/Rpc/Recommender/ArticleNew');
$rpc_params = [
'app' => 'train',
'dataCategoryId' => '',
'dataId' => $ed_id,
'title' => $education['ed_name'],
'summary' => $education['ed_introductions'],
'attachId' => $education['ed_cover_id'],
'pic' => !empty($education['ed_cover_id']) ? imgUrlReal($education['ed_cover_id']) : '',
'url' => 'Train/Frontend/Index/Msg?&ed_id=' . $ed_id,
];
// 获取权限表数据
$right_all = $this->right_service->get_by_conds([
'obj_type' => 1, // 权限类型为全公司
'ed_id' => $ed_id
]);
// 如果不是全公司
if (empty($right_all)) {
$right_user_list = $this->right_user_service->list_by_conds(['ed_id' => $ed_id], null, [],
'ru_uid');
$r_uids = array_unique(array_filter(array_column($right_user_list, 'ru_uid')));
if (!empty($r_uids)) {
$rpc_params['right'] = $this->format_feed_data($r_uids);
\Com\Rpc::phprpc($url)->invoke('Index', $rpc_params);
}
} else {
$rpc_params['right'] = [];
\Com\Rpc::phprpc($url)->invoke('Index', $rpc_params);
}
}
}
}
/**
* 格式化feed流权限数据
*
* @author houyingcai
* @param array $right 权限数组
*
* @return array
*/
protected function format_feed_data($uids = [])
{
$feed_right = [];
// 格式化权限字段
if (!empty($right)) {
$feed_right['users'] = !empty($uids) ? $uids : '';
$feed_right['departments'] = '';
$feed_right['jobs'] = '';
$feed_right['roles'] = '';
}
return $feed_right;
}
}