Skip to content

Commit

Permalink
MDL-76665 quizaccess: Allow disable of SEB templates when in use.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kotlyar committed Aug 20, 2024
1 parent 6cd5507 commit 8f6b463
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/

use quizaccess_seb\seb_quiz_settings;
use quizaccess_seb\settings_provider;
use quizaccess_seb\template;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -73,7 +75,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);
Expand Down Expand Up @@ -112,7 +122,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]);
}
}

}
Expand Down
9 changes: 4 additions & 5 deletions mod/quiz/accessrule/seb/classes/local/form/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
13 changes: 11 additions & 2 deletions mod/quiz/accessrule/seb/classes/settings_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 8f6b463

Please sign in to comment.