From 887daf932cac2554982f4dc9bcb49db2da9ded5a Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 13 Feb 2020 16:09:33 +0000 Subject: [PATCH] MDL-67947 questions: questions_in_use should ask all components Previously it was only checking mods. --- lang/en/question.php | 2 +- lib/questionlib.php | 43 ++++++++++++++++++++++--------------------- question/upgrade.txt | 9 +++++++++ 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/lang/en/question.php b/lang/en/question.php index 94cc9df9879..47788cb4afc 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -287,7 +287,7 @@ $string['questiondoesnotexist'] = 'This question does not exist'; $string['questionname'] = 'Question name'; $string['questionno'] = 'Question {$a}'; $string['questionsaveerror'] = 'Errors occur during saving question - ({$a})'; -$string['questionsinuse'] = '(* Questions marked by an asterisk are already in use in some quizzes. These questions will not be deleted from these quizzes but only from the category list.)'; +$string['questionsinuse'] = '(* Questions marked with an asterisk are used somewhere, for example in a quiz. Therefore, if you proceed, these questions will not really be deleted, they will just be hidden.)'; $string['questionsmovedto'] = 'Questions still in use moved to "{$a}" in the parent course category.'; $string['questionsrescuedfrom'] = 'Questions saved from context {$a}.'; $string['questionsrescuedfrominfo'] = 'These questions (some of which may be hidden) were saved when context {$a} was deleted because they are still used by some quizzes or other activities.'; diff --git a/lib/questionlib.php b/lib/questionlib.php index 8f17df03927..c84905240ce 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -119,34 +119,35 @@ function question_save_qtype_order($neworder, $config = null) { * @return boolean whether any of these questions are being used by any part of Moodle. */ function questions_in_use($questionids) { - global $CFG; + // Are they used by the core question system? if (question_engine::questions_in_use($questionids)) { return true; } - foreach (core_component::get_plugin_list('mod') as $module => $path) { - $lib = $path . '/lib.php'; - if (is_readable($lib)) { - include_once($lib); + // Check if any plugins are using these questions. + $callbacksbytype = get_plugins_with_function('questions_in_use'); + foreach ($callbacksbytype as $callbacks) { + foreach ($callbacks as $function) { + if ($function($questionids)) { + return true; + } + } + } - $fn = $module . '_questions_in_use'; - if (function_exists($fn)) { - if ($fn($questionids)) { - return true; - } - } else { + // Finally check legacy callback. + $legacycallbacks = get_plugin_list_with_function('mod', 'question_list_instances'); + foreach ($legacycallbacks as $plugin => $function) { + debugging($plugin . ' implements deprecated method ' . $function . + '. ' . $plugin . '_questions_in_use should be implemented instead.', DEBUG_DEVELOPER); - // Fallback for legacy modules. - $fn = $module . '_question_list_instances'; - if (function_exists($fn)) { - foreach ($questionids as $questionid) { - $instances = $fn($questionid); - if (!empty($instances)) { - return true; - } - } - } + if (isset($callbacksbytype['mod'][substr($plugin, 4)])) { + continue; // Already done. + } + + foreach ($questionids as $questionid) { + if (!empty($function($questionid))) { + return true; } } } diff --git a/question/upgrade.txt b/question/upgrade.txt index 6f0475d91b3..00251c72a1f 100644 --- a/question/upgrade.txt +++ b/question/upgrade.txt @@ -1,5 +1,14 @@ This files describes API changes for code that uses the question API. +=== 3.9 == + +For years, the ..._questions_in_use callback has been the right way for plugins to +tell the core question system if questions are required. Previously this callback +only worked in mods. Now it works in all plugins. + +At the same time, if you are still relying on the legacy ..._question_list_instances +callback for this, you will now get a debugging warning telling you to upgrade. + === 3.8 === If you have customised the display of the question bank (using $CFG->questionbankcolumns)