From ae1a4cf5a7fe2343c8beead2bffee2d44feee70d Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Wed, 30 Mar 2022 15:30:42 +0700 Subject: [PATCH] MDL-69552 Quiz: Fix tag filter does not work when adding random question --- mod/quiz/amd/build/add_random_form.min.js | 2 +- mod/quiz/amd/build/add_random_form.min.js.map | 2 +- mod/quiz/amd/src/add_random_form.js | 3 +- .../tests/behat/editing_add_random.feature | 32 +++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/mod/quiz/amd/build/add_random_form.min.js b/mod/quiz/amd/build/add_random_form.min.js index f04b10b000f..50654404201 100644 --- a/mod/quiz/amd/build/add_random_form.min.js +++ b/mod/quiz/amd/build/add_random_form.min.js @@ -5,6 +5,6 @@ * @copyright 2018 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define("mod_quiz/add_random_form",["jquery","mod_quiz/random_question_form_preview"],(function($,RandomQuestionFormPreview){var SELECTORS_PREVIEW_CONTAINER='[data-region="random-question-preview-container"]',SELECTORS_CATEGORY_FORM_ELEMENT='[name="category"]',SELECTORS_SUBCATEGORY_FORM_ELEMENT='[name="includesubcategories"]',SELECTORS_TAG_IDS_FORM_ELEMENT='[name="fromtags[]"]',getCategorySelectValue=function(form){return form.find(SELECTORS_CATEGORY_FORM_ELEMENT).val()},shouldIncludeSubcategories=function(form,topCategories){return!!function(form,topCategories){var selectedValue=getCategorySelectValue(form);return topCategories.indexOf(selectedValue)>-1}(form,topCategories)||form.find(SELECTORS_SUBCATEGORY_FORM_ELEMENT).is(":checked")},reloadQuestionPreview=function(form,contextId,topCategories){var previewContainer=form.find(SELECTORS_PREVIEW_CONTAINER);RandomQuestionFormPreview.reload(previewContainer,function(form){return getCategorySelectValue(form).split(",")[0]}(form),shouldIncludeSubcategories(form,topCategories),function(form){return form.find(SELECTORS_TAG_IDS_FORM_ELEMENT).val().map((function(value){return value.split(",")[0]}))}(form),contextId)},addEventListeners=function(form,contextId,topCategories){var reloadTimerId=null;form.on("change",(function(e){var element;((element=$(e.target)).closest(SELECTORS_CATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_SUBCATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_TAG_IDS_FORM_ELEMENT).length>0)&&(RandomQuestionFormPreview.showLoadingIcon(form),reloadTimerId&&clearTimeout(reloadTimerId),reloadTimerId=setTimeout((function(){reloadQuestionPreview(form,contextId,topCategories)}),2e3))}))};return{init:function(formId,contextId,topCategories,isTagsEnabled){if(1==isTagsEnabled){var form=$("#"+formId);reloadQuestionPreview(form,contextId,topCategories),addEventListeners(form,contextId,topCategories)}}}})); +define("mod_quiz/add_random_form",["jquery","mod_quiz/random_question_form_preview"],(function($,RandomQuestionFormPreview){var SELECTORS_PREVIEW_CONTAINER='[data-region="random-question-preview-container"]',SELECTORS_CATEGORY_FORM_ELEMENT='[name="category"]',SELECTORS_SUBCATEGORY_FORM_ELEMENT='[name="includesubcategories"]',SELECTORS_TAG_IDS_FORM_ELEMENT='[name="fromtags[]"]',getCategorySelectValue=function(form){return form.find(SELECTORS_CATEGORY_FORM_ELEMENT).val()},shouldIncludeSubcategories=function(form,topCategories){return!!function(form,topCategories){var selectedValue=getCategorySelectValue(form);return topCategories.indexOf(selectedValue)>-1}(form,topCategories)||form.find(SELECTORS_SUBCATEGORY_FORM_ELEMENT).is(":checked")},reloadQuestionPreview=function(form,contextId,topCategories){var previewContainer=form.find(SELECTORS_PREVIEW_CONTAINER);RandomQuestionFormPreview.reload(previewContainer,function(form){return getCategorySelectValue(form).split(",")[0]}(form),shouldIncludeSubcategories(form,topCategories),function(form){return form.find(SELECTORS_TAG_IDS_FORM_ELEMENT).val().map((function(value){return value.split(",")[0]}))}(form),contextId)},addEventListeners=function(form,contextId,topCategories){var reloadTimerId=null,tagsFilter=form.find(SELECTORS_TAG_IDS_FORM_ELEMENT);form.add(tagsFilter).on("change",(function(e){var element;((element=$(e.target)).closest(SELECTORS_CATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_SUBCATEGORY_FORM_ELEMENT).length>0||element.closest(SELECTORS_TAG_IDS_FORM_ELEMENT).length>0)&&(RandomQuestionFormPreview.showLoadingIcon(form),reloadTimerId&&clearTimeout(reloadTimerId),reloadTimerId=setTimeout((function(){reloadQuestionPreview(form,contextId,topCategories)}),2e3))}))};return{init:function(formId,contextId,topCategories,isTagsEnabled){if(1==isTagsEnabled){var form=$("#"+formId);reloadQuestionPreview(form,contextId,topCategories),addEventListeners(form,contextId,topCategories)}}}})); //# sourceMappingURL=add_random_form.min.js.map \ No newline at end of file diff --git a/mod/quiz/amd/build/add_random_form.min.js.map b/mod/quiz/amd/build/add_random_form.min.js.map index 7379d2bd19c..912ebb90bdc 100644 --- a/mod/quiz/amd/build/add_random_form.min.js.map +++ b/mod/quiz/amd/build/add_random_form.min.js.map @@ -1 +1 @@ -{"version":3,"file":"add_random_form.min.js","sources":["../src/add_random_form.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * JavaScript for the add_random_form class.\n *\n * @module mod_quiz/add_random_form\n * @copyright 2018 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(\n [\n 'jquery',\n 'mod_quiz/random_question_form_preview'\n ],\n function(\n $,\n RandomQuestionFormPreview\n ) {\n\n // Wait 2 seconds before reloading the question set just in case\n // the user is still changing the criteria.\n var RELOAD_DELAY = 2000;\n var SELECTORS = {\n PREVIEW_CONTAINER: '[data-region=\"random-question-preview-container\"]',\n CATEGORY_FORM_ELEMENT: '[name=\"category\"]',\n SUBCATEGORY_FORM_ELEMENT: '[name=\"includesubcategories\"]',\n TAG_IDS_FORM_ELEMENT: '[name=\"fromtags[]\"]'\n };\n\n /**\n * Get the selected category value from the form.\n *\n * @param {jquery} form The form element.\n * @return {string} The category value.\n */\n var getCategorySelectValue = function(form) {\n return form.find(SELECTORS.CATEGORY_FORM_ELEMENT).val();\n };\n\n /**\n * Get the category id from the form.\n *\n * @param {jquery} form The form element.\n * @return {string} The category id.\n */\n var getCategoryId = function(form) {\n // The value string is the category id and category context id joined\n // by a comma.\n var valueString = getCategorySelectValue(form);\n // Split the two ids.\n var values = valueString.split(',');\n // Return just the category id.\n return values[0];\n };\n\n /**\n * Check if a top level category is selected in the form.\n *\n * @param {jquery} form The form element.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n * @return {bool}\n */\n var isTopLevelCategorySelected = function(form, topCategories) {\n var selectedValue = getCategorySelectValue(form);\n return (topCategories.indexOf(selectedValue) > -1);\n };\n\n /**\n * Check if the form indicates we should include include subcategories in\n * the filter.\n *\n * @param {jquery} form The form element.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n * @return {bool}\n */\n var shouldIncludeSubcategories = function(form, topCategories) {\n if (isTopLevelCategorySelected(form, topCategories)) {\n return true;\n } else {\n return form.find(SELECTORS.SUBCATEGORY_FORM_ELEMENT).is(':checked');\n }\n };\n\n /**\n * Get the tag ids for the selected tags in the form.\n *\n * @param {jquery} form The form element.\n * @return {string[]} The tag ids.\n */\n var getTagIds = function(form) {\n var values = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT).val();\n return values.map(function(value) {\n // The tag element value is the tag id and tag name joined\n // by a comma. So we need to split them to get the tag id.\n var parts = value.split(',');\n return parts[0];\n });\n };\n\n /**\n * Reload the preview section with a new set of filters.\n *\n * @param {jquery} form The form element.\n * @param {int} contextId The current context id.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n */\n var reloadQuestionPreview = function(form, contextId, topCategories) {\n var previewContainer = form.find(SELECTORS.PREVIEW_CONTAINER);\n RandomQuestionFormPreview.reload(\n previewContainer,\n getCategoryId(form),\n shouldIncludeSubcategories(form, topCategories),\n getTagIds(form),\n contextId\n );\n };\n\n /**\n * Is this an element we're interested in listening to changes on.\n *\n * @param {jquery} element The element to check.\n * @return {bool}\n */\n var isInterestingElement = function(element) {\n if (element.closest(SELECTORS.CATEGORY_FORM_ELEMENT).length > 0) {\n return true;\n }\n\n if (element.closest(SELECTORS.SUBCATEGORY_FORM_ELEMENT).length > 0) {\n return true;\n }\n\n if (element.closest(SELECTORS.TAG_IDS_FORM_ELEMENT).length > 0) {\n return true;\n }\n\n return false;\n };\n\n /**\n * Listen for changes to any of the interesting elements and reload the form\n * preview with the new filter values if they are changed.\n *\n * The reload is delayed for a small amount of time (see RELOAD_DELAY) in case\n * the user is actively editing the form. This allows us to avoid having to\n * send multiple requests to the server on each change.\n *\n * Instead we can just send a single request when the user appears to have\n * finished editing the form.\n *\n * @param {jquery} form The form element.\n * @param {int} contextId The current context id.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n */\n var addEventListeners = function(form, contextId, topCategories) {\n var reloadTimerId = null;\n\n form.on('change', function(e) {\n // Only reload the preview when elements that will change the result\n // are modified.\n if (!isInterestingElement($(e.target))) {\n return;\n }\n\n // Show the loading icon to let the user know that the preview\n // will be updated after their actions.\n RandomQuestionFormPreview.showLoadingIcon(form);\n\n if (reloadTimerId) {\n // Reset the timer each time the form is modified.\n clearTimeout(reloadTimerId);\n }\n\n // Don't immediately reload the question preview section just\n // in case the user is still modifying the form. We don't want to\n // spam reload requests.\n reloadTimerId = setTimeout(function() {\n reloadQuestionPreview(form, contextId, topCategories);\n }, RELOAD_DELAY);\n });\n };\n\n /**\n * Trigger the first load of the preview section and then listen for modifications\n * to the form to reload the preview with new filter values.\n *\n * @param {jquery} formId The form element id.\n * @param {int} contextId The current context id.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n * @param {bool} isTagsEnabled Whether tags feature is enabled or not.\n */\n var init = function(formId, contextId, topCategories, isTagsEnabled) {\n if (isTagsEnabled == true) {\n var form = $('#' + formId);\n reloadQuestionPreview(form, contextId, topCategories, isTagsEnabled);\n addEventListeners(form, contextId, topCategories, isTagsEnabled);\n }\n };\n\n return {\n init: init\n };\n});\n"],"names":["define","$","RandomQuestionFormPreview","SELECTORS","getCategorySelectValue","form","find","val","shouldIncludeSubcategories","topCategories","selectedValue","indexOf","isTopLevelCategorySelected","is","reloadQuestionPreview","contextId","previewContainer","reload","split","getCategoryId","map","value","getTagIds","addEventListeners","reloadTimerId","on","e","element","target","closest","length","showLoadingIcon","clearTimeout","setTimeout","init","formId","isTagsEnabled"],"mappings":";;;;;;;AAsBAA,kCACI,CACI,SACA,0CAEJ,SACIC,EACAC,+BAMAC,4BACmB,oDADnBA,gCAEuB,oBAFvBA,mCAG0B,gCAH1BA,+BAIsB,sBAStBC,uBAAyB,SAASC,aAC3BA,KAAKC,KAAKH,iCAAiCI,OAuClDC,2BAA6B,SAASH,KAAMI,uBAbf,SAASJ,KAAMI,mBACxCC,cAAgBN,uBAAuBC,aACnCI,cAAcE,QAAQD,gBAAkB,EAY5CE,CAA2BP,KAAMI,gBAG1BJ,KAAKC,KAAKH,oCAAoCU,GAAG,aA2B5DC,sBAAwB,SAAST,KAAMU,UAAWN,mBAC9CO,iBAAmBX,KAAKC,KAAKH,6BACjCD,0BAA0Be,OACtBD,iBAhEY,SAASX,aAGPD,uBAAuBC,MAEhBa,MAAM,KAEjB,GA0DVC,CAAcd,MACdG,2BAA2BH,KAAMI,eAtBzB,SAASJ,aACRA,KAAKC,KAAKH,gCAAgCI,MACzCa,KAAI,SAASC,cAGXA,MAAMH,MAAM,KACX,MAiBbI,CAAUjB,MACVU,YAyCJQ,kBAAoB,SAASlB,KAAMU,UAAWN,mBAC1Ce,cAAgB,KAEpBnB,KAAKoB,GAAG,UAAU,SAASC,GAlCJ,IAASC,UAAAA,QAqCF1B,EAAEyB,EAAEE,SApCtBC,QAAQ1B,iCAAiC2B,OAAS,GAI1DH,QAAQE,QAAQ1B,oCAAoC2B,OAAS,GAI7DH,QAAQE,QAAQ1B,gCAAgC2B,OAAS,KAkCzD5B,0BAA0B6B,gBAAgB1B,MAEtCmB,eAEAQ,aAAaR,eAMjBA,cAAgBS,YAAW,WACvBnB,sBAAsBT,KAAMU,UAAWN,iBA5JhC,gBAkLZ,CACHyB,KATO,SAASC,OAAQpB,UAAWN,cAAe2B,kBAC5B,GAAjBA,cAAuB,KACnB/B,KAAOJ,EAAE,IAAMkC,QACnBrB,sBAAsBT,KAAMU,UAAWN,eACvCc,kBAAkBlB,KAAMU,UAAWN"} \ No newline at end of file +{"version":3,"file":"add_random_form.min.js","sources":["../src/add_random_form.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * JavaScript for the add_random_form class.\n *\n * @module mod_quiz/add_random_form\n * @copyright 2018 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(\n [\n 'jquery',\n 'mod_quiz/random_question_form_preview'\n ],\n function(\n $,\n RandomQuestionFormPreview\n ) {\n\n // Wait 2 seconds before reloading the question set just in case\n // the user is still changing the criteria.\n var RELOAD_DELAY = 2000;\n var SELECTORS = {\n PREVIEW_CONTAINER: '[data-region=\"random-question-preview-container\"]',\n CATEGORY_FORM_ELEMENT: '[name=\"category\"]',\n SUBCATEGORY_FORM_ELEMENT: '[name=\"includesubcategories\"]',\n TAG_IDS_FORM_ELEMENT: '[name=\"fromtags[]\"]'\n };\n\n /**\n * Get the selected category value from the form.\n *\n * @param {jquery} form The form element.\n * @return {string} The category value.\n */\n var getCategorySelectValue = function(form) {\n return form.find(SELECTORS.CATEGORY_FORM_ELEMENT).val();\n };\n\n /**\n * Get the category id from the form.\n *\n * @param {jquery} form The form element.\n * @return {string} The category id.\n */\n var getCategoryId = function(form) {\n // The value string is the category id and category context id joined\n // by a comma.\n var valueString = getCategorySelectValue(form);\n // Split the two ids.\n var values = valueString.split(',');\n // Return just the category id.\n return values[0];\n };\n\n /**\n * Check if a top level category is selected in the form.\n *\n * @param {jquery} form The form element.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n * @return {bool}\n */\n var isTopLevelCategorySelected = function(form, topCategories) {\n var selectedValue = getCategorySelectValue(form);\n return (topCategories.indexOf(selectedValue) > -1);\n };\n\n /**\n * Check if the form indicates we should include include subcategories in\n * the filter.\n *\n * @param {jquery} form The form element.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n * @return {bool}\n */\n var shouldIncludeSubcategories = function(form, topCategories) {\n if (isTopLevelCategorySelected(form, topCategories)) {\n return true;\n } else {\n return form.find(SELECTORS.SUBCATEGORY_FORM_ELEMENT).is(':checked');\n }\n };\n\n /**\n * Get the tag ids for the selected tags in the form.\n *\n * @param {jquery} form The form element.\n * @return {string[]} The tag ids.\n */\n var getTagIds = function(form) {\n var values = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT).val();\n return values.map(function(value) {\n // The tag element value is the tag id and tag name joined\n // by a comma. So we need to split them to get the tag id.\n var parts = value.split(',');\n return parts[0];\n });\n };\n\n /**\n * Reload the preview section with a new set of filters.\n *\n * @param {jquery} form The form element.\n * @param {int} contextId The current context id.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n */\n var reloadQuestionPreview = function(form, contextId, topCategories) {\n var previewContainer = form.find(SELECTORS.PREVIEW_CONTAINER);\n RandomQuestionFormPreview.reload(\n previewContainer,\n getCategoryId(form),\n shouldIncludeSubcategories(form, topCategories),\n getTagIds(form),\n contextId\n );\n };\n\n /**\n * Is this an element we're interested in listening to changes on.\n *\n * @param {jquery} element The element to check.\n * @return {bool}\n */\n var isInterestingElement = function(element) {\n if (element.closest(SELECTORS.CATEGORY_FORM_ELEMENT).length > 0) {\n return true;\n }\n\n if (element.closest(SELECTORS.SUBCATEGORY_FORM_ELEMENT).length > 0) {\n return true;\n }\n\n if (element.closest(SELECTORS.TAG_IDS_FORM_ELEMENT).length > 0) {\n return true;\n }\n\n return false;\n };\n\n /**\n * Listen for changes to any of the interesting elements and reload the form\n * preview with the new filter values if they are changed.\n *\n * The reload is delayed for a small amount of time (see RELOAD_DELAY) in case\n * the user is actively editing the form. This allows us to avoid having to\n * send multiple requests to the server on each change.\n *\n * Instead we can just send a single request when the user appears to have\n * finished editing the form.\n *\n * @param {jquery} form The form element.\n * @param {int} contextId The current context id.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n */\n var addEventListeners = function(form, contextId, topCategories) {\n var reloadTimerId = null;\n const tagsFilter = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT);\n\n form.add(tagsFilter).on('change', function(e) {\n // Only reload the preview when elements that will change the result\n // are modified.\n if (!isInterestingElement($(e.target))) {\n return;\n }\n\n // Show the loading icon to let the user know that the preview\n // will be updated after their actions.\n RandomQuestionFormPreview.showLoadingIcon(form);\n\n if (reloadTimerId) {\n // Reset the timer each time the form is modified.\n clearTimeout(reloadTimerId);\n }\n\n // Don't immediately reload the question preview section just\n // in case the user is still modifying the form. We don't want to\n // spam reload requests.\n reloadTimerId = setTimeout(function() {\n reloadQuestionPreview(form, contextId, topCategories);\n }, RELOAD_DELAY);\n });\n };\n\n /**\n * Trigger the first load of the preview section and then listen for modifications\n * to the form to reload the preview with new filter values.\n *\n * @param {jquery} formId The form element id.\n * @param {int} contextId The current context id.\n * @param {string[]} topCategories List of top category values (matching the select box values)\n * @param {bool} isTagsEnabled Whether tags feature is enabled or not.\n */\n var init = function(formId, contextId, topCategories, isTagsEnabled) {\n if (isTagsEnabled == true) {\n var form = $('#' + formId);\n reloadQuestionPreview(form, contextId, topCategories, isTagsEnabled);\n addEventListeners(form, contextId, topCategories, isTagsEnabled);\n }\n };\n\n return {\n init: init\n };\n});\n"],"names":["define","$","RandomQuestionFormPreview","SELECTORS","getCategorySelectValue","form","find","val","shouldIncludeSubcategories","topCategories","selectedValue","indexOf","isTopLevelCategorySelected","is","reloadQuestionPreview","contextId","previewContainer","reload","split","getCategoryId","map","value","getTagIds","addEventListeners","reloadTimerId","tagsFilter","add","on","e","element","target","closest","length","showLoadingIcon","clearTimeout","setTimeout","init","formId","isTagsEnabled"],"mappings":";;;;;;;AAsBAA,kCACI,CACI,SACA,0CAEJ,SACIC,EACAC,+BAMAC,4BACmB,oDADnBA,gCAEuB,oBAFvBA,mCAG0B,gCAH1BA,+BAIsB,sBAStBC,uBAAyB,SAASC,aAC3BA,KAAKC,KAAKH,iCAAiCI,OAuClDC,2BAA6B,SAASH,KAAMI,uBAbf,SAASJ,KAAMI,mBACxCC,cAAgBN,uBAAuBC,aACnCI,cAAcE,QAAQD,gBAAkB,EAY5CE,CAA2BP,KAAMI,gBAG1BJ,KAAKC,KAAKH,oCAAoCU,GAAG,aA2B5DC,sBAAwB,SAAST,KAAMU,UAAWN,mBAC9CO,iBAAmBX,KAAKC,KAAKH,6BACjCD,0BAA0Be,OACtBD,iBAhEY,SAASX,aAGPD,uBAAuBC,MAEhBa,MAAM,KAEjB,GA0DVC,CAAcd,MACdG,2BAA2BH,KAAMI,eAtBzB,SAASJ,aACRA,KAAKC,KAAKH,gCAAgCI,MACzCa,KAAI,SAASC,cAGXA,MAAMH,MAAM,KACX,MAiBbI,CAAUjB,MACVU,YAyCJQ,kBAAoB,SAASlB,KAAMU,UAAWN,mBAC1Ce,cAAgB,KACdC,WAAapB,KAAKC,KAAKH,gCAE7BE,KAAKqB,IAAID,YAAYE,GAAG,UAAU,SAASC,GAnCpB,IAASC,UAAAA,QAsCF5B,EAAE2B,EAAEE,SArCtBC,QAAQ5B,iCAAiC6B,OAAS,GAI1DH,QAAQE,QAAQ5B,oCAAoC6B,OAAS,GAI7DH,QAAQE,QAAQ5B,gCAAgC6B,OAAS,KAmCzD9B,0BAA0B+B,gBAAgB5B,MAEtCmB,eAEAU,aAAaV,eAMjBA,cAAgBW,YAAW,WACvBrB,sBAAsBT,KAAMU,UAAWN,iBA7JhC,gBAmLZ,CACH2B,KATO,SAASC,OAAQtB,UAAWN,cAAe6B,kBAC5B,GAAjBA,cAAuB,KACnBjC,KAAOJ,EAAE,IAAMoC,QACnBvB,sBAAsBT,KAAMU,UAAWN,eACvCc,kBAAkBlB,KAAMU,UAAWN"} \ No newline at end of file diff --git a/mod/quiz/amd/src/add_random_form.js b/mod/quiz/amd/src/add_random_form.js index 8983a21f7a2..00548406ac7 100644 --- a/mod/quiz/amd/src/add_random_form.js +++ b/mod/quiz/amd/src/add_random_form.js @@ -167,8 +167,9 @@ define( */ var addEventListeners = function(form, contextId, topCategories) { var reloadTimerId = null; + const tagsFilter = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT); - form.on('change', function(e) { + form.add(tagsFilter).on('change', function(e) { // Only reload the preview when elements that will change the result // are modified. if (!isInterestingElement($(e.target))) { diff --git a/mod/quiz/tests/behat/editing_add_random.feature b/mod/quiz/tests/behat/editing_add_random.feature index 1664cf02cbd..f28cac5303f 100644 --- a/mod/quiz/tests/behat/editing_add_random.feature +++ b/mod/quiz/tests/behat/editing_add_random.feature @@ -21,14 +21,21 @@ Feature: Adding random questions to a quiz based on category and tags | contextlevel | reference | name | | Course | C1 | Questions Category 1 | | Course | C1 | Questions Category 2 | + And the following "question categories" exist: + | contextlevel | reference | name | questioncategory | + | Course | C1 | Subcategory | Questions Category 1 | And the following "questions" exist: | questioncategory | qtype | name | user | questiontext | | Questions Category 1 | essay | question 1 name | admin | Question 1 text | | Questions Category 1 | essay | question 2 name | teacher1 | Question 2 text | + | Subcategory | essay | question 3 name | teacher1 | Question 3 text | + | Subcategory | essay | question 4 name | teacher1 | Question 4 text | And the following "core_question > Tags" exist: | question | tag | | question 1 name | foo | | question 2 name | bar | + | question 3 name | foo | + | question 4 name | bar | Scenario: Available tags are shown in the autocomplete tag field Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1" @@ -38,6 +45,31 @@ Feature: Adding random questions to a quiz based on category and tags Then "foo" "autocomplete_suggestions" should exist And "bar" "autocomplete_suggestions" should exist + Scenario: Questions can be filtered by tags + Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1" + When I open the "last" add to quiz menu + And I follow "a random question" + And I set the field "Category" to "Top for Course 1" + And I wait until the page is ready + And I open the autocomplete suggestions list + And I click on "foo" item in the autocomplete list + Then I should see "question 1 name" + And I should see "question 3 name" + And I should not see "question 2 name" + And I should not see "question 4 name" + And I set the field "Category" to "Questions Category 1" + And I wait until the page is ready + And I should see "question 1 name" + And I should not see "question 3 name" + And I should not see "question 2 name" + And I should not see "question 4 name" + And I click on "Include questions from subcategories too" "checkbox" + And I wait until the page is ready + And I should see "question 1 name" + And I should see "question 3 name" + And I should not see "question 2 name" + And I should not see "question 4 name" + Scenario: A random question can be added to the quiz Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1" When I open the "last" add to quiz menu