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.
13//
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/>.
16
17/**
18 * Admin settings class for the quiz review options.
19 *
20 * @package   mod_quiz
21 * @copyright 2008 Tim Hunt
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Admin settings class for the quiz review options.
31 *
32 * @copyright  2008 Tim Hunt
33 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class mod_quiz_admin_review_setting extends admin_setting {
36    /**
37     * @var integer should match the constants defined in
38     * {@link mod_quiz_display_options}. Copied for performance reasons.
39     */
40    const DURING            = 0x10000;
41
42    /**
43     * @var integer should match the constants defined in
44     * {@link mod_quiz_display_options}. Copied for performance reasons.
45     */
46    const IMMEDIATELY_AFTER = 0x01000;
47
48    /**
49     * @var integer should match the constants defined in
50     * {@link mod_quiz_display_options}. Copied for performance reasons.
51     */
52    const LATER_WHILE_OPEN  = 0x00100;
53
54    /**
55     * @var integer should match the constants defined in
56     * {@link mod_quiz_display_options}. Copied for performance reasons.
57     */
58    const AFTER_CLOSE       = 0x00010;
59
60    /**
61     * @var boolean|null forced checked / disabled attributes for the during time.
62     */
63    protected $duringstate;
64
65    /**
66     * This should match {@link mod_quiz_mod_form::$reviewfields} but copied
67     * here because generating the admin tree needs to be fast.
68     * @return array
69     */
70    public static function fields() {
71        return array(
72            'attempt'          => get_string('theattempt', 'quiz'),
73            'correctness'      => get_string('whethercorrect', 'question'),
74            'marks'            => get_string('marks', 'question'),
75            'specificfeedback' => get_string('specificfeedback', 'question'),
76            'generalfeedback'  => get_string('generalfeedback', 'question'),
77            'rightanswer'      => get_string('rightanswer', 'question'),
78            'overallfeedback'  => get_string('overallfeedback', 'quiz'),
79        );
80    }
81
82    /**
83     * Constructor.
84     *
85     * @param string $name unique ascii name, either 'mysetting' for settings that in config,
86     *                     or 'myplugin/mysetting' for ones in config_plugins.
87     * @param string $visiblename localised name
88     * @param string $description localised long description
89     * @param mixed $defaultsetting string or array depending on implementation
90     * @param bool|null $duringstate
91     */
92    public function __construct($name, $visiblename, $description,
93            $defaultsetting, $duringstate = null) {
94        $this->duringstate = $duringstate;
95        parent::__construct($name, $visiblename, $description, $defaultsetting);
96    }
97
98    /**
99     * Return the combination that means all times.
100     * @return int all times.
101     */
102    public static function all_on() {
103        return self::DURING | self::IMMEDIATELY_AFTER | self::LATER_WHILE_OPEN |
104                self::AFTER_CLOSE;
105    }
106
107    /**
108     * Get an array of the names of all the possible times.
109     * @return array an array of time constant => lang string.
110     */
111    protected static function times() {
112        return array(
113            self::DURING            => get_string('reviewduring', 'quiz'),
114            self::IMMEDIATELY_AFTER => get_string('reviewimmediately', 'quiz'),
115            self::LATER_WHILE_OPEN  => get_string('reviewopen', 'quiz'),
116            self::AFTER_CLOSE       => get_string('reviewclosed', 'quiz'),
117        );
118    }
119
120    protected function normalise_data($data) {
121        $times = self::times();
122        $value = 0;
123        foreach ($times as $timemask => $name) {
124            if ($timemask == self::DURING && !is_null($this->duringstate)) {
125                if ($this->duringstate) {
126                    $value += $timemask;
127                }
128            } else if (!empty($data[$timemask])) {
129                $value += $timemask;
130            }
131        }
132        return $value;
133    }
134
135    public function get_setting() {
136        return $this->config_read($this->name);
137    }
138
139    public function write_setting($data) {
140        if (is_array($data) || empty($data)) {
141            $data = $this->normalise_data($data);
142        }
143        $this->config_write($this->name, $data);
144        return '';
145    }
146
147    public function output_html($data, $query = '') {
148        if (is_array($data) || empty($data)) {
149            $data = $this->normalise_data($data);
150        }
151
152        $return = '<div class="group"><input type="hidden" name="' .
153                    $this->get_full_name() . '[' . self::DURING . ']" value="0" />';
154        foreach (self::times() as $timemask => $namestring) {
155            $id = $this->get_id(). '_' . $timemask;
156            $state = '';
157            if ($data & $timemask) {
158                $state = 'checked="checked" ';
159            }
160            if ($timemask == self::DURING && !is_null($this->duringstate)) {
161                $state = 'disabled="disabled" ';
162                if ($this->duringstate) {
163                    $state .= 'checked="checked" ';
164                }
165            }
166            $return .= '<span><input type="checkbox" name="' .
167                    $this->get_full_name() . '[' . $timemask . ']" value="1" id="' . $id .
168                    '" ' . $state . '/> <label for="' . $id . '">' .
169                    $namestring . "</label></span>\n";
170        }
171        $return .= "</div>\n";
172
173        return format_admin_setting($this, $this->visiblename, $return,
174                $this->description, true, '', get_string('everythingon', 'quiz'), $query);
175    }
176}
177