From adf8f3f9ad1a0a909fd2296c76affab3192f8d46 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 30 Jun 2009 16:55:56 +0000 Subject: [PATCH] MDL-19668 improvements of _with_advanced admin setting classes finished, quiz settings updated too --- lib/adminlib.php | 192 +++++++++++++++++++++++++++----------- lib/db/upgrade.php | 15 +++ mod/quiz/mod_form.php | 36 +++---- mod/quiz/settingslib.php | 32 +++---- mod/quiz/settingstree.php | 28 +++--- version.php | 2 +- 6 files changed, 198 insertions(+), 107 deletions(-) diff --git a/lib/adminlib.php b/lib/adminlib.php index c3237d139ce..1d182d6bf3e 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -3572,31 +3572,36 @@ class admin_setting_pickroles extends admin_setting_configmulticheckbox { } /** - * Text field with an advanced checkbox, that controls a additional "fix_$name" setting. + * Text field with an advanced checkbox, that controls a additional $name.'_adv' setting. * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class admin_setting_configtext_with_advanced extends admin_setting_configtext { /** - * Calls parent::__construct with specific arguments + * Constructor + * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. + * @param string $visiblename localised + * @param string $description long localised info + * @param array $defaultsetting ('value'=>string, '__construct'=>bool) + * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex + * @param int $size default field size */ - public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype) { - parent::__construct($name, $visiblename, $description, - $defaultsetting, $paramtype); + public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $size=null) { + parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $size); } /** * Loads the current setting and returns array * - * @return array Returns array value=>xx, fix=>xx + * @return array Returns array value=>xx, __construct=>xx */ public function get_setting() { $value = parent::get_setting(); - $fix = $this->config_read('fix_' . $this->name); - if (is_null($value) or is_null($fix)) { + $adv = $this->config_read($this->name.'_adv'); + if (is_null($value) or is_null($adv)) { return NULL; } - return array('value' => $value, 'fix' => $fix); + return array('value' => $value, 'adv' => $adv); } /** @@ -3609,14 +3614,8 @@ class admin_setting_configtext_with_advanced extends admin_setting_configtext { public function write_setting($data) { $error = parent::write_setting($data['value']); if (!$error) { - if (empty($data['fix'])) { - $ok = $this->config_write('fix_' . $this->name, 0); - } else { - $ok = $this->config_write('fix_' . $this->name, 1); - } - if (!$ok) { - $error = get_string('errorsetting', 'admin'); - } + $value = empty($data['adv']) ? 0 : 1; + $this->config_write($this->name.'_adv', $value); } return $error; } @@ -3638,19 +3637,19 @@ class admin_setting_configtext_with_advanced extends admin_setting_configtext { $defaultinfo[] = $default['value']; } } - if (!empty($default['fix'])) { + if (!empty($default['adv'])) { $defaultinfo[] = get_string('advanced'); } $defaultinfo = implode(', ', $defaultinfo); - $fix = !empty($data['fix']); + $adv = !empty($data['adv']); $return = '
' . '' . ' ' . - '
'; return format_admin_setting($this, $this->visiblename, $return, @@ -3659,7 +3658,111 @@ class admin_setting_configtext_with_advanced extends admin_setting_configtext { } /** - * Dropdown menu with an advanced checkbox, that controls a additional "fix_$name" setting. + * Checkbox with an advanced checkbox that controls an additional $name.'_adv' config setting. + * + * @copyright 2009 Petr Skoda (http://skodak.org) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_configcheckbox_with_advanced extends admin_setting_configcheckbox { + + /** + * Constructor + * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. + * @param string $visiblename localised + * @param string $description long localised info + * @param array $defaultsetting ('value'=>string, 'adv'=>bool) + * @param string $yes value used when checked + * @param string $no value used when not checked + */ + public function __construct($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') { + parent::__construct($name, $visiblename, $description, $defaultsetting, $yes, $no); + } + + /** + * Loads the current setting and returns array + * + * @return array Returns array value=>xx, adv=>xx + */ + public function get_setting() { + $value = parent::get_setting(); + $adv = $this->config_read($this->name.'_adv'); + if (is_null($value) or is_null($adv)) { + return NULL; + } + return array('value' => $value, 'adv' => $adv); + } + + /** + * Sets the value for the setting + * + * Sets the value for the setting to either the yes or no values + * of the object by comparing $data to yes + * + * @param mixed $data Gets converted to str for comparison against yes value + * @return string empty string or error + */ + public function write_setting($data) { + $error = parent::write_setting($data['value']); + if (!$error) { + $value = empty($data['adv']) ? 0 : 1; + $this->config_write($this->name.'_adv', $value); + } + return $error; + } + + /** + * Returns an XHTML checkbox field and with extra advanced cehckbox + * + * @param string $data If $data matches yes then checkbox is checked + * @param string $query + * @return string XHTML field + */ + public function output_html($data, $query='') { + $defaults = $this->get_defaultsetting(); + $defaultinfo = array(); + if (!is_null($defaults)) { + if ((string)$defaults['value'] === $this->yes) { + $defaultinfo[] = get_string('checkboxyes', 'admin'); + } else { + $defaultinfo[] = get_string('checkboxno', 'admin'); + } + if (!empty($defaults['adv'])) { + $defaultinfo[] = get_string('advanced'); + } + } + $defaultinfo = implode(', ', $defaultinfo); + + if ((string)$data['value'] === $this->yes) { // convert to strings before comparison + $checked = 'checked="checked"'; + } else { + $checked = ''; + } + if (!empty($data['adv'])) { + $advanced = 'checked="checked"'; + } else { + $advanced = ''; + } + + $fullname = $this->get_full_name(); + $novalue = s($this->no); + $yesvalue = s($this->yes); + $id = $this->get_id(); + $stradvanced = get_string('advanced'); + $return = << + + + + + +EOT; + return format_admin_setting($this, $this->visiblename, $return, $this->description, + true, '', $defaultinfo, $query); + } +} + +/** + * Dropdown menu with an advanced checkbox, that controls a additional $name.'_adv' setting. * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -3674,15 +3777,15 @@ class admin_setting_configselect_with_advanced extends admin_setting_configselec /** * Loads the current setting and returns array * - * @return array Returns array value=>xx, fix=>xx + * @return array Returns array value=>xx, adv=>xx */ public function get_setting() { $value = parent::get_setting(); - $fix = $this->config_read('fix_' . $this->name); - if (is_null($value) or is_null($fix)) { + $adv = $this->config_read($this->name.'_adv'); + if (is_null($value) or is_null($adv)) { return NULL; } - return array('value' => $value, 'fix' => $fix); + return array('value' => $value, 'adv' => $adv); } /** @@ -3695,14 +3798,8 @@ class admin_setting_configselect_with_advanced extends admin_setting_configselec public function write_setting($data) { $error = parent::write_setting($data['value']); if (!$error) { - if (empty($data['fix'])) { - $ok = $this->config_write('fix_' . $this->name, 0); - } else { - $ok = $this->config_write('fix_' . $this->name, 1); - } - if (!$ok) { - $error = get_string('errorsetting', 'admin'); - } + $value = empty($data['adv']) ? 0 : 1; + $this->config_write($this->name.'_adv', $value); } return $error; } @@ -3729,7 +3826,7 @@ class admin_setting_configselect_with_advanced extends admin_setting_configselec if (isset($this->choices[$default['value']])) { $defaultinfo[] = $this->choices[$default['value']]; } - if (!empty($default['fix'])) { + if (!empty($default['adv'])) { $defaultinfo[] = get_string('advanced'); } $defaultinfo = implode(', ', $defaultinfo); @@ -3737,33 +3834,18 @@ class admin_setting_configselect_with_advanced extends admin_setting_configselec $defaultinfo = ''; } - $fix = !empty($data['fix']); + $adv = !empty($data['adv']); $return = '
' . $selecthtml . ' ' . - '
'; return format_admin_setting($this, $this->visiblename, $return, $this->description, true, $warning, $defaultinfo, $query); } } -/** - * Specialisation of admin_setting_configselect_with_advanced for easy yes/no choices. - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class admin_setting_yesno_with_advanced extends admin_setting_configselect_with_advanced { - /** - * Calls parent::__construct with specific arguments - */ - public function __construct($name, $visiblename, $description, $defaultsetting) { - parent::__construct($name, $visiblename, $description, - $defaultsetting, array(get_string('no'), get_string('yes'))); - } -} - /** * Graded roles in gradebook * diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 3e838c8ebd6..13f7b90dae2 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2251,6 +2251,21 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); upgrade_main_savepoint($result, 2009061706); } + if ($result && $oldversion < 2009063000) { + // upgrade format of _with_advanced settings - quiz only + // note: this can be removed later, not needed for upgrades from 1.9.x + $quiz = get_config('quiz'); + foreach ($quiz as $name=>$value) { + if (strpos($name, 'fix_') !== 0) { + continue; + } + $newname = substr($name,4).'_adv'; + set_config($newname, $value, 'quiz'); + unset_config($name, 'quiz'); + } + upgrade_main_savepoint($result, 2009063000); + } + return $result; } diff --git a/mod/quiz/mod_form.php b/mod/quiz/mod_form.php index f258a220ad2..99206340f47 100644 --- a/mod/quiz/mod_form.php +++ b/mod/quiz/mod_form.php @@ -66,7 +66,7 @@ class mod_quiz_mod_form extends moodleform_mod { /// Time limit. $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true)); $mform->setHelpButton('timelimit', array('timelimit', get_string('quiztimer','quiz'), 'quiz')); - $mform->setAdvanced('timelimit', $quizconfig->fix_timelimit); + $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv); $mform->setDefault('timelimit', $quizconfig->timelimit); /// Number of attempts. @@ -76,13 +76,13 @@ class mod_quiz_mod_form extends moodleform_mod { } $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions); $mform->setHelpButton('attempts', array('attempts', get_string('attemptsallowed','quiz'), 'quiz')); - $mform->setAdvanced('attempts', $quizconfig->fix_attempts); + $mform->setAdvanced('attempts', $quizconfig->attempts_adv); $mform->setDefault('attempts', $quizconfig->attempts); /// Grading method. $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options()); $mform->setHelpButton('grademethod', array('grademethod', get_string('grademethod','quiz'), 'quiz')); - $mform->setAdvanced('grademethod', $quizconfig->fix_grademethod); + $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv); $mform->setDefault('grademethod', $quizconfig->grademethod); $mform->disabledIf('grademethod', 'attempts', 'eq', 1); @@ -93,7 +93,7 @@ class mod_quiz_mod_form extends moodleform_mod { $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz')); $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions')); $mform->setHelpButton('shufflequestions', array('shufflequestions', get_string('shufflequestions','quiz'), 'quiz')); - $mform->setAdvanced('shufflequestions', $quizconfig->fix_shufflequestions); + $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv); $mform->setDefault('shufflequestions', $quizconfig->shufflequestions); /// Questions per page. @@ -117,7 +117,7 @@ class mod_quiz_mod_form extends moodleform_mod { $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false); $mform->setHelpButton('questionsperpagegrp', array('questionsperpage', get_string('newpageevery', 'quiz'), 'quiz')); - $mform->setAdvanced('questionsperpagegrp', $quizconfig->fix_questionsperpage); + $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv); //------------------------------------------------------------------------------- $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz')); @@ -125,33 +125,33 @@ class mod_quiz_mod_form extends moodleform_mod { /// Shuffle within questions. $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz')); $mform->setHelpButton('shuffleanswers', array('shufflewithin', get_string('shufflewithin','quiz'), 'quiz')); - $mform->setAdvanced('shuffleanswers', $quizconfig->fix_shuffleanswers); + $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv); $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers); /// Adaptive mode. $mform->addElement('selectyesno', 'adaptive', get_string('adaptive', 'quiz')); $mform->setHelpButton('adaptive', array('adaptive', get_string('adaptive','quiz'), 'quiz')); - $mform->setAdvanced('adaptive', $quizconfig->fix_optionflags); + $mform->setAdvanced('adaptive', $quizconfig->optionflags_adv); $mform->setDefault('adaptive', $quizconfig->optionflags & QUESTION_ADAPTIVE); /// Apply penalties. $mform->addElement('selectyesno', 'penaltyscheme', get_string('penaltyscheme', 'quiz')); $mform->setHelpButton('penaltyscheme', array('penaltyscheme', get_string('penaltyscheme','quiz'), 'quiz')); - $mform->setAdvanced('penaltyscheme', $quizconfig->fix_penaltyscheme); + $mform->setAdvanced('penaltyscheme', $quizconfig->penaltyscheme_adv); $mform->setDefault('penaltyscheme', $quizconfig->penaltyscheme); $mform->disabledIf('penaltyscheme', 'adaptive', 'neq', 1); /// Each attempt builds on last. $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz')); $mform->setHelpButton('attemptonlast', array('repeatattempts', get_string('eachattemptbuildsonthelast', 'quiz'), 'quiz')); - $mform->setAdvanced('attemptonlast', $quizconfig->fix_attemptonlast); + $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv); $mform->setDefault('attemptonlast', $quizconfig->attemptonlast); $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1); //------------------------------------------------------------------------------- $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz')); $mform->setHelpButton('reviewoptionshdr', array('reviewoptions', get_string('reviewoptionsheading','quiz'), 'quiz')); - $mform->setAdvanced('reviewoptionshdr', $quizconfig->fix_review); + $mform->setAdvanced('reviewoptionshdr', $quizconfig->review_adv); /// Review options. $immediatelyoptionsgrp=array(); @@ -206,7 +206,7 @@ class mod_quiz_mod_form extends moodleform_mod { /// Show user picture. $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz')); $mform->setHelpButton('showuserpicture', array('showuserpicture', get_string('showuserpicture', 'quiz'), 'quiz')); - $mform->setAdvanced('showuserpicture', $quizconfig->fix_showuserpicture); + $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv); $mform->setDefault('showuserpicture', $quizconfig->showuserpicture); /// Overall decimal points. @@ -216,7 +216,7 @@ class mod_quiz_mod_form extends moodleform_mod { } $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options); $mform->setHelpButton('decimalpoints', array('decimalpoints', get_string('decimalplaces','quiz'), 'quiz')); - $mform->setAdvanced('decimalpoints', $quizconfig->fix_decimalpoints); + $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv); $mform->setDefault('decimalpoints', $quizconfig->decimalpoints); /// Question decimal points. @@ -226,7 +226,7 @@ class mod_quiz_mod_form extends moodleform_mod { } $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options); $mform->setHelpButton('questiondecimalpoints', array('decimalplacesquestion', get_string('decimalplacesquestion','quiz'), 'quiz')); - $mform->setAdvanced('questiondecimalpoints', $quizconfig->fix_questiondecimalpoints); + $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv); $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints); //------------------------------------------------------------------------------- @@ -236,26 +236,26 @@ class mod_quiz_mod_form extends moodleform_mod { $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz')); $mform->setType('quizpassword', PARAM_TEXT); $mform->setHelpButton('quizpassword', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz')); - $mform->setAdvanced('quizpassword', $quizconfig->fix_password); + $mform->setAdvanced('quizpassword', $quizconfig->password_adv); $mform->setDefault('quizpassword', $quizconfig->password); /// IP address. $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz')); $mform->setType('subnet', PARAM_TEXT); $mform->setHelpButton('subnet', array('requiresubnet', get_string('requiresubnet', 'quiz'), 'quiz')); - $mform->setAdvanced('subnet', $quizconfig->fix_subnet); + $mform->setAdvanced('subnet', $quizconfig->subnet_adv); $mform->setDefault('subnet', $quizconfig->subnet); /// Enforced time delay between quiz attempts. $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true)); $mform->setHelpButton('delay1', array('timedelay1', get_string('delay1st2nd', 'quiz'), 'quiz')); - $mform->setAdvanced('delay1', $quizconfig->fix_delay1); + $mform->setAdvanced('delay1', $quizconfig->delay1_adv); $mform->setDefault('delay1', $quizconfig->delay1); $mform->disabledIf('delay1', 'attempts', 'eq', 1); $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true)); $mform->setHelpButton('delay2', array('timedelay2', get_string('delaylater', 'quiz'), 'quiz')); - $mform->setAdvanced('delay2', $quizconfig->fix_delay2); + $mform->setAdvanced('delay2', $quizconfig->delay2_adv); $mform->setDefault('delay2', $quizconfig->delay2); $mform->disabledIf('delay2', 'attempts', 'eq', 1); $mform->disabledIf('delay2', 'attempts', 'eq', 2); @@ -263,7 +263,7 @@ class mod_quiz_mod_form extends moodleform_mod { /// 'Secure' window. $mform->addElement('selectyesno', 'popup', get_string('showinsecurepopup', 'quiz')); $mform->setHelpButton('popup', array('popup', get_string('showinsecurepopup', 'quiz'), 'quiz')); - $mform->setAdvanced('popup', $quizconfig->fix_popup); + $mform->setAdvanced('popup', $quizconfig->popup_adv); $mform->setDefault('popup', $quizconfig->popup); //------------------------------------------------------------------------------- diff --git a/mod/quiz/settingslib.php b/mod/quiz/settingslib.php index 5768d4152e9..96f8b1ff5d4 100644 --- a/mod/quiz/settingslib.php +++ b/mod/quiz/settingslib.php @@ -4,7 +4,9 @@ if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page. } -// Quiz specific admin settings class. +/** + * Quiz specific admin settings class. + */ class admin_setting_quiz_reviewoptions extends admin_setting { private static $times = array( QUIZ_REVIEW_IMMEDIATELY => 'reviewimmediately', @@ -37,28 +39,20 @@ class admin_setting_quiz_reviewoptions extends admin_setting { public function get_setting() { $value = $this->config_read($this->name); - $fix = $this->config_read('fix_' . $this->name); - if (is_null($value) or is_null($fix)) { + $adv = $this->config_read($this->name.'_adv'); + if (is_null($value) or is_null($adv)) { return NULL; } - return array('value' => $value, 'fix' => $fix); + return array('value' => $value, 'adv' => $adv); } public function write_setting($data) { if (!isset($data['value'])) { $data['value'] = $this->normalise_data($data); } - $ok = $this->config_write($this->name, $data['value']); - if ($ok) { - if (empty($data['fix'])) { - $ok = $this->config_write('fix_' . $this->name, 0); - } else { - $ok = $this->config_write('fix_' . $this->name, 1); - } - } - if (!$ok) { - return get_string('errorsetting', 'admin'); - } + $this->config_write($this->name, $data['value']); + $value = empty($data['adv']) ? 0 : 1; + $this->config_write($this->name.'_adv', $value); return ''; } @@ -87,11 +81,11 @@ class admin_setting_quiz_reviewoptions extends admin_setting { } $return .= "\n"; - $fix = !empty($data['fix']); + $adv = !empty($data['adv']); $return .= '' . - '