From 2a912052bc577504cd8338c31b2844387d831c06 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 | 40 +++++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lang/en/question.php b/lang/en/question.php index 7dc290e5ef7..8663ab9ebeb 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -283,7 +283,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 756ede77340..0cbe9b4978f 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -119,34 +119,32 @@ 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) { + if (isset($callbacksbytype['mod'][substr($plugin, 4)])) { + continue; // Already done. + } - // 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; - } - } - } + foreach ($questionids as $questionid) { + if (!empty($function($questionid))) { + return true; } } }