MDL-37961 webservice: PARAM_BOOL with PARAM_DEFAULT accepts boolean default
[moodle.git] / mod / quiz / settingslib.php
blobaf5e0d35f477edf8774f35db050050c5a7ac7602
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This page is the entry page into the quiz UI. Displays information about the
19 * quiz to students and teachers, and lets students see their previous attempts.
21 * @package mod
22 * @subpackage quiz
23 * @copyright 2008 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Admin settings class for the quiz review opitions.
34 * @copyright 2008 Tim Hunt
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_quiz_admin_review_setting extends admin_setting {
38 /**#@+
39 * @var integer should match the constants defined in {@link mod_quiz_display_options}.
40 * again, copied for performance reasons.
42 const DURING = 0x10000;
43 const IMMEDIATELY_AFTER = 0x01000;
44 const LATER_WHILE_OPEN = 0x00100;
45 const AFTER_CLOSE = 0x00010;
46 /**#@-*/
48 /**
49 * @var boolean|null forced checked / disabled attributes for the during time.
51 protected $duringstate;
53 /**
54 * This should match {@link mod_quiz_mod_form::$reviewfields} but copied
55 * here because generating the admin tree needs to be fast.
56 * @return array
58 public static function fields() {
59 return array(
60 'attempt' => get_string('theattempt', 'quiz'),
61 'correctness' => get_string('whethercorrect', 'question'),
62 'marks' => get_string('marks', 'question'),
63 'specificfeedback' => get_string('specificfeedback', 'question'),
64 'generalfeedback' => get_string('generalfeedback', 'question'),
65 'rightanswer' => get_string('rightanswer', 'question'),
66 'overallfeedback' => get_string('overallfeedback', 'quiz'),
70 public function __construct($name, $visiblename, $description,
71 $defaultsetting, $duringstate = null) {
72 $this->duringstate = $duringstate;
73 parent::__construct($name, $visiblename, $description, $defaultsetting);
76 /**
77 * @return int all times.
79 public static function all_on() {
80 return self::DURING | self::IMMEDIATELY_AFTER | self::LATER_WHILE_OPEN |
81 self::AFTER_CLOSE;
84 protected static function times() {
85 return array(
86 self::DURING => get_string('reviewduring', 'quiz'),
87 self::IMMEDIATELY_AFTER => get_string('reviewimmediately', 'quiz'),
88 self::LATER_WHILE_OPEN => get_string('reviewopen', 'quiz'),
89 self::AFTER_CLOSE => get_string('reviewclosed', 'quiz'),
93 protected function normalise_data($data) {
94 $times = self::times();
95 $value = 0;
96 foreach ($times as $timemask => $name) {
97 if ($timemask == self::DURING && !is_null($this->duringstate)) {
98 if ($this->duringstate) {
99 $value += $timemask;
101 } else if (!empty($data[$timemask])) {
102 $value += $timemask;
105 return $value;
108 public function get_setting() {
109 return $this->config_read($this->name);
112 public function write_setting($data) {
113 if (is_array($data) || empty($data)) {
114 $data = $this->normalise_data($data);
116 $this->config_write($this->name, $data);
117 return '';
120 public function output_html($data, $query = '') {
121 if (is_array($data) || empty($data)) {
122 $data = $this->normalise_data($data);
125 $return = '<div class="group"><input type="hidden" name="' .
126 $this->get_full_name() . '[' . self::DURING . ']" value="0" />';
127 foreach (self::times() as $timemask => $namestring) {
128 $id = $this->get_id(). '_' . $timemask;
129 $state = '';
130 if ($data & $timemask) {
131 $state = 'checked="checked" ';
133 if ($timemask == self::DURING && !is_null($this->duringstate)) {
134 $state = 'disabled="disabled" ';
135 if ($this->duringstate) {
136 $state .= 'checked="checked" ';
139 $return .= '<span><input type="checkbox" name="' .
140 $this->get_full_name() . '[' . $timemask . ']" value="1" id="' . $id .
141 '" ' . $state . '/> <label for="' . $id . '">' .
142 $namestring . "</label></span>\n";
144 $return .= "</div>\n";
146 return format_admin_setting($this, $this->visiblename, $return,
147 $this->description, true, '', get_string('everythingon', 'quiz'), $query);
153 * Admin settings class for the quiz grading method.
155 * Just so we can lazy-load the choices.
157 * @copyright 2011 The Open University
158 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
160 class mod_quiz_admin_setting_grademethod extends admin_setting_configselect_with_advanced {
161 public function load_choices() {
162 global $CFG;
164 if (is_array($this->choices)) {
165 return true;
168 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
169 $this->choices = quiz_get_grading_options();
171 return true;
177 * Admin settings class for the quiz browser security option.
179 * Just so we can lazy-load the choices.
181 * @copyright 2011 The Open University
182 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
184 class mod_quiz_admin_setting_browsersecurity extends admin_setting_configselect_with_advanced {
185 public function load_choices() {
186 global $CFG;
188 if (is_array($this->choices)) {
189 return true;
192 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
193 $this->choices = quiz_access_manager::get_browser_security_choices();
195 return true;
201 * Admin settings class for the quiz overdue attempt handling method.
203 * Just so we can lazy-load the choices.
205 * @copyright 2011 The Open University
206 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
208 class mod_quiz_admin_setting_overduehandling extends admin_setting_configselect_with_advanced {
209 public function load_choices() {
210 global $CFG;
212 if (is_array($this->choices)) {
213 return true;
216 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
217 $this->choices = quiz_get_overdue_handling_options();
219 return true;