ScoreSaveController.class.php
5.12 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2018-3-29
* Time: 15:06:45
*/
namespace Api\Controller\Course;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\CourseArticleService;
use Common\Service\CourseItemService;
use Common\Service\CourseScoreDetailService;
use Common\Service\CourseScoreRecordService;
class ScoreSaveController extends \Api\Controller\AbstractController
{
/**
* ScoreSave
* @author tangxingguo
* @desc 评分保存接口
* @param Int article_id:true 课程ID
* @param Int item[].item_id:true 评分项ID
* @param Int item[].score_value:true 分值
* @return Array
*/
public function Index_post()
{
$rules = [
'article_id' => 'require|integer',
'item' => 'require|array',
];
// 验证请求数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
$articleId = $postData['article_id'];
// 取课程
$articleServ = new CourseArticleService();
$article = $articleServ->get_by_conds([
'article_id' => $articleId,
'is_score' => Constant::TEACHER_IS_SCORE_OPEN]);
if (empty($article)) {
E('_ERR_ARTICLE_DATA_NOT_FOUND');
}
// 是否已评
$scoreRecordServ = new CourseScoreRecordService();
$count = $scoreRecordServ->count_by_conds(['article_id' => $articleId, 'uid' => $this->uid]);
if ($count > 0) {
E('_ERR_COURSE_SCORE_REPEAT');
}
// 格式化评分
$data = $this->_formatPostData($postData);
// 评分记录保存
$scoreRecordServ->insert_all($data['record']);
// 个人平均分保存
$scoreDetailServ = new CourseScoreDetailService();
$scoreDetailServ->insert($data['detail']);
// 更新评分项
$itemServ = new CourseItemService();
foreach ($data['record'] as $record) {
$itemServ->update_by_conds(['item_id' => $record['item_id']], [
'score_total = score_total + ?' => $record['score_value'],
'user_total = user_total + ?' => 1
]);
}
}
/**
* 请求数据格式化
* @author tangxingguo
* @param $postData
* @return array
*/
private function _formatPostData($postData)
{
$data = [
// 记录列表
'record' => [],
// 个人平均分
'detail' => [],
// 总分
'score_total' => 0,
// 内容总分
'content_score_total' => 0,
// 讲师总分
'teacher_score_total' => 0,
];
// 课程评分项
$articleId = $postData['article_id'];
$itemServ = new CourseItemService();
$itemList = $itemServ->list_by_conds(['article_id' => $articleId]);
if (empty($itemList)) {
E('_ERR_SCORE_ITEM_NOT_FOUND');
}
// 评分项检查
if (array_diff(array_column($itemList, 'item_id'), array_column($postData['item'], 'item_id'))) {
E('_ERR_SCORE_ITEM_NOT_MATCHING');
}
// 讲师评分项个数
$teacherItemCount = 0;
// 内容评分项个数
$contentItemCount = 0;
// 格式化评分
$itemList = array_combine_by_key($itemList, 'item_id');
foreach ($postData['item'] as $v) {
// 评分值检查
if (!isset($v['score_value']) || !is_numeric($v['score_value']) ||
$v['score_value'] < 1 || $v['score_value'] > 5) {
E('_ERR_SCORE_VALUE_ERROR');
}
// 内容评分
if ($itemList[$v['item_id']]['score_type'] == Constant::TEACHER_SCORE_TYPE_CONTENT) {
$data['content_score_total'] += $v['score_value'];
$contentItemCount++;
// 讲师评分
} elseif ($itemList[$v['item_id']]['score_type'] == Constant::TEACHER_SCORE_TYPE_TEACHER) {
$data['teacher_score_total'] += $v['score_value'];
$teacherItemCount ++;
}
// 总评分
$data['score_total'] += $v['score_value'];
// 评分记录
$data['record'][] = [
'article_id' => $articleId,
'uid' => $this->uid,
'username' => $this->_login->user['memUsername'],
'item_id' => $v['item_id'],
'score_type' => $itemList[$v['item_id']]['score_type'],
'score_value' => $v['score_value'],
];
}
// 个人平均分
$data['detail'] = [
'article_id' => $articleId,
'uid' => $this->uid,
'username' => $this->_login->user['memUsername'],
// 内容平均分
'content_score' => $data['content_score_total'] > 0 ? $data['content_score_total'] / $contentItemCount : 0,
// 讲师平均分
'teacher_score' => $data['teacher_score_total'] > 0 ? $data['teacher_score_total'] / $teacherItemCount : 0,
];
return $data;
}
}