MDL-40988 quiz: ability to break quizzes into sections
[moodle.git] / mod / quiz / yui / src / quizbase / js / quizbase.js
blobe93fabe5d3254c202d9222d158bc649b2bef4a26
1 /**
2  * The quizbase class to provide shared functionality to Modules within Moodle.
3  *
4  * @module moodle-mod_quiz-quizbase
5  */
6 var QUIZBASENAME = 'mod_quiz-quizbase';
8 var QUIZBASE = function() {
9     QUIZBASE.superclass.constructor.apply(this, arguments);
12 /**
13  * The coursebase class to provide shared functionality to Modules within
14  * Moodle.
15  *
16  * @class M.course.coursebase
17  * @constructor
18  */
19 Y.extend(QUIZBASE, Y.Base, {
20     // Registered Modules
21     registermodules : [],
23     /**
24      * Register a new Javascript Module
25      *
26      * @method register_module
27      * @param {Object} The instantiated module to call functions on
28      * @chainable
29      */
30     register_module : function(object) {
31         this.registermodules.push(object);
33         return this;
34     },
36     /**
37      * Invoke the specified function in all registered modules with the given arguments
38      *
39      * @method invoke_function
40      * @param {String} functionname The name of the function to call
41      * @param {mixed} args The argument supplied to the function
42      * @chainable
43      */
44     invoke_function : function(functionname, args) {
45         var module;
46         for (module in this.registermodules) {
47             if (functionname in this.registermodules[module]) {
48                 this.registermodules[module][functionname](args);
49             }
50         }
52         return this;
53     }
54 }, {
55     NAME : QUIZBASENAME,
56     ATTRS : {}
57 });
59 // Ensure that M.course exists and that coursebase is initialised correctly
60 M.mod_quiz = M.mod_quiz || {};
61 M.mod_quiz.quizbase = M.mod_quiz.quizbase || new QUIZBASE();
63 // Abstract functions that needs to be defined per format (course/format/somename/format.js)
64 M.mod_quiz.edit = M.mod_quiz.edit || {};
66 /**
67  * Swap section (should be defined in format.js if requred)
68  *
69  * @param {YUI} Y YUI3 instance
70  * @param {string} node1 node to swap to
71  * @param {string} node2 node to swap with
72  * @return {NodeList} section list
73  */
74 M.mod_quiz.edit.swap_sections = function(Y, node1, node2) {
75     var CSS = {
76         COURSECONTENT : 'mod-quiz-edit-content',
77         SECTIONADDMENUS : 'section_add_menus'
78     };
80     var sectionlist = Y.Node.all('.' + CSS.COURSECONTENT + ' li.section');
81     // Swap menus.
82     sectionlist.item(node1).one('.' + CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.' + CSS.SECTIONADDMENUS));
85 /**
86  * Process sections after ajax response (should be defined in format.js)
87  * If some response is expected, we pass it over to format, as it knows better
88  * hot to process it.
89  *
90  * @param {YUI} Y YUI3 instance
91  * @param {NodeList} list of sections
92  * @param {array} response ajax response
93  * @param {string} sectionfrom first affected section
94  * @param {string} sectionto last affected section
95  * @return void
96  */
97 M.mod_quiz.edit.process_sections = function(Y, sectionlist, response, sectionfrom, sectionto) {
98     var CSS = {
99         SECTIONNAME : 'sectionname'
100     },
101     SELECTORS = {
102         SECTIONLEFTSIDE : '.left .section-handle img'
103     };
105     if (response.action === 'move') {
106         // If moving up swap around 'sectionfrom' and 'sectionto' so the that loop operates.
107         if (sectionfrom > sectionto) {
108             var temp = sectionto;
109             sectionto = sectionfrom;
110             sectionfrom = temp;
111         }
113         // Update titles and move icons in all affected sections.
114         var ele, str, stridx, newstr;
116         for (var i = sectionfrom; i <= sectionto; i++) {
117             // Update section title.
118             sectionlist.item(i).one('.' + CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
120             // Update move icon.
121             ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);
122             str = ele.getAttribute('alt');
123             stridx = str.lastIndexOf(' ');
124             newstr = str.substr(0, stridx + 1) + i;
125             ele.setAttribute('alt', newstr);
126             ele.setAttribute('title', newstr); // For FireFox as 'alt' is not refreshed.
128             // Remove the current class as section has been moved.
129             sectionlist.item(i).removeClass('current');
130         }
131         // If there is a current section, apply corresponding class in order to highlight it.
132         if (response.current !== -1) {
133             // Add current class to the required section.
134             sectionlist.item(response.current).addClass('current');
135         }
136     }