1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Competency rule points module.
20 * @copyright 2015 Frédéric Massart - FMCorz.net
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 'tool_lp/competency_rule',
29 function($, Str, Templates, RuleBase) {
32 * Competency rule points class.
34 var Rule = function() {
35 RuleBase.apply(this, arguments);
37 Rule.prototype = Object.create(RuleBase.prototype);
39 /** @type {Node} Reference to the container in which the template was included. */
40 Rule.prototype._container = null;
41 /** @type {Boolean} Whether or not the template was included. */
42 Rule.prototype._templateLoaded = false;
45 * The config established by this rule.
50 Rule.prototype.getConfig = function() {
51 return JSON.stringify({
53 points: this._getRequiredPoints(),
55 competencies: this._getCompetenciesConfig()
60 * Gathers the input provided by the user for competencies.
62 * @return {Array} Containing id, points and required.
63 * @method _getCompetenciesConfig
66 Rule.prototype._getCompetenciesConfig = function() {
67 var competencies = [];
69 this._container.find('[data-competency]').each(function() {
71 id = node.data('competency'),
72 points = parseInt(node.find('[name="points"]').val(), 10),
73 required = node.find('[name="required"]').prop('checked');
78 required: required ? 1 : 0
86 * Fetches the required points set by the user.
89 * @method _getRequiredPoints
92 Rule.prototype._getRequiredPoints = function() {
93 return parseInt(this._container.find('[name="requiredpoints"]').val() || 1, 10);
97 * Return the type of the module.
102 Rule.prototype.getType = function() {
103 return 'core_competency\\competency_rule_points';
107 * Callback to inject the template.
109 * @param {Node} container Node to inject in.
110 * @return {Promise} Resolved when done.
111 * @method injectTemplate
113 Rule.prototype.injectTemplate = function(container) {
115 children = this._tree.getChildren(this._competency.id),
122 this._templateLoaded = false;
124 // Only pre-load the configuration when the competency is using this rule.
125 if (self._competency.ruletype == self.getType()) {
127 config = JSON.parse(self._competency.ruleconfig);
133 requiredpoints: (config && config.base) ? config.base.points : 2,
134 competency: self._competency,
138 $.each(children, function(index, child) {
141 shortname: child.shortname,
147 $.each(config.competencies, function(index, comp) {
148 if (comp.id == competency.id) {
149 competency.required = comp.required ? true : false;
150 competency.points = comp.points;
155 context.children.push(competency);
158 return Templates.render('tool_lp/competency_rule_points', context).then(function(html) {
159 self._container = container;
160 container.html(html);
161 container.find('input').change(function() {
162 self._triggerChange();
165 // We're done, let's trigger a change.
166 self._templateLoaded = true;
167 self._triggerChange();
172 * Whether or not the current config is valid.
177 Rule.prototype.isValid = function() {
178 if (!this._templateLoaded) {
182 var required = this._getRequiredPoints(),
186 $.each(this._getCompetenciesConfig(), function(index, competency) {
187 if (competency.points < 0) {
190 max += competency.points;
193 valid = valid && max >= required;
197 return /** @alias module:tool_lp/competency_rule_all */ Rule;