MDL-47368 Edit quiz: page should not reload so often

The approach is that we have a new JavaScript function
M.mod_quiz.resource_toolbox.reorganise_edit_page which, after a ajax
action, fixes up everything like page breaks, page and question numbers,
that might now be wrong.

We call that function instead of reloading the page.

Also, there are a lot more Behat tests to verify this works correctly.

AMOS BEGIN
 MOV [joinpages,mod_quiz],[removepagebreak,mod_quiz]
 MOV [splitpages,mod_quiz],[addpagebreak,mod_quiz]
AMOS END
This commit is contained in:
Colin Chambers 2014-10-14 15:41:48 +01:00 committed by Tim Hunt
parent d63a81c507
commit a69f81f0d3
25 changed files with 1842 additions and 360 deletions

View file

@ -313,7 +313,6 @@ Y.extend(DRAGRESOURCE, M.core.dragdrop, {
this.groups = ['resource'];
this.samenodeclass = CSS.ACTIVITY;
this.parentnodeclass = CSS.SECTION;
//this.resourcedraghandle = this.get_drag_handle(M.util.get_string('movecoursemodule', 'moodle'), CSS.EDITINGMOVE, CSS.ICONCLASS, true);
this.resourcedraghandle = this.get_drag_handle(M.str.moodle.move, CSS.EDITINGMOVE, CSS.ICONCLASS, true);
this.samenodelabel = {
@ -467,12 +466,11 @@ Y.extend(DRAGRESOURCE, M.core.dragdrop, {
var responsetext = Y.JSON.parse(response.responseText);
var params = {element: dragnode, visible: responsetext.visible};
M.mod_quiz.quizbase.invoke_function('set_visibility_resource_ui', params);
Y.Moodle.mod_quiz.util.slot.reorder_slots();
this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
window.setTimeout(function() {
spinner.hide();
}, 250);
window.location.reload(true);
M.mod_quiz.resource_toolbox.reorganise_edit_page();
},
failure: function(tid, response) {
this.ajax_failure(response);
@ -556,7 +554,9 @@ M.mod_quiz.init_resource_dragdrop = function(params) {
"moodle-core-dragdrop",
"moodle-core-notification",
"moodle-mod_quiz-quizbase",
"moodle-mod_quiz-util",
"moodle-mod_quiz-util-base",
"moodle-mod_quiz-util-page",
"moodle-mod_quiz-util-slot",
"moodle-course-util"
]
});

File diff suppressed because one or more lines are too long

View file

@ -308,7 +308,6 @@ Y.extend(DRAGRESOURCE, M.core.dragdrop, {
this.groups = ['resource'];
this.samenodeclass = CSS.ACTIVITY;
this.parentnodeclass = CSS.SECTION;
//this.resourcedraghandle = this.get_drag_handle(M.util.get_string('movecoursemodule', 'moodle'), CSS.EDITINGMOVE, CSS.ICONCLASS, true);
this.resourcedraghandle = this.get_drag_handle(M.str.moodle.move, CSS.EDITINGMOVE, CSS.ICONCLASS, true);
this.samenodelabel = {
@ -462,12 +461,11 @@ Y.extend(DRAGRESOURCE, M.core.dragdrop, {
var responsetext = Y.JSON.parse(response.responseText);
var params = {element: dragnode, visible: responsetext.visible};
M.mod_quiz.quizbase.invoke_function('set_visibility_resource_ui', params);
Y.Moodle.mod_quiz.util.slot.reorder_slots();
this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
window.setTimeout(function() {
spinner.hide();
}, 250);
window.location.reload(true);
M.mod_quiz.resource_toolbox.reorganise_edit_page();
},
failure: function(tid, response) {
this.ajax_failure(response);
@ -551,7 +549,9 @@ M.mod_quiz.init_resource_dragdrop = function(params) {
"moodle-core-dragdrop",
"moodle-core-notification",
"moodle-mod_quiz-quizbase",
"moodle-mod_quiz-util",
"moodle-mod_quiz-util-base",
"moodle-mod_quiz-util-page",
"moodle-mod_quiz-util-slot",
"moodle-course-util"
]
});

View file

@ -79,9 +79,9 @@ M.mod_quiz.edit.swap_sections = function(Y, node1, node2) {
SECTIONADDMENUS : 'section_add_menus'
};
var sectionlist = Y.Node.all('.'+CSS.COURSECONTENT+' '+M.mod_quiz.edit.get_section_selector(Y));
var sectionlist = Y.Node.all('.' + CSS.COURSECONTENT + ' ' + M.mod_quiz.edit.get_section_selector(Y));
// Swap menus.
sectionlist.item(node1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.'+CSS.SECTIONADDMENUS));
sectionlist.item(node1).one('.' + CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.' + CSS.SECTIONADDMENUS));
};
/**
@ -117,7 +117,7 @@ M.mod_quiz.edit.process_sections = function(Y, sectionlist, response, sectionfro
for (var i = sectionfrom; i <= sectionto; i++) {
// Update section title.
sectionlist.item(i).one('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
sectionlist.item(i).one('.' + CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
// Update move icon.
ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);

View file

@ -79,9 +79,9 @@ M.mod_quiz.edit.swap_sections = function(Y, node1, node2) {
SECTIONADDMENUS : 'section_add_menus'
};
var sectionlist = Y.Node.all('.'+CSS.COURSECONTENT+' '+M.mod_quiz.edit.get_section_selector(Y));
var sectionlist = Y.Node.all('.' + CSS.COURSECONTENT + ' ' + M.mod_quiz.edit.get_section_selector(Y));
// Swap menus.
sectionlist.item(node1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.'+CSS.SECTIONADDMENUS));
sectionlist.item(node1).one('.' + CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.' + CSS.SECTIONADDMENUS));
};
/**
@ -117,7 +117,7 @@ M.mod_quiz.edit.process_sections = function(Y, sectionlist, response, sectionfro
for (var i = sectionfrom; i <= sectionto; i++) {
// Update section title.
sectionlist.item(i).one('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
sectionlist.item(i).one('.' + CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
// Update move icon.
ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);

View file

@ -311,10 +311,10 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// The user is deleting the activity.
this.delete_with_confirmation(ev, node, activity, action);
break;
case 'linkpage':
case 'unlinkpage':
// The user is linking or unlinking pages.
this.link_page(ev, node, activity, action);
case 'addpagebreak':
case 'removepagebreak':
// The user is adding or removing a page break.
this.update_page_break(ev, node, activity, action);
break;
default:
// Nothing to do here!
@ -350,10 +350,10 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* @chainable
*/
delete_with_confirmation: function(ev, button, activity) {
// Prevent the default button action
// Prevent the default button action.
ev.preventDefault();
// Get the element we're working on
// Get the element we're working on.
var element = activity,
// Create confirm string (different if element has or does not have name)
confirmstring = '',
@ -370,19 +370,24 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// If it is confirmed.
confirm.on('complete-yes', function() {
// Actually remove the element.
element.remove();
Y.Moodle.mod_quiz.util.slot.reorder_slots();
var spinner = this.add_spinner(element);
var data = {
'class': 'resource',
'action': 'DELETE',
'id': Y.Moodle.mod_quiz.util.slot.getId(element)
};
this.send_request(data);
if (M.core.actionmenu && M.core.actionmenu.instance) {
M.core.actionmenu.instance.hideMenu();
}
window.location.reload(true);
this.send_request(data, spinner, function(response) {
if (response.deleted) {
// Actually remove the element.
Y.Moodle.mod_quiz.util.slot.remove(element);
this.reorganise_edit_page();
if (M.core.actionmenu && M.core.actionmenu.instance) {
M.core.actionmenu.instance.hideMenu();
}
} else {
window.location.reload(true);
}
});
}, this);
@ -425,12 +430,12 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
M.core.actionmenu.instance.hideMenu();
}
// Try to retrieve the existing string from the server
// Try to retrieve the existing string from the server.
if (response.instancemaxmark) {
maxmarktext = response.instancemaxmark;
}
// Create the editor and submit button
// Create the editor and submit button.
var editform = Y.Node.create('<form action="#" />');
var editinstructions = Y.Node.create('<span class="' + CSS.EDITINSTRUCTIONS + '" id="id_editinstructions" />')
.set('innerHTML', M.util.get_string('edittitleinstructions', 'moodle'));
@ -442,7 +447,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
'size' : parseInt(this.get('config').questiondecimalpoints, 10) + 2
});
// Clear the existing content and put the editor in
// Clear the existing content and put the editor in.
editform.appendChild(editor);
editform.setData('anchor', anchor);
instance.insert(editinstructions, 'before');
@ -457,7 +462,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// We hide various components whilst editing:
activity.addClass(CSS.EDITINGMAXMARK);
// Focus and select the editor text
// Focus and select the editor text.
editor.focus().select();
// Cancel the edit if we lose focus or the escape key is pressed.
@ -482,7 +487,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* @param {String} originalmaxmark The original maxmark the activity or resource had.
*/
edit_maxmark_submit : function(ev, activity, originalmaxmark) {
// We don't actually want to submit anything
// We don't actually want to submit anything.
ev.preventDefault();
var newmaxmark = Y.Lang.trim(activity.one(SELECTOR.ACTIVITYFORM + ' ' + SELECTOR.ACTIVITYMAXMARK).get('value'));
var spinner = this.add_spinner(activity);
@ -560,97 +565,59 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* the other slots
*
* @protected
* @method link_page
* @method update_page_break
* @param {EventFacade} ev The event that was fired.
* @param {Node} button The button that triggered this action.
* @param {Node} activity The activity node that this action will be performed on.
* @chainable
*/
link_page: function(ev, button, activity, action) {
update_page_break: function(ev, button, activity, action) {
// Prevent the default button action
ev.preventDefault();
activity = activity.next('li.activity.slot');
var spinner = this.add_spinner(activity),
nextactivity = activity.next('li.activity.slot');
var spinner = this.add_spinner(nextactivity),
slotid = 0;
var value = action === 'linkpage' ? 1:2;
var value = action === 'removepagebreak' ? 1 : 2;
var data = {
'class': 'resource',
'field': 'linkslottopage',
'field': 'updatepagebreak',
'id': slotid,
'value': value
};
slotid = Y.Moodle.mod_quiz.util.slot.getId(activity);
slotid = Y.Moodle.mod_quiz.util.slot.getId(nextactivity);
if (slotid) {
data.id = Number(slotid);
}
this.send_request(data, spinner, function(response) {
window.location.reload(true);
// if (response.slots) {
// this.repaginate_slots(response.slots);
// }
if (response.slots) {
if (action === 'addpagebreak') {
Y.Moodle.mod_quiz.util.page.add(activity);
} else {
var page = activity.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE);
Y.Moodle.mod_quiz.util.page.remove(page, true);
}
this.reorganise_edit_page();
} else {
window.location.reload(true);
}
});
return this;
},
repaginate_slots: function(slots) {
this.slots = slots;
var section = Y.one(SELECTOR.PAGECONTENT + ' ' + SELECTOR.SECTIONUL),
activities = section.all(SELECTOR.ACTIVITYLI);
activities.each(function(node) {
// What element is it? page/slot/link
// what is the current slot?
var type;
var slot;
if(node.hasClass(CSS.PAGE)){
type = this.NODE_PAGE;
slot = node.next(SELECTOR.SLOTLI);
} else if (node.hasClass(CSS.SLOT)){
type = this.NODE_SLOT;
slot = node;
} else if (node.hasClass(CSS.JOIN)){
type = this.NODE_JOIN;
slot = node.previous(SELECTOR.SLOTLI);
}
// getSlotnumber() Should be a method of util.slot
var slotnumber = Number(Y.Moodle.mod_quiz.util.slot.getNumber(slot));
if(!type){
// Nothing we can do.
return;
}
// Is it correct?
if(!this.slots.hasOwnProperty(slotnumber)){
// An error. We should handle this.
return;
}
var slotdata = this.slots[slotnumber];
if(type === this.NODE_PAGE){
// Get page number
var pagenumber = Y.Moodle.mod_quiz.util.page.getNumber(node);
// Is the page number correct?
if (slotdata.page === pagenumber) {
console.log('slotdata.page == pagenumber return');
return;
}
if (pagenumber < slotdata.page) {
// Remove page node.
node.remove();
}
else {
// Add page node.
console.log('pagenumber > slotdata.page update page number');
}
}
}, this);
/**
* Reorganise the UI after every edit action.
*
* @protected
* @method reorganise_edit_page
*/
reorganise_edit_page: function() {
Y.Moodle.mod_quiz.util.slot.reorderSlots();
Y.Moodle.mod_quiz.util.slot.reorderPageBreaks();
Y.Moodle.mod_quiz.util.page.reorderPages();
},
NAME : 'mod_quiz-resource-toolbox',

File diff suppressed because one or more lines are too long

View file

@ -311,10 +311,10 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// The user is deleting the activity.
this.delete_with_confirmation(ev, node, activity, action);
break;
case 'linkpage':
case 'unlinkpage':
// The user is linking or unlinking pages.
this.link_page(ev, node, activity, action);
case 'addpagebreak':
case 'removepagebreak':
// The user is adding or removing a page break.
this.update_page_break(ev, node, activity, action);
break;
default:
// Nothing to do here!
@ -350,10 +350,10 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* @chainable
*/
delete_with_confirmation: function(ev, button, activity) {
// Prevent the default button action
// Prevent the default button action.
ev.preventDefault();
// Get the element we're working on
// Get the element we're working on.
var element = activity,
// Create confirm string (different if element has or does not have name)
confirmstring = '',
@ -370,19 +370,24 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// If it is confirmed.
confirm.on('complete-yes', function() {
// Actually remove the element.
element.remove();
Y.Moodle.mod_quiz.util.slot.reorder_slots();
var spinner = this.add_spinner(element);
var data = {
'class': 'resource',
'action': 'DELETE',
'id': Y.Moodle.mod_quiz.util.slot.getId(element)
};
this.send_request(data);
if (M.core.actionmenu && M.core.actionmenu.instance) {
M.core.actionmenu.instance.hideMenu();
}
window.location.reload(true);
this.send_request(data, spinner, function(response) {
if (response.deleted) {
// Actually remove the element.
Y.Moodle.mod_quiz.util.slot.remove(element);
this.reorganise_edit_page();
if (M.core.actionmenu && M.core.actionmenu.instance) {
M.core.actionmenu.instance.hideMenu();
}
} else {
window.location.reload(true);
}
});
}, this);
@ -425,12 +430,12 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
M.core.actionmenu.instance.hideMenu();
}
// Try to retrieve the existing string from the server
// Try to retrieve the existing string from the server.
if (response.instancemaxmark) {
maxmarktext = response.instancemaxmark;
}
// Create the editor and submit button
// Create the editor and submit button.
var editform = Y.Node.create('<form action="#" />');
var editinstructions = Y.Node.create('<span class="' + CSS.EDITINSTRUCTIONS + '" id="id_editinstructions" />')
.set('innerHTML', M.util.get_string('edittitleinstructions', 'moodle'));
@ -442,7 +447,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
'size' : parseInt(this.get('config').questiondecimalpoints, 10) + 2
});
// Clear the existing content and put the editor in
// Clear the existing content and put the editor in.
editform.appendChild(editor);
editform.setData('anchor', anchor);
instance.insert(editinstructions, 'before');
@ -457,7 +462,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// We hide various components whilst editing:
activity.addClass(CSS.EDITINGMAXMARK);
// Focus and select the editor text
// Focus and select the editor text.
editor.focus().select();
// Cancel the edit if we lose focus or the escape key is pressed.
@ -482,7 +487,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* @param {String} originalmaxmark The original maxmark the activity or resource had.
*/
edit_maxmark_submit : function(ev, activity, originalmaxmark) {
// We don't actually want to submit anything
// We don't actually want to submit anything.
ev.preventDefault();
var newmaxmark = Y.Lang.trim(activity.one(SELECTOR.ACTIVITYFORM + ' ' + SELECTOR.ACTIVITYMAXMARK).get('value'));
var spinner = this.add_spinner(activity);
@ -560,97 +565,59 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* the other slots
*
* @protected
* @method link_page
* @method update_page_break
* @param {EventFacade} ev The event that was fired.
* @param {Node} button The button that triggered this action.
* @param {Node} activity The activity node that this action will be performed on.
* @chainable
*/
link_page: function(ev, button, activity, action) {
update_page_break: function(ev, button, activity, action) {
// Prevent the default button action
ev.preventDefault();
activity = activity.next('li.activity.slot');
var spinner = this.add_spinner(activity),
nextactivity = activity.next('li.activity.slot');
var spinner = this.add_spinner(nextactivity),
slotid = 0;
var value = action === 'linkpage' ? 1:2;
var value = action === 'removepagebreak' ? 1 : 2;
var data = {
'class': 'resource',
'field': 'linkslottopage',
'field': 'updatepagebreak',
'id': slotid,
'value': value
};
slotid = Y.Moodle.mod_quiz.util.slot.getId(activity);
slotid = Y.Moodle.mod_quiz.util.slot.getId(nextactivity);
if (slotid) {
data.id = Number(slotid);
}
this.send_request(data, spinner, function(response) {
window.location.reload(true);
// if (response.slots) {
// this.repaginate_slots(response.slots);
// }
if (response.slots) {
if (action === 'addpagebreak') {
Y.Moodle.mod_quiz.util.page.add(activity);
} else {
var page = activity.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE);
Y.Moodle.mod_quiz.util.page.remove(page, true);
}
this.reorganise_edit_page();
} else {
window.location.reload(true);
}
});
return this;
},
repaginate_slots: function(slots) {
this.slots = slots;
var section = Y.one(SELECTOR.PAGECONTENT + ' ' + SELECTOR.SECTIONUL),
activities = section.all(SELECTOR.ACTIVITYLI);
activities.each(function(node) {
// What element is it? page/slot/link
// what is the current slot?
var type;
var slot;
if(node.hasClass(CSS.PAGE)){
type = this.NODE_PAGE;
slot = node.next(SELECTOR.SLOTLI);
} else if (node.hasClass(CSS.SLOT)){
type = this.NODE_SLOT;
slot = node;
} else if (node.hasClass(CSS.JOIN)){
type = this.NODE_JOIN;
slot = node.previous(SELECTOR.SLOTLI);
}
// getSlotnumber() Should be a method of util.slot
var slotnumber = Number(Y.Moodle.mod_quiz.util.slot.getNumber(slot));
if(!type){
// Nothing we can do.
return;
}
// Is it correct?
if(!this.slots.hasOwnProperty(slotnumber)){
// An error. We should handle this.
return;
}
var slotdata = this.slots[slotnumber];
if(type === this.NODE_PAGE){
// Get page number
var pagenumber = Y.Moodle.mod_quiz.util.page.getNumber(node);
// Is the page number correct?
if (slotdata.page === pagenumber) {
console.log('slotdata.page == pagenumber return');
return;
}
if (pagenumber < slotdata.page) {
// Remove page node.
node.remove();
}
else {
// Add page node.
console.log('pagenumber > slotdata.page update page number');
}
}
}, this);
/**
* Reorganise the UI after every edit action.
*
* @protected
* @method reorganise_edit_page
*/
reorganise_edit_page: function() {
Y.Moodle.mod_quiz.util.slot.reorderSlots();
Y.Moodle.mod_quiz.util.slot.reorderPageBreaks();
Y.Moodle.mod_quiz.util.page.reorderPages();
},
NAME : 'mod_quiz-resource-toolbox',

View file

@ -16,13 +16,23 @@ Y.namespace('Moodle.mod_quiz.util.page');
* @static
*/
Y.Moodle.mod_quiz.util.page = {
CSS: {
PAGE : 'page'
},
CONSTANTS: {
PAGEIDPREFIX : 'page-',
PAGENUMBERPREFIX : 'Page '
ACTIONMENUIDPREFIX: 'action-menu-',
ACTIONMENUBARIDSUFFIX: '-menubar',
ACTIONMENUMENUIDSUFFIX: '-menu',
PAGEIDPREFIX: 'page-',
PAGENUMBERPREFIX: M.util.get_string('page', 'moodle') + ' '
},
SELECTORS: {
ACTIONMENU: 'div.moodle-actionmenu',
ACTIONMENUBAR: 'ul.menubar',
ACTIONMENUMENU: 'ul.menu',
PAGE: 'li.page',
INSTANCENAME: '.instancename'
INSTANCENAME: '.instancename',
NUMBER: 'span.text'
},
/**
@ -37,7 +47,18 @@ Y.Moodle.mod_quiz.util.page = {
},
/**
* Determines the page ID for the provided page.
* Retrieve the page item from one of it's previous siblings.
*
* @method getPageFromSlot
* @param pagecomponent {Node} The component Node.
* @return {Node|null} The Page Node.
*/
getPageFromSlot: function(slot) {
return Y.one(slot).previous(this.SELECTORS.PAGE);
},
/**
* Returns the page ID for the provided page.
*
* @method getId
* @param page {Node} The page to find an ID for.
@ -56,6 +77,18 @@ Y.Moodle.mod_quiz.util.page = {
return false;
},
/**
* Updates the page id for the provided page.
*
* @method setId
* @param page {Node} The page to update the number for.
* @param id int The id value.
* @return void
*/
setId: function(page, id) {
page.set('id', this.CONSTANTS.PAGEIDPREFIX + id);
},
/**
* Determines the page name for the provided page.
*
@ -79,8 +112,8 @@ Y.Moodle.mod_quiz.util.page = {
* @return {Number|false} The number of the page in question or false if no number was found.
*/
getNumber: function(page) {
// We perform a simple substitution operation to get the ID.
var number = page.get('text').replace(
// We perform a simple substitution operation to get the number.
var number = page.one(this.SELECTORS.NUMBER).get('text').replace(
this.CONSTANTS.PAGENUMBERPREFIX, '');
// Attempt to validate the ID.
@ -89,6 +122,206 @@ Y.Moodle.mod_quiz.util.page = {
return number;
}
return false;
},
/**
* Updates the page number for the provided page.
*
* @method setNumber
* @param page {Node} The page to update the number for.
* @return void
*/
setNumber: function(page, number) {
page.one(this.SELECTORS.NUMBER).set('text', this.CONSTANTS.PAGENUMBERPREFIX + number);
},
/**
* Returns a list of all page elements.
*
* @method getPages
* @return {node[]} An array containing page nodes.
*/
getPages: function() {
return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.PAGE);
},
/**
* Is the given element a page element?
*
* @method isPage
* @param page Page node
* @return boolean
*/
isPage: function(page) {
if (!page) {
return false;
}
return page.hasClass(this.CSS.PAGE);
},
/**
* Does the page have atleast one slot?
*
* @method isEmpty
* @param page Page node
* @return boolean
*/
isEmpty: function(page) {
var activity = page.next('li.activity');
if (!activity) {
return true;
}
return !activity.hasClass('slot');
},
/**
* Add a page and related elements to the list of slots.
*
* @method add
* @param beforenode Int | Node | HTMLElement | String to add
* @return page Page node
*/
add: function(beforenode) {
var pagenumber = this.getNumber(this.getPageFromSlot(beforenode)) + 1;
var pagehtml = M.mod_quiz.resource_toolbox.get('config').pagehtml;
// Normalise the page number.
pagehtml = pagehtml.replace(/%%PAGENUMBER%%/g, pagenumber);
// Create the page node.
var page = Y.Node.create(pagehtml);
// Assign is as a drop target.
YUI().use('dd-drop', function(Y) {
var drop = new Y.DD.Drop({
node: page,
groups: M.mod_quiz.dragres.groups
});
page.drop = drop;
});
// Insert in the correct place.
beforenode.insert(page, 'after');
// Enhance the add menu to make if fully visible and clickable.
M.core.actionmenu.newDOMNode(page);
return page;
},
/**
* Remove a page and related elements from the list of slots.
*
* @method remove
* @param page Page node
* @return void
*/
remove: function(page, keeppagebreak) {
// Remove page break from previous slot.
var previousslot = page.previous(Y.Moodle.mod_quiz.util.slot.SELECTORS.SLOT);
if (!keeppagebreak && previousslot) {
Y.Moodle.mod_quiz.util.slot.removePageBreak(previousslot);
}
page.remove();
},
/**
* Reset the order of the numbers given to each page.
*
* @method reorderPages
* @return void
*/
reorderPages: function() {
// Get list of page nodes.
var pages = this.getPages(), currentpagenumber = 0;
// Loop through pages incrementing the number each time.
pages.each(function(page) {
// Is the page empty?
if (this.isEmpty(page)) {
var keeppagebreak = page.next('li.slot') ? true : false;
this.remove(page, keeppagebreak);
return;
}
currentpagenumber++;
// Set page number.
this.setNumber(page, currentpagenumber);
this.setId(page, currentpagenumber);
}, this);
// Reorder action menus
this.reorderActionMenus();
},
/**
* Reset the order of the numbers given to each action menu.
*
* @method reorderActionMenus
* @return void
*/
reorderActionMenus: function() {
// Get list of action menu nodes.
var actionmenus = this.getActionMenus();
// Loop through pages incrementing the number each time.
actionmenus.each(function(actionmenu, key) {
var previousActionMenu = actionmenus.item(key - 1);
previousActionMenunumber = 0;
if (previousActionMenu) {
previousActionMenunumber = this.getActionMenuId(previousActionMenu);
}
var id = previousActionMenunumber + 1;
// Set menu id.
this.setActionMenuId(actionmenu, id);
// Update action-menu-1-menubar
var menubar = actionmenu.one(this.SELECTORS.ACTIONMENUBAR);
menubar.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUBARIDSUFFIX);
// Update action-menu-1-menu
var menumenu = actionmenu.one(this.SELECTORS.ACTIONMENUMENU);
menumenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUMENUIDSUFFIX);
}, this);
},
/**
* Returns a list of all page elements.
*
* @method getActionMenus
* @return {node[]} An array containing page nodes.
*/
getActionMenus: function() {
return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.ACTIONMENU);
},
/**
* Returns the ID for the provided action menu.
*
* @method getId
* @param actionmenu {Node} The actionmenu to find an ID for.
* @return {Number|false} The ID of the actionmenu in question or false if no ID was found.
*/
getActionMenuId: function(actionmenu) {
// We perform a simple substitution operation to get the ID.
var id = actionmenu.get('id').replace(
this.CONSTANTS.ACTIONMENUIDPREFIX, '');
// Attempt to validate the ID.
id = parseInt(id, 10);
if (typeof id === 'number' && isFinite(id)) {
return id;
}
return false;
},
/**
* Updates the page id for the provided page.
*
* @method setId
* @param page {Node} The page to update the number for.
* @param id int The id value.
* @return void
*/
setActionMenuId: function(actionmenu, id) {
actionmenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id);
}
};

View file

@ -1 +1 @@
YUI.add("moodle-mod_quiz-util-page",function(e,t){e.namespace("Moodle.mod_quiz.util.page"),e.Moodle.mod_quiz.util.page={CONSTANTS:{PAGEIDPREFIX:"page-",PAGENUMBERPREFIX:"Page "},SELECTORS:{PAGE:"li.page",INSTANCENAME:".instancename"},getPageFromComponent:function(t){return e.one(t).ancestor(this.SELECTORS.PAGE,!0)},getId:function(e){var t=e.get("id").replace(this.CONSTANTS.PAGEIDPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},getName:function(e){var t=e.one(this.SELECTORS.INSTANCENAME);return t?t.get("firstChild").get("data"):null},getNumber:function(e){var t=e.get("text").replace(this.CONSTANTS.PAGENUMBERPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1}}},"@VERSION@",{requires:["node","moodle-mod_quiz-util-base"]});
YUI.add("moodle-mod_quiz-util-page",function(e,t){e.namespace("Moodle.mod_quiz.util.page"),e.Moodle.mod_quiz.util.page={CSS:{PAGE:"page"},CONSTANTS:{ACTIONMENUIDPREFIX:"action-menu-",ACTIONMENUBARIDSUFFIX:"-menubar",ACTIONMENUMENUIDSUFFIX:"-menu",PAGEIDPREFIX:"page-",PAGENUMBERPREFIX:M.util.get_string("page","moodle")+" "},SELECTORS:{ACTIONMENU:"div.moodle-actionmenu",ACTIONMENUBAR:"ul.menubar",ACTIONMENUMENU:"ul.menu",PAGE:"li.page",INSTANCENAME:".instancename",NUMBER:"span.text"},getPageFromComponent:function(t){return e.one(t).ancestor(this.SELECTORS.PAGE,!0)},getPageFromSlot:function(t){return e.one(t).previous(this.SELECTORS.PAGE)},getId:function(e){var t=e.get("id").replace(this.CONSTANTS.PAGEIDPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},setId:function(e,t){e.set("id",this.CONSTANTS.PAGEIDPREFIX+t)},getName:function(e){var t=e.one(this.SELECTORS.INSTANCENAME);return t?t.get("firstChild").get("data"):null},getNumber:function(e){var t=e.one(this.SELECTORS.NUMBER).get("text").replace(this.CONSTANTS.PAGENUMBERPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},setNumber:function(e,t){e.one(this.SELECTORS.NUMBER).set("text",this.CONSTANTS.PAGENUMBERPREFIX+t)},getPages:function(){return e.all(e.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT+" "+e.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL+" "+this.SELECTORS.PAGE)},isPage:function(e){return e?e.hasClass(this.CSS.PAGE):!1},isEmpty:function(e){var t=e.next("li.activity");return t?!t.hasClass("slot"):!0},add:function(t){var n=this.getNumber(this.getPageFromSlot(t))+1,r=M.mod_quiz.resource_toolbox.get("config").pagehtml;r=r.replace(/%%PAGENUMBER%%/g,n);var i=e.Node.create(r);return YUI().use("dd-drop",function(e){var t=new e.DD.Drop({node:i,groups:M.mod_quiz.dragres.groups});i.drop=t}),t.insert(i,"after"),M.core.actionmenu.newDOMNode(i),i},remove:function(t,n){var r=t.previous(e.Moodle.mod_quiz.util.slot.SELECTORS.SLOT);!n&&r&&e.Moodle.mod_quiz.util.slot.removePageBreak(r),t.remove()},reorderPages:function(){var e=this.getPages(),t=0;e.each(function(e){if(this.isEmpty(e)){var n=e.next("li.slot")?!0:!1;this.remove(e,n);return}t++,this.setNumber(e,t),this.setId(e,t)},this),this.reorderActionMenus()},reorderActionMenus:function(){var e=this.getActionMenus();e.each(function(t,n){var r=e.item(n-1);previousActionMenunumber=0,r&&(previousActionMenunumber=this.getActionMenuId(r));var i=previousActionMenunumber+1;this.setActionMenuId(t,i);var s=t.one(this.SELECTORS.ACTIONMENUBAR);s.set("id",this.CONSTANTS.ACTIONMENUIDPREFIX+i+this.CONSTANTS.ACTIONMENUBARIDSUFFIX);var o=t.one(this.SELECTORS.ACTIONMENUMENU);o.set("id",this.CONSTANTS.ACTIONMENUIDPREFIX+i+this.CONSTANTS.ACTIONMENUMENUIDSUFFIX)},this)},getActionMenus:function(){return e.all(e.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT+" "+e.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL+" "+this.SELECTORS.ACTIONMENU)},getActionMenuId:function(e){var t=e.get("id").replace(this.CONSTANTS.ACTIONMENUIDPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},setActionMenuId:function(e,t){e.set("id",this.CONSTANTS.ACTIONMENUIDPREFIX+t)}}},"@VERSION@",{requires:["node","moodle-mod_quiz-util-base"]});

View file

@ -16,13 +16,23 @@ Y.namespace('Moodle.mod_quiz.util.page');
* @static
*/
Y.Moodle.mod_quiz.util.page = {
CSS: {
PAGE : 'page'
},
CONSTANTS: {
PAGEIDPREFIX : 'page-',
PAGENUMBERPREFIX : 'Page '
ACTIONMENUIDPREFIX: 'action-menu-',
ACTIONMENUBARIDSUFFIX: '-menubar',
ACTIONMENUMENUIDSUFFIX: '-menu',
PAGEIDPREFIX: 'page-',
PAGENUMBERPREFIX: M.util.get_string('page', 'moodle') + ' '
},
SELECTORS: {
ACTIONMENU: 'div.moodle-actionmenu',
ACTIONMENUBAR: 'ul.menubar',
ACTIONMENUMENU: 'ul.menu',
PAGE: 'li.page',
INSTANCENAME: '.instancename'
INSTANCENAME: '.instancename',
NUMBER: 'span.text'
},
/**
@ -37,7 +47,18 @@ Y.Moodle.mod_quiz.util.page = {
},
/**
* Determines the page ID for the provided page.
* Retrieve the page item from one of it's previous siblings.
*
* @method getPageFromSlot
* @param pagecomponent {Node} The component Node.
* @return {Node|null} The Page Node.
*/
getPageFromSlot: function(slot) {
return Y.one(slot).previous(this.SELECTORS.PAGE);
},
/**
* Returns the page ID for the provided page.
*
* @method getId
* @param page {Node} The page to find an ID for.
@ -56,6 +77,18 @@ Y.Moodle.mod_quiz.util.page = {
return false;
},
/**
* Updates the page id for the provided page.
*
* @method setId
* @param page {Node} The page to update the number for.
* @param id int The id value.
* @return void
*/
setId: function(page, id) {
page.set('id', this.CONSTANTS.PAGEIDPREFIX + id);
},
/**
* Determines the page name for the provided page.
*
@ -79,8 +112,8 @@ Y.Moodle.mod_quiz.util.page = {
* @return {Number|false} The number of the page in question or false if no number was found.
*/
getNumber: function(page) {
// We perform a simple substitution operation to get the ID.
var number = page.get('text').replace(
// We perform a simple substitution operation to get the number.
var number = page.one(this.SELECTORS.NUMBER).get('text').replace(
this.CONSTANTS.PAGENUMBERPREFIX, '');
// Attempt to validate the ID.
@ -89,6 +122,206 @@ Y.Moodle.mod_quiz.util.page = {
return number;
}
return false;
},
/**
* Updates the page number for the provided page.
*
* @method setNumber
* @param page {Node} The page to update the number for.
* @return void
*/
setNumber: function(page, number) {
page.one(this.SELECTORS.NUMBER).set('text', this.CONSTANTS.PAGENUMBERPREFIX + number);
},
/**
* Returns a list of all page elements.
*
* @method getPages
* @return {node[]} An array containing page nodes.
*/
getPages: function() {
return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.PAGE);
},
/**
* Is the given element a page element?
*
* @method isPage
* @param page Page node
* @return boolean
*/
isPage: function(page) {
if (!page) {
return false;
}
return page.hasClass(this.CSS.PAGE);
},
/**
* Does the page have atleast one slot?
*
* @method isEmpty
* @param page Page node
* @return boolean
*/
isEmpty: function(page) {
var activity = page.next('li.activity');
if (!activity) {
return true;
}
return !activity.hasClass('slot');
},
/**
* Add a page and related elements to the list of slots.
*
* @method add
* @param beforenode Int | Node | HTMLElement | String to add
* @return page Page node
*/
add: function(beforenode) {
var pagenumber = this.getNumber(this.getPageFromSlot(beforenode)) + 1;
var pagehtml = M.mod_quiz.resource_toolbox.get('config').pagehtml;
// Normalise the page number.
pagehtml = pagehtml.replace(/%%PAGENUMBER%%/g, pagenumber);
// Create the page node.
var page = Y.Node.create(pagehtml);
// Assign is as a drop target.
YUI().use('dd-drop', function(Y) {
var drop = new Y.DD.Drop({
node: page,
groups: M.mod_quiz.dragres.groups
});
page.drop = drop;
});
// Insert in the correct place.
beforenode.insert(page, 'after');
// Enhance the add menu to make if fully visible and clickable.
M.core.actionmenu.newDOMNode(page);
return page;
},
/**
* Remove a page and related elements from the list of slots.
*
* @method remove
* @param page Page node
* @return void
*/
remove: function(page, keeppagebreak) {
// Remove page break from previous slot.
var previousslot = page.previous(Y.Moodle.mod_quiz.util.slot.SELECTORS.SLOT);
if (!keeppagebreak && previousslot) {
Y.Moodle.mod_quiz.util.slot.removePageBreak(previousslot);
}
page.remove();
},
/**
* Reset the order of the numbers given to each page.
*
* @method reorderPages
* @return void
*/
reorderPages: function() {
// Get list of page nodes.
var pages = this.getPages(), currentpagenumber = 0;
// Loop through pages incrementing the number each time.
pages.each(function(page) {
// Is the page empty?
if (this.isEmpty(page)) {
var keeppagebreak = page.next('li.slot') ? true : false;
this.remove(page, keeppagebreak);
return;
}
currentpagenumber++;
// Set page number.
this.setNumber(page, currentpagenumber);
this.setId(page, currentpagenumber);
}, this);
// Reorder action menus
this.reorderActionMenus();
},
/**
* Reset the order of the numbers given to each action menu.
*
* @method reorderActionMenus
* @return void
*/
reorderActionMenus: function() {
// Get list of action menu nodes.
var actionmenus = this.getActionMenus();
// Loop through pages incrementing the number each time.
actionmenus.each(function(actionmenu, key) {
var previousActionMenu = actionmenus.item(key - 1);
previousActionMenunumber = 0;
if (previousActionMenu) {
previousActionMenunumber = this.getActionMenuId(previousActionMenu);
}
var id = previousActionMenunumber + 1;
// Set menu id.
this.setActionMenuId(actionmenu, id);
// Update action-menu-1-menubar
var menubar = actionmenu.one(this.SELECTORS.ACTIONMENUBAR);
menubar.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUBARIDSUFFIX);
// Update action-menu-1-menu
var menumenu = actionmenu.one(this.SELECTORS.ACTIONMENUMENU);
menumenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUMENUIDSUFFIX);
}, this);
},
/**
* Returns a list of all page elements.
*
* @method getActionMenus
* @return {node[]} An array containing page nodes.
*/
getActionMenus: function() {
return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.ACTIONMENU);
},
/**
* Returns the ID for the provided action menu.
*
* @method getId
* @param actionmenu {Node} The actionmenu to find an ID for.
* @return {Number|false} The ID of the actionmenu in question or false if no ID was found.
*/
getActionMenuId: function(actionmenu) {
// We perform a simple substitution operation to get the ID.
var id = actionmenu.get('id').replace(
this.CONSTANTS.ACTIONMENUIDPREFIX, '');
// Attempt to validate the ID.
id = parseInt(id, 10);
if (typeof id === 'number' && isFinite(id)) {
return id;
}
return false;
},
/**
* Updates the page id for the provided page.
*
* @method setId
* @param page {Node} The page to update the number for.
* @param id int The id value.
* @return void
*/
setActionMenuId: function(actionmenu, id) {
actionmenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id);
}
};

View file

@ -16,14 +16,22 @@ Y.namespace('Moodle.mod_quiz.util.slot');
* @static
*/
Y.Moodle.mod_quiz.util.slot = {
CSS: {
SLOT : 'slot',
QUESTIONTYPEDESCRIPTION : 'qtype_description'
},
CONSTANTS: {
SLOTIDPREFIX : 'slot-'
SLOTIDPREFIX : 'slot-',
QUESTION : M.util.get_string('question', 'moodle')
},
SELECTORS: {
SLOT: 'li.slot',
INSTANCENAME: '.instancename',
NUMBER: 'span.slotnumber',
PAGECONTENT : 'div#page-content',
PAGEBREAK : 'span.page_split_join_wrapper',
ICON : 'img.smallicon',
QUESTIONTYPEDESCRIPTION : '.qtype_description',
SECTIONUL : 'ul.section'
},
@ -81,7 +89,12 @@ Y.Moodle.mod_quiz.util.slot = {
* @return {Number|false} The number of the slot in question or false if no number was found.
*/
getNumber: function(slot) {
var number = slot.one(this.SELECTORS.NUMBER).get('text');
if (!slot) {
return false;
}
// We perform a simple substitution operation to get the number.
var number = slot.one(this.SELECTORS.NUMBER).get('text').replace(
this.CONSTANTS.QUESTION, '');
// Attempt to validate the ID.
number = parseInt(number, 10);
if (typeof number === 'number' && isFinite(number)) {
@ -98,7 +111,8 @@ Y.Moodle.mod_quiz.util.slot = {
* @return void
*/
setNumber: function(slot, number) {
slot.one(this.SELECTORS.NUMBER).set('text', number);
var numbernode = slot.one(this.SELECTORS.NUMBER);
numbernode.setHTML('<span class="accesshide">' + this.CONSTANTS.QUESTION + '</span> ' + number);
},
/**
@ -112,7 +126,19 @@ Y.Moodle.mod_quiz.util.slot = {
},
/**
* Returns the previous slot to the give slot.
* Returns a list of all slot elements on the page that have numbers. Excudes description questions.
*
* @method getSlots
* @return {node[]} An array containing slot nodes.
*/
getNumberedSlots: function() {
var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL;
selector += ' ' + this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')';
return Y.all(selector);
},
/**
* Returns the previous slot to the given slot.
*
* @method getPrevious
* @param slot Slot node
@ -122,27 +148,198 @@ Y.Moodle.mod_quiz.util.slot = {
return slot.previous(this.SELECTORS.SLOT);
},
/**
* Returns the previous numbered slot to the given slot.
*
* Ignores slots containing description question types.
*
* @method getPrevious
* @param slot Slot node
* @return {node|false} The previous slot node or false.
*/
getPreviousNumbered: function(slot) {
return slot.previous(this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')');
},
/**
* Reset the order of the numbers given to each slot.
*
* @method reorder_slots
* @method reorderSlots
* @return void
*/
reorder_slots: function() {
reorderSlots: function() {
// Get list of slot nodes.
var slots = this.getSlots();
// Loop through slots incrementing the number each time.
slots.each(function(slot) {
var previousSlot = this.getPrevious(slot),
previousslotnumber = 0;
if(previousSlot){
if (!Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot)) {
// Move the next page to the front.
var nextpage = slot.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE);
slot.swap(nextpage);
}
var previousSlot = this.getPreviousNumbered(slot);
previousslotnumber = 0;
if (slot.hasClass(this.CSS.QUESTIONTYPEDESCRIPTION)) {
return;
}
if (previousSlot) {
previousslotnumber = this.getNumber(previousSlot);
}
// Set slot number.
this.setNumber(slot, previousslotnumber + 1);
}, this);
},
/**
* Remove a slot and related elements from the list of slots.
*
* @method remove
* @param slot Slot node
* @return void
*/
remove: function(slot) {
var page = Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot);
slot.remove();
// Is the page empty.
if (!Y.Moodle.mod_quiz.util.page.isEmpty(page)) {
return;
}
// If so remove it. Including add menu and page break.
Y.Moodle.mod_quiz.util.page.remove(page);
},
/**
* Returns a list of all page break elements on the page.
*
* @method getPageBreaks
* @return {node[]} An array containing page break nodes.
*/
getPageBreaks: function() {
var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL;
selector += ' ' + this.SELECTORS.SLOT + this.SELECTORS.PAGEBREAK;
return Y.all(selector);
},
/**
* Retrieve the page break element item from the given slot.
*
* @method getPageBreak
* @param slot Slot node
* @return {Node|null} The Page Break Node.
*/
getPageBreak: function(slot) {
return Y.one(slot).one(this.SELECTORS.PAGEBREAK);
},
/**
* Add a page break and related elements to the list of slots.
*
* @method addPageBreak
* @param beforenode Int | Node | HTMLElement | String to add
* @return pagebreak PageBreak node
*/
addPageBreak: function(slot) {
var nodetext = M.mod_quiz.resource_toolbox.get('config').addpageiconhtml;
nodetext = nodetext.replace('%%SLOT%%', this.getNumber(slot));
var pagebreak = Y.Node.create(nodetext);
slot.one('div').insert(pagebreak, 'after');
return pagebreak;
},
/**
* Remove a pagebreak from the given slot.
*
* @method removePageBreak
* @param slot Slot node
* @return boolean
*/
removePageBreak: function(slot) {
var pagebreak = this.getPageBreak(slot);
if (!pagebreak) {
return false;
}
pagebreak.remove();
return true;
},
/**
* Reorder each pagebreak by iterating through each related slot.
*
* @method reorderPageBreaks
* @return void
*/
reorderPageBreaks: function() {
// Get list of slot nodes.
var slots = this.getSlots(), slotnumber = 0;
// Loop through slots incrementing the number each time.
slots.each (function(slot, key) {
slotnumber++;
var pagebreak = this.getPageBreak(slot);
// Last slot won't have a page break.
if (!pagebreak && key === slots.size() - 1) {
return;
}
// No pagebreak and not last slot. Add one.
if (!pagebreak && key !== slots.size() - 1) {
pagebreak = this.addPageBreak(slot);
}
// Remove last page break if there is one.
if (pagebreak && key === slots.size() - 1) {
this.removePageBreak(slot);
}
// Get page break anchor element.
var pagebreaklink = pagebreak.get('childNodes').item(0);
// Get the correct title.
var nextactivity = slot.next('li.activity');
var titlename = '', action = '', uri = M.cfg.wwwroot;
var iconsrc = uri + '/theme/image.php?theme=clean&component=core';
// IE8 can't handle svg images.
if (Y.one('body.ie8')) {
iconsrc += '&svg=e%2F0';
}
if (Y.Moodle.mod_quiz.util.page.isPage(nextactivity)) {
action = titlename = 'removepagebreak';
iconsrc += '&image=e%2Fremove_page_break';
} else {
action = titlename = 'addpagebreak';
iconsrc += '&image=e%2Finsert_page_break';
}
var title = M.util.get_string(titlename, 'quiz');
// Update the link and image titles
pagebreaklink.set('title', title);
pagebreaklink.setData('action', action);
// Update the image title.
var icon = pagebreaklink.one(this.SELECTORS.ICON);
icon.set('title', title);
// Update the image src.
icon.set('src', iconsrc);
// Get anchor url parameters as an associative array.
var params = Y.QueryString.parse(pagebreaklink.get('href'));
// Update slot number.
params.slot = slotnumber;
// Create the new url.
var newurl = '';
for (var index in params) {
if (newurl.length) {
newurl += "&";
}
newurl += index + "=" + params[index];
}
// Update the anchor.
pagebreaklink.set('href', newurl);
}, this);
}
};

View file

@ -1 +1 @@
YUI.add("moodle-mod_quiz-util-slot",function(e,t){e.namespace("Moodle.mod_quiz.util.slot"),e.Moodle.mod_quiz.util.slot={CONSTANTS:{SLOTIDPREFIX:"slot-"},SELECTORS:{SLOT:"li.slot",INSTANCENAME:".instancename",NUMBER:"span.slotnumber",PAGECONTENT:"div#page-content",SECTIONUL:"ul.section"},getSlotFromComponent:function(t){return e.one(t).ancestor(this.SELECTORS.SLOT,!0)},getId:function(e){var t=e.get("id").replace(this.CONSTANTS.SLOTIDPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},getName:function(e){var t=e.one(this.SELECTORS.INSTANCENAME);return t?t.get("firstChild").get("data"):null},getNumber:function(e){var t=e.one(this.SELECTORS.NUMBER).get("text");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},setNumber:function(e,t){e.one(this.SELECTORS.NUMBER).set("text",t)},getSlots:function(){return e.all(this.SELECTORS.PAGECONTENT+" "+this.SELECTORS.SECTIONUL+" "+this.SELECTORS.SLOT)},getPrevious:function(e){return e.previous(this.SELECTORS.SLOT)},reorder_slots:function(){var e=this.getSlots();e.each(function(e){var t=this.getPrevious(e),n=0;t&&(n=this.getNumber(t)),this.setNumber(e,n+1)},this)}}},"@VERSION@",{requires:["node","moodle-mod_quiz-util-base"]});
YUI.add("moodle-mod_quiz-util-slot",function(e,t){e.namespace("Moodle.mod_quiz.util.slot"),e.Moodle.mod_quiz.util.slot={CSS:{SLOT:"slot",QUESTIONTYPEDESCRIPTION:"qtype_description"},CONSTANTS:{SLOTIDPREFIX:"slot-",QUESTION:M.util.get_string("question","moodle")},SELECTORS:{SLOT:"li.slot",INSTANCENAME:".instancename",NUMBER:"span.slotnumber",PAGECONTENT:"div#page-content",PAGEBREAK:"span.page_split_join_wrapper",ICON:"img.smallicon",QUESTIONTYPEDESCRIPTION:".qtype_description",SECTIONUL:"ul.section"},getSlotFromComponent:function(t){return e.one(t).ancestor(this.SELECTORS.SLOT,!0)},getId:function(e){var t=e.get("id").replace(this.CONSTANTS.SLOTIDPREFIX,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},getName:function(e){var t=e.one(this.SELECTORS.INSTANCENAME);return t?t.get("firstChild").get("data"):null},getNumber:function(e){if(!e)return!1;var t=e.one(this.SELECTORS.NUMBER).get("text").replace(this.CONSTANTS.QUESTION,"");return t=parseInt(t,10),typeof t=="number"&&isFinite(t)?t:!1},setNumber:function(e,t){var n=e.one(this.SELECTORS.NUMBER);n.setHTML('<span class="accesshide">'+this.CONSTANTS.QUESTION+"</span> "+t)},getSlots:function(){return e.all(this.SELECTORS.PAGECONTENT+" "+this.SELECTORS.SECTIONUL+" "+this.SELECTORS.SLOT)},getNumberedSlots:function(){var t=this.SELECTORS.PAGECONTENT+" "+this.SELECTORS.SECTIONUL;return t+=" "+this.SELECTORS.SLOT+":not("+this.SELECTORS.QUESTIONTYPEDESCRIPTION+")",e.all(t)},getPrevious:function(e){return e.previous(this.SELECTORS.SLOT)},getPreviousNumbered:function(e){return e.previous(this.SELECTORS.SLOT+":not("+this.SELECTORS.QUESTIONTYPEDESCRIPTION+")")},reorderSlots:function(){var t=this.getSlots();t.each(function(t){if(!e.Moodle.mod_quiz.util.page.getPageFromSlot(t)){var n=t.next(e.Moodle.mod_quiz.util.page.SELECTORS.PAGE);t.swap(n)}var r=this.getPreviousNumbered(t);previousslotnumber=0;if(t.hasClass(this.CSS.QUESTIONTYPEDESCRIPTION))return;r&&(previousslotnumber=this.getNumber(r)),this.setNumber(t,previousslotnumber+1)},this)},remove:function(t){var n=e.Moodle.mod_quiz.util.page.getPageFromSlot(t);t.remove();if(!e.Moodle.mod_quiz.util.page.isEmpty(n))return;e.Moodle.mod_quiz.util.page.remove(n)},getPageBreaks:function(){var t=this.SELECTORS.PAGECONTENT+" "+this.SELECTORS.SECTIONUL;return t+=" "+this.SELECTORS.SLOT+this.SELECTORS.PAGEBREAK,e.all(t)},getPageBreak:function(t){return e.one(t).one(this.SELECTORS.PAGEBREAK)},addPageBreak:function(t){var n=M.mod_quiz.resource_toolbox.get("config").addpageiconhtml;n=n.replace("%%SLOT%%",this.getNumber(t));var r=e.Node.create(n);return t.one("div").insert(r,"after"),r},removePageBreak:function(e){var t=this.getPageBreak(e);return t?(t.remove(),!0):!1},reorderPageBreaks:function(){var t=this.getSlots(),n=0;t.each(function(r,i){n++;var s=this.getPageBreak(r);if(!s&&i===t.size()-1)return;!s&&i!==t.size()-1&&(s=this.addPageBreak(r)),s&&i===t.size()-1&&this.removePageBreak(r);var o=s.get("childNodes").item(0),u=r.next("li.activity"),a="",f="",l=M.cfg.wwwroot,c=l+"/theme/image.php?theme=clean&component=core";e.one("body.ie8")&&(c+="&svg=e%2F0"),e.Moodle.mod_quiz.util.page.isPage(u)?(f=a="removepagebreak",c+="&image=e%2Fremove_page_break"):(f=a="addpagebreak",c+="&image=e%2Finsert_page_break");var h=M.util.get_string(a,"quiz");o.set("title",h),o.setData("action",f);var p=o.one(this.SELECTORS.ICON);p.set("title",h),p.set("src",c);var d=e.QueryString.parse(o.get("href"));d.slot=n;var v="";for(var m in d)v.length&&(v+="&"),v+=m+"="+d[m];o.set("href",v)},this)}}},"@VERSION@",{requires:["node","moodle-mod_quiz-util-base"]});

View file

@ -16,14 +16,22 @@ Y.namespace('Moodle.mod_quiz.util.slot');
* @static
*/
Y.Moodle.mod_quiz.util.slot = {
CSS: {
SLOT : 'slot',
QUESTIONTYPEDESCRIPTION : 'qtype_description'
},
CONSTANTS: {
SLOTIDPREFIX : 'slot-'
SLOTIDPREFIX : 'slot-',
QUESTION : M.util.get_string('question', 'moodle')
},
SELECTORS: {
SLOT: 'li.slot',
INSTANCENAME: '.instancename',
NUMBER: 'span.slotnumber',
PAGECONTENT : 'div#page-content',
PAGEBREAK : 'span.page_split_join_wrapper',
ICON : 'img.smallicon',
QUESTIONTYPEDESCRIPTION : '.qtype_description',
SECTIONUL : 'ul.section'
},
@ -81,7 +89,12 @@ Y.Moodle.mod_quiz.util.slot = {
* @return {Number|false} The number of the slot in question or false if no number was found.
*/
getNumber: function(slot) {
var number = slot.one(this.SELECTORS.NUMBER).get('text');
if (!slot) {
return false;
}
// We perform a simple substitution operation to get the number.
var number = slot.one(this.SELECTORS.NUMBER).get('text').replace(
this.CONSTANTS.QUESTION, '');
// Attempt to validate the ID.
number = parseInt(number, 10);
if (typeof number === 'number' && isFinite(number)) {
@ -98,7 +111,8 @@ Y.Moodle.mod_quiz.util.slot = {
* @return void
*/
setNumber: function(slot, number) {
slot.one(this.SELECTORS.NUMBER).set('text', number);
var numbernode = slot.one(this.SELECTORS.NUMBER);
numbernode.setHTML('<span class="accesshide">' + this.CONSTANTS.QUESTION + '</span> ' + number);
},
/**
@ -112,7 +126,19 @@ Y.Moodle.mod_quiz.util.slot = {
},
/**
* Returns the previous slot to the give slot.
* Returns a list of all slot elements on the page that have numbers. Excudes description questions.
*
* @method getSlots
* @return {node[]} An array containing slot nodes.
*/
getNumberedSlots: function() {
var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL;
selector += ' ' + this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')';
return Y.all(selector);
},
/**
* Returns the previous slot to the given slot.
*
* @method getPrevious
* @param slot Slot node
@ -122,27 +148,198 @@ Y.Moodle.mod_quiz.util.slot = {
return slot.previous(this.SELECTORS.SLOT);
},
/**
* Returns the previous numbered slot to the given slot.
*
* Ignores slots containing description question types.
*
* @method getPrevious
* @param slot Slot node
* @return {node|false} The previous slot node or false.
*/
getPreviousNumbered: function(slot) {
return slot.previous(this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')');
},
/**
* Reset the order of the numbers given to each slot.
*
* @method reorder_slots
* @method reorderSlots
* @return void
*/
reorder_slots: function() {
reorderSlots: function() {
// Get list of slot nodes.
var slots = this.getSlots();
// Loop through slots incrementing the number each time.
slots.each(function(slot) {
var previousSlot = this.getPrevious(slot),
previousslotnumber = 0;
if(previousSlot){
if (!Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot)) {
// Move the next page to the front.
var nextpage = slot.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE);
slot.swap(nextpage);
}
var previousSlot = this.getPreviousNumbered(slot);
previousslotnumber = 0;
if (slot.hasClass(this.CSS.QUESTIONTYPEDESCRIPTION)) {
return;
}
if (previousSlot) {
previousslotnumber = this.getNumber(previousSlot);
}
// Set slot number.
this.setNumber(slot, previousslotnumber + 1);
}, this);
},
/**
* Remove a slot and related elements from the list of slots.
*
* @method remove
* @param slot Slot node
* @return void
*/
remove: function(slot) {
var page = Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot);
slot.remove();
// Is the page empty.
if (!Y.Moodle.mod_quiz.util.page.isEmpty(page)) {
return;
}
// If so remove it. Including add menu and page break.
Y.Moodle.mod_quiz.util.page.remove(page);
},
/**
* Returns a list of all page break elements on the page.
*
* @method getPageBreaks
* @return {node[]} An array containing page break nodes.
*/
getPageBreaks: function() {
var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL;
selector += ' ' + this.SELECTORS.SLOT + this.SELECTORS.PAGEBREAK;
return Y.all(selector);
},
/**
* Retrieve the page break element item from the given slot.
*
* @method getPageBreak
* @param slot Slot node
* @return {Node|null} The Page Break Node.
*/
getPageBreak: function(slot) {
return Y.one(slot).one(this.SELECTORS.PAGEBREAK);
},
/**
* Add a page break and related elements to the list of slots.
*
* @method addPageBreak
* @param beforenode Int | Node | HTMLElement | String to add
* @return pagebreak PageBreak node
*/
addPageBreak: function(slot) {
var nodetext = M.mod_quiz.resource_toolbox.get('config').addpageiconhtml;
nodetext = nodetext.replace('%%SLOT%%', this.getNumber(slot));
var pagebreak = Y.Node.create(nodetext);
slot.one('div').insert(pagebreak, 'after');
return pagebreak;
},
/**
* Remove a pagebreak from the given slot.
*
* @method removePageBreak
* @param slot Slot node
* @return boolean
*/
removePageBreak: function(slot) {
var pagebreak = this.getPageBreak(slot);
if (!pagebreak) {
return false;
}
pagebreak.remove();
return true;
},
/**
* Reorder each pagebreak by iterating through each related slot.
*
* @method reorderPageBreaks
* @return void
*/
reorderPageBreaks: function() {
// Get list of slot nodes.
var slots = this.getSlots(), slotnumber = 0;
// Loop through slots incrementing the number each time.
slots.each (function(slot, key) {
slotnumber++;
var pagebreak = this.getPageBreak(slot);
// Last slot won't have a page break.
if (!pagebreak && key === slots.size() - 1) {
return;
}
// No pagebreak and not last slot. Add one.
if (!pagebreak && key !== slots.size() - 1) {
pagebreak = this.addPageBreak(slot);
}
// Remove last page break if there is one.
if (pagebreak && key === slots.size() - 1) {
this.removePageBreak(slot);
}
// Get page break anchor element.
var pagebreaklink = pagebreak.get('childNodes').item(0);
// Get the correct title.
var nextactivity = slot.next('li.activity');
var titlename = '', action = '', uri = M.cfg.wwwroot;
var iconsrc = uri + '/theme/image.php?theme=clean&component=core';
// IE8 can't handle svg images.
if (Y.one('body.ie8')) {
iconsrc += '&svg=e%2F0';
}
if (Y.Moodle.mod_quiz.util.page.isPage(nextactivity)) {
action = titlename = 'removepagebreak';
iconsrc += '&image=e%2Fremove_page_break';
} else {
action = titlename = 'addpagebreak';
iconsrc += '&image=e%2Finsert_page_break';
}
var title = M.util.get_string(titlename, 'quiz');
// Update the link and image titles
pagebreaklink.set('title', title);
pagebreaklink.setData('action', action);
// Update the image title.
var icon = pagebreaklink.one(this.SELECTORS.ICON);
icon.set('title', title);
// Update the image src.
icon.set('src', iconsrc);
// Get anchor url parameters as an associative array.
var params = Y.QueryString.parse(pagebreaklink.get('href'));
// Update slot number.
params.slot = slotnumber;
// Create the new url.
var newurl = '';
for (var index in params) {
if (newurl.length) {
newurl += "&";
}
newurl += index + "=" + params[index];
}
// Update the anchor.
pagebreaklink.set('href', newurl);
}, this);
}
};

View file

@ -14,7 +14,6 @@ Y.extend(DRAGRESOURCE, M.core.dragdrop, {
this.groups = ['resource'];
this.samenodeclass = CSS.ACTIVITY;
this.parentnodeclass = CSS.SECTION;
//this.resourcedraghandle = this.get_drag_handle(M.util.get_string('movecoursemodule', 'moodle'), CSS.EDITINGMOVE, CSS.ICONCLASS, true);
this.resourcedraghandle = this.get_drag_handle(M.str.moodle.move, CSS.EDITINGMOVE, CSS.ICONCLASS, true);
this.samenodelabel = {
@ -168,12 +167,11 @@ Y.extend(DRAGRESOURCE, M.core.dragdrop, {
var responsetext = Y.JSON.parse(response.responseText);
var params = {element: dragnode, visible: responsetext.visible};
M.mod_quiz.quizbase.invoke_function('set_visibility_resource_ui', params);
Y.Moodle.mod_quiz.util.slot.reorder_slots();
this.unlock_drag_handle(drag, CSS.EDITINGMOVE);
window.setTimeout(function() {
spinner.hide();
}, 250);
window.location.reload(true);
M.mod_quiz.resource_toolbox.reorganise_edit_page();
},
failure: function(tid, response) {
this.ajax_failure(response);

View file

@ -10,7 +10,9 @@
"moodle-core-dragdrop",
"moodle-core-notification",
"moodle-mod_quiz-quizbase",
"moodle-mod_quiz-util",
"moodle-mod_quiz-util-base",
"moodle-mod_quiz-util-page",
"moodle-mod_quiz-util-slot",
"moodle-course-util"
]
}

View file

@ -77,9 +77,9 @@ M.mod_quiz.edit.swap_sections = function(Y, node1, node2) {
SECTIONADDMENUS : 'section_add_menus'
};
var sectionlist = Y.Node.all('.'+CSS.COURSECONTENT+' '+M.mod_quiz.edit.get_section_selector(Y));
var sectionlist = Y.Node.all('.' + CSS.COURSECONTENT + ' ' + M.mod_quiz.edit.get_section_selector(Y));
// Swap menus.
sectionlist.item(node1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.'+CSS.SECTIONADDMENUS));
sectionlist.item(node1).one('.' + CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.' + CSS.SECTIONADDMENUS));
};
/**
@ -115,7 +115,7 @@ M.mod_quiz.edit.process_sections = function(Y, sectionlist, response, sectionfro
for (var i = sectionfrom; i <= sectionto; i++) {
// Update section title.
sectionlist.item(i).one('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
sectionlist.item(i).one('.' + CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
// Update move icon.
ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);

View file

@ -101,10 +101,10 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// The user is deleting the activity.
this.delete_with_confirmation(ev, node, activity, action);
break;
case 'linkpage':
case 'unlinkpage':
// The user is linking or unlinking pages.
this.link_page(ev, node, activity, action);
case 'addpagebreak':
case 'removepagebreak':
// The user is adding or removing a page break.
this.update_page_break(ev, node, activity, action);
break;
default:
// Nothing to do here!
@ -140,10 +140,10 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* @chainable
*/
delete_with_confirmation: function(ev, button, activity) {
// Prevent the default button action
// Prevent the default button action.
ev.preventDefault();
// Get the element we're working on
// Get the element we're working on.
var element = activity,
// Create confirm string (different if element has or does not have name)
confirmstring = '',
@ -160,19 +160,24 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// If it is confirmed.
confirm.on('complete-yes', function() {
// Actually remove the element.
element.remove();
Y.Moodle.mod_quiz.util.slot.reorder_slots();
var spinner = this.add_spinner(element);
var data = {
'class': 'resource',
'action': 'DELETE',
'id': Y.Moodle.mod_quiz.util.slot.getId(element)
};
this.send_request(data);
if (M.core.actionmenu && M.core.actionmenu.instance) {
M.core.actionmenu.instance.hideMenu();
}
window.location.reload(true);
this.send_request(data, spinner, function(response) {
if (response.deleted) {
// Actually remove the element.
Y.Moodle.mod_quiz.util.slot.remove(element);
this.reorganise_edit_page();
if (M.core.actionmenu && M.core.actionmenu.instance) {
M.core.actionmenu.instance.hideMenu();
}
} else {
window.location.reload(true);
}
});
}, this);
@ -215,12 +220,12 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
M.core.actionmenu.instance.hideMenu();
}
// Try to retrieve the existing string from the server
// Try to retrieve the existing string from the server.
if (response.instancemaxmark) {
maxmarktext = response.instancemaxmark;
}
// Create the editor and submit button
// Create the editor and submit button.
var editform = Y.Node.create('<form action="#" />');
var editinstructions = Y.Node.create('<span class="' + CSS.EDITINSTRUCTIONS + '" id="id_editinstructions" />')
.set('innerHTML', M.util.get_string('edittitleinstructions', 'moodle'));
@ -232,7 +237,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
'size' : parseInt(this.get('config').questiondecimalpoints, 10) + 2
});
// Clear the existing content and put the editor in
// Clear the existing content and put the editor in.
editform.appendChild(editor);
editform.setData('anchor', anchor);
instance.insert(editinstructions, 'before');
@ -247,7 +252,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
// We hide various components whilst editing:
activity.addClass(CSS.EDITINGMAXMARK);
// Focus and select the editor text
// Focus and select the editor text.
editor.focus().select();
// Cancel the edit if we lose focus or the escape key is pressed.
@ -272,7 +277,7 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* @param {String} originalmaxmark The original maxmark the activity or resource had.
*/
edit_maxmark_submit : function(ev, activity, originalmaxmark) {
// We don't actually want to submit anything
// We don't actually want to submit anything.
ev.preventDefault();
var newmaxmark = Y.Lang.trim(activity.one(SELECTOR.ACTIVITYFORM + ' ' + SELECTOR.ACTIVITYMAXMARK).get('value'));
var spinner = this.add_spinner(activity);
@ -350,97 +355,59 @@ Y.extend(RESOURCETOOLBOX, TOOLBOX, {
* the other slots
*
* @protected
* @method link_page
* @method update_page_break
* @param {EventFacade} ev The event that was fired.
* @param {Node} button The button that triggered this action.
* @param {Node} activity The activity node that this action will be performed on.
* @chainable
*/
link_page: function(ev, button, activity, action) {
update_page_break: function(ev, button, activity, action) {
// Prevent the default button action
ev.preventDefault();
activity = activity.next('li.activity.slot');
var spinner = this.add_spinner(activity),
nextactivity = activity.next('li.activity.slot');
var spinner = this.add_spinner(nextactivity),
slotid = 0;
var value = action === 'linkpage' ? 1:2;
var value = action === 'removepagebreak' ? 1 : 2;
var data = {
'class': 'resource',
'field': 'linkslottopage',
'field': 'updatepagebreak',
'id': slotid,
'value': value
};
slotid = Y.Moodle.mod_quiz.util.slot.getId(activity);
slotid = Y.Moodle.mod_quiz.util.slot.getId(nextactivity);
if (slotid) {
data.id = Number(slotid);
}
this.send_request(data, spinner, function(response) {
window.location.reload(true);
// if (response.slots) {
// this.repaginate_slots(response.slots);
// }
if (response.slots) {
if (action === 'addpagebreak') {
Y.Moodle.mod_quiz.util.page.add(activity);
} else {
var page = activity.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE);
Y.Moodle.mod_quiz.util.page.remove(page, true);
}
this.reorganise_edit_page();
} else {
window.location.reload(true);
}
});
return this;
},
repaginate_slots: function(slots) {
this.slots = slots;
var section = Y.one(SELECTOR.PAGECONTENT + ' ' + SELECTOR.SECTIONUL),
activities = section.all(SELECTOR.ACTIVITYLI);
activities.each(function(node) {
// What element is it? page/slot/link
// what is the current slot?
var type;
var slot;
if(node.hasClass(CSS.PAGE)){
type = this.NODE_PAGE;
slot = node.next(SELECTOR.SLOTLI);
} else if (node.hasClass(CSS.SLOT)){
type = this.NODE_SLOT;
slot = node;
} else if (node.hasClass(CSS.JOIN)){
type = this.NODE_JOIN;
slot = node.previous(SELECTOR.SLOTLI);
}
// getSlotnumber() Should be a method of util.slot
var slotnumber = Number(Y.Moodle.mod_quiz.util.slot.getNumber(slot));
if(!type){
// Nothing we can do.
return;
}
// Is it correct?
if(!this.slots.hasOwnProperty(slotnumber)){
// An error. We should handle this.
return;
}
var slotdata = this.slots[slotnumber];
if(type === this.NODE_PAGE){
// Get page number
var pagenumber = Y.Moodle.mod_quiz.util.page.getNumber(node);
// Is the page number correct?
if (slotdata.page === pagenumber) {
console.log('slotdata.page == pagenumber return');
return;
}
if (pagenumber < slotdata.page) {
// Remove page node.
node.remove();
}
else {
// Add page node.
console.log('pagenumber > slotdata.page update page number');
}
}
}, this);
/**
* Reorganise the UI after every edit action.
*
* @protected
* @method reorganise_edit_page
*/
reorganise_edit_page: function() {
Y.Moodle.mod_quiz.util.slot.reorderSlots();
Y.Moodle.mod_quiz.util.slot.reorderPageBreaks();
Y.Moodle.mod_quiz.util.page.reorderPages();
},
NAME : 'mod_quiz-resource-toolbox',

View file

@ -14,13 +14,23 @@ Y.namespace('Moodle.mod_quiz.util.page');
* @static
*/
Y.Moodle.mod_quiz.util.page = {
CSS: {
PAGE : 'page'
},
CONSTANTS: {
PAGEIDPREFIX : 'page-',
PAGENUMBERPREFIX : 'Page '
ACTIONMENUIDPREFIX: 'action-menu-',
ACTIONMENUBARIDSUFFIX: '-menubar',
ACTIONMENUMENUIDSUFFIX: '-menu',
PAGEIDPREFIX: 'page-',
PAGENUMBERPREFIX: M.util.get_string('page', 'moodle') + ' '
},
SELECTORS: {
ACTIONMENU: 'div.moodle-actionmenu',
ACTIONMENUBAR: 'ul.menubar',
ACTIONMENUMENU: 'ul.menu',
PAGE: 'li.page',
INSTANCENAME: '.instancename'
INSTANCENAME: '.instancename',
NUMBER: 'span.text'
},
/**
@ -35,7 +45,18 @@ Y.Moodle.mod_quiz.util.page = {
},
/**
* Determines the page ID for the provided page.
* Retrieve the page item from one of it's previous siblings.
*
* @method getPageFromSlot
* @param pagecomponent {Node} The component Node.
* @return {Node|null} The Page Node.
*/
getPageFromSlot: function(slot) {
return Y.one(slot).previous(this.SELECTORS.PAGE);
},
/**
* Returns the page ID for the provided page.
*
* @method getId
* @param page {Node} The page to find an ID for.
@ -54,6 +75,18 @@ Y.Moodle.mod_quiz.util.page = {
return false;
},
/**
* Updates the page id for the provided page.
*
* @method setId
* @param page {Node} The page to update the number for.
* @param id int The id value.
* @return void
*/
setId: function(page, id) {
page.set('id', this.CONSTANTS.PAGEIDPREFIX + id);
},
/**
* Determines the page name for the provided page.
*
@ -77,8 +110,8 @@ Y.Moodle.mod_quiz.util.page = {
* @return {Number|false} The number of the page in question or false if no number was found.
*/
getNumber: function(page) {
// We perform a simple substitution operation to get the ID.
var number = page.get('text').replace(
// We perform a simple substitution operation to get the number.
var number = page.one(this.SELECTORS.NUMBER).get('text').replace(
this.CONSTANTS.PAGENUMBERPREFIX, '');
// Attempt to validate the ID.
@ -87,5 +120,205 @@ Y.Moodle.mod_quiz.util.page = {
return number;
}
return false;
},
/**
* Updates the page number for the provided page.
*
* @method setNumber
* @param page {Node} The page to update the number for.
* @return void
*/
setNumber: function(page, number) {
page.one(this.SELECTORS.NUMBER).set('text', this.CONSTANTS.PAGENUMBERPREFIX + number);
},
/**
* Returns a list of all page elements.
*
* @method getPages
* @return {node[]} An array containing page nodes.
*/
getPages: function() {
return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.PAGE);
},
/**
* Is the given element a page element?
*
* @method isPage
* @param page Page node
* @return boolean
*/
isPage: function(page) {
if (!page) {
return false;
}
return page.hasClass(this.CSS.PAGE);
},
/**
* Does the page have atleast one slot?
*
* @method isEmpty
* @param page Page node
* @return boolean
*/
isEmpty: function(page) {
var activity = page.next('li.activity');
if (!activity) {
return true;
}
return !activity.hasClass('slot');
},
/**
* Add a page and related elements to the list of slots.
*
* @method add
* @param beforenode Int | Node | HTMLElement | String to add
* @return page Page node
*/
add: function(beforenode) {
var pagenumber = this.getNumber(this.getPageFromSlot(beforenode)) + 1;
var pagehtml = M.mod_quiz.resource_toolbox.get('config').pagehtml;
// Normalise the page number.
pagehtml = pagehtml.replace(/%%PAGENUMBER%%/g, pagenumber);
// Create the page node.
var page = Y.Node.create(pagehtml);
// Assign is as a drop target.
YUI().use('dd-drop', function(Y) {
var drop = new Y.DD.Drop({
node: page,
groups: M.mod_quiz.dragres.groups
});
page.drop = drop;
});
// Insert in the correct place.
beforenode.insert(page, 'after');
// Enhance the add menu to make if fully visible and clickable.
M.core.actionmenu.newDOMNode(page);
return page;
},
/**
* Remove a page and related elements from the list of slots.
*
* @method remove
* @param page Page node
* @return void
*/
remove: function(page, keeppagebreak) {
// Remove page break from previous slot.
var previousslot = page.previous(Y.Moodle.mod_quiz.util.slot.SELECTORS.SLOT);
if (!keeppagebreak && previousslot) {
Y.Moodle.mod_quiz.util.slot.removePageBreak(previousslot);
}
page.remove();
},
/**
* Reset the order of the numbers given to each page.
*
* @method reorderPages
* @return void
*/
reorderPages: function() {
// Get list of page nodes.
var pages = this.getPages(), currentpagenumber = 0;
// Loop through pages incrementing the number each time.
pages.each(function(page) {
// Is the page empty?
if (this.isEmpty(page)) {
var keeppagebreak = page.next('li.slot') ? true : false;
this.remove(page, keeppagebreak);
return;
}
currentpagenumber++;
// Set page number.
this.setNumber(page, currentpagenumber);
this.setId(page, currentpagenumber);
}, this);
// Reorder action menus
this.reorderActionMenus();
},
/**
* Reset the order of the numbers given to each action menu.
*
* @method reorderActionMenus
* @return void
*/
reorderActionMenus: function() {
// Get list of action menu nodes.
var actionmenus = this.getActionMenus();
// Loop through pages incrementing the number each time.
actionmenus.each(function(actionmenu, key) {
var previousActionMenu = actionmenus.item(key - 1);
previousActionMenunumber = 0;
if (previousActionMenu) {
previousActionMenunumber = this.getActionMenuId(previousActionMenu);
}
var id = previousActionMenunumber + 1;
// Set menu id.
this.setActionMenuId(actionmenu, id);
// Update action-menu-1-menubar
var menubar = actionmenu.one(this.SELECTORS.ACTIONMENUBAR);
menubar.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUBARIDSUFFIX);
// Update action-menu-1-menu
var menumenu = actionmenu.one(this.SELECTORS.ACTIONMENUMENU);
menumenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id + this.CONSTANTS.ACTIONMENUMENUIDSUFFIX);
}, this);
},
/**
* Returns a list of all page elements.
*
* @method getActionMenus
* @return {node[]} An array containing page nodes.
*/
getActionMenus: function() {
return Y.all(Y.Moodle.mod_quiz.util.slot.SELECTORS.PAGECONTENT + ' ' + Y.Moodle.mod_quiz.util.slot.SELECTORS.SECTIONUL + ' ' + this.SELECTORS.ACTIONMENU);
},
/**
* Returns the ID for the provided action menu.
*
* @method getId
* @param actionmenu {Node} The actionmenu to find an ID for.
* @return {Number|false} The ID of the actionmenu in question or false if no ID was found.
*/
getActionMenuId: function(actionmenu) {
// We perform a simple substitution operation to get the ID.
var id = actionmenu.get('id').replace(
this.CONSTANTS.ACTIONMENUIDPREFIX, '');
// Attempt to validate the ID.
id = parseInt(id, 10);
if (typeof id === 'number' && isFinite(id)) {
return id;
}
return false;
},
/**
* Updates the page id for the provided page.
*
* @method setId
* @param page {Node} The page to update the number for.
* @param id int The id value.
* @return void
*/
setActionMenuId: function(actionmenu, id) {
actionmenu.set('id', this.CONSTANTS.ACTIONMENUIDPREFIX + id);
}
};

View file

@ -14,14 +14,22 @@ Y.namespace('Moodle.mod_quiz.util.slot');
* @static
*/
Y.Moodle.mod_quiz.util.slot = {
CSS: {
SLOT : 'slot',
QUESTIONTYPEDESCRIPTION : 'qtype_description'
},
CONSTANTS: {
SLOTIDPREFIX : 'slot-'
SLOTIDPREFIX : 'slot-',
QUESTION : M.util.get_string('question', 'moodle')
},
SELECTORS: {
SLOT: 'li.slot',
INSTANCENAME: '.instancename',
NUMBER: 'span.slotnumber',
PAGECONTENT : 'div#page-content',
PAGEBREAK : 'span.page_split_join_wrapper',
ICON : 'img.smallicon',
QUESTIONTYPEDESCRIPTION : '.qtype_description',
SECTIONUL : 'ul.section'
},
@ -79,7 +87,12 @@ Y.Moodle.mod_quiz.util.slot = {
* @return {Number|false} The number of the slot in question or false if no number was found.
*/
getNumber: function(slot) {
var number = slot.one(this.SELECTORS.NUMBER).get('text');
if (!slot) {
return false;
}
// We perform a simple substitution operation to get the number.
var number = slot.one(this.SELECTORS.NUMBER).get('text').replace(
this.CONSTANTS.QUESTION, '');
// Attempt to validate the ID.
number = parseInt(number, 10);
if (typeof number === 'number' && isFinite(number)) {
@ -96,7 +109,8 @@ Y.Moodle.mod_quiz.util.slot = {
* @return void
*/
setNumber: function(slot, number) {
slot.one(this.SELECTORS.NUMBER).set('text', number);
var numbernode = slot.one(this.SELECTORS.NUMBER);
numbernode.setHTML('<span class="accesshide">' + this.CONSTANTS.QUESTION + '</span> ' + number);
},
/**
@ -110,7 +124,19 @@ Y.Moodle.mod_quiz.util.slot = {
},
/**
* Returns the previous slot to the give slot.
* Returns a list of all slot elements on the page that have numbers. Excudes description questions.
*
* @method getSlots
* @return {node[]} An array containing slot nodes.
*/
getNumberedSlots: function() {
var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL;
selector += ' ' + this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')';
return Y.all(selector);
},
/**
* Returns the previous slot to the given slot.
*
* @method getPrevious
* @param slot Slot node
@ -120,26 +146,197 @@ Y.Moodle.mod_quiz.util.slot = {
return slot.previous(this.SELECTORS.SLOT);
},
/**
* Returns the previous numbered slot to the given slot.
*
* Ignores slots containing description question types.
*
* @method getPrevious
* @param slot Slot node
* @return {node|false} The previous slot node or false.
*/
getPreviousNumbered: function(slot) {
return slot.previous(this.SELECTORS.SLOT + ':not(' + this.SELECTORS.QUESTIONTYPEDESCRIPTION + ')');
},
/**
* Reset the order of the numbers given to each slot.
*
* @method reorder_slots
* @method reorderSlots
* @return void
*/
reorder_slots: function() {
reorderSlots: function() {
// Get list of slot nodes.
var slots = this.getSlots();
// Loop through slots incrementing the number each time.
slots.each(function(slot) {
var previousSlot = this.getPrevious(slot),
previousslotnumber = 0;
if(previousSlot){
if (!Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot)) {
// Move the next page to the front.
var nextpage = slot.next(Y.Moodle.mod_quiz.util.page.SELECTORS.PAGE);
slot.swap(nextpage);
}
var previousSlot = this.getPreviousNumbered(slot);
previousslotnumber = 0;
if (slot.hasClass(this.CSS.QUESTIONTYPEDESCRIPTION)) {
return;
}
if (previousSlot) {
previousslotnumber = this.getNumber(previousSlot);
}
// Set slot number.
this.setNumber(slot, previousslotnumber + 1);
}, this);
},
/**
* Remove a slot and related elements from the list of slots.
*
* @method remove
* @param slot Slot node
* @return void
*/
remove: function(slot) {
var page = Y.Moodle.mod_quiz.util.page.getPageFromSlot(slot);
slot.remove();
// Is the page empty.
if (!Y.Moodle.mod_quiz.util.page.isEmpty(page)) {
return;
}
// If so remove it. Including add menu and page break.
Y.Moodle.mod_quiz.util.page.remove(page);
},
/**
* Returns a list of all page break elements on the page.
*
* @method getPageBreaks
* @return {node[]} An array containing page break nodes.
*/
getPageBreaks: function() {
var selector = this.SELECTORS.PAGECONTENT + ' ' + this.SELECTORS.SECTIONUL;
selector += ' ' + this.SELECTORS.SLOT + this.SELECTORS.PAGEBREAK;
return Y.all(selector);
},
/**
* Retrieve the page break element item from the given slot.
*
* @method getPageBreak
* @param slot Slot node
* @return {Node|null} The Page Break Node.
*/
getPageBreak: function(slot) {
return Y.one(slot).one(this.SELECTORS.PAGEBREAK);
},
/**
* Add a page break and related elements to the list of slots.
*
* @method addPageBreak
* @param beforenode Int | Node | HTMLElement | String to add
* @return pagebreak PageBreak node
*/
addPageBreak: function(slot) {
var nodetext = M.mod_quiz.resource_toolbox.get('config').addpageiconhtml;
nodetext = nodetext.replace('%%SLOT%%', this.getNumber(slot));
var pagebreak = Y.Node.create(nodetext);
slot.one('div').insert(pagebreak, 'after');
return pagebreak;
},
/**
* Remove a pagebreak from the given slot.
*
* @method removePageBreak
* @param slot Slot node
* @return boolean
*/
removePageBreak: function(slot) {
var pagebreak = this.getPageBreak(slot);
if (!pagebreak) {
return false;
}
pagebreak.remove();
return true;
},
/**
* Reorder each pagebreak by iterating through each related slot.
*
* @method reorderPageBreaks
* @return void
*/
reorderPageBreaks: function() {
// Get list of slot nodes.
var slots = this.getSlots(), slotnumber = 0;
// Loop through slots incrementing the number each time.
slots.each (function(slot, key) {
slotnumber++;
var pagebreak = this.getPageBreak(slot);
// Last slot won't have a page break.
if (!pagebreak && key === slots.size() - 1) {
return;
}
// No pagebreak and not last slot. Add one.
if (!pagebreak && key !== slots.size() - 1) {
pagebreak = this.addPageBreak(slot);
}
// Remove last page break if there is one.
if (pagebreak && key === slots.size() - 1) {
this.removePageBreak(slot);
}
// Get page break anchor element.
var pagebreaklink = pagebreak.get('childNodes').item(0);
// Get the correct title.
var nextactivity = slot.next('li.activity');
var titlename = '', action = '', uri = M.cfg.wwwroot;
var iconsrc = uri + '/theme/image.php?theme=clean&component=core';
// IE8 can't handle svg images.
if (Y.one('body.ie8')) {
iconsrc += '&svg=e%2F0';
}
if (Y.Moodle.mod_quiz.util.page.isPage(nextactivity)) {
action = titlename = 'removepagebreak';
iconsrc += '&image=e%2Fremove_page_break';
} else {
action = titlename = 'addpagebreak';
iconsrc += '&image=e%2Finsert_page_break';
}
var title = M.util.get_string(titlename, 'quiz');
// Update the link and image titles
pagebreaklink.set('title', title);
pagebreaklink.setData('action', action);
// Update the image title.
var icon = pagebreaklink.one(this.SELECTORS.ICON);
icon.set('title', title);
// Update the image src.
icon.set('src', iconsrc);
// Get anchor url parameters as an associative array.
var params = Y.QueryString.parse(pagebreaklink.get('href'));
// Update slot number.
params.slot = slotnumber;
// Create the new url.
var newurl = '';
for (var index in params) {
if (newurl.length) {
newurl += "&";
}
newurl += index + "=" + params[index];
}
// Update the anchor.
pagebreaklink.set('href', newurl);
}, this);
}
};