Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Handle opening a dialogue to configure scale data.
18
 *
19
 * @module     tool_lp/scaleconfig
20
 * @copyright  2015 Adrian Greeve <adrian@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/notification', 'core/templates', 'core/ajax', 'tool_lp/dialogue', 'tool_lp/scalevalues'],
24
    function($, notification, templates, ajax, Dialogue, ModScaleValues) {
25
 
26
    /**
27
     * Scale config object.
28
     * @param {String} selectSelector The select box selector.
29
     * @param {String} inputSelector The hidden input field selector.
30
     * @param {String} triggerSelector The trigger selector.
31
     */
32
    var ScaleConfig = function(selectSelector, inputSelector, triggerSelector) {
33
        this.selectSelector = selectSelector;
34
        this.inputSelector = inputSelector;
35
        this.triggerSelector = triggerSelector;
36
 
37
        // Get the current scale ID.
38
        this.originalscaleid = $(selectSelector).val();
39
        $(selectSelector).on('change', this.scaleChangeHandler.bind(this)).change();
40
        $(triggerSelector).click(this.showConfig.bind(this));
41
    };
42
 
43
    /** @var {String} The select box selector. */
44
    ScaleConfig.prototype.selectSelector = null;
45
    /** @var {String} The hidden field selector. */
46
    ScaleConfig.prototype.inputSelector = null;
47
    /** @var {String} The trigger selector. */
48
    ScaleConfig.prototype.triggerSelector = null;
49
    /** @var {Array} scalevalues ID and name of the scales. */
50
    ScaleConfig.prototype.scalevalues = null;
51
    /** @var {Number) originalscaleid Original scale ID when the page loads. */
52
    ScaleConfig.prototype.originalscaleid = 0;
53
    /** @var {Number} scaleid Current scale ID. */
54
    ScaleConfig.prototype.scaleid = 0;
55
    /** @var {Dialogue} Reference to the popup. */
56
    ScaleConfig.prototype.popup = null;
57
 
58
    /**
59
     * Displays the scale configuration dialogue.
60
     *
61
     * @method showConfig
62
     */
63
    ScaleConfig.prototype.showConfig = function() {
64
        var self = this;
65
 
66
        this.scaleid = $(this.selectSelector).val();
67
        if (this.scaleid <= 0) {
68
            // This should not happen.
69
            return;
70
        }
71
 
72
        var scalename = $(this.selectSelector).find("option:selected").text();
73
        this.getScaleValues(this.scaleid).done(function() {
74
 
75
            var context = {
76
                scalename: scalename,
77
                scales: self.scalevalues
78
            };
79
 
80
            // Dish up the form.
81
            templates.render('tool_lp/scale_configuration_page', context)
82
                .done(function(html) {
83
                    new Dialogue(
84
                        scalename,
85
                        html,
86
                        self.initScaleConfig.bind(self)
87
                    );
88
                }).fail(notification.exception);
89
        }).fail(notification.exception);
90
    };
91
 
92
    /**
93
     * Gets the original scale configuration if it was set.
94
     *
95
     * @method retrieveOriginalScaleConfig
96
     * @return {Object|String} scale configuration or empty string.
97
     */
98
    ScaleConfig.prototype.retrieveOriginalScaleConfig = function() {
99
        var jsonstring = $(this.inputSelector).val();
100
        if (jsonstring !== '') {
101
            var scaleconfiguration = $.parseJSON(jsonstring);
102
            // The first object should contain the scale ID for the configuration.
103
            var scaledetail = scaleconfiguration.shift();
104
            // Check that this scale id matches the one from the page before returning the configuration.
105
            if (scaledetail.scaleid === this.originalscaleid) {
106
                return scaleconfiguration;
107
            }
108
        }
109
        return '';
110
    };
111
 
112
    /**
113
     * Initialises the scale configuration dialogue.
114
     *
115
     * @method initScaleConfig
116
     * @param {Dialogue} popup Dialogue object to initialise.
117
     */
118
    ScaleConfig.prototype.initScaleConfig = function(popup) {
119
        this.popup = popup;
120
        var body = $(popup.getContent());
121
        if (this.originalscaleid === this.scaleid) {
122
            // Set up the popup to show the current configuration.
123
            var currentconfig = this.retrieveOriginalScaleConfig();
124
            // Set up the form only if there is configuration settings to set.
125
            if (currentconfig !== '') {
126
                currentconfig.forEach(function(value) {
127
                    if (value.scaledefault === 1) {
128
                        body.find('[data-field="tool_lp_scale_default_' + value.id + '"]').attr('checked', true);
129
                    }
130
                    if (value.proficient === 1) {
131
                        body.find('[data-field="tool_lp_scale_proficient_' + value.id + '"]').attr('checked', true);
132
                    }
133
                });
134
            }
135
        }
136
        body.on('click', '[data-action="close"]', function() {
137
            this.setScaleConfig();
138
            popup.close();
139
        }.bind(this));
140
        body.on('click', '[data-action="cancel"]', function() {
141
            popup.close();
142
        });
143
    };
144
 
145
    /**
146
     * Set the scale configuration back into a JSON string in the hidden element.
147
     *
148
     * @method setScaleConfig
149
     */
150
    ScaleConfig.prototype.setScaleConfig = function() {
151
        var body = $(this.popup.getContent());
152
        // Get the data.
153
        var data = [{scaleid: this.scaleid}];
154
        this.scalevalues.forEach(function(value) {
155
            var scaledefault = 0;
156
            var proficient = 0;
157
            if (body.find('[data-field="tool_lp_scale_default_' + value.id + '"]').is(':checked')) {
158
                scaledefault = 1;
159
            }
160
            if (body.find('[data-field="tool_lp_scale_proficient_' + value.id + '"]').is(':checked')) {
161
                proficient = 1;
162
            }
163
 
164
            if (!scaledefault && !proficient) {
165
                return;
166
            }
167
 
168
            data.push({
169
                id: value.id,
170
                scaledefault: scaledefault,
171
                proficient: proficient
172
            });
173
         });
174
        var datastring = JSON.stringify(data);
175
        // Send to the hidden field on the form.
176
        $(this.inputSelector).val(datastring);
177
        // Once the configuration has been saved then the original scale ID is set to the current scale ID.
178
        this.originalscaleid = this.scaleid;
179
    };
180
 
181
    /**
182
     * Get the scale values for the selected scale.
183
     *
184
     * @method getScaleValues
185
     * @param {Number} scaleid The scale ID of the selected scale.
186
     * @return {Promise} A deffered object with the scale values.
187
     */
188
    ScaleConfig.prototype.getScaleValues = function(scaleid) {
189
        return ModScaleValues.get_values(scaleid).then(function(values) {
190
            this.scalevalues = values;
191
            return values;
192
        }.bind(this));
193
    };
194
 
195
    /**
196
     * Triggered when a scale is selected.
197
     *
198
     * @name   scaleChangeHandler
199
     * @param  {Event} e
200
     * @function
201
     */
202
    ScaleConfig.prototype.scaleChangeHandler = function(e) {
203
        if ($(e.target).val() <= 0) {
204
            $(this.triggerSelector).prop('disabled', true);
205
        } else {
206
            $(this.triggerSelector).prop('disabled', false);
207
        }
208
 
209
    };
210
 
211
    return {
212
 
213
        /**
214
         * Main initialisation.
215
         *
216
         * @param {String} selectSelector The select box selector.
217
         * @param {String} inputSelector The hidden input field selector.
218
         * @param {String} triggerSelector The trigger selector.
219
         * @return {ScaleConfig} A new instance of ScaleConfig.
220
         * @method init
221
         */
222
        init: function(selectSelector, inputSelector, triggerSelector) {
223
            return new ScaleConfig(selectSelector, inputSelector, triggerSelector);
224
        }
225
    };
226
});