CourseItemService.class.php
3.96 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 18/5/16
* Time: 17:46
*/
namespace Common\Service;
use Common\Model\CourseItemModel;
use Common\Common\Constant;
class CourseItemService extends AbstractService
{
public function __construct()
{
parent::__construct();
$this->_d = new CourseItemModel();
}
/**
* 评分项数量、评分类型是否有变化
* @author liyifei
* @param int $article_id 课程ID
* @param array $data 评分项数据
* @return bool
*/
public function diff_item($article_id, $data)
{
// 数据库数据
$db_data = $this->list_by_conds(['article_id' => $article_id]);
if (empty($db_data) && empty($data)) {
return true;
}
// 评分项数量不可修改
$item_ids = array_column($data, 'item_id');
$db_item_ids = array_column($db_data, 'item_id');
if (array_diff($item_ids, $db_item_ids) || count($item_ids) != count($db_item_ids)) {
return false;
}
// 评分类型不可修改
$db_data = array_combine_by_key($db_data, 'item_id');
foreach ($data as $v) {
$item_id = $v['item_id'];
if ($v['score_type'] != $db_data[$item_id]['score_type']) {
return false;
}
}
return true;
}
/**
* 校验评分设置项
* @author liyifei
* @param array $data 评分项数据
* @return bool
*/
public function check_data($data)
{
// 评分项参数值格式错误
if (!is_array($data)) {
E('_ERR_SCORE_ITEM_PARAM_FORMAT');
}
// 评分项限制最多添加9个
if (count($data) > Constant::TEACHER_SCORE_ITEM_MAX) {
E('_ERR_SCORE_ITEM_LENGTH');
}
// 完整评分项参数
$allow_score_item = [
'item_id',
'content',
'score_type',
'score_order',
];
// 可设置的评分类型范围
$allow_score_type = [
Constant::TEACHER_SCORE_TYPE_CONTENT,
Constant::TEACHER_SCORE_TYPE_TEACHER,
];
foreach ($data as $item) {
// 评分项参数不完整
$item_key = array_keys($item);
if (array_diff($allow_score_item, $item_key)) {
E('_ERR_SCORE_ITEM_PARAM_LOST');
}
// 评分内容最多15字符
if (mb_strlen($item['content']) > Constant::TEACHER_SCORE_ITEM_CONTENT_MAX) {
E('_ERR_SCORE_ITEM_CONTENT_LENGTH');
}
// 评分类型仅支持课程内容、课程讲师
if (!in_array($item['score_type'], $allow_score_type)) {
E('_ERR_SCORE_ITEM_RANGE');
}
}
return true;
}
/**
* 保存评分设置项
* @author liyifei
* @param int $article_id 课程ID
* @param array $score_item 评分项
* @return bool
*/
public function save_item($article_id, $score_item)
{
$insert_data = [];
foreach ($score_item as $item) {
// 新增数据
if ($item['item_id'] == 0) {
$insert_data[] = [
'article_id' => $article_id,
'content' => $item['content'],
'score_type' => $item['score_type'],
'score_order' => $item['score_order'],
];
// 修改数据(发布原草稿模式的培训时,可以修改评分类型)
} else {
$data = [
'content' => $item['content'],
'score_type' => $item['score_type'],
'score_order' => $item['score_order'],
];
$this->update($item['item_id'], $data);
}
}
if (!empty($insert_data)) {
$this->insert_all($insert_data);
}
return true;
}
}