forked from vfremaux/moodle-mod_customlabel
/
metadatatypes.controller.php
200 lines (172 loc) · 6.11 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
<?php
/************************************* 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){
// getting ordering type of the current node
global $CFG;
$res = $DB->get_record($CFG->classification_type_table, array('id' => $id));
if (!$res) { // fallback : we give the ordering
$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){
$objet->id = $asub->id;
$objet->sortorder = $ordering;
$DB->update_record($CFG->classification_type_table, $objet);
$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;
$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
";
// echo $query;
$result = $DB->get_record_sql($query);
$resid = $result->id;
// swapping
$objet->id = $resid;
$objet->sortorder = $res->sortorder;
$DB->update_record($CFG->classification_type_table, $objet);
$objet->id = $id;
$objet->sortorder = $newordering;
$DB->update_record($CFG->classification_type_table, $objet);
}
}
/**
* 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;
$res = $DB->get_record($CFG->classification_type_table, array('id' => $id));
$query = "
SELECT
MAX(sortorder) AS sortorder
FROM
{{$CFG->classification_type_table}}
";
// echo $query;
$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
$objet->id = $resid;
$objet->sortorder = $res->sortorder;
$DB->update_record($CFG->classification_type_table, $objet);
$objet->id = $id;
$objet->sortorder = $newordering;
$DB->update_record($CFG->classification_type_table, $objet);
}
}
?>