-
Notifications
You must be signed in to change notification settings - Fork 2
/
classificationlib.php
206 lines (182 loc) · 7.95 KB
/
classificationlib.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
<?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 for classification manipulations.
*
* @package mod_sharedresource
* @author Frederic GUILLOU
* @author Valery Fremaux <[email protected]>
* @copyright Valery Fremaux (activeprolearn.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/local/sharedresources/classes/navigator.class.php');
use local_sharedresources\browser\navigation;
/*
* This php script contains all the stuff to use classifications
* This is used in the metadata form of a sharedresource and
* in the search engine of a sharedresource.
*/
/**
* get all classification options, recursively
* @see metadata_form.php
* @param int $taxonomyid the taxonomy id. If 0, retrieves all active taxonomies.
* @return an optgroup structure for a list
*/
function metadata_get_classification_options($taxonomyid = 0) {
$taxonomies = navigation::get_taxonomies();
if ($taxonomyid != 0) {
$taxonomy = $taxonomies[$taxonomyid];
$classification = new \local_sharedresources\browser\navigation($taxonomy);
$options = $classification->get_full_tree('flat');
return $options;
}
$optgroups = [];
foreach ($taxonomies as $taxonomy) {
$classification = new \local_sharedresources\browser\navigation($taxonomy);
if (!$classification->can_use()) {
continue;
}
$optgroup = $classification->get_full_tree('flat');
$optgroups[][$taxonomy->name] = $optgroup;
}
return $optgroups;
}
/**
* Recursive exploration of the classification to form a select option set.
* @param string $name the classification domain name
* @param object $finalclassif the whole classification tree
* @param $id
* @param $path
* @param $selectedlabel which option is actually selected
*/
function metadata_get_classification_option_rec($name, $finalclassif, $id, $path='', $selectedlabel='') {
$str = '';
foreach ($finalclassif[$id]['childs'] as $ordering => $taxonid) {
if (in_array($taxonid, $classifarray[$name]['taxonselect'])) {
$temppath = $path.'/'.$finalclassif[$taxonid]['label'];
if ($name.':'.$taxonid == substr($selectedlabel, 0, strripos($selectedlabel, ':'))) {
$str .= '<option selected="selected" value="'.$name.':'.$taxonid.':'.$temppath.'">'.$temppath.'</option>';
} else {
$str .= '<option value="'.$name.':'.$taxonid.':'.$temppath.'">'.$temppath.'</option>';
}
if (!empty($finalclassif[$taxonid]['childs'])) {
$str .= metadata_print_classification_options_rec($name, $classifarray, $finalclassif, $taxonid, $temppath,
$selectedlabel);
}
}
}
return $str;
}
/**
* print_classif2 and print_classification_childs print all classifications, displaying successively SELECT (used in
* the search form)
* @param array $classifarray
*/
function print_classif2($classifarray) {
$str = '';
if (!empty($classifarray)) {
foreach ($classifarray as $name => $infos) {
if ($infos['select'] == 1) {
$str .= '<option class="sharedresource-listsection" value="'.$name.'">'.$infos['classname'].'</option>';
}
}
}
return $str;
}
/**
* Print classification childs
* @param string $name
* @param int $num
* @param string $key
* @param object $classif
* @param mixed $value
*/
function print_classification_childs($name, $num, $key, $classif, $value) {
global $CFG, $DB;
$str = '';
if ($name == 'defaultvalue') {
return;
}
$config = get_config('sharedresource');
$classifarray = unserialize($config->classifarray);
// If we are searching for taxons just after choosing a classification (taxons without parents).
if (array_key_exists($name, $classifarray)) {
if ($classifarray[$name]['restriction'] == '') {
$classtable = $DB->get_records($name);
} else {
$classtable = $DB->get_records_sql("SELECT * FROM {{$name}} WHERE ".$classifarray[$name]['restriction']);
}
$finalclassif = metadata_create_classification($classtable, $classifarray, $name);
$restriction = $classifarray[$name]['taxonselect'];
if (!empty($finalclassif[$name]['childs'])) {
$str .= '<select name=classif:'.$num;
$str .= ' onChange="javascript:classif(\''.$CFG->wwwroot.'\', this.options[selectedIndex].text,'.($num + 1).',';
if ($key != '') {
$str .= '\''.$key.'/\'+this.options[selectedIndex].text,\'';
$str .= $classif.'\',this.options[this.selectedIndex].value);">';
} else {
$str .= 'this.options[selectedIndex].text,\''.$classif.'\',this.options[this.selectedIndex].value);">';
}
$str .= '<option selected value="basicvalue"> </option>';
foreach ($finalclassif[$name]['childs'] as $ordering => $id) {
if (in_array($id, $restriction)) {
if ($key != '') {
$tempkey = $key.'/'.$finalclassif[$id]['label'];
} else {
$tempkey = $finalclassif[$id]['label'];
}
$str .= '<option value="'.$id.'\\'.$tempkey.'">'.$finalclassif[$id]['label'].'</option>';
}
}
$str .= '</select>';
}
// If we are searching the childs of a taxon.
} else {
if (!empty($classif)) {
if ($classifarray[$classif]['restriction'] == '') {
$classtable = $DB->get_records($classif);
} else {
$classtable = $DB->get_records_sql($classifarray[$classif]['restriction']);
}
$finalclassif = metadata_create_classification($classtable, $classifarray, $classif);
$restriction = $classifarray[$classif]['taxonselect'];
if (!empty($finalclassif[substr($value, 0, strpos($value, '\\'))]['childs'])) {
$str .= '<select name=classif:'.$num.' onChange="javascript:classif(\'';
$str .= $CFG->wwwroot.'\', this.options[selectedIndex].text,'.($num + 1).',';
if ($key != '') {
$str .= '\''.$key.'/\'+this.options[selectedIndex].text,\'';
$str .= $classif.'\',this.options[this.selectedIndex].value);">';
} else {
$str .= 'this.options[selectedIndex].text,\''.$classif.'\',this.options[this.selectedIndex].value);">';
}
$str .= '<option selected value="basicvalue"> </option>';
foreach ($finalclassif[substr($value, 0, strpos($value, '\\'))]['childs'] as $ordering => $label) {
if (in_array($label, $restriction)) {
if ($key != '') {
$tempkey = $key.'/'.$finalclassif[$label]['label'];
} else {
$tempkey = $finalclassif[$label]['label'];
}
$str .= '<option value="'.$label.'\\'.$tempkey.'">'.$finalclassif[$label]['label'].'</option>';
}
}
$str .= '</select>';
}
}
}
return $str;
}