From f87b22327fae5ee778df25b9324dbcd195b49efc Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 2 Apr 2014 09:41:31 +0800 Subject: [PATCH 1/2] MDL-32729 Question: Correct button selection when openin qbank chooser Some browsers do not bubble form submission events to the window, and we had more than one form on the page in certain conditions. We need to select all of the forms on a page and then test the action. --- question/tests/behat/behat_question_base.php | 2 +- .../moodle-question-chooser-debug.js | 6 +++++- .../moodle-question-chooser/moodle-question-chooser-min.js | 2 +- .../moodle-question-chooser/moodle-question-chooser.js | 6 +++++- question/yui/src/chooser/js/chooser.js | 6 +++++- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/question/tests/behat/behat_question_base.php b/question/tests/behat/behat_question_base.php index 58f9adb6714..9c7fdb429a8 100644 --- a/question/tests/behat/behat_question_base.php +++ b/question/tests/behat/behat_question_base.php @@ -54,7 +54,7 @@ class behat_question_base extends behat_base { return array( new Given('I set the field "' . $this->escape($questiontypename) . '" to "1"'), - new Given('I click on "#chooseqtype_submit" "css_element"'), + new Given('I click on ".submitbutton" "css_element"'), new Given('I set the following fields to these values:', $questiondata), new Given('I press "id_submitbutton"') ); diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js index 72d4f21335b..6e269b9e429 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js @@ -14,7 +14,11 @@ function Chooser() { Y.extend(Chooser, M.core.chooserdialogue, { initializer: function() { - Y.one(SELECTORS.CREATENEWQUESTIONFORM).on('submit', this.displayQuestionChooser, this); + Y.all('form').each(function(node) { + if (/question\/addquestion\.php/.test(node.getAttribute('action'))) { + node.on('submit', this.displayQuestionChooser, this); + } + }, this); }, displayQuestionChooser: function(e) { var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE), diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js index 08e989154fd..ae8a99de4b7 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js @@ -1 +1 @@ -YUI.add("moodle-question-chooser",function(e,t){function r(){r.superclass.constructor.apply(this,arguments)}var n={CREATENEWQUESTION:"div.createnewquestion",CREATENEWQUESTIONFORM:"div.createnewquestion form",CHOOSERDIALOGUE:"div.chooserdialogue",CHOOSERHEADER:"div.choosertitle",QBANKCATEGORY:"#qbankcategory"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.one(n.CREATENEWQUESTIONFORM).on("submit",this.displayQuestionChooser,this)},displayQuestionChooser:function(t){var r=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERDIALOGUE),i=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERHEADER);this.container===null&&(this.setup_chooser_dialogue(r,i,{}),this.prepare_chooser()),this.container.one(n.QBANKCATEGORY).set("value",e.one(n.CREATENEWQUESTIONFORM).get("category").get("value")),this.display_chooser(t)}},{NAME:"questionChooser"}),M.question=M.question||{},M.question.init_chooser=function(e){return new r(e)}},"@VERSION@",{requires:["moodle-core-chooserdialogue"]}); +YUI.add("moodle-question-chooser",function(e,t){function r(){r.superclass.constructor.apply(this,arguments)}var n={CREATENEWQUESTION:"div.createnewquestion",CREATENEWQUESTIONFORM:"div.createnewquestion form",CHOOSERDIALOGUE:"div.chooserdialogue",CHOOSERHEADER:"div.choosertitle",QBANKCATEGORY:"#qbankcategory"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.all("form").each(function(e){/question\/addquestion\.php/.test(e.getAttribute("action"))&&e.on("submit",this.displayQuestionChooser,this)},this)},displayQuestionChooser:function(t){var r=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERDIALOGUE),i=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERHEADER);this.container===null&&(this.setup_chooser_dialogue(r,i,{}),this.prepare_chooser()),this.container.one(n.QBANKCATEGORY).set("value",e.one(n.CREATENEWQUESTIONFORM).get("category").get("value")),this.display_chooser(t)}},{NAME:"questionChooser"}),M.question=M.question||{},M.question.init_chooser=function(e){return new r(e)}},"@VERSION@",{requires:["moodle-core-chooserdialogue"]}); diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser.js index 72d4f21335b..6e269b9e429 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser.js @@ -14,7 +14,11 @@ function Chooser() { Y.extend(Chooser, M.core.chooserdialogue, { initializer: function() { - Y.one(SELECTORS.CREATENEWQUESTIONFORM).on('submit', this.displayQuestionChooser, this); + Y.all('form').each(function(node) { + if (/question\/addquestion\.php/.test(node.getAttribute('action'))) { + node.on('submit', this.displayQuestionChooser, this); + } + }, this); }, displayQuestionChooser: function(e) { var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE), diff --git a/question/yui/src/chooser/js/chooser.js b/question/yui/src/chooser/js/chooser.js index afee1168cfd..db75e581faf 100644 --- a/question/yui/src/chooser/js/chooser.js +++ b/question/yui/src/chooser/js/chooser.js @@ -12,7 +12,11 @@ function Chooser() { Y.extend(Chooser, M.core.chooserdialogue, { initializer: function() { - Y.one(SELECTORS.CREATENEWQUESTIONFORM).on('submit', this.displayQuestionChooser, this); + Y.all('form').each(function(node) { + if (/question\/addquestion\.php/.test(node.getAttribute('action'))) { + node.on('submit', this.displayQuestionChooser, this); + } + }, this); }, displayQuestionChooser: function(e) { var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE), From 5c1af530db9db0b05c0498982e0c6454b51e3b64 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 2 Apr 2014 10:52:02 +0800 Subject: [PATCH 2/2] MDL-32729 Question: Load all hidden fields from the source form --- .../moodle-question-chooser-debug.js | 18 ++++++++++++------ .../moodle-question-chooser-min.js | 2 +- .../moodle-question-chooser.js | 18 ++++++++++++------ question/yui/src/chooser/js/chooser.js | 18 ++++++++++++------ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js index 6e269b9e429..dc9bac7a839 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js @@ -4,8 +4,7 @@ var SELECTORS = { CREATENEWQUESTION: 'div.createnewquestion', CREATENEWQUESTIONFORM: 'div.createnewquestion form', CHOOSERDIALOGUE: 'div.chooserdialogue', - CHOOSERHEADER: 'div.choosertitle', - QBANKCATEGORY: '#qbankcategory' + CHOOSERHEADER: 'div.choosertitle' }; function Chooser() { @@ -30,10 +29,17 @@ Y.extend(Chooser, M.core.chooserdialogue, { this.prepare_chooser(); } - // Set the category ID in the form - this may have been updated since the dialogue - // was previously displayed so we must update it here. - this.container.one(SELECTORS.QBANKCATEGORY).set('value', - Y.one(SELECTORS.CREATENEWQUESTIONFORM).get('category').get('value')); + // Update all of the hidden fields within the questionbank form. + var originForm = e.target.ancestor('form', true), + targetForm = this.container.one('form'), + hiddenElements = originForm.all('input[type="hidden"]'); + + targetForm.all('input.customfield').remove(); + hiddenElements.each(function(field) { + targetForm.appendChild(field.cloneNode()) + .removeAttribute('id') + .addClass('customfield'); + }); // Display the chooser dialogue. this.display_chooser(e); diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js index ae8a99de4b7..e80bd32f936 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js @@ -1 +1 @@ -YUI.add("moodle-question-chooser",function(e,t){function r(){r.superclass.constructor.apply(this,arguments)}var n={CREATENEWQUESTION:"div.createnewquestion",CREATENEWQUESTIONFORM:"div.createnewquestion form",CHOOSERDIALOGUE:"div.chooserdialogue",CHOOSERHEADER:"div.choosertitle",QBANKCATEGORY:"#qbankcategory"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.all("form").each(function(e){/question\/addquestion\.php/.test(e.getAttribute("action"))&&e.on("submit",this.displayQuestionChooser,this)},this)},displayQuestionChooser:function(t){var r=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERDIALOGUE),i=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERHEADER);this.container===null&&(this.setup_chooser_dialogue(r,i,{}),this.prepare_chooser()),this.container.one(n.QBANKCATEGORY).set("value",e.one(n.CREATENEWQUESTIONFORM).get("category").get("value")),this.display_chooser(t)}},{NAME:"questionChooser"}),M.question=M.question||{},M.question.init_chooser=function(e){return new r(e)}},"@VERSION@",{requires:["moodle-core-chooserdialogue"]}); +YUI.add("moodle-question-chooser",function(e,t){function r(){r.superclass.constructor.apply(this,arguments)}var n={CREATENEWQUESTION:"div.createnewquestion",CREATENEWQUESTIONFORM:"div.createnewquestion form",CHOOSERDIALOGUE:"div.chooserdialogue",CHOOSERHEADER:"div.choosertitle"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.all("form").each(function(e){/question\/addquestion\.php/.test(e.getAttribute("action"))&&e.on("submit",this.displayQuestionChooser,this)},this)},displayQuestionChooser:function(t){var r=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERDIALOGUE),i=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERHEADER);this.container===null&&(this.setup_chooser_dialogue(r,i,{}),this.prepare_chooser());var s=t.target.ancestor("form",!0),o=this.container.one("form"),u=s.all('input[type="hidden"]');o.all("input.customfield").remove(),u.each(function(e){o.appendChild(e.cloneNode()).removeAttribute("id").addClass("customfield")}),this.display_chooser(t)}},{NAME:"questionChooser"}),M.question=M.question||{},M.question.init_chooser=function(e){return new r(e)}},"@VERSION@",{requires:["moodle-core-chooserdialogue"]}); diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser.js index 6e269b9e429..dc9bac7a839 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser.js @@ -4,8 +4,7 @@ var SELECTORS = { CREATENEWQUESTION: 'div.createnewquestion', CREATENEWQUESTIONFORM: 'div.createnewquestion form', CHOOSERDIALOGUE: 'div.chooserdialogue', - CHOOSERHEADER: 'div.choosertitle', - QBANKCATEGORY: '#qbankcategory' + CHOOSERHEADER: 'div.choosertitle' }; function Chooser() { @@ -30,10 +29,17 @@ Y.extend(Chooser, M.core.chooserdialogue, { this.prepare_chooser(); } - // Set the category ID in the form - this may have been updated since the dialogue - // was previously displayed so we must update it here. - this.container.one(SELECTORS.QBANKCATEGORY).set('value', - Y.one(SELECTORS.CREATENEWQUESTIONFORM).get('category').get('value')); + // Update all of the hidden fields within the questionbank form. + var originForm = e.target.ancestor('form', true), + targetForm = this.container.one('form'), + hiddenElements = originForm.all('input[type="hidden"]'); + + targetForm.all('input.customfield').remove(); + hiddenElements.each(function(field) { + targetForm.appendChild(field.cloneNode()) + .removeAttribute('id') + .addClass('customfield'); + }); // Display the chooser dialogue. this.display_chooser(e); diff --git a/question/yui/src/chooser/js/chooser.js b/question/yui/src/chooser/js/chooser.js index db75e581faf..8ab920006cd 100644 --- a/question/yui/src/chooser/js/chooser.js +++ b/question/yui/src/chooser/js/chooser.js @@ -2,8 +2,7 @@ var SELECTORS = { CREATENEWQUESTION: 'div.createnewquestion', CREATENEWQUESTIONFORM: 'div.createnewquestion form', CHOOSERDIALOGUE: 'div.chooserdialogue', - CHOOSERHEADER: 'div.choosertitle', - QBANKCATEGORY: '#qbankcategory' + CHOOSERHEADER: 'div.choosertitle' }; function Chooser() { @@ -28,10 +27,17 @@ Y.extend(Chooser, M.core.chooserdialogue, { this.prepare_chooser(); } - // Set the category ID in the form - this may have been updated since the dialogue - // was previously displayed so we must update it here. - this.container.one(SELECTORS.QBANKCATEGORY).set('value', - Y.one(SELECTORS.CREATENEWQUESTIONFORM).get('category').get('value')); + // Update all of the hidden fields within the questionbank form. + var originForm = e.target.ancestor('form', true), + targetForm = this.container.one('form'), + hiddenElements = originForm.all('input[type="hidden"]'); + + targetForm.all('input.customfield').remove(); + hiddenElements.each(function(field) { + targetForm.appendChild(field.cloneNode()) + .removeAttribute('id') + .addClass('customfield'); + }); // Display the chooser dialogue. this.display_chooser(e);