[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 YUI.add('moodle-mod_quiz-util-slot', function (Y, NAME) { 2 3 /** 4 * A collection of utility classes for use with slots. 5 * 6 * @module moodle-mod_quiz-util 7 * @submodule moodle-mod_quiz-util-slot 8 */ 9 10 Y.namespace('Moodle.mod_quiz.util.slot'); 11 12 /** 13 * A collection of utility classes for use with slots. 14 * 15 * @class Moodle.mod_quiz.util.slot 16 * @static 17 */ 18 Y.Moodle.mod_quiz.util.slot = { 19 CSS: { 20 SLOT : 'slot', 21 QUESTIONTYPEDESCRIPTION : 'qtype_description' 22 }, 23 CONSTANTS: { 24 SLOTIDPREFIX : 'slot-', 25 QUESTION : M.util.get_string('question', 'moodle') 26 }, 27 SELECTORS: { 28 SLOT: 'li.slot', 29 INSTANCENAME: '.instancename', 30 NUMBER: 'span.slotnumber', 31 PAGECONTENT : 'div#page-content', 32 PAGEBREAK : 'span.page_split_join_wrapper', 33 ICON : 'img.smallicon', 34 QUESTIONTYPEDESCRIPTION : '.qtype_description', 35 SECTIONUL : 'ul.section' 36 }, 37 38 /** 39 * Retrieve the slot item from one of it's child Nodes. 40 * 41 * @method getSlotFromComponent 42 * @param slotcomponent {Node} The component Node. 43 * @return {Node|null} The Slot Node. 44 */ 45 getSlotFromComponent: function(slotcomponent) { 46 return Y.one(slotcomponent).ancestor(this.SELECTORS.SLOT, true); 47 }, 48 49 /** 50 * Determines the slot ID for the provided slot. 51 * 52 * @method getId 53 * @param slot {Node} The slot to find an ID for. 54 * @return {Number|false} The ID of the slot in question or false if no ID was found. 55 */ 56 getId: function(slot) { 57 // We perform a simple substitution operation to get the ID. 58 var id = slot.get('id').replace( 59 this.CONSTANTS.SLOTIDPREFIX, ''); 60 61 // Attempt to validate the ID. 62 id = parseInt(id, 10); 63 if (typeof id === 'number' && isFinite(id)) { 64 return id; 65 } 66 return false; 67 }, 68 69 /** 70 * Determines the slot name for the provided slot. 71 * 72 * @method getName 73 * @param slot {Node} The slot to find a name for. 74 * @return {string|false} The name of the slot in question or false if no ID was found. 75 */ 76 getName: function(slot) { 77 var instance = slot.one(this.SELECTORS.INSTANCENAME); 78 if (instance) { 79 return instance.get('firstChild').get('data'); 80 } 81 return null; 82 }, 83 84 /** 85 * Determines the slot number for the provided slot. 86 * 87 * @method getNumber 88 * @param slot {Node} The slot to find the number for. 89 * @return {Number|false} The number of the slot in question or false if no number was found. 90 */ 91 getNumber: function(slot) { 92 if (!slot) { 93 return false; 94 } 95 // We perform a simple substitution operation to get the number. 96 var number = slot.one(this.SELECTORS.NUMBER).get('text').replace( 97 this.CONSTANTS.QUESTION, ''); 98 // Attempt to validate the ID. 99 number = parseInt(number, 10); 100 if (typeof number === 'number' && isFinite(number)) { 101 return number; 102 } 103 return false; 104 }, 105 106 /** 107 * Updates the slot number for the provided slot. 108 * 109 * @method setNumber 110 * @param slot {Node} The slot to update the number for. 111 * @return void 112 */ 113 setNumber: function(slot, number) { 114 var numbernode = slot.one(this.SELECTORS.NUMBER); 115 numbernode.setHTML('<span class="accesshide">' + this.CONSTANTS.QUESTION + '</span> ' + number); 116 }, 117 118 /** 119 * Returns a list of all slot elements on the page. 120 * 121 * @method getSlots 122 * @return {node[]} An array containing slot nodes. 123 */ 124 getSlots: function() { 125 return Y.all(this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.SLOT); 126 }, 127 128 /** 129 * Returns a list of all slot elements on the page that have numbers. Excudes description questions. 130 * 131 * @method getSlots 132 * @return {node[]} An array containing slot nodes. 133 */ 134 getNumberedSlots: function() { 135 var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL; 136 selector += ' ' + this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')'; 137 return Y.all(selector); 138 }, 139 140 /** 141 * Returns the previous slot to the given slot. 142 * 143 * @method getPrevious 144 * @param slot Slot node 145 * @return {node|false} The previous slot node or false. 146 */ 147 getPrevious: function(slot) { 148 return slot.previous(this.SELECTORS.SLOT); 149 }, 150 151 /** 152 * Returns the previous numbered slot to the given slot. 153 * 154 * Ignores slots containing description question types. 155 * 156 * @method getPrevious 157 * @param slot Slot node 158 * @return {node|false} The previous slot node or false. 159 */ 160 getPreviousNumbered: function(slot) { 161 return slot.previous(this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')'); 162 }, 163 164 /** 165 * Reset the order of the numbers given to each slot. 166 * 167 * @method reorderSlots 168 * @return void 169 */ 170 reorderSlots: function() { 171 // Get list of slot nodes. 172 var slots = this.getSlots(); 173 // Loop through slots incrementing the number each time. 174 slots.each(function(slot) { 175 176 if (!Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot)) { 177 // Move the next page to the front. 178 var nextpage = slot.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE); 179 slot.swap(nextpage); 180 } 181 182 var previousSlot = this.getPreviousNumbered(slot); 183 previousslotnumber = 0; 184 if (slot.hasClass(this.CSS.QUESTIONTYPEDESCRIPTION)) { 185 return; 186 } 187 188 if (previousSlot) { 189 previousslotnumber = this.getNumber(previousSlot); 190 } 191 192 // Set slot number. 193 this.setNumber(slot, previousslotnumber + 1); 194 }, this); 195 }, 196 197 /** 198 * Remove a slot and related elements from the list of slots. 199 * 200 * @method remove 201 * @param slot Slot node 202 * @return void 203 */ 204 remove: function(slot) { 205 var page = Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot); 206 slot.remove(); 207 // Is the page empty. 208 if (!Y.Moodle.mod_quiz.util.page.isEmpty(page)) { 209 return; 210 } 211 // If so remove it. Including add menu and page break. 212 Y.Moodle.mod_quiz.util.page.remove(page); 213 }, 214 215 /** 216 * Returns a list of all page break elements on the page. 217 * 218 * @method getPageBreaks 219 * @return {node[]} An array containing page break nodes. 220 */ 221 getPageBreaks: function() { 222 var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL; 223 selector += ' ' + this.SELECTORS.SLOT + this.SELECTORS.PAGEBREAK; 224 return Y.all(selector); 225 }, 226 227 /** 228 * Retrieve the page break element item from the given slot. 229 * 230 * @method getPageBreak 231 * @param slot Slot node 232 * @return {Node|null} The Page Break Node. 233 */ 234 getPageBreak: function(slot) { 235 return Y.one(slot).one(this.SELECTORS.PAGEBREAK); 236 }, 237 238 /** 239 * Add a page break and related elements to the list of slots. 240 * 241 * @method addPageBreak 242 * @param beforenode Int | Node | HTMLElement | String to add 243 * @return pagebreak PageBreak node 244 */ 245 addPageBreak: function(slot) { 246 var nodetext = M.mod_quiz.resource_toolbox.get('config').addpageiconhtml; 247 nodetext = nodetext.replace('%%SLOT%%', this.getNumber(slot)); 248 var pagebreak = Y.Node.create(nodetext); 249 slot.one('div').insert(pagebreak, 'after'); 250 return pagebreak; 251 }, 252 253 /** 254 * Remove a pagebreak from the given slot. 255 * 256 * @method removePageBreak 257 * @param slot Slot node 258 * @return boolean 259 */ 260 removePageBreak: function(slot) { 261 var pagebreak = this.getPageBreak(slot); 262 if (!pagebreak) { 263 return false; 264 } 265 pagebreak.remove(); 266 return true; 267 }, 268 269 /** 270 * Reorder each pagebreak by iterating through each related slot. 271 * 272 * @method reorderPageBreaks 273 * @return void 274 */ 275 reorderPageBreaks: function() { 276 // Get list of slot nodes. 277 var slots = this.getSlots(), slotnumber = 0; 278 // Loop through slots incrementing the number each time. 279 slots.each (function(slot, key) { 280 slotnumber++; 281 var pagebreak = this.getPageBreak(slot); 282 // Last slot won't have a page break. 283 if (!pagebreak && key === slots.size() - 1) { 284 return; 285 } 286 287 // No pagebreak and not last slot. Add one. 288 if (!pagebreak && key !== slots.size() - 1) { 289 pagebreak = this.addPageBreak(slot); 290 } 291 292 // Remove last page break if there is one. 293 if (pagebreak && key === slots.size() - 1) { 294 this.removePageBreak(slot); 295 } 296 297 // Get page break anchor element. 298 var pagebreaklink = pagebreak.get('childNodes').item(0); 299 300 // Get the correct title. 301 var action = '', iconname = ''; 302 if (Y.Moodle.mod_quiz.util.page.isPage(slot.next('li.activity'))) { 303 action = 'removepagebreak'; 304 iconname = 'e/remove_page_break'; 305 } else { 306 action = 'addpagebreak'; 307 iconname = 'e/insert_page_break'; 308 } 309 310 // Update the link and image titles 311 pagebreaklink.set('title', M.util.get_string(action, 'quiz')); 312 pagebreaklink.setData('action', action); 313 // Update the image title. 314 var icon = pagebreaklink.one(this.SELECTORS.ICON); 315 icon.set('title', M.util.get_string(action, 'quiz')); 316 icon.set('alt', M.util.get_string(action, 'quiz')); 317 318 // Update the image src. 319 icon.set('src', M.util.image_url(iconname)); 320 321 // Get anchor url parameters as an associative array. 322 var params = Y.QueryString.parse(pagebreaklink.get('href')); 323 // Update slot number. 324 params.slot = slotnumber; 325 // Create the new url. 326 var newurl = ''; 327 for (var index in params) { 328 if (newurl.length) { 329 newurl += "&"; 330 } 331 newurl += index + "=" + params[index]; 332 } 333 // Update the anchor. 334 pagebreaklink.set('href', newurl); 335 }, this); 336 } 337 }; 338 339 340 }, '@VERSION@', {"requires": ["node", "moodle-mod_quiz-util-base"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |