Skip to content

Commit

Permalink
MDL-20636 Review option defaults in the admin settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Feb 10, 2011
1 parent 55b81c1 commit 1493619
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 70 deletions.
16 changes: 7 additions & 9 deletions mod/quiz/mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ function definition() {
$mform->setAdvanced('reviewoptionshdr', $quizconfig->review_adv);

/// Review options.
$this->add_review_options_group($mform, 'during', mod_quiz_display_options::DURING);
$this->add_review_options_group($mform, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER);
$this->add_review_options_group($mform, 'open', mod_quiz_display_options::LATER_WHILE_OPEN);
$this->add_review_options_group($mform, 'closed', mod_quiz_display_options::AFTER_CLOSE);
$this->add_review_options_group($mform, $quizconfig, 'during', mod_quiz_display_options::DURING);
$this->add_review_options_group($mform, $quizconfig, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER);
$this->add_review_options_group($mform, $quizconfig, 'open', mod_quiz_display_options::LATER_WHILE_OPEN);
$this->add_review_options_group($mform, $quizconfig, 'closed', mod_quiz_display_options::AFTER_CLOSE);

foreach ($behaviours as $behaviour => $notused) {
$unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
Expand Down Expand Up @@ -325,18 +325,16 @@ function definition() {
$this->add_action_buttons();
}

protected function add_review_options_group($mform, $whenname, $when) {
global $CFG;

protected function add_review_options_group($mform, $quizconfig, $whenname, $when) {
$group = array();
foreach (self::$reviewfields as $field => $label) {
$group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
}
$mform->addGroup($group, $whenname . 'optionsgrp', get_string('review' . $whenname, 'quiz'), null, false);

foreach (self::$reviewfields as $field => $notused) {
$cfgfield = 'quiz_review' . $field;
if ($CFG->$cfgfield & $when) {
$cfgfield = 'review' . $field;
if ($quizconfig->$cfgfield & $when) {
$mform->setDefault($field . $whenname, 1);
} else {
$mform->setDefault($field . $whenname, 0);
Expand Down
19 changes: 14 additions & 5 deletions mod/quiz/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,20 @@
get_string('eachattemptbuildsonthelast', 'quiz'), get_string('configeachattemptbuildsonthelast', 'quiz'),
array('value' => 0, 'adv' => true)));

//// Review options.
// TODO
//$quizsettings->add(new admin_setting_quiz_reviewoptions('quiz/review',
// get_string('reviewoptions', 'quiz'), get_string('configreviewoptions', 'quiz'),
// array('value' => QUIZ_REVIEW_IMMEDIATELY | QUIZ_REVIEW_OPEN | QUIZ_REVIEW_CLOSED, 'fix' => false)));
// Review options.
$quizsettings->add(new admin_setting_heading('reviewheading', get_string('reviewoptionsheading', 'quiz'), ''));
foreach (mod_quiz_admin_review_setting::fields() as $field => $name) {
$default = mod_quiz_admin_review_setting::all_on();
$forceduring = null;
if ($field == 'attempt') {
$forceduring = true;
} else if ($field == 'overallfeedback') {
$default = $default ^ mod_quiz_admin_review_setting::DURING;
$forceduring = false;
}
$quizsettings->add(new mod_quiz_admin_review_setting('quiz/review' . $field,
$name, '', $default, $forceduring));
}

// Show the user's picture
$quizsettings->add(new admin_setting_configcheckbox_with_advanced('quiz/showuserpicture',
Expand Down
137 changes: 82 additions & 55 deletions mod/quiz/settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,114 @@
/**
* Quiz specific admin settings class.
*/
class admin_setting_quiz_reviewoptions extends admin_setting {
private static $times = array(
QUIZ_REVIEW_IMMEDIATELY => 'reviewimmediately',
QUIZ_REVIEW_OPEN => 'reviewopen',
QUIZ_REVIEW_CLOSED => 'reviewclosed');
private static $things = array(
QUIZ_REVIEW_RESPONSES => 'responses',
QUIZ_REVIEW_ANSWERS => 'answers',
QUIZ_REVIEW_FEEDBACK => 'feedback',
QUIZ_REVIEW_GENERALFEEDBACK => 'generalfeedback',
QUIZ_REVIEW_SCORES => 'scores',
QUIZ_REVIEW_OVERALLFEEDBACK => 'overallfeedback');
class mod_quiz_admin_review_setting extends admin_setting {
/**#@+
* @var integer should match the constants defined in {@link mod_quiz_display_options}.
* again, copied for performance reasons.
*/
const DURING = 0x10000;
const IMMEDIATELY_AFTER = 0x01000;
const LATER_WHILE_OPEN = 0x00100;
const AFTER_CLOSE = 0x00010;
/**#@-*/

public function __construct($name, $visiblename, $description, $defaultsetting) {
$this->plugin = 'quiz';
/**
* @var boolean|null forced checked / disabled attributes for the during time.
*/
protected $duringstate;

/**
* This should match {@link mod_quiz_mod_form::$reviewfields} but copied
* here becuase generating the admin tree needs to be fast.
* @return array
*/
public static function fields() {
return array(
'attempt' => get_string('theattempt', 'quiz'),
'correctness' => get_string('whethercorrect', 'question'),
'marks' => get_string('marks', 'question'),
'specificfeedback' => get_string('specificfeedback', 'question'),
'generalfeedback' => get_string('generalfeedback', 'question'),
'rightanswer' => get_string('rightanswer', 'question'),
'overallfeedback' => get_string('overallfeedback', 'quiz'),
);
}

public function __construct($name, $visiblename, $description, $defaultsetting, $duringstate = null) {
$this->duringstate = $duringstate;
parent::__construct($name, $visiblename, $description, $defaultsetting);
}

private function normalise_data($data) {
/**
* @return integer all times.
*/
public static function all_on() {
return self::DURING | self::IMMEDIATELY_AFTER | self::LATER_WHILE_OPEN |
self::AFTER_CLOSE;
}

protected static function times() {
return array(
self::DURING => get_string('reviewduring', 'quiz'),
self::IMMEDIATELY_AFTER => get_string('reviewimmediately', 'quiz'),
self::LATER_WHILE_OPEN => get_string('reviewopen', 'quiz'),
self::AFTER_CLOSE => get_string('reviewclosed', 'quiz'),
);
}

protected function normalise_data($data) {
$times = self::times();
$value = 0;
foreach (admin_setting_quiz_reviewoptions::$times as $timemask => $timestring) {
foreach (admin_setting_quiz_reviewoptions::$things as $thingmask => $thingstring) {
if (!empty($data[$timemask][$thingmask])) {
$value += $timemask & $thingmask;
foreach ($times as $timemask => $name) {
if ($timemask == self::DURING && !is_null($this->duringstate)) {
if ($this->duringstate) {
$value += $timemask;
}
} else if (!empty($data[$timemask])) {
$value += $timemask;
}
}
return $value;
}

public function get_setting() {
$value = $this->config_read($this->name);
$adv = $this->config_read($this->name.'_adv');
if (is_null($value) or is_null($adv)) {
return NULL;
}
return array('value' => $value, 'adv' => $adv);
return $this->config_read($this->name);
}

public function write_setting($data) {
if (!isset($data['value'])) {
$data['value'] = $this->normalise_data($data);
if (is_array($data) || empty($data)) {
$data = $this->normalise_data($data);
}
$this->config_write($this->name, $data['value']);
$value = empty($data['adv']) ? 0 : 1;
$this->config_write($this->name.'_adv', $value);
$this->config_write($this->name, $data);
return '';
}

public function output_html($data, $query='') {
if (!isset($data['value'])) {
$data['value'] = $this->normalise_data($data);
public function output_html($data, $query = '') {
if (is_array($data) || empty($data)) {
$data = $this->normalise_data($data);
}

$return = '<div id="adminquizreviewoptions" class="clearfix">' . "\n";
foreach (admin_setting_quiz_reviewoptions::$times as $timemask => $timestring) {
$return .= '<div class="group"><div class="fitemtitle">' . get_string($timestring, 'quiz') . "</div>\n";
$nameprefix = $this->get_full_name() . '[' . $timemask . ']';
$idprefix = $this->get_id(). '_' . $timemask . '_';
foreach (admin_setting_quiz_reviewoptions::$things as $thingmask => $thingstring) {
$id = $idprefix . $thingmask;
$state = '';
if ($data['value'] & $timemask & $thingmask) {
$state = 'checked="checked" ';
$return = '<div class="group"><input type="hidden" name="' .
$this->get_full_name() . '[' . self::DURING . ']" value="0" />';
foreach (self::times() as $timemask => $namestring) {
$id = $this->get_id(). '_' . $timemask;
$state = '';
if ($data & $timemask) {
$state = 'checked="checked" ';
}
if ($timemask == self::DURING && !is_null($this->duringstate)) {
$state = 'disabled="disabled" ';
if ($this->duringstate) {
$state .= 'checked="checked" ';
}
$return .= '<span><input type="checkbox" name="' .
$nameprefix . '[' . $thingmask . ']" value="1" id="' . $id .
'" ' . $state . '/> <label for="' . $id . '">' .
get_string($thingstring, 'quiz') . "</label></span>\n";
}
$return .= "</div>\n";
$return .= '<span><input type="checkbox" name="' .
$this->get_full_name() . '[' . $timemask . ']" value="1" id="' . $id .
'" ' . $state . '/> <label for="' . $id . '">' .
$namestring . "</label></span>\n";
}
$return .= "</div>\n";

$adv = !empty($data['adv']);
$return .= '<input type="checkbox" class="form-checkbox" id="' .
$this->get_id() . '_adv" name="' . $this->get_full_name() .
'[adv]" value="1" ' . ($adv ? 'checked="checked"' : '') . ' />' .
' <label for="' . $this->get_id() . '_adv">' .
get_string('advanced') . '</label> ';

return format_admin_setting($this, $this->visiblename, $return,
$this->description, true, '', get_string('everythingon', 'quiz'), $query);
}
Expand Down
2 changes: 1 addition & 1 deletion question/todo/diffstat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ DONE mod/quiz/protect_js.php | 56 -
DONE mod/quiz/review.php | 520 ++--
DONE mod/quiz/reviewoptions.html | 124 +-
DONE mod/quiz/reviewquestion.php | 267 +-
mod/quiz/settingslib.php
DONE mod/quiz/settingslib.php
DONE mod/quiz/simpletest/testaccessrules.php | 518 ++++
DONE mod/quiz/simpletest/testlib.php | 58 +
DONE mod/quiz/simpletest/testlocallib.php | 76 +
Expand Down

0 comments on commit 1493619

Please sign in to comment.