ItemService.class.php
2.52 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 18/3/28
* Time: 15:51
*/
namespace Common\Service;
use Common\Model\ItemModel;
class ItemService extends AbstractService
{
// 构造方法
public function __construct()
{
parent::__construct();
$this->_d = new ItemModel();
}
/**
* 评分项数量、评分类型是否有变化
* @author liyifei
* @param int $article_id 课程ID
* @param array $data 评分项数据
* @return bool
*/
public function diffItem($article_id, $data)
{
if (empty($data) || !is_array($data)) {
return false;
}
// 数据库数据
$db_data = $this->list_by_conds(['article_id' => $article_id]);
if (empty($db_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 int $article_id 课程ID
* @param array $score_item 评分项
* @return bool
*/
public function saveData($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);
}
}
}