MDL-85069 qbank: Add tool_generator callback

This adds a callback for tool_generator and the required lang strings to
allow qbanks to be generated as part of test courses, by passing the
--additionalmodules=qbank argument to maketestcourse.php.
This commit is contained in:
Mark Johnson
2025-06-30 15:11:07 +01:00
parent aa4a364d1d
commit 5224fa5c60
3 changed files with 45 additions and 0 deletions
+2
View File
@@ -26,6 +26,7 @@
$string['addanotherbank'] = 'Add question bank';
$string['coursecategory'] = 'Shared teaching resources for category: {$a}';
$string['courserestore'] = 'Course restore';
$string['createqbank'] = 'Create question bank {$a}';
$string['modulename'] = 'Question bank';
$string['modulename_help'] = 'This activity allows a teacher to create, preview, and edit questions in a database of question categories.
@@ -37,6 +38,7 @@ $string['noqbankinstances'] = 'There are no question banks in this course.';
$string['pluginadministration'] = 'Question bank administration';
$string['pluginname'] = 'Question bank';
$string['privacy:metadata'] = 'The Question bank plugin does not store any personal data, core_question automatically tracks all sorts of data for questions.';
$string['progress_createqbank'] = 'Creating question banks';
$string['qbank:addinstance'] = 'Add a new question bank';
$string['qbank:view'] = 'View question bank';
$string['qbankname'] = 'Question bank name';
+37
View File
@@ -107,3 +107,40 @@ function qbank_delete_instance(int $id): bool {
return true;
}
/**
* Callback for tool_generator so we can add qbanks to generated courses.
*
* @param tool_generator_course_backend $backend
* @param testing_data_generator $generator
* @param int $courseid
* @param int $number
* @return void
* @throws coding_exception
*/
function qbank_course_backend_generator_create_activity(
tool_generator_course_backend $backend,
testing_data_generator $generator,
int $courseid,
int $number,
) {
// Set up generator.
$generator = $generator->get_plugin_generator('mod_qbank');
// Create assignments.
$backend->log('createqbank', $number, true, 'mod_qbank');
for ($i = 1; $i <= $number; $i++) {
$qbank = $generator->create_instance(
[
'course' => $courseid,
'name' => "Question bank course {$courseid} bank {$i}",
],
[
'section' => 0,
],
);
question_get_default_category(\core\context\module::instance($qbank->cmid)->id, true);
$backend->dot($i, $number);
}
$backend->end_log();
}
+6
View File
@@ -32,6 +32,12 @@ class mod_qbank_generator extends testing_module_generator {
if (empty($record->type)) {
$record->type = core_question\local\bank\question_bank_helper::TYPE_STANDARD;
}
if (isset($options['section']) && (int) $options['section'] !== 0) {
throw new \core\exception\coding_exception(
"Attempted to create a mod_qbank instance in section {$options['section']}. " .
"Question banks can only be created in section 0.",
);
}
return parent::create_instance($record, $options);
}
}