diff --git a/mod/quiz/accessrule/seb/backup/moodle2/restore_quizaccess_seb_subplugin.class.php b/mod/quiz/accessrule/seb/backup/moodle2/restore_quizaccess_seb_subplugin.class.php index 1cbdd677f0089..3ea967b2ea599 100644 --- a/mod/quiz/accessrule/seb/backup/moodle2/restore_quizaccess_seb_subplugin.class.php +++ b/mod/quiz/accessrule/seb/backup/moodle2/restore_quizaccess_seb_subplugin.class.php @@ -24,7 +24,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -use quizaccess_seb\seb_quiz_settings; +use quizaccess_seb\{seb_quiz_settings, settings_provider, template}; defined('MOODLE_INTERNAL') || die(); @@ -73,7 +73,15 @@ public function process_quizaccess_seb_quizsettings($data) { unset($data->id); $data->timecreated = $data->timemodified = time(); $data->usermodified = $USER->id; - $DB->insert_record(quizaccess_seb\seb_quiz_settings::TABLE, $data); + + // Do not use template if it is no longer enabled. + if ($data->requiresafeexambrowser == settings_provider::USE_SEB_TEMPLATE && + !$DB->record_exists(template::TABLE, ['id' => $data->templateid, 'enabled' => '1'])) { + $data->templateid = 0; + $data->requiresafeexambrowser = settings_provider::USE_SEB_NO; + } + + $DB->insert_record(seb_quiz_settings::TABLE, $data); // Process attached files. $this->add_related_files('quizaccess_seb', 'filemanager_sebconfigfile', null); @@ -112,7 +120,10 @@ public function process_quizaccess_seb_template($data) { } // Update the restored quiz settings to use restored template. - $DB->set_field(\quizaccess_seb\seb_quiz_settings::TABLE, 'templateid', $template->get('id'), ['quizid' => $quizid]); + // Check if template is enabled before using it. + if ($template->get('enabled')) { + $DB->set_field(seb_quiz_settings::TABLE, 'templateid', $template->get('id'), ['quizid' => $quizid]); + } } } diff --git a/mod/quiz/accessrule/seb/classes/local/form/template.php b/mod/quiz/accessrule/seb/classes/local/form/template.php index f1a6c8e11d291..6caa890a53917 100644 --- a/mod/quiz/accessrule/seb/classes/local/form/template.php +++ b/mod/quiz/accessrule/seb/classes/local/form/template.php @@ -62,12 +62,11 @@ protected function definition() { $mform->addElement('selectyesno', 'enabled', get_string('enabled', 'quizaccess_seb')); $mform->setType('enabled', PARAM_INT); - $this->add_action_buttons(); - - if (!empty($this->get_persistent()) && !$this->get_persistent()->can_delete()) { - $mform->hardFreezeAllVisibleExcept([]); - $mform->addElement('cancel'); + if ($this->get_persistent()->get('id')) { + $mform->hardFreezeAllVisibleExcept(['enabled']); } + + $this->add_action_buttons(); } /** diff --git a/mod/quiz/accessrule/seb/classes/settings_provider.php b/mod/quiz/accessrule/seb/classes/settings_provider.php index f7a8ffdaa10f4..7d5c1a2229e81 100644 --- a/mod/quiz/accessrule/seb/classes/settings_provider.php +++ b/mod/quiz/accessrule/seb/classes/settings_provider.php @@ -585,11 +585,20 @@ public static function get_requiresafeexambrowser_options(\context $context): ar * @return array */ protected static function get_template_options(): array { + global $DB, $PAGE; $templates = []; - $records = template::get_records(['enabled' => 1], 'name'); + $templatetable = template::TABLE; + $sebquizsettingstable = seb_quiz_settings::TABLE; + $cmid = $PAGE->cm->id ?? -1; + $sql = "SELECT * + FROM {{$templatetable}} t + WHERE enabled = 1 + OR EXISTS (SELECT 1 FROM {{$sebquizsettingstable}} WHERE templateid = t.id AND cmid = ?)"; + + $records = $DB->get_records_sql($sql, [$cmid]); if ($records) { foreach ($records as $record) { - $templates[$record->get('id')] = $record->get('name'); + $templates[$record->id] = $record->name; } }