diff --git a/mod/qbank/lang/en/qbank.php b/mod/qbank/lang/en/qbank.php index 4b8138d980f..8fd24af0450 100644 --- a/mod/qbank/lang/en/qbank.php +++ b/mod/qbank/lang/en/qbank.php @@ -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'; diff --git a/mod/qbank/lib.php b/mod/qbank/lib.php index 4e796e8bd99..09e2b58b6f3 100644 --- a/mod/qbank/lib.php +++ b/mod/qbank/lib.php @@ -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(); +} diff --git a/mod/qbank/tests/generator/lib.php b/mod/qbank/tests/generator/lib.php index 5857eb34925..afaa2eaba10 100644 --- a/mod/qbank/tests/generator/lib.php +++ b/mod/qbank/tests/generator/lib.php @@ -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); } }