| 1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Competency rule points module.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/competency_rule_points
|
|
|
20 |
* @copyright 2015 Frédéric Massart - FMCorz.net
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
define(['jquery',
|
|
|
25 |
'core/str',
|
|
|
26 |
'core/templates',
|
|
|
27 |
'tool_lp/competency_rule',
|
|
|
28 |
],
|
|
|
29 |
function($, Str, Templates, RuleBase) {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Competency rule points class.
|
|
|
33 |
*/
|
|
|
34 |
var Rule = function() {
|
|
|
35 |
RuleBase.apply(this, arguments);
|
|
|
36 |
};
|
|
|
37 |
Rule.prototype = Object.create(RuleBase.prototype);
|
|
|
38 |
|
|
|
39 |
/** @property {Node} Reference to the container in which the template was included. */
|
|
|
40 |
Rule.prototype._container = null;
|
|
|
41 |
/** @property {Boolean} Whether or not the template was included. */
|
|
|
42 |
Rule.prototype._templateLoaded = false;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* The config established by this rule.
|
|
|
46 |
*
|
|
|
47 |
* @return {String}
|
|
|
48 |
* @method getConfig
|
|
|
49 |
*/
|
|
|
50 |
Rule.prototype.getConfig = function() {
|
|
|
51 |
return JSON.stringify({
|
|
|
52 |
base: {
|
|
|
53 |
points: this._getRequiredPoints(),
|
|
|
54 |
},
|
|
|
55 |
competencies: this._getCompetenciesConfig()
|
|
|
56 |
});
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Gathers the input provided by the user for competencies.
|
|
|
61 |
*
|
|
|
62 |
* @return {Array} Containing id, points and required.
|
|
|
63 |
* @method _getCompetenciesConfig
|
|
|
64 |
* @protected
|
|
|
65 |
*/
|
|
|
66 |
Rule.prototype._getCompetenciesConfig = function() {
|
|
|
67 |
var competencies = [];
|
|
|
68 |
|
|
|
69 |
this._container.find('[data-competency]').each(function() {
|
|
|
70 |
var node = $(this),
|
|
|
71 |
id = node.data('competency'),
|
|
|
72 |
points = parseInt(node.find('[name="points"]').val(), 10),
|
|
|
73 |
required = node.find('[name="required"]').prop('checked');
|
|
|
74 |
|
|
|
75 |
competencies.push({
|
|
|
76 |
id: id,
|
|
|
77 |
points: points,
|
|
|
78 |
required: required ? 1 : 0
|
|
|
79 |
});
|
|
|
80 |
});
|
|
|
81 |
|
|
|
82 |
return competencies;
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Fetches the required points set by the user.
|
|
|
87 |
*
|
|
|
88 |
* @return {Number}
|
|
|
89 |
* @method _getRequiredPoints
|
|
|
90 |
* @protected
|
|
|
91 |
*/
|
|
|
92 |
Rule.prototype._getRequiredPoints = function() {
|
|
|
93 |
return parseInt(this._container.find('[name="requiredpoints"]').val() || 1, 10);
|
|
|
94 |
};
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Return the type of the module.
|
|
|
98 |
*
|
|
|
99 |
* @return {String}
|
|
|
100 |
* @method getType
|
|
|
101 |
*/
|
|
|
102 |
Rule.prototype.getType = function() {
|
|
|
103 |
return 'core_competency\\competency_rule_points';
|
|
|
104 |
};
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Callback to inject the template.
|
|
|
108 |
*
|
|
|
109 |
* @param {Node} container Node to inject in.
|
|
|
110 |
* @return {Promise} Resolved when done.
|
|
|
111 |
* @method injectTemplate
|
|
|
112 |
*/
|
|
|
113 |
Rule.prototype.injectTemplate = function(container) {
|
|
|
114 |
var self = this,
|
|
|
115 |
children = this._tree.getChildren(this._competency.id),
|
|
|
116 |
context,
|
|
|
117 |
config = {
|
|
|
118 |
base: {points: 2},
|
|
|
119 |
competencies: []
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
this._templateLoaded = false;
|
|
|
123 |
|
|
|
124 |
// Only pre-load the configuration when the competency is using this rule.
|
|
|
125 |
if (self._competency.ruletype == self.getType()) {
|
|
|
126 |
try {
|
|
|
127 |
config = JSON.parse(self._competency.ruleconfig);
|
|
|
128 |
} catch (e) {
|
|
|
129 |
// eslint-disable-line no-empty
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
context = {
|
|
|
134 |
requiredpoints: (config && config.base) ? config.base.points : 2,
|
|
|
135 |
competency: self._competency,
|
|
|
136 |
children: []
|
|
|
137 |
};
|
|
|
138 |
|
|
|
139 |
$.each(children, function(index, child) {
|
|
|
140 |
var competency = {
|
|
|
141 |
id: child.id,
|
|
|
142 |
shortname: child.shortname,
|
|
|
143 |
required: false,
|
|
|
144 |
points: 0
|
|
|
145 |
};
|
|
|
146 |
|
|
|
147 |
if (config) {
|
|
|
148 |
$.each(config.competencies, function(index, comp) {
|
|
|
149 |
if (comp.id == competency.id) {
|
|
|
150 |
competency.required = comp.required ? true : false;
|
|
|
151 |
competency.points = comp.points;
|
|
|
152 |
}
|
|
|
153 |
});
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
context.children.push(competency);
|
|
|
157 |
});
|
|
|
158 |
|
|
|
159 |
return Templates.render('tool_lp/competency_rule_points', context).then(function(html) {
|
|
|
160 |
self._container = container;
|
|
|
161 |
container.html(html);
|
|
|
162 |
container.find('input').change(function() {
|
|
|
163 |
self._triggerChange();
|
|
|
164 |
});
|
|
|
165 |
|
|
|
166 |
// We're done, let's trigger a change.
|
|
|
167 |
self._templateLoaded = true;
|
|
|
168 |
self._triggerChange();
|
|
|
169 |
return;
|
|
|
170 |
});
|
|
|
171 |
};
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Whether or not the current config is valid.
|
|
|
175 |
*
|
|
|
176 |
* @return {Boolean}
|
|
|
177 |
* @method isValid
|
|
|
178 |
*/
|
|
|
179 |
Rule.prototype.isValid = function() {
|
|
|
180 |
if (!this._templateLoaded) {
|
|
|
181 |
return false;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
var required = this._getRequiredPoints(),
|
|
|
185 |
max = 0,
|
|
|
186 |
valid = true;
|
|
|
187 |
|
|
|
188 |
$.each(this._getCompetenciesConfig(), function(index, competency) {
|
|
|
189 |
if (competency.points < 0) {
|
|
|
190 |
valid = false;
|
|
|
191 |
}
|
|
|
192 |
max += competency.points;
|
|
|
193 |
});
|
|
|
194 |
|
|
|
195 |
valid = valid && max >= required;
|
|
|
196 |
return valid;
|
|
|
197 |
};
|
|
|
198 |
|
|
|
199 |
return Rule;
|
|
|
200 |
});
|