CommentOptionsService.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
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
<?php
/**
* 评价选项表Service
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-08-29 16:31:28
* @version $Id$
*/
namespace Common\Service;
use Common\Model\CommentOptionsModel;
use Common\Model\CompositeScoreModel;
class CommentOptionsService extends AbstractService
{
// 培训过程
const OPTION_TYPE_PROCESS = 1;
// 培训内容
const OPTION_TYPE_CONTENT = 2;
// 培训讲师
const OPTION_TYPE_TEACHER = 3;
// 评价选项操作:添加
const OPTION_ADD = 'add';
// 评价选项操作:编辑
const OPTION_EDIT = 'edit';
// 评价选项最多9项
const OPTION_MAX_COUNT = 9;
/** @var CommentOptionsModel */
protected $_d;
/** @var CompositeScoreModel */
protected $composite_score_d;
public function __construct()
{
$this->_d = new CommentOptionsModel();
$this->composite_score_d = new CompositeScoreModel();
parent::__construct();
}
/**
* 验证评价选项
*
* @author houyingcai
*
* @param array $data 评价选项数组
*
* @return bool
*/
public function validate_com_opt_list($data = [])
{
if (!is_array($data)) {
E('_ERR_OPTION_INVALID'); // 评价选项参数无效
}
if (count($data) > self::OPTION_MAX_COUNT) {
E('_ERR_OPTION_MAX_COUNT'); // 评价选项最多9项
}
// 临时存放选项名称数组,用于判断名称是否有重复
$temp_option_names = [];
foreach ($data as $v) {
if (empty($v['option_name'])) {
E('_EMPTY_OPTION_NAME'); // 选项名称不能为空
}
if (!empty($temp_option_names) && in_array($v['option_name'], $temp_option_names)) {
E('_ERR_OPTION_NAME_REPEAT'); // 选项名称有重复
}
if (empty($v['option_type'])) {
E('_EMPTY_OPTION_TYPE'); // 评价类型不能为空
}
if (!in_array($v['option_type'],
[self::OPTION_TYPE_PROCESS, self::OPTION_TYPE_CONTENT, self::OPTION_TYPE_TEACHER])
) {
E('_ERR_OPTION_TYPE_INVALID'); // 无效的评价类型
}
if (!intval($v['option_score'])) {
E('_EMPTY_OPTION_SCORE'); // 评价分值不能为空
}
$temp_option_names[] = $v['option_name'];
}
return true;
}
/**
* 保存评价选项
*
* @author houyingcai
*
* @param int $ed_id 培训ID
* @param array $options 评价选项数组
* @param string $type add:新增(默认),edit:编辑
*
* @return bool
*/
public function save_comment_option($ed_id = 0, $options = [], $type = 'add')
{
// 验证参数是否正确
if (!$ed_id || empty($options) || !is_array($options)) {
return false;
}
if (self::OPTION_EDIT == $type) {
$this->delete_by_conds(['ed_id' => $ed_id]);
}
// 组装数据
$insert_data = [];
foreach ($options as $k => &$v) {
$v['option_order'] = $k + 1;
$v['option_name'] = !empty($v['option_name']) ? raddslashes($v['option_name']) : '';
$v['option_type'] = $v['option_type'] ? intval($v['option_type']) : 0;
$v['option_score'] = $v['option_score'] ? intval($v['option_score']) : 0;
$insert_data[] = array_merge(['ed_id' => $ed_id], $v);
}
// 批量插入新增数据
if (!empty($insert_data)) {
$row = $this->insert_all($insert_data);
if (!$row) {
E('_ERR_COMMENT_OPTION_ADD');
}
}
return true;
}
/**
* 获取培训综合评分信息
* @author wanghuan
*
* @param int $ed_id 培训id
*
* @return array 综合评分、各评分项综合评分
*/
public function get_composite_score($ed_id = 0)
{
// 综合评分
$score = 0;
$composite_cond['ed_id'] = $ed_id;
// 培训各项综合评分
$composite_score = $this->composite_score_d->list_by_conds($composite_cond);
// 评分项总数
$options_count = count($composite_score);
// 总数不为0,计算综合评分
if ($options_count) {
// 取综合分值列
$options_score = array_column($composite_score, 'score');
// 综合分值之和
$score_sum = array_sum($options_score);
if ($score_sum) {
// 综合评分
$score = round(($score_sum / $options_count), 1);
}
}
// 培训评分项综合分
$list = [];
// 查询条件
$options_cond['ed_id'] = $ed_id;
// 分页
$page_option = null;
// 排序
$order_option = [
'option_order' => 'ASC'
];
// 培训评分项
$options = $this->_d->list_by_conds($options_cond, $page_option, $order_option);
// 初始化选项总分
$total_score = 0;
if (!empty($options)) {
// 各评分项综合分
$composite_score = array_combine_by_key($composite_score, 'option_id');
// 综合评分信息
$comment_score = 0;
// 返回数据格式化
foreach ($options as $key => $val) {
if (!empty($composite_score)) {
$composite = $composite_score[$val['option_id']];
$comment_score = $composite['score'];
}
$list[$key] = [
'option_id' => intval($val['option_id']),
'option_name' => $val['option_name'],
'option_score' => $val['option_score'],
'option_type' => $val['option_type'],
'comment_score' => round($comment_score, 1)
];
// 获取选项总分
$total_score = $val['option_score'];
}
}
return [
'score' => $score,
'options_score' => $list,
'total_score' => $total_score
];
}
}