From d57c35472ad8e7d92f374f1faea66cb7776f60e4 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 6 Mar 2019 10:25:28 +0800 Subject: [PATCH 1/2] MDL-62992 question: conditionaly enable question tags feature --- question/classes/bank/view.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/question/classes/bank/view.php b/question/classes/bank/view.php index ec5b81f5c99..5bf2367c6a7 100644 --- a/question/classes/bank/view.php +++ b/question/classes/bank/view.php @@ -462,7 +462,7 @@ class view { */ public function display($tabname, $page, $perpage, $cat, $recurse, $showhidden, $showquestiontext, $tagids = []) { - global $PAGE; + global $PAGE, $CFG; if ($this->process_actions_needing_ui()) { return; @@ -473,7 +473,14 @@ class view { $thiscontext = $this->get_most_specific_context(); // Category selection form. $this->display_question_bank_header(); - array_unshift($this->searchconditions, new \core_question\bank\search\tag_condition([$catcontext, $thiscontext], $tagids)); + + // Display tag filter if usetags setting is enabled. + if ($CFG->usetags) { + array_unshift($this->searchconditions, + new \core_question\bank\search\tag_condition([$catcontext, $thiscontext], $tagids)); + $PAGE->requires->js_call_amd('core_question/edit_tags', 'init', ['#questionscontainer']); + } + array_unshift($this->searchconditions, new \core_question\bank\search\hidden_condition(!$showhidden)); array_unshift($this->searchconditions, new \core_question\bank\search\category_condition( $cat, $recurse, $editcontexts, $this->baseurl, $this->course)); @@ -485,7 +492,6 @@ class view { null, $page, $perpage, $showhidden, $showquestiontext, $this->contexts->having_cap('moodle/question:add')); - $PAGE->requires->js_call_amd('core_question/edit_tags', 'init', ['#questionscontainer']); } protected function print_choose_category_message($categoryandcontext) { From 98ab2d0a449398ee285fc984797792eccc9b61c6 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 6 Mar 2019 10:30:36 +0800 Subject: [PATCH 2/2] MDL-62992 quiz: fix tags filtering on random question modal --- mod/quiz/addrandomform.php | 27 ++++++++++--------- mod/quiz/amd/build/add_random_form.min.js | 2 +- mod/quiz/amd/src/add_random_form.js | 12 +++++---- .../random_question_form_preview.mustache | 2 +- .../random_question_form_preview.mustache | 2 +- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/mod/quiz/addrandomform.php b/mod/quiz/addrandomform.php index 6121d874662..a6b9b2804a4 100644 --- a/mod/quiz/addrandomform.php +++ b/mod/quiz/addrandomform.php @@ -37,7 +37,7 @@ require_once($CFG->libdir.'/formslib.php'); class quiz_add_random_form extends moodleform { protected function definition() { - global $OUTPUT, $PAGE; + global $OUTPUT, $PAGE, $CFG; $mform =& $this->_form; $mform->setDisableShortforms(); @@ -58,17 +58,19 @@ class quiz_add_random_form extends moodleform { $tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id')); $mform->hideIf('includesubcategories', 'category', 'in', $tops); - $tags = core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts); - $tagstrings = array(); - foreach ($tags as $tag) { - $tagstrings["{$tag->id},{$tag->name}"] = $tag->name; + if ($CFG->usetags) { + $tagstrings = array(); + $tags = core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts); + foreach ($tags as $tag) { + $tagstrings["{$tag->id},{$tag->name}"] = $tag->name; + } + $options = array( + 'multiple' => true, + 'noselectionstring' => get_string('anytags', 'quiz'), + ); + $mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options); + $mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz'); } - $options = array( - 'multiple' => true, - 'noselectionstring' => get_string('anytags', 'quiz'), - ); - $mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options); - $mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz'); $mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'), $this->get_number_of_questions_to_add_choices()); @@ -107,7 +109,8 @@ class quiz_add_random_form extends moodleform { $PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [ $mform->getAttribute('id'), $contexts->lowest()->id, - $tops + $tops, + $CFG->usetags ]); } diff --git a/mod/quiz/amd/build/add_random_form.min.js b/mod/quiz/amd/build/add_random_form.min.js index 977a623131c..5e9947b4541 100644 --- a/mod/quiz/amd/build/add_random_form.min.js +++ b/mod/quiz/amd/build/add_random_form.min.js @@ -1 +1 @@ -define(["jquery","mod_quiz/random_question_form_preview"],function(a,b){var c=2e3,d={PREVIEW_CONTAINER:'[data-region="random-question-preview-container"]',CATEGORY_FORM_ELEMENT:'[name="category"]',SUBCATEGORY_FORM_ELEMENT:'[name="includesubcategories"]',TAG_IDS_FORM_ELEMENT:'[name="fromtags[]"]'},e=function(a){return a.find(d.CATEGORY_FORM_ELEMENT).val()},f=function(a){var b=e(a),c=b.split(",");return c[0]},g=function(a,b){var c=e(a);return b.indexOf(c)>-1},h=function(a,b){return!!g(a,b)||a.find(d.SUBCATEGORY_FORM_ELEMENT).is(":checked")},i=function(a){var b=a.find(d.TAG_IDS_FORM_ELEMENT).val();return b.map(function(a){var b=a.split(",");return b[0]})},j=function(a,c,e){var g=a.find(d.PREVIEW_CONTAINER);b.reload(g,f(a),h(a,e),i(a),c)},k=function(a){return a.closest(d.CATEGORY_FORM_ELEMENT).length>0||(a.closest(d.SUBCATEGORY_FORM_ELEMENT).length>0||a.closest(d.TAG_IDS_FORM_ELEMENT).length>0)},l=function(d,e,f){var g=null;d.on("change",function(h){k(a(h.target))&&(b.showLoadingIcon(d),g&&clearTimeout(g),g=setTimeout(function(){j(d,e,f)},c))})},m=function(b,c,d){var e=a("#"+b);j(e,c,d),l(e,c,d)};return{init:m}}); \ No newline at end of file +define(["jquery","mod_quiz/random_question_form_preview"],function(a,b){var c=2e3,d={PREVIEW_CONTAINER:'[data-region="random-question-preview-container"]',CATEGORY_FORM_ELEMENT:'[name="category"]',SUBCATEGORY_FORM_ELEMENT:'[name="includesubcategories"]',TAG_IDS_FORM_ELEMENT:'[name="fromtags[]"]'},e=function(a){return a.find(d.CATEGORY_FORM_ELEMENT).val()},f=function(a){var b=e(a),c=b.split(",");return c[0]},g=function(a,b){var c=e(a);return b.indexOf(c)>-1},h=function(a,b){return!!g(a,b)||a.find(d.SUBCATEGORY_FORM_ELEMENT).is(":checked")},i=function(a){var b=a.find(d.TAG_IDS_FORM_ELEMENT).val();return b.map(function(a){var b=a.split(",");return b[0]})},j=function(a,c,e){var g=a.find(d.PREVIEW_CONTAINER);b.reload(g,f(a),h(a,e),i(a),c)},k=function(a){return a.closest(d.CATEGORY_FORM_ELEMENT).length>0||(a.closest(d.SUBCATEGORY_FORM_ELEMENT).length>0||a.closest(d.TAG_IDS_FORM_ELEMENT).length>0)},l=function(d,e,f){var g=null;d.on("change",function(h){k(a(h.target))&&(b.showLoadingIcon(d),g&&clearTimeout(g),g=setTimeout(function(){j(d,e,f)},c))})},m=function(b,c,d,e){if(1==e){var f=a("#"+b);j(f,c,d,e),l(f,c,d,e)}};return{init:m}}); \ 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 35ebd32d0f7..f9de7d512c0 100644 --- a/mod/quiz/amd/src/add_random_form.js +++ b/mod/quiz/amd/src/add_random_form.js @@ -201,12 +201,14 @@ define( * @param {jquery} formId The form element id. * @param {int} contextId The current context id. * @param {string[]} topCategories List of top category values (matching the select box values) + * @param {bool} isTagsEnabled Whether tags feature is enabled or not. */ - var init = function(formId, contextId, topCategories) { - var form = $('#' + formId); - - reloadQuestionPreview(form, contextId, topCategories); - addEventListeners(form, contextId, topCategories); + var init = function(formId, contextId, topCategories, isTagsEnabled) { + if (isTagsEnabled == true) { + var form = $('#' + formId); + reloadQuestionPreview(form, contextId, topCategories, isTagsEnabled); + addEventListeners(form, contextId, topCategories, isTagsEnabled); + } }; return { diff --git a/mod/quiz/templates/random_question_form_preview.mustache b/mod/quiz/templates/random_question_form_preview.mustache index 1be47e2b589..dbd7e4fc88e 100644 --- a/mod/quiz/templates/random_question_form_preview.mustache +++ b/mod/quiz/templates/random_question_form_preview.mustache @@ -36,5 +36,5 @@
- {{< core/overlay_loading }}{{$hiddenclass}}{{/hiddenclass}}{{/ core/overlay_loading }} + {{> core/overlay_loading}}
diff --git a/theme/bootstrapbase/templates/mod_quiz/random_question_form_preview.mustache b/theme/bootstrapbase/templates/mod_quiz/random_question_form_preview.mustache index b1e4e344dd3..441ce5ef789 100644 --- a/theme/bootstrapbase/templates/mod_quiz/random_question_form_preview.mustache +++ b/theme/bootstrapbase/templates/mod_quiz/random_question_form_preview.mustache @@ -36,5 +36,5 @@
- {{< core/overlay_loading }}{{$hiddenclass}}{{/hiddenclass}}{{/ core/overlay_loading }} + {{> core/overlay_loading}}