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