MDL-84247 questions: rename is_valid_question -> is_qtype_usable

and move it next to is_qtype_installed. This makes the API of the
question_bank class more consistent
This commit is contained in:
Tim Hunt
2025-03-17 17:12:50 +00:00
parent 77777a0726
commit ece18d3d22
5 changed files with 21 additions and 21 deletions
+1 -1
View File
@@ -924,7 +924,7 @@ class edit_renderer extends \plugin_renderer_base {
public function question_preview_icon($quiz, $questiondata, $label = null, $variant = null, $restartversion = null) {
$question = clone($questiondata);
if (!\question_bank::is_question_valid($question)) {
if (!\question_bank::is_qtype_usable($question->qtype)) {
return '';
}
@@ -52,7 +52,7 @@ class preview_action_column extends \core_question\local\bank\column_base {
if (!question_has_capability_on($question, 'use')) {
return;
}
if (!\question_bank::is_question_valid($question)) {
if (!\question_bank::is_qtype_usable($question->qtype)) {
return;
}
$editrenderer = $PAGE->get_renderer('quiz', 'edit');
@@ -63,7 +63,7 @@ class question_name_idnumber_tags_column extends viewquestionname_column_helper
echo \html_writer::end_tag('div');
// If the question is invalid, show a warning badge.
if (!\question_bank::is_question_valid($question)) {
if (!\question_bank::is_qtype_usable($question->qtype)) {
echo \html_writer::span(get_string('invalidquestiontype', 'question', $question->qtype),
'badge bg-danger text-white');
}
+1 -1
View File
@@ -1532,7 +1532,7 @@ class view {
$attributes = [];
// If the question type is invalid we highlight it red.
if (!\question_bank::is_question_valid($question)) {
if (!\question_bank::is_qtype_usable($question->qtype)) {
$rowclasses .= ' table-danger';
}
if ($rowclasses) {
+17 -17
View File
@@ -83,6 +83,23 @@ abstract class question_bank {
return $plugindir && is_readable($plugindir . '/questiontype.php');
}
/**
* Check if a given question type is one that is installed and usable.
*
* Use this before doing things like rendering buttons/options which will only work for
* installed question types.
*
* When loaded through most of the core_question areas, qtype will still be the uninstalled type, e.g. 'mytype',
* but when we get to the quiz pages, it will have been converted to 'missingtype'. So we need to check that
* as well here.
*
* @param string $qtypename e.g. 'multichoice'.
* @return bool
*/
public static function is_qtype_usable(string $qtypename): bool {
return self::is_qtype_installed($qtypename) && $qtypename !== 'missingtype';
}
/**
* Get the question type class for a particular question type.
* @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.
@@ -506,23 +523,6 @@ abstract class question_bank {
$qtypes = $DB->get_fieldset_sql($sql, $params);
return $qtypes;
}
/**
* Check if a given question is valid.
* This is used in places where we want to do things like render buttons/options which will only work for
* valid, installed question types.
*
* When loaded through most of the core_question areas, qtype will still be the uninstalled type, e.g. 'mytype',
* but when we get to the quiz pages, it will have been converted to 'missingtype'. So we need to check that
* as well here.
*
* @param stdClass $questiondata
* @return bool
*/
public static function is_question_valid(stdClass $questiondata): bool {
return (self::is_qtype_installed($questiondata->qtype) && $questiondata->qtype !== 'missingtype');
}
}