-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtreelib.php
150 lines (136 loc) · 5.12 KB
/
treelib.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
<?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/>.
/**
* Library of tree dedicated operations.
*
* @package mod_sharedresource
* @author Valery Fremaux <[email protected]>
* @author LUU Tao Meng, So Gerard (parts of treelib.php), Guillaume Magnien, Olivier Petit
* @copyright Valery Fremaux (activeprolearn.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
/**
* deletes into tree a full branch. note that it will work either
* @param int $id the root node id
* @param string $table the table where the tree is in
* @param boolean $istree if istree is not set, considers table as a simple ordered list
* @return an array of deleted ids
*/
function sharedresource_tree_delete($id, $table, $istree = 1) {
sharedresource_tree_updatesortorder($id, $table, $istree);
return sharedresource_tree_delete_rec($id, $table, $istree);
}
/**
* deletes recursively a node and its subnodes. this is the recursion deletion
* @param int $id
* @param object $table
* @param bool $istree
* @param string $predeletecallback
* @param string $postdeletecallback
* @return an array of deleted ids
*/
function sharedresource_tree_delete_rec($id, $table, $istree, $predeletecallback = '', $postdeletecallback = '') {
global $DB;
$deleted = [];
if (empty($id)) {
return $deleted;
}
// Getting all subnodes to delete if is tree.
if ($istree) {
$sql = "
SELECT
id,id
FROM
{{$table}}
WHERE
parent = {$id}
";
// Deleting subnodes if any.
if ($subs = $DB->get_records_sql($sql)) {
foreach ($subs as $asub) {
if (!empty($predeletecallback)) {
$predeletecallback($asub);
}
$deleted = array_merge($deleted, tree_delete_rec($asub->id, $table, $istree));
if (!empty($postdeletecallback)) {
$postdeletecallback($asub);
}
}
}
}
// Deleting current node.
$DB->delete_records($table, ['id' => $id]);
$deleted[] = $id;
return $deleted;
}
/**
* raises a node in the tree, resortorder all what needed
* @param int $id the id of the raised node
* @param object $classif classification
* @return void
*/
function sharedresource_tree_up($id, $classif) {
global $DB;
$res = $DB->get_record('sharedresource_taxonomy', ['id' => $id]);
if (!$res) {
return;
}
if ($res->sortorder > $classif->sqlsortorderstart) {
$result = false;
$newsortorder = $res->sortorder - 1;
$select = " classificationid = ? AND sortorder = ? AND parent = ? ORDER BY sortorder";
$params = [$res->classificationid, $newsortorder, $res->parent];
if ($resid = $DB->get_field_select('sharedresource_taxonomy', 'id', $select, $params)) {
// Swapping.
$object = new StdClass();
$object->id = $resid;
$object->sortorder = $res->sortorder;
$DB->update_record('sharedresource_taxonomy', $object);
}
$object = new StdClass();
$object->id = $id;
$object->sortorder = $newsortorder;
$DB->update_record('sharedresource_taxonomy', $object);
}
}
/**
* lowers a node on its branch. this is done by swapping sortorder.
* @param int $id the node id
s * @param boolean $istree if not set, performs swapping on a single list
*/
function sharedresource_tree_down($id) {
global $DB;
$res = $DB->get_record('sharedresource_taxonomy', ['id' => $id]);
$select = " parent = ? AND classificationid = ? ";
$params = [$res->parent, $res->classificationid];
$maxsortorder = $DB->get_field_select('sharedresource_taxonomy', " MAX(sortorder) ", $select, $params);
if ($res->sortorder < $maxsortorder) {
$newsortorder = $res->sortorder + 1;
$select = " classificationid = ? AND sortorder = ? AND parent = ? ";
$params = [$res->classificationid, $newsortorder, $res->parent];
if ($resid = $DB->get_field_select('sharedresource_taxonomy', 'id', $select, $params)) {
// Swapping.
$object = new StdClass;
$object->id = $resid;
$object->sortorder = $res->sortorder;
$DB->update_record('sharedresource_taxonomy', $object);
}
$object = new StdClass;
$object->id = $id;
$object->sortorder = $newsortorder;
$DB->update_record('sharedresource_taxonomy', $object);
}
}