mirrored from git://git.moodle.org/moodle.git
/
competency_framework.php
273 lines (243 loc) · 8.9 KB
/
competency_framework.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?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/>.
/**
* Class for loading/storing competency frameworks from the DB.
*
* @package tool_lp
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp;
use context;
use lang_string;
use stdClass;
/**
* Class for loading/storing competency frameworks from the DB.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class competency_framework extends persistent {
const TABLE = 'tool_lp_competency_framework';
/** Taxonomy constant. */
const TAXONOMY_BEHAVIOUR = 'behaviour';
/** Taxonomy constant. */
const TAXONOMY_COMPETENCY = 'competency';
/** Taxonomy constant. */
const TAXONOMY_CONCEPT = 'concept';
/** Taxonomy constant. */
const TAXONOMY_DOMAIN = 'domain';
/** Taxonomy constant. */
const TAXONOMY_INDICATOR = 'indicator';
/** Taxonomy constant. */
const TAXONOMY_LEVEL = 'level';
/** Taxonomy constant. */
const TAXONOMY_OUTCOME = 'outcome';
/** Taxonomy constant. */
const TAXONOMY_PRACTICE = 'practice';
/** Taxonomy constant. */
const TAXONOMY_PROFICIENCY = 'proficiency';
/** Taxonomy constant. */
const TAXONOMY_SKILL = 'skill';
/** Taxonomy constant. */
const TAXONOMY_VALUE = 'value';
/**
* Get the context.
*
* @return context The context
*/
public function get_context() {
return context::instance_by_id($this->get_contextid());
}
/**
* Return the definition of the properties of this model.
*
* @return array
*/
protected static function define_properties() {
return array(
'shortname' => array(
'type' => PARAM_TEXT
),
'idnumber' => array(
'type' => PARAM_TEXT
),
'description' => array(
'type' => PARAM_TEXT,
'default' => ''
),
'descriptionformat' => array(
'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
'type' => PARAM_INT,
'default' => FORMAT_HTML
),
'visible' => array(
'type' => PARAM_BOOL,
'default' => 1
),
// TODO MDL-51442 make this mandatory.
'scaleid' => array(
'type' => PARAM_INT,
'default' => 0
),
// TODO MDL-51442 make this mandatory.
'scaleconfiguration' => array(
'type' => PARAM_RAW,
'default' => ''
),
'contextid' => array(
'type' => PARAM_INT
),
'taxonomies' => array(
'type' => PARAM_RAW,
'default' => ''
)
);
}
/**
* Get the translated name for a level.
*
* @param int $level The level of the term.
* @return lang_string
*/
public function get_taxonomy($level) {
$taxonomies = $this->get_taxonomies();
if (empty($taxonomies[$level])) {
// If for some reason we cannot find the level, we fallback onto competency.
$constant = self::TAXONOMY_COMPETENCY;
} else {
$constant = $taxonomies[$level];
}
return self::get_taxonomy_from_constant($constant);
}
/**
* Return the taxonomy constants indexed by level.
*
* @return array Contains the list of taxonomy constants indexed by level.
*/
public function get_taxonomies() {
$taxonomies = explode(',', $this->get('taxonomies'));
// Indexing first level at 1.
array_unshift($taxonomies, null);
unset($taxonomies[0]);
// Ensure that we do not return empty levels.
for ($i = 1; $i <= self::get_taxonomies_max_level(); $i++) {
if (empty($taxonomies[$i])) {
$taxonomies[$i] = self::TAXONOMY_COMPETENCY;
}
}
return $taxonomies;
}
/**
* Convenience method to set taxonomies from an array or string.
*
* @param string|array $taxonomies A string, or an array where the values are the term constants.
*/
public function set_taxonomies($taxonomies) {
if (is_array($taxonomies)) {
$taxonomies = implode(',', $taxonomies);
}
$this->set('taxonomies', $taxonomies);
}
/**
* Validate the context ID.
*
* @param int $value The context ID.
* @return bool|lang_string
*/
public function validate_contextid($value) {
global $DB;
$context = context::instance_by_id($value, IGNORE_MISSING);
if (!$context) {
return new lang_string('invalidcontext', 'error');
} else if ($context->contextlevel != CONTEXT_SYSTEM && $context->contextlevel != CONTEXT_COURSECAT) {
return new lang_string('invalidcontext', 'error');
}
// During update.
if ($this->get_id()) {
// The context must never change.
$oldcontextid = $DB->get_field(self::TABLE, 'contextid', array('id' => $this->get_id()), MUST_EXIST);
if ($this->get_contextid() != $oldcontextid) {
return new lang_string('invalidcontext', 'error');
}
}
return true;
}
/**
* Validate taxonomies.
*
* @param mixed $value The taxonomies.
* @return true|lang_string
*/
protected function validate_taxonomies($value) {
$terms = explode(',', $value);
if (count($terms) > self::get_taxonomies_max_level()) {
return new lang_string('invaliddata', 'error');
}
foreach ($terms as $term) {
if (!empty($term) && !array_key_exists($term, self::get_taxonomies_list())) {
return new lang_string('invalidtaxonomy', 'tool_lp', $term);
}
}
return true;
}
/**
* Get the string of a taxonomy from a constant
*
* @param string $constant The taxonomy constant.
* @return lang_string
*/
public static function get_taxonomy_from_constant($constant) {
return self::get_taxonomies_list()[$constant];
}
/**
* Return the maximum number of taxonomy levels.
*
* This is a method and not a constant because we want to make it easy to adapt
* to the number of levels desired in the future.
*
* @return int
*/
public static function get_taxonomies_max_level() {
return 4;
}
/**
* Get the list of all taxonomies.
*
* @return array Where the key is the taxonomy constant, and the value its translation.
*/
public static function get_taxonomies_list() {
static $list = null;
// At some point we'll have to switch to not using static cache, mainly for Unit Tests in case we
// decide to allow more taxonomies to be added dynamically from a CFG variable for instance.
if ($list === null) {
$list = array(
self::TAXONOMY_BEHAVIOUR => new lang_string('taxonomy_' . self::TAXONOMY_BEHAVIOUR, 'tool_lp'),
self::TAXONOMY_COMPETENCY => new lang_string('taxonomy_' . self::TAXONOMY_COMPETENCY, 'tool_lp'),
self::TAXONOMY_CONCEPT => new lang_string('taxonomy_' . self::TAXONOMY_CONCEPT, 'tool_lp'),
self::TAXONOMY_DOMAIN => new lang_string('taxonomy_' . self::TAXONOMY_DOMAIN, 'tool_lp'),
self::TAXONOMY_INDICATOR => new lang_string('taxonomy_' . self::TAXONOMY_INDICATOR, 'tool_lp'),
self::TAXONOMY_LEVEL => new lang_string('taxonomy_' . self::TAXONOMY_LEVEL, 'tool_lp'),
self::TAXONOMY_OUTCOME => new lang_string('taxonomy_' . self::TAXONOMY_OUTCOME, 'tool_lp'),
self::TAXONOMY_PRACTICE => new lang_string('taxonomy_' . self::TAXONOMY_PRACTICE, 'tool_lp'),
self::TAXONOMY_PROFICIENCY => new lang_string('taxonomy_' . self::TAXONOMY_PROFICIENCY, 'tool_lp'),
self::TAXONOMY_SKILL => new lang_string('taxonomy_' . self::TAXONOMY_SKILL, 'tool_lp'),
self::TAXONOMY_VALUE => new lang_string('taxonomy_' . self::TAXONOMY_VALUE, 'tool_lp'),
);
}
return $list;
}
}