db4bfe42269e023112bc5f2255eb6b30e7697186
[moodle.git] / admin / tool / lp / amd / src / competency_rule_points.js
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/>.
16 /**
17  * Competency rule points module.
18  *
19  * @package    tool_lp
20  * @copyright  2015 Frédéric Massart - FMCorz.net
21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 define(['jquery',
25         'core/str',
26         'core/templates',
27         'tool_lp/competency_rule',
28         ],
29         function($, Str, Templates, RuleBase) {
31     /**
32      * Competency rule points class.
33      */
34     var Rule = function() {
35         RuleBase.apply(this, arguments);
36     };
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;
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     };
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 = [];
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');
75             competencies.push({
76                 id: id,
77                 points: points,
78                 required: required ? 1 : 0
79             });
80         });
82         return competencies;
83     };
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     };
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     };
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             };
122         this._templateLoaded = false;
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         }
133         context = {
134             requiredpoints: (config && config.base) ? config.base.points : 2,
135             competency: self._competency,
136             children: []
137         };
139         $.each(children, function(index, child) {
140             var competency = {
141                 id: child.id,
142                 shortname: child.shortname,
143                 required: false,
144                 points: 0
145             };
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             }
156             context.children.push(competency);
157         });
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             });
166             // We're done, let's trigger a change.
167             self._templateLoaded = true;
168             self._triggerChange();
169         });
170     };
172     /**
173      * Whether or not the current config is valid.
174      *
175      * @return {Boolean}
176      * @method isValid
177      */
178     Rule.prototype.isValid = function() {
179         if (!this._templateLoaded) {
180             return false;
181         }
183         var required = this._getRequiredPoints(),
184             max = 0,
185             valid = true;
187         $.each(this._getCompetenciesConfig(), function(index, competency) {
188             if (competency.points < 0) {
189                 valid = false;
190             }
191             max += competency.points;
192         });
194         valid = valid && max >= required;
195         return valid;
196     };
198     return /** @alias module:tool_lp/competency_rule_all */ Rule;
200 });