-
Notifications
You must be signed in to change notification settings - Fork 3
/
metadatatypes.controller.php
235 lines (199 loc) · 7.55 KB
/
metadatatypes.controller.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
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
232
233
234
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package mod_customlabel
* @category mod
* @author Valery Fremaux <valery.fremaux@gmail.com>
* @copyright (C) 2008 onwards Valery Fremaux (http://www.mylearningfactory.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
*/
defined('MOODLE_INTERNAL') || die();
// Add *********************************************************.
if ($action == 'add') {
$data = $mform->get_data();
$metadatatype = new StdClass;
$metadatatype->type = clean_param($data->type, PARAM_TEXT);
$metadatatype->code = clean_param($data->code, PARAM_ALPHANUM);
$metadatatype->name = clean_param($data->name, PARAM_CLEANHTML);
$metadatatype->description = clean_param($data->description, PARAM_CLEANHTML);
$maxordering = $DB->get_field($CFG->classification_type_table, ' MAX(sortorder) ', array());
$metadatatype->sortorder = $maxordering + 1;
if (!$DB->insert_record($CFG->classification_type_table, $metadatatype)) {
error('Could not insert a new type');
}
redirect($url.'?view=classifiers');
}
// Update ********************************************************.
if ($action == 'update') {
$data = $mform->get_data();
$metadatatype = new StdClass;
$metadatatype->id = clean_param($data->id, PARAM_INT);
$metadatatype->type = clean_param($data->type, PARAM_TEXT);
$metadatatype->code = clean_param($data->code, PARAM_ALPHANUM);
$metadatatype->name = clean_param($data->name, PARAM_CLEANHTML);
$metadatatype->description = clean_param($data->description, PARAM_CLEANHTML);
if (!$DB->update_record($CFG->classification_type_table, $metadatatype)) {
error('Could not insert a new type');
}
redirect($url.'?view=classifiers');
}
// Get a type for editing ******************************************.
if ($action == 'edit') {
$typeid = required_param('typeid', PARAM_INT);
$data = $DB->get_record($CFG->classification_type_table, array('id' => $typeid));
}
// Moves up *************************************************************.
if ($action == 'up') {
$id = required_param('typeid', PARAM_INT);
classification_tree_up($id);
}
// Moves down **********************************************************.
if ($action == 'down') {
$id = required_param('typeid', PARAM_INT);
classification_tree_down($id);
}
// Delete safe (only if not used) ***************************************.
if ($action == 'delete') {
$id = required_param('typeid', PARAM_INT);
$typeorder = $DB->get_field($CFG->classification_type_table, 'sortorder', array('id' => $id));
if (!$DB->delete_records($CFG->classification_type_table, array('id' => $id))) {
print_error('could not delete classifier');
}
// Clear all sub values.
if ($valueids = $DB->get_records_menu($CFG->classification_type_table, array('type' => $id), 'id,id')) {
$DB->delete_records($CFG->classification_value_table, array($CFG->classification_value_type_key => $id));
// Clear constraint records.
$valueidslist = implode("','", array_keys($valueids));
$DB->delete_records_select($CFG->classification_constraint_table, "value1 IN ('$valueidslist') ");
$DB->delete_records_select($CFG->classification_constraint_table, "value2 IN ('$valueidslist') ");
// Clear course assignations.
$DB->delete_records_select($CFG->course_metadata_table, " {$CFG->course_metadata_value_key} IN ('$valueidslist') ");
}
classification_tree_updateordering($typeorder);
}
/**
* updates ordering of a tree branch from a specific node, reordering
* all subsequent siblings.
* @param id the node from where to reorder
* @param table the table-tree
*/
function classification_tree_updateordering($id) {
global $CFG, $DB;
$res = $DB->get_record($CFG->classification_type_table, array('id' => $id));
if (!$res) {
// Fallback : we give the ordering.
$res = new StdClass;
$res->sortorder = $id;
};
// Start reorder from the immediate lower (works from sortorder = 0).
$prev = $res->sortorder - 1;
// Getting subsequent nodes.
$query = "
SELECT
id
FROM
{customlabel_mtd_type}
WHERE
sortorder > {$prev}
ORDER BY
sortorder
";
// Reordering subsequent nodes using an object.
if ( $nextsubs = $DB->get_records_sql($query)) {
$ordering = $res->sortorder;
foreach ($nextsubs as $asub) {
$obj = new StdClass;
$obj->id = $asub->id;
$obj->sortorder = $ordering;
$DB->update_record($CFG->classification_type_table, $obj);
$ordering++;
}
}
}
/**
* raises a node in the tree, reordering all what needed
* @param id the id of the raised node
* @param table the table-tree where to operate
* @return void
*/
function classification_tree_up($id) {
global $CFG, $DB;
$res = $DB->get_record($CFG->classification_type_table, array('id' => $id));
if (!$res) {
return;
}
if ($res->sortorder >= 1) {
$newordering = $res->sortorder - 1;
$query = "
SELECT
id
FROM
{{$CFG->classification_type_table}}
WHERE
sortorder = $newordering
";
$result = $DB->get_record_sql($query);
$resid = $result->id;
// Swapping.
$obj = new StdClass;
$obj->id = $resid;
$obj->sortorder = $res->sortorder;
$DB->update_record($CFG->classification_type_table, $obj);
$obj = new StdClass;
$obj->id = $id;
$obj->sortorder = $newordering;
$DB->update_record($CFG->classification_type_table, $obj);
}
}
/**
* lowers a node on its branch. this is done by swapping ordering.
* @param id the node id
* @param istree if not set, performs swapping on a single list
*/
function classification_tree_down($id) {
global $CFG, $DB;
$res = $DB->get_record($CFG->classification_type_table, array('id' => $id));
$query = "
SELECT
MAX(sortorder) AS sortorder
FROM
{{$CFG->classification_type_table}}
";
$resmaxordering = $DB->get_record_sql($query);
$maxordering = $resmaxordering->sortorder;
if ($res->sortorder < $maxordering) {
$newordering = $res->sortorder + 1;
$query = "
SELECT
id
FROM
{{$CFG->classification_type_table}}
WHERE
sortorder = $newordering
";
$result = $DB->get_record_sql($query);
$resid = $result->id;
// Swapping.
$obj = new StdClass;
$obj->id = $resid;
$obj->sortorder = $res->sortorder;
$DB->update_record($CFG->classification_type_table, $obj);
$obj = new StdClass;
$obj->id = $id;
$obj->sortorder = $newordering;
$DB->update_record($CFG->classification_type_table, $obj);
}
}