UpdateController.class.php
12.5 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
324
325
326
327
328
329
330
331
332
333
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 2017/6/5
* Time: 14:33
*/
namespace Rpc\Controller\Exam;
use Common\Common\ArticleHelper;
use Common\Service\CompleteService;
use Think\Log;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\IntegralStrategy;
use Common\Service\ArticleService;
use Common\Service\ArticleChapterService;
use Common\Service\StudyService;
use Common\Service\StudyRecordService;
use Common\Service\ExamService;
use Common\Service\UserActionService;
class UpdateController extends \Rpc\Controller\AbstractController
{
/**
* Update
* @author zhonglei
* @desc 更新课程测评结果RPC接口
* @param String uid:true 用户ID
* @param Int article_id:true 课程ID
* @param Int article_chapter_id:true 章节ID,对课程测评时为0
* @param Int source_id:true 素材ID,对课程测评时为0
* @param Int customtask_id:true 常规课程传0
* @param int plan_id:true 培训计划ID(常规课程传0,线下培训类课程必须)
* @param int ed_id:true 培训ID(常规课程传0,线下培训类课程必须)
* @param Int is_pass:true 测评是否通过(1=未通过;2=已通过)
* @param Int yes_total:true 测评答对题数
* @param Int no_total:true 测评答错题数
* @param Int map_id 学习地图ID
* @param Int path_id 学习路径ID
* @return array
*/
public function Index()
{
Log::record('params: ' . var_export($this->_params, true), Log::INFO);
$post_data = $this->_params;
$result = [];
// 验证规则
$rules = [
'uid' => 'require',
'article_id' => 'require|integer',
'article_chapter_id' => 'integer',
'source_id' => 'integer',
'customtask_id' => 'integer',
'map_id' => 'integer',
'path_id' => 'integer',
'plan_id' => 'integer',
'ed_id' => 'integer',
'is_pass' => 'require|in:1,2',
'yes_total' => 'require|integer',
'no_total' => 'require|integer',
];
// 验证请求数据
$validate = new PackageValidate();
$validate->postData = $post_data;
$validate->validateParams($rules);
$customtask_id = isset($post_data['customtask_id']) ? $post_data['customtask_id'] : 0;
$plan_id = isset($post_data['plan_id']) ? $post_data['plan_id'] : 0;
$ed_id = isset($post_data['ed_id']) ? $post_data['ed_id'] : 0;
$map_id = $post_data['map_id'] ?? 0;
$path_id = $post_data['path_id'] ?? 0;
// 测评通过题数百分比
$post_data['rate'] = round($post_data['yes_total'] / ($post_data['yes_total'] + $post_data['no_total']), 2) * 100;
$articleServ = new ArticleService();
$article = $articleServ->get($post_data['article_id']);
// 未找到课程
if (empty($article)) {
Log::record('not found article', Log::INFO);
return $result;
}
// 数据中心-课程中心改造 1 start {
$examServ = new ExamService();
$exam_data = [
'article_id' => $post_data['article_id'],
'customtask_id' => $customtask_id,
'plan_id' => $plan_id,
'ed_id' => $ed_id,
'map_id' => $map_id,
'path_id' => $path_id,
'uid' => $post_data['uid'],
'is_pass' => $post_data['is_pass'],
];
// 数据中心-课程中心改造 1 end }
// 对课程进行测评
if (empty($post_data['article_chapter_id']) && empty($post_data['source_id'])) {
$studyServ = new StudyService();
$study = $studyServ->get_by_conds([
'article_id' => $post_data['article_id'],
'uid' => $post_data['uid'],
]);
// 未找到课程已学数据
if (empty($study)) {
Log::record('not found study', Log::INFO);
return $result;
}
// 记录考试结果
$exam_data['username'] = $study['username'];
$examServ->insert($exam_data);
// 数据中心-课程中心改造 2 start {
// 计算课程是否已完成(主课程测评通过,说明子课程已全部学习且无测评或子课程测评已通过)
if ($post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
$complete_data = [
'article_id' => $post_data['article_id'],
'uid' => $post_data['uid'],
'username' => $study['username'],
];
}
// 数据中心-课程中心改造 2 end }
// 对章节素材进行测评
} else {
$recordServ = new StudyRecordService();
$record = $recordServ->get_by_conds([
'article_id' => $post_data['article_id'],
'article_chapter_id' => $post_data['article_chapter_id'],
'source_id' => $post_data['source_id'],
'uid' => $post_data['uid'],
]);
// 未找到章节素材已学数据
if (empty($record)) {
Log::record('not found study record', Log::INFO);
return $result;
}
// 记录考试结果
$exam_data['article_chapter_id'] = $post_data['article_chapter_id'];
$exam_data['source_id'] = $post_data['source_id'];
$exam_data['username'] = $record['username'];
$examServ->insert($exam_data);
// 数据中心-课程中心改造 3 start {
// 计算课程是否已完成(若主课程无测评,则子课程全部测评通过,即为已完成)
if ($article['is_exam'] == Constant::ARTICLE_IS_EXAM_FALSE && $post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
$articleHelper = &ArticleHelper::instance();
$all_study = $articleHelper->getStudyStatus($post_data['uid'], $post_data['article_id'], $customtask_id, $plan_id, $ed_id, $map_id, $path_id);
if ($all_study) {
$complete_data = [
'article_id' => $post_data['article_id'],
'uid' => $post_data['uid'],
'username' => $record['username'],
];
}
}
// 数据中心-课程中心改造 3 end }
}
// 数据中心-课程中心改造 4 start {
// 记录课程已完成(仅限常规课程)
if ($article['course_type'] == Constant::COURSE_TYPE_NORMAL && !empty($complete_data)) {
$completeServ = new CompleteService();
$complete = $completeServ->get_by_conds([
'article_id' => $post_data['article_id'],
'uid' => $post_data['uid'],
]);
if (empty($complete)) {
$completeServ->insert($complete_data);
}
}
// 数据中心-课程中心改造 4 end }
if (($article['credit_strategy_setting'] != Constant::COURSE_CREDIT_STRATEGY_DISABLE ||
$article['strategy_setting'] != Constant::COURSE_STRATEGY_DISABLE) &&
!in_array($article['course_type'], [Constant::COURSE_TYPE_TASK, Constant::COURSE_TYPE_TRAIN])) {
// 对课程进行测评
if (empty($post_data['article_chapter_id']) && empty($post_data['source_id'])) {
$result = $this->_triggerArticleExam($article, $post_data);
// 对章节素材进行测评
} else {
$chapterServ = new ArticleChapterService();
$article_chapter = $chapterServ->get_by_conds([
'parent_id' => $post_data['article_chapter_id'],
'source_id' => $post_data['source_id'],
]);
if (!empty($article_chapter)) {
$result = $this->_triggerSourceExam($article, $article_chapter, $post_data);
}
}
}
return $result;
}
/**
* 触发课程测评埋点
* @author zhonglei
* @param array $article 课程数据
* @param array $post_data 请求数据
* @return array
*/
private function _triggerArticleExam($article, $post_data)
{
$action_key = $article['article_type'] == Constant::ARTICLE_TYPE_SINGLE ? Constant::INT_ACT_ONE_EVALUATION : Constant::INT_ACT_CHILD_EVALUATION;
$trigger_types = [];
// 测评通过
if ($post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
$actionServ = new UserActionService();
$act_result = $actionServ->save($post_data['uid'], $article['article_id'], $action_key);
// 首次测评通过
if ($act_result) {
// 获取单课程/系列课程测评通过总数
$count = $actionServ->countByActionKey($post_data['uid'], $action_key);
// 单课程
if ($article['article_type'] == Constant::ARTICLE_TYPE_SINGLE) {
// 单课程测评通过
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_ONE_PASS,
'value' => 1,
];
// 单课程测评通过数
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_ONE_PASS_NUM,
'value' => $count,
];
// 系列课程
} else {
// 系列课程测评通过
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_CHILD_PASS,
'value' => 1,
];
// 单课程测评通过数
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_CHILD_PASS_NUM,
'value' => $count,
];
}
}
}
// 单课程
if ($article['article_type'] == Constant::ARTICLE_TYPE_SINGLE) {
// 单课程测评通过题数百分比
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_ONE_PASS_RATE,
'value' => $post_data['rate'],
];
// 系列课程
} else {
// 系列课程测评通过题数百分比
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_CHILD_PASS_RATE,
'value' => $post_data['rate'],
];
}
$articleHelper = ArticleHelper::instance();
$triggers = $articleHelper->buildTrigger($article, $action_key, $trigger_types);
// 触发积分规则,获得积分数
$result = [];
if (!empty($triggers)) {
$strategyServ = &IntegralStrategy::instance();
$result = $strategyServ->triggerPoint($post_data['uid'], $article['article_id'], $action_key, $triggers);
}
return $result;
}
/**
* 触发子课程测评埋点
* @author zhonglei
* @param array $article 课程数据
* @param array $article_chapter 章节素材数据
* @param array $post_data 请求数据
* @return array
*/
private function _triggerSourceExam($article, $article_chapter, $post_data)
{
$action_key = Constant::INT_ACT_SERIES_EVALUATION;
$trigger_types = [];
// 测评通过
if ($post_data['is_pass'] == Constant::ARTICLE_EXAM_IS_PASS) {
$actionServ = new UserActionService();
$act_result = $actionServ->save($post_data['uid'], $article_chapter['article_chapter_id'], $action_key);
// 首次测评通过
if ($act_result) {
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_SERIES_PASS,
'value' => 1,
];
}
}
// 通过题数百分比
$trigger_types[] = [
'triggerKey' => Constant::INT_TRIGGER_SERIES_PASS_RATE,
'value' => $post_data['rate'],
];
$articleHelper = ArticleHelper::instance();
$triggers = $articleHelper->buildTrigger($article, $action_key, $trigger_types);
$result = [];
if (!empty($triggers)) {
$strategyServ = &IntegralStrategy::instance();
$result = $strategyServ->triggerPoint($post_data['uid'], 'article_chapter_id_' . $article_chapter['article_chapter_id'], $action_key, $triggers);
}
return $result;
}
}