forked from moodlemoot/taxonomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
203 lines (151 loc) · 4.81 KB
/
lib.php
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
<?php
function taxonomy_vocabulary_list() {
global $DB;
return $DB->get_records_sql('SELECT * FROM {taxonomy_vocabulary} ORDER by weight desc');
}
/**
* Load vocabulary by id
*
* @param int $id
* @return mixed returns vocabulary database record or false if none found
*/
function taxonomy_vocabulary_load($id) {
global $DB;
return $DB->get_record('taxonomy_vocabulary', array('id' => $id));
}
function taxonomy_vocabulary_create($vocabulary) {
global $DB;
if (!isset($vocabulary->name)) {
throw new coding_exception(get_string('missingvocabularyname','local_taxonomy'));
}
if (!isset($vocabulary->shortname)) {
throw new coding_exception(get_string('missingvocabularyshortname','local_taxonomy'));
}
if (!isset($vocabulary->description)) {
$vocabulary->description = '';
}
if (!isset($vocabulary->weight)) {
$vocabulary->weight = 0;
}
$vocabulary->id = $DB->insert_record('taxonomy_vocabulary', $vocabulary);
// trigger event
/*
$event = \local\taxonomy\vocabulary_created::create(array(
'objectid' => $vocabulary->id,
));
$event->add_record_snapshot('taxonomy_vocabulary', $vocabulary);
$event->trigger();
*/
return $vocabulary->id;
}
function taxonomy_vocabulary_update($vocabulary) {
global $DB;
$result = $DB->update_record('taxonomy_vocabulary', $vocabulary);
// trigger event
/*
$event = \local\taxonomy\vocabulary_updated::create(array(
'objectid' => $vocabulary->id,
));
$event->add_record_snapshot('taxonomy_vocabulary', $vocabulary);
$event->trigger();
*/
return $result;
}
function taxonomy_vocabulary_delete($vocabulary) {
global $DB;
// delete terms
$result = $DB->delete_records('taxonomy_term', array('vid' => $vocabulary->id));
// delete vocabulary
$result = $DB->delete_records('taxonomy_vocabulary', array('id' => $vocabulary->id));
// trigger event
/*
$event = \local\taxonomy\vocabulary_deleted::create(array(
'objectid' => $vocabulary->id,
));
$event->add_record_snapshot('taxonomy_vocabulary', $vocabulary);
$event->trigger();
*/
return $result;
}
function taxonomy_term_list($vid) {
global $DB;
return $DB->get_records_sql('SELECT * FROM {taxonomy_term} WHERE vid = ? ORDER by weight desc', array($vid));
}
function taxonomy_term_load($id) {
global $DB;
return $DB->get_record('taxonomy_term', array('id' => $id));
}
function taxonomy_term_create($term) {
global $DB;
if (!isset($term->name)) {
throw new coding_exception(get_string('missingtermname','local_taxonomy'));
}
if (!isset($term->shortname)) {
throw new coding_exception(get_string('missingtermshortname','local_taxonomy'));
}
if (!isset($term->description)) {
$term->description = '';
}
if (!isset($term->weight)) {
$term->weight = 0;
}
$term->id = $DB->insert_record('taxonomy_term', $term);
// trigger event
/*
$event = \local\taxonomy\term_created::create(array(
'objectid' => $term->id,
));
$event->add_record_snapshot('taxonomy_term', $term);
$event->trigger();
*/
return $term->id;
}
function taxonomy_term_update($term) {
global $DB;
$result = $DB->update_record('taxonomy_term', $term);
// trigger event
/*
$event = \local\taxonomy\term_updated::create(array(
'objectid' => $term->id,
));
$event->add_record_snapshot('taxonomy_term', $term);
$event->trigger();
*/
return $result;
}
function taxonomy_term_delete($term) {
global $DB;
$result = $DB->delete_records('taxonomy_term', array('id' => $term->id));
// trigger event
/*
$event = \local\taxonomy\term_deleted::create(array(
'objectid' => $term->id,
));
$event->add_record_snapshot('taxonomy_term', $term);
$event->trigger();
*/
return $result;
}
function taxonomy_term_data_load($term_data) {
global $DB;
return $DB->get_record('taxonomy_term_data', array('id' => $term_data->id));
}
function taxonomy_term_data_add($term_data) {
global $DB;
if ( false === taxonomy_term_load($term_data->term_id)) {
throw new coding_exception(get_string('term:notfound','local_taxonomy'));
}
if ( !isset($term_data->instance_id) ) {
throw new coding_exception( get_string('instance:missing','local_taxonomy') );
}
if ( !isset($term_data->component) ) {
throw new coding_exception( get_string('component:missing','local_taxonomy') );
}
$term_data->id = $DB->insert_record('taxonomy_term_data', $term_data);
return $term_data->id;
}
function taxonomy_term_data_remove($term_data) {
global $DB;
$result = $DB->delete_records('taxonomy_term_data', array('id' => $term_data->id));
return $result;
}