mirrored from git://git.moodle.org/moodle.git
/
competency.php
141 lines (120 loc) · 5.42 KB
/
competency.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
<?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/>.
/**
* This file contains the form add/update a competency framework.
*
* @package tool_lp
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\form;
defined('MOODLE_INTERNAL') || die();
use stdClass;
/**
* Competency framework form.
*
* @package tool_lp
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class competency extends persistent {
protected static $persistentclass = 'tool_lp\\competency';
/**
* Define the form - called by parent constructor
*/
public function definition() {
global $PAGE;
$mform = $this->_form;
$framework = $this->_customdata['competencyframework'];
$parent = $this->_customdata['parent'];
$competency = $this->get_persistent();
$mform->addElement('hidden', 'competencyframeworkid');
$mform->setType('competencyframeworkid', PARAM_INT);
$mform->setConstant('competencyframeworkid', $framework->get_id());
$mform->addElement('header', 'generalhdr', get_string('general'));
$mform->addElement('static',
'frameworkdesc',
get_string('competencyframework', 'tool_lp'),
s($framework->get_shortname()));
if ($parent) {
$mform->addElement('hidden', 'parentid');
$mform->setType('parentid', PARAM_INT);
$mform->setConstant('parentid', $parent->get_id());
$mform->addElement('static',
'parentdesc',
get_string('taxonomy_parent_' . $framework->get_taxonomy($parent->get_level()), 'tool_lp'),
s($parent->get_shortname()));
}
$mform->addElement('text', 'shortname',
get_string('shortname', 'tool_lp'));
$mform->setType('shortname', PARAM_TEXT);
$mform->addRule('shortname', null, 'required', null, 'client');
$mform->addElement('editor', 'description',
get_string('description', 'tool_lp'), array('rows' => 4));
$mform->setType('description', PARAM_RAW);
$mform->addElement('text', 'idnumber',
get_string('idnumber', 'tool_lp'));
$mform->setType('idnumber', PARAM_TEXT);
$mform->addRule('idnumber', null, 'required', null, 'client');
$frameworkscale = $framework->get_scale();
$scales = array(null => get_string('inheritfromframework', 'tool_lp')) + get_scales_menu();
$scaleid = $mform->addElement('select', 'scaleid', get_string('scale', 'tool_lp'), $scales);
$mform->setType('scaleid', PARAM_INT);
$mform->addHelpButton('scaleid', 'scale', 'tool_lp');
$mform->addElement('hidden', 'scaleconfiguration', '', array('id' => 'tool_lp_scaleconfiguration'));
$mform->setType('scaleconfiguration', PARAM_RAW);
$scaleconfig = $mform->addElement('button', 'scaleconfigbutton', get_string('configurescale', 'tool_lp'));
$PAGE->requires->js_call_amd('tool_lp/scaleconfig', 'init');
if ($competency && $competency->has_user_competencies()) {
// The scale is used so we "freeze" the element. Though, the javascript code for the scale
// configuration requires this field so we only disable it. It is fine as setting the value
// as a constant will ensure that nobody can change it. And it's validated in the persistent anyway.
$scaleid->updateAttributes(array('disabled' => 'disabled'));
$mform->setConstant('scaleid', $competency->get_scaleid());
}
$this->add_action_buttons(true, get_string('savechanges', 'tool_lp'));
}
/**
* Convert some fields.
*
* @return object
*/
protected static function convert_fields(stdClass $data) {
$data = parent::convert_fields($data);
if (empty($data->scaleid)) {
$data->scaleid = null;
$data->scaleconfiguration = null;
}
return $data;
}
/**
* Extra validation.
*
* @param stdClass $data Data to validate.
* @param array $files Array of files.
* @param array $errors Currently reported errors.
* @return array of additional errors, or overridden errors.
*/
protected function extra_validation($data, $files, array &$errors) {
$newerrors = array();
// Move the error from scaleconfiguration to the form element scale ID.
if (isset($errors['scaleconfiguration']) && !isset($errors['scaleid'])) {
$newerrors['scaleid'] = $errors['scaleconfiguration'];
unset($errors['scaleconfiguration']);
}
return $newerrors;
}
}