MDL-29627 load the existing settings when the quiz settings are re-edited.

This commit is contained in:
Tim Hunt
2011-10-13 11:13:08 +01:00
parent b83c32d39f
commit c18ba64c3e
3 changed files with 96 additions and 0 deletions
+53
View File
@@ -92,6 +92,10 @@ class quiz_access_manager {
/**
* Add any form fields that the access rules require to the settings form.
*
* Note that the standard plugins do not use this mechanism, becuase all their
* settings are stored in the quiz table.
*
* @param mod_quiz_mod_form $quizform the quiz settings form that is being built.
* @param MoodleQuickForm $mform the wrapped MoodleQuickForm.
*/
@@ -105,6 +109,10 @@ class quiz_access_manager {
/**
* Save any submitted settings when the quiz settings form is submitted.
*
* Note that the standard plugins do not use this mechanism, becuase all their
* settings are stored in the quiz table.
*
* @param object $quiz the data from the quiz form, including $quiz->id
* which is the is of the quiz being saved.
*/
@@ -115,6 +123,51 @@ class quiz_access_manager {
}
}
/**
* Load any settings required by the access rules. We try to do this with
* a single DB query.
*
* Note that the standard plugins do not use this mechanism, becuase all their
* settings are stored in the quiz table.
*
* @param int $quizid the quiz id.
* @return array setting value name => value. The value names should all
* start with the name of the corresponding plugin to avoid collisions.
*/
public static function load_settings($quizid) {
global $DB;
$rules = get_plugin_list_with_class('quizaccess', '', 'rule.php');
$allfields = '';
$alljoins = '{quiz} quiz';
$allparams = array('quizid' => $quizid);
foreach ($rules as $rule) {
list($fields, $joins, $params) = $rule::get_settings_sql($quizid);
if ($fields) {
if ($allfields) {
$allfields .= ', ';
}
$allfields .= $fields;
}
if ($joins) {
$alljoins .= ' ' . $joins;
}
if ($params) {
$allparams += $params;
}
}
$data = (array) $DB->get_record_sql("
SELECT $allfields
FROM $alljoins
WHERE quiz.id = :quizid", $allparams);
foreach ($rules as $rule) {
$data += $rule::get_extra_settings($quizid);
}
return $data;
}
protected function accumulate_messages(&$messages, $new) {
if (is_array($new)) {
$messages = array_merge($messages, $new);
+35
View File
@@ -128,4 +128,39 @@ abstract class quiz_access_rule_base {
public static function save_settings($quiz) {
// By default do nothing.
}
/**
* Return the bits of SQL needed to load all the settings from all the access
* plugins in one DB query. The easiest way to understand what you need to do
* here is probalby to read the code of {@link quiz_access_manager::load_settings()}.
*
* If you have some settings that cannot be loaded in this way, then you can
* use the {@link get_extra_settings()} method instead, but that has
* performance implications.
*
* @param int $quizid the id of the quiz we are loading settings for. This
* can also be accessed as quiz.id in the SQL. (quiz is a table alisas for {quiz}.)
* @return array with three elements:
* 1. fields: any fields to add to the select list. These should be alised
* if neccessary so that the field name starts the name of the plugin.
* 2. joins: any joins (should probably be LEFT JOINS) with other tables that
* are needed.
* 3. params: array of placeholder values that are needed by the SQL. You must
* used named placeholders, and the placeholder names should start with the
* plugin name, to avoid collisions.
*/
public static function get_settings_sql($quizid) {
return array('', '', array());
}
/**
* You can use this method to load any extra settings your plugin has that
* cannot be loaded efficiently with get_settings_sql().
* @param int $quizid the quiz id.
* @return array setting value name => value. The value names should all
* start with the name of your plugin to avoid collisions.
*/
public static function get_extra_settings($quizid) {
return array();
}
}
+8
View File
@@ -452,6 +452,14 @@ class mod_quiz_mod_form extends moodleform_mod {
$toform['quizpassword'] = $toform['password'];
unset($toform['password']);
}
// Load any settings belonging to the access rules.
if (!empty($toform['instance'])) {
$accesssettings = quiz_access_manager::load_settings($toform['instance']);
foreach ($accesssettings as $name => $value) {
$toform[$name] = $value;
}
}
}
public function validation($data, $files) {