From 6ee51ae39ff634116ba9bee774758e0b05376ac4 Mon Sep 17 00:00:00 2001 From: Sumaiya Javed Date: Wed, 1 Nov 2023 16:29:12 +1300 Subject: [PATCH] Add default value at the global level/plugin's settings page. --- rule.php | 24 ++++++++++++------------ settings.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 settings.php diff --git a/rule.php b/rule.php index 857406a..9232a53 100644 --- a/rule.php +++ b/rule.php @@ -82,25 +82,25 @@ public static function add_settings_form_fields( mod_quiz_mod_form $quizform, MoodleQuickForm $mform) { $mform->addElement('select', 'honestycheckrequired', get_string('honestycheckrequired', 'quizaccess_honestycheck'), - array( + [ 0 => get_string('notrequired', 'quizaccess_honestycheck'), 1 => get_string('honestycheckrequiredoption', 'quizaccess_honestycheck'), - )); - $mform->addHelpButton('honestycheckrequired', - 'honestycheckrequired', 'quizaccess_honestycheck'); + ]); + $mform->setDefault('honestycheckrequired', get_config('quizaccess_honestycheck', 'honestycheckrequired')); + $mform->addHelpButton('honestycheckrequired', 'honestycheckrequired', 'quizaccess_honestycheck'); } public static function save_settings($quiz) { global $DB; - if (empty($quiz->honestycheckrequired)) { - $DB->delete_records('quizaccess_honestycheck', array('quizid' => $quiz->id)); + $record = [ + "quizid" => $quiz->id, + "honestycheckrequired" => (empty($quiz->honestycheckrequired)) ? 0 : 1, + ]; + if ($DB->record_exists('quizaccess_honestycheck', array('quizid' => $quiz->id))) { + $sql = 'UPDATE {quizaccess_honestycheck} SET honestycheckrequired = :honestycheckrequired WHERE quizid = :quizid'; + $DB->execute($sql, $record); } else { - if (!$DB->record_exists('quizaccess_honestycheck', array('quizid' => $quiz->id))) { - $record = new stdClass(); - $record->quizid = $quiz->id; - $record->honestycheckrequired = 1; - $DB->insert_record('quizaccess_honestycheck', $record); - } + $DB->insert_record('quizaccess_honestycheck', $record); } } diff --git a/settings.php b/settings.php new file mode 100644 index 0000000..f197e71 --- /dev/null +++ b/settings.php @@ -0,0 +1,36 @@ +. + +/** + * Global configuration settings for the quizaccess_honestycheck plugin. + * + * @package quizaccess_honestycheck + * @author Sumaiya Javed + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +$options = [ + 0 => new lang_string('notrequired', 'quizaccess_honestycheck'), + 1 => new lang_string('honestycheckrequiredoption', 'quizaccess_honestycheck'), +]; +$setting = new admin_setting_configselect('quizaccess_honestycheck/honestycheckrequired', + new lang_string('honestycheckrequired', 'quizaccess_honestycheck'), new lang_string('honestycheckrequired_help', 'quizaccess_honestycheck'), + 0, $options); +$setting->set_advanced_flag_options(admin_setting_flag::ENABLED, true); +$setting->set_locked_flag_options(admin_setting_flag::ENABLED, false); +$settings->add($setting);