Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace qbank_previewquestion;
18
 
19
use question_display_options;
20
 
21
/**
22
 * Displays question preview options as default and set the options.
23
 *
24
 * Setting default, getting and setting user preferences in question preview options.
25
 *
26
 * @package    qbank_previewquestion
27
 * @copyright  2010 The Open University
28
 * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class question_preview_options extends question_display_options {
32
 
33
    /** @var string the behaviour to use for this preview. */
34
    public $behaviour;
35
 
36
    /** @var number the maximum mark to use for this preview. */
37
    public $maxmark;
38
 
39
    /** @var int the variant of the question to preview. */
40
    public $variant;
41
 
42
    /** @var string prefix to append to field names to get user_preference names. */
43
    const OPTIONPREFIX = 'question_preview_options_';
44
 
45
    /** @var int Special value for question version ID to indicate that we should always use the latest version */
46
    const ALWAYS_LATEST = 0;
47
 
48
    /**
49
     * Constructor.
1441 ariadna 50
     *
51
     * @param \stdClass|\question_definition $question only the ->defaultmark field is used.
1 efrain 52
     */
53
    public function __construct($question) {
54
        $this->behaviour = 'deferredfeedback';
55
        $this->maxmark = $question->defaultmark;
56
        $this->variant = null;
57
        $this->correctness = self::VISIBLE;
58
        $this->marks = self::MARK_AND_MAX;
59
        $this->markdp = get_config('quiz', 'decimalpoints');
60
        $this->feedback = self::VISIBLE;
61
        $this->numpartscorrect = $this->feedback;
62
        $this->generalfeedback = self::VISIBLE;
63
        $this->rightanswer = self::VISIBLE;
64
        $this->history = self::HIDDEN;
65
        $this->flags = self::HIDDEN;
66
        $this->manualcomment = self::HIDDEN;
67
    }
68
 
69
    /**
70
     * Names of the options we store in the user preferences table.
71
     * @return array
72
     */
73
    protected function get_user_pref_fields(): array {
74
        return ['behaviour', 'correctness', 'marks', 'markdp', 'feedback', 'generalfeedback', 'rightanswer', 'history'];
75
    }
76
 
77
    /**
78
     * Names and param types of the options we read from the request.
79
     * @return array
80
     */
81
    protected function get_field_types(): array {
82
        return [
83
                'behaviour' => PARAM_ALPHA,
84
                'maxmark' => PARAM_LOCALISEDFLOAT,
85
                'variant' => PARAM_INT,
86
                'correctness' => PARAM_BOOL,
87
                'marks' => PARAM_INT,
88
                'markdp' => PARAM_INT,
89
                'feedback' => PARAM_BOOL,
90
                'generalfeedback' => PARAM_BOOL,
91
                'rightanswer' => PARAM_BOOL,
92
                'history' => PARAM_BOOL,
93
        ];
94
    }
95
 
96
    /**
97
     * Load the value of the options from the user_preferences table.
98
     */
99
    public function load_user_defaults(): void {
100
        $defaults = get_config('question_preview');
101
        foreach ($this->get_user_pref_fields() as $field) {
102
            $this->$field = get_user_preferences(
103
                    self::OPTIONPREFIX . $field, $defaults->$field);
104
        }
105
        $this->numpartscorrect = $this->feedback;
106
    }
107
 
108
    /**
109
     * Save a change to the user's preview options to the database.
110
     * @param object $newoptions
111
     */
112
    public function save_user_preview_options($newoptions): void {
113
        foreach ($this->get_user_pref_fields() as $field) {
114
            if (isset($newoptions->$field)) {
115
                set_user_preference(self::OPTIONPREFIX . $field, $newoptions->$field);
116
            }
117
        }
118
    }
119
 
120
    /**
121
     * Set the value of any fields included in the request.
122
     */
123
    public function set_from_request(): void {
124
        foreach ($this->get_field_types() as $field => $type) {
125
            $this->$field = optional_param($field, $this->$field, $type);
126
        }
127
        $this->numpartscorrect = $this->feedback;
128
    }
129
 
130
    /**
131
     * Parameters needed in the URL when continuing this preview.
132
     *
133
     * @return array URL fragment.
134
     */
135
    public function get_url_params(): array {
136
        $params = [];
137
        foreach ($this->get_field_types() as $field => $notused) {
138
            if ($field === 'behaviour' || $field === 'maxmark' || is_null($this->$field)) {
139
                continue;
140
            }
141
            $params[$field] = $this->$field;
142
        }
143
        return $params;
144
    }
145
}