From 73c4aa313bcdf4fffda52964f84d250feb5ebb2b Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 3 May 2019 18:39:13 +1000 Subject: [PATCH] MDL-64988 mod_quiz: Fix questions always being added to the last section --- .../amd/build/modal_quiz_question_bank.min.js | 2 +- mod/quiz/amd/src/modal_quiz_question_bank.js | 13 ++++++++++ .../editing_add_from_question_bank.feature | 24 ++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/mod/quiz/amd/build/modal_quiz_question_bank.min.js b/mod/quiz/amd/build/modal_quiz_question_bank.min.js index 63b526ef271..8e52442f11f 100644 --- a/mod/quiz/amd/build/modal_quiz_question_bank.min.js +++ b/mod/quiz/amd/build/modal_quiz_question_bank.min.js @@ -1 +1 @@ -define(["jquery","core/yui","core/notification","core/modal","core/modal_events","core/modal_registry","core/fragment"],function(a,b,c,d,e,f,g){var h=!1,i={ADD_TO_QUIZ_CONTAINER:"td.addtoquizaction",ANCHOR:"a[href]",PREVIEW_CONTAINER:"td.previewaction",SEARCH_OPTIONS:"#advancedsearch",DISPLAY_OPTIONS:"#displayoptions"},j=function(a){d.call(this,a),this.contextId=null,this.addOnPageId=null};return j.TYPE="mod_quiz-quiz-question-bank",j.prototype=Object.create(d.prototype),j.prototype.constructor=j,j.prototype.setContextId=function(a){this.contextId=a},j.prototype.getContextId=function(){return this.contextId},j.prototype.setAddOnPageId=function(a){this.addOnPageId=a},j.prototype.getAddOnPageId=function(){return this.addOnPageId},j.prototype.show=function(){return this.reloadBodyContent(window.location.search),d.prototype.show.call(this)},j.prototype.reloadBodyContent=function(a){var b=g.loadFragment("mod_quiz","quiz_question_bank",this.getContextId(),{querystring:a}).fail(c.exception);this.setBody(b)},j.prototype.handleAddToQuizEvent=function(a,b){var c=b.attr("href")+"&addonpage="+this.getAddOnPageId();b.attr("href",c)},j.prototype.handlePreviewContainerEvent=function(a,b){var c=["height=600","width=800","top=0","left=0","menubar=0","location=0","scrollbars","resizable","toolbar","status","directories=0","fullscreen=0","dependent"];window.openpopup(a,{url:b.attr("href"),name:"questionpreview",options:c.join(",")})},j.prototype.handleDisplayOptionFormEvent=function(b){b.stopPropagation(),b.preventDefault();var c=a(b.target).closest(i.DISPLAY_OPTIONS),d="?"+c.serialize();this.reloadBodyContent(d)},j.prototype.registerDisplayOptionListeners=function(){this.getModal().on("change",i.DISPLAY_OPTIONS,function(b){var c=a(b.target);c.attr("aria-autocomplete")||this.handleDisplayOptionFormEvent(b)}.bind(this)),this.getModal().on("submit",i.DISPLAY_OPTIONS,function(a){this.handleDisplayOptionFormEvent(a)}.bind(this))},j.prototype.registerEventListeners=function(){d.prototype.registerEventListeners.call(this),this.registerDisplayOptionListeners(),this.getModal().on("click",i.ANCHOR,function(b){var c=a(b.currentTarget);return c.closest(i.ADD_TO_QUIZ_CONTAINER).length?void this.handleAddToQuizEvent(b,c):c.closest(i.PREVIEW_CONTAINER).length?void this.handlePreviewContainerEvent(b,c):void(c.closest(i.SEARCH_OPTIONS).length||(b.preventDefault(),this.reloadBodyContent(c.prop("search"))))}.bind(this)),this.getRoot().on(e.bodyRendered,function(){b.use("moodle-core-formchangechecker",function(){M.core_formchangechecker.reset_form_dirty_state()})})},h||(f.register(j.TYPE,j,"core/modal"),h=!0),j}); \ No newline at end of file +define(["jquery","core/yui","core/notification","core/modal","core/modal_events","core/modal_registry","core/fragment"],function(a,b,c,d,e,f,g){var h=!1,i={ADD_TO_QUIZ_CONTAINER:"td.addtoquizaction",ANCHOR:"a[href]",PREVIEW_CONTAINER:"td.previewaction",SEARCH_OPTIONS:"#advancedsearch",DISPLAY_OPTIONS:"#displayoptions",ADD_QUESTIONS_FORM:'form[action="edit.php"]'},j=function(a){d.call(this,a),this.contextId=null,this.addOnPageId=null};return j.TYPE="mod_quiz-quiz-question-bank",j.prototype=Object.create(d.prototype),j.prototype.constructor=j,j.prototype.setContextId=function(a){this.contextId=a},j.prototype.getContextId=function(){return this.contextId},j.prototype.setAddOnPageId=function(a){this.addOnPageId=a},j.prototype.getAddOnPageId=function(){return this.addOnPageId},j.prototype.show=function(){return this.reloadBodyContent(window.location.search),d.prototype.show.call(this)},j.prototype.reloadBodyContent=function(a){var b=g.loadFragment("mod_quiz","quiz_question_bank",this.getContextId(),{querystring:a}).fail(c.exception);this.setBody(b)},j.prototype.handleAddToQuizEvent=function(a,b){var c=b.attr("href")+"&addonpage="+this.getAddOnPageId();b.attr("href",c)},j.prototype.handlePreviewContainerEvent=function(a,b){var c=["height=600","width=800","top=0","left=0","menubar=0","location=0","scrollbars","resizable","toolbar","status","directories=0","fullscreen=0","dependent"];window.openpopup(a,{url:b.attr("href"),name:"questionpreview",options:c.join(",")})},j.prototype.handleDisplayOptionFormEvent=function(b){b.stopPropagation(),b.preventDefault();var c=a(b.target).closest(i.DISPLAY_OPTIONS),d="?"+c.serialize();this.reloadBodyContent(d)},j.prototype.registerDisplayOptionListeners=function(){this.getModal().on("change",i.DISPLAY_OPTIONS,function(b){var c=a(b.target);c.attr("aria-autocomplete")||this.handleDisplayOptionFormEvent(b)}.bind(this)),this.getModal().on("submit",i.DISPLAY_OPTIONS,function(a){this.handleDisplayOptionFormEvent(a)}.bind(this))},j.prototype.registerEventListeners=function(){d.prototype.registerEventListeners.call(this),this.registerDisplayOptionListeners(),this.getModal().on("submit",i.ADD_QUESTIONS_FORM,function(b){var c=a(b.currentTarget);a("").attr("type","hidden").attr("name","addonpage").attr("value",this.getAddOnPageId()).appendTo(c)}.bind(this)),this.getModal().on("click",i.ANCHOR,function(b){var c=a(b.currentTarget);return c.closest(i.ADD_TO_QUIZ_CONTAINER).length?void this.handleAddToQuizEvent(b,c):c.closest(i.PREVIEW_CONTAINER).length?void this.handlePreviewContainerEvent(b,c):void(c.closest(i.SEARCH_OPTIONS).length||(b.preventDefault(),this.reloadBodyContent(c.prop("search"))))}.bind(this)),this.getRoot().on(e.bodyRendered,function(){b.use("moodle-core-formchangechecker",function(){M.core_formchangechecker.reset_form_dirty_state()})})},h||(f.register(j.TYPE,j,"core/modal"),h=!0),j}); \ No newline at end of file diff --git a/mod/quiz/amd/src/modal_quiz_question_bank.js b/mod/quiz/amd/src/modal_quiz_question_bank.js index 618064583aa..f7f3535b1ff 100644 --- a/mod/quiz/amd/src/modal_quiz_question_bank.js +++ b/mod/quiz/amd/src/modal_quiz_question_bank.js @@ -47,6 +47,7 @@ function( PREVIEW_CONTAINER: 'td.previewaction', SEARCH_OPTIONS: '#advancedsearch', DISPLAY_OPTIONS: '#displayoptions', + ADD_QUESTIONS_FORM: 'form[action="edit.php"]', }; /** @@ -258,6 +259,18 @@ function( // Set up the event handlers for all of the display options. this.registerDisplayOptionListeners(); + this.getModal().on('submit', SELECTORS.ADD_QUESTIONS_FORM, function(e) { + // If the user clicks on the "Add selected questions to the quiz" button to add some questions to the page + // then we need to intercept the submit in order to include the correct "add on page id" before the form is + // submitted. + var formElement = $(e.currentTarget); + + $('').attr('type', 'hidden') + .attr('name', "addonpage") + .attr('value', this.getAddOnPageId()) + .appendTo(formElement); + }.bind(this)); + this.getModal().on('click', SELECTORS.ANCHOR, function(e) { var anchorElement = $(e.currentTarget); diff --git a/mod/quiz/tests/behat/editing_add_from_question_bank.feature b/mod/quiz/tests/behat/editing_add_from_question_bank.feature index 3db5abdd018..73e5d37e9b3 100644 --- a/mod/quiz/tests/behat/editing_add_from_question_bank.feature +++ b/mod/quiz/tests/behat/editing_add_from_question_bank.feature @@ -1,4 +1,4 @@ -@core @core_question +@mod @mod_quiz @javascript Feature: Adding questions to a quiz from the question bank In order to re-use questions As a teacher @@ -47,3 +47,25 @@ Feature: Adding questions to a quiz from the question bank And I press key "13" in the field "Filter by tags..." Then I should see "question 1 name" in the "categoryquestions" "table" And I should not see "question 2 name" in the "categoryquestions" "table" + + Scenario: Questions are added in the right place with multiple sections + Given the following "questions" exist: + | questioncategory | qtype | name | questiontext | + | Test questions | essay | question 3 name | question 3 text | + And quiz "Quiz 1" contains the following questions: + | question | page | + | question 1 name | 1 | + | question 2 name | 2 | + And quiz "Quiz 1" contains the following sections: + | heading | firstslot | shuffle | + | Section 1 | 1 | 0 | + | Section 2 | 2 | 0 | + And I am on "Course 1" course homepage + And I follow "Quiz 1" + When I navigate to "Edit quiz" in current page administration + And I open the "Page 1" add to quiz menu + And I follow "from question bank" + And I set the field with xpath "//tr[contains(normalize-space(.), 'question 3 name')]//input[@type='checkbox']" to "1" + And I click on "Add selected questions to the quiz" "button" + Then I should see "question 3 name" on quiz page "1" + And I should see "question 1 name" before "question 3 name" on the edit quiz page \ No newline at end of file