MDL-47959 mod_quiz: fixed 0 questions bug
[moodle.git] / mod / quiz / yui / src / toolboxes / js / toolbox.js
CommitLineData
e1a2d0d9
CC
1/**
2 * Resource and activity toolbox class.
3 *
4 * This class is responsible for managing AJAX interactions with activities and resources
5 * when viewing a course in editing mode.
6 *
7 * @module moodle-course-toolboxes
8 * @namespace M.course.toolboxes
9 */
10
11// The CSS classes we use.
12 var CSS = {
13 ACTIVITYINSTANCE : 'activityinstance',
14 AVAILABILITYINFODIV : 'div.availabilityinfo',
15 CONTENTWITHOUTLINK : 'contentwithoutlink',
16 CONDITIONALHIDDEN : 'conditionalhidden',
17 DIMCLASS : 'dimmed',
18 DIMMEDTEXT : 'dimmed_text',
19 EDITINSTRUCTIONS : 'editinstructions',
20 EDITINGMAXMARK: 'editor_displayed',
21 HIDE : 'hide',
22 JOIN: 'page_join',
23 MODINDENTCOUNT : 'mod-indent-',
24 MODINDENTHUGE : 'mod-indent-huge',
25 MODULEIDPREFIX : 'slot-',
26 PAGE: 'page',
27 SECTIONHIDDENCLASS : 'hidden',
28 SECTIONIDPREFIX : 'section-',
29 SLOT : 'slot',
30 SHOW : 'editing_show',
31 TITLEEDITOR : 'titleeditor'
32 },
33 // The CSS selectors we use.
34 SELECTOR = {
35 ACTIONAREA: '.actions',
36 ACTIONLINKTEXT : '.actionlinktext',
37 ACTIVITYACTION : 'a.cm-edit-action[data-action], a.editing_maxmark',
38 ACTIVITYFORM : 'span.instancemaxmarkcontainer form',
39 ACTIVITYICON : 'img.activityicon',
40 ACTIVITYINSTANCE : '.' + CSS.ACTIVITYINSTANCE,
41 ACTIVITYLINK: '.' + CSS.ACTIVITYINSTANCE + ' > a',
42 ACTIVITYLI : 'li.activity',
43 ACTIVITYMAXMARK : 'input[name=maxmark]',
44 COMMANDSPAN : '.commands',
45 CONTENTAFTERLINK : 'div.contentafterlink',
46 CONTENTWITHOUTLINK : 'div.contentwithoutlink',
47 EDITMAXMARK: 'a.editing_maxmark',
48 HIDE : 'a.editing_hide',
49 HIGHLIGHT : 'a.editing_highlight',
50 INSTANCENAME : 'span.instancename',
51 INSTANCEMAXMARK : 'span.instancemaxmark',
52 MODINDENTDIV : '.mod-indent',
53 MODINDENTOUTER : '.mod-indent-outer',
6375e98c 54 NUMQUESTIONS : '.numberofquestions',
e1a2d0d9
CC
55 PAGECONTENT : 'div#page-content',
56 PAGELI : 'li.page',
57 SECTIONUL : 'ul.section',
58 SHOW : 'a.' + CSS.SHOW,
59 SHOWHIDE : 'a.editing_showhide',
60 SLOTLI : 'li.slot',
61 SUMMARKS : '.mod_quiz_summarks'
62 },
63 BODY = Y.one(document.body);
64
65// Setup the basic namespace.
66M.mod_quiz = M.mod_quiz || {};
67
68/**
69 * The toolbox class is a generic class which should never be directly
70 * instantiated. Please extend it instead.
71 *
72 * @class toolbox
73 * @constructor
74 * @protected
75 * @extends Base
76 */
77var TOOLBOX = function() {
78 TOOLBOX.superclass.constructor.apply(this, arguments);
79};
80
81Y.extend(TOOLBOX, Y.Base, {
82 /**
83 * Send a request using the REST API
84 *
85 * @method send_request
86 * @param {Object} data The data to submit with the AJAX request
87 * @param {Node} [statusspinner] A statusspinner which may contain a section loader
88 * @param {Function} success_callback The callback to use on success
89 * @param {Object} [optionalconfig] Any additional configuration to submit
90 * @chainable
91 */
92 send_request: function(data, statusspinner, success_callback, optionalconfig) {
93 // Default data structure
94 if (!data) {
95 data = {};
96 }
97 // Handle any variables which we must pass back through to
98 var pageparams = this.get('config').pageparams,
99 varname;
100 for (varname in pageparams) {
101 data[varname] = pageparams[varname];
102 }
103
104 data.sesskey = M.cfg.sesskey;
105 data.courseid = this.get('courseid');
106 data.quizid = this.get('quizid');
107
108 var uri = M.cfg.wwwroot + this.get('ajaxurl');
109
110 // Define the configuration to send with the request
111 var responsetext = [];
112 var config = {
113 method: 'POST',
114 data: data,
115 on: {
116 success: function(tid, response) {
117 try {
118 responsetext = Y.JSON.parse(response.responseText);
119 if (responsetext.error) {
120 new M.core.ajaxException(responsetext);
121 }
122 } catch (e) {}
123
124 // Run the callback if we have one.
1a14f8a6 125 if (responsetext.hasOwnProperty('newsummarks')) {
e1a2d0d9
CC
126 Y.one(SELECTOR.SUMMARKS).setHTML(responsetext.newsummarks);
127 }
1a14f8a6 128 if (responsetext.hasOwnProperty('newnumquestions')) {
6375e98c
CC
129 Y.one(SELECTOR.NUMQUESTIONS).setHTML(M.util.get_string('numquestionsx', 'quiz', responsetext.newnumquestions));
130 }
e1a2d0d9
CC
131 if (success_callback) {
132 Y.bind(success_callback, this, responsetext)();
133 }
134
135 if (statusspinner) {
136 window.setTimeout(function() {
137 statusspinner.hide();
138 }, 400);
139 }
140 },
141 failure: function(tid, response) {
142 if (statusspinner) {
143 statusspinner.hide();
144 }
145 new M.core.ajaxException(response);
146 }
147 },
148 context: this
149 };
150
151 // Apply optional config
152 if (optionalconfig) {
153 for (varname in optionalconfig) {
154 config[varname] = optionalconfig[varname];
155 }
156 }
157
158 if (statusspinner) {
159 statusspinner.show();
160 }
161
162 // Send the request
163 Y.io(uri, config);
164 return this;
165 }
166},
167{
168 NAME: 'mod_quiz-toolbox',
169 ATTRS: {
170 /**
171 * The ID of the Moodle Course being edited.
172 *
173 * @attribute courseid
174 * @default 0
175 * @type Number
176 */
177 courseid: {
178 'value': 0
179 },
180
181 /**
182 * The Moodle course format.
183 *
184 * @attribute format
185 * @default 'topics'
186 * @type String
187 */
188 quizid: {
189 'value': 0
190 },
191 /**
192 * The URL to use when submitting requests.
193 * @attribute ajaxurl
194 * @default null
195 * @type String
196 */
197 ajaxurl: {
198 'value': null
199 },
200 /**
201 * Any additional configuration passed when creating the instance.
202 *
203 * @attribute config
204 * @default {}
205 * @type Object
206 */
207 config: {
208 'value': {}
209 }
210 }
211}
212);