Proyectos de Subversion Moodle

Rev

| 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 mod_quiz\local\structure;
18
 
19
use context_module;
20
 
21
/**
22
 * Class slot_random, represents a random question slot type.
23
 *
24
 * @package    mod_quiz
25
 * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
26
 * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class slot_random {
30
 
31
    /** @var \stdClass Slot's properties. A record retrieved from the quiz_slots table. */
32
    protected $record;
33
 
34
    /**
35
     * @var \stdClass set reference record
36
     */
37
    protected $referencerecord;
38
 
39
    /**
40
     * @var \stdClass The quiz this question slot belongs to.
41
     */
42
    protected $quiz = null;
43
 
44
    /**
45
     * @var \core_tag_tag[] List of tags for this slot.
46
     */
47
    protected $tags = [];
48
 
49
    /**
50
     * @var string filter condition
51
     */
52
    protected $filtercondition = null;
53
 
54
    /**
55
     * slot_random constructor.
56
     *
57
     * @param \stdClass $slotrecord Represents a record in the quiz_slots table.
58
     */
59
    public function __construct($slotrecord = null) {
60
        $this->record = new \stdClass();
61
        $this->referencerecord = new \stdClass();
62
 
63
        $slotproperties = ['id', 'slot', 'quizid', 'page', 'requireprevious', 'maxmark'];
64
        $setreferenceproperties = ['usingcontextid', 'questionscontextid'];
65
 
66
        foreach ($slotproperties as $property) {
67
            if (isset($slotrecord->$property)) {
68
                $this->record->$property = $slotrecord->$property;
69
            }
70
        }
71
 
72
        foreach ($setreferenceproperties as $referenceproperty) {
73
            if (isset($slotrecord->$referenceproperty)) {
74
                $this->referencerecord->$referenceproperty = $slotrecord->$referenceproperty;
75
            }
76
        }
77
    }
78
 
79
    /**
80
     * Returns the quiz for this question slot.
81
     * The quiz is fetched the first time it is requested and then stored in a member variable to be returned each subsequent time.
82
     *
83
     * @return mixed
84
     * @throws \coding_exception
85
     */
86
    public function get_quiz() {
87
        global $DB;
88
 
89
        if (empty($this->quiz)) {
90
            if (empty($this->record->quizid)) {
91
                throw new \coding_exception('quizid is not set.');
92
            }
93
            $this->quiz = $DB->get_record('quiz', ['id' => $this->record->quizid]);
94
        }
95
 
96
        return $this->quiz;
97
    }
98
 
99
    /**
100
     * Sets the quiz object for the quiz slot.
101
     * It is not mandatory to set the quiz as the quiz slot can fetch it the first time it is accessed,
102
     * however it helps with the performance to set the quiz if you already have it.
103
     *
104
     * @param \stdClass $quiz The qui object.
105
     */
106
    public function set_quiz($quiz) {
107
        $this->quiz = $quiz;
108
        $this->record->quizid = $quiz->id;
109
    }
110
 
111
    /**
112
     * Set some tags for this quiz slot.
113
     *
114
     * @param \core_tag_tag[] $tags
115
     *
116
     * @deprecated since Moodle 4.3
117
     * @todo Final deprecation on Moodle 4.7 MDL-78091
118
     */
119
    public function set_tags($tags) {
120
        debugging('Method set_tags() is deprecated, ' .
121
            'please do not use this function.', DEBUG_DEVELOPER);
122
        $this->tags = [];
123
        foreach ($tags as $tag) {
124
            // We use $tag->id as the key for the array so not only it handles duplicates of the same tag being given,
125
            // but also it is consistent with the behaviour of set_tags_by_id() below.
126
            $this->tags[$tag->id] = $tag;
127
        }
128
    }
129
 
130
    /**
131
     * Set some tags for this quiz slot. This function uses tag ids to find tags.
132
     *
133
     * @param int[] $tagids
134
     * @deprecated since Moodle 4.3
135
     * @todo Final deprecation on Moodle 4.7 MDL-78091
136
     */
137
    public function set_tags_by_id($tagids) {
138
        debugging(
139
            'Method set_tags_by_id() is deprecated, please do not use this function.',
140
            DEBUG_DEVELOPER
141
        );
142
        $this->tags = \core_tag_tag::get_bulk($tagids, 'id, name');
143
    }
144
 
145
    /**
146
     * Set filter condition.
147
     *
148
     * @param \string $filters
149
     */
150
    public function set_filter_condition(string $filters): void {
151
        $this->filtercondition = $filters;
152
    }
153
 
154
    /**
155
     * Inserts the quiz slot at the $page page.
156
     * It is required to call this function if you are building a quiz slot object from scratch.
157
     *
158
     * @param int $page The page that this slot will be inserted at.
159
     */
160
    public function insert($page) {
161
        global $DB;
162
 
163
        $slots = $DB->get_records('quiz_slots', ['quizid' => $this->record->quizid],
164
                'slot', 'id, slot, page');
165
        $quiz = $this->get_quiz();
166
 
167
        $trans = $DB->start_delegated_transaction();
168
 
169
        $maxpage = 1;
170
        $numonlastpage = 0;
171
        foreach ($slots as $slot) {
172
            if ($slot->page > $maxpage) {
173
                $maxpage = $slot->page;
174
                $numonlastpage = 1;
175
            } else {
176
                $numonlastpage += 1;
177
            }
178
        }
179
 
180
        if (is_int($page) && $page >= 1) {
181
            // Adding on a given page.
182
            $lastslotbefore = 0;
183
            foreach (array_reverse($slots) as $otherslot) {
184
                if ($otherslot->page > $page) {
185
                    $DB->set_field('quiz_slots', 'slot', $otherslot->slot + 1, ['id' => $otherslot->id]);
186
                } else {
187
                    $lastslotbefore = $otherslot->slot;
188
                    break;
189
                }
190
            }
191
            $this->record->slot = $lastslotbefore + 1;
192
            $this->record->page = min($page, $maxpage + 1);
193
 
194
            quiz_update_section_firstslots($this->record->quizid, 1, max($lastslotbefore, 1));
195
        } else {
196
            $lastslot = end($slots);
197
            if ($lastslot) {
198
                $this->record->slot = $lastslot->slot + 1;
199
            } else {
200
                $this->record->slot = 1;
201
            }
202
            if ($quiz->questionsperpage && $numonlastpage >= $quiz->questionsperpage) {
203
                $this->record->page = $maxpage + 1;
204
            } else {
205
                $this->record->page = $maxpage;
206
            }
207
        }
208
 
209
        $this->record->id = $DB->insert_record('quiz_slots', $this->record);
210
 
211
        $this->referencerecord->component = 'mod_quiz';
212
        $this->referencerecord->questionarea = 'slot';
213
        $this->referencerecord->itemid = $this->record->id;
214
        $this->referencerecord->filtercondition = $this->filtercondition;
215
        $DB->insert_record('question_set_references', $this->referencerecord);
216
 
217
        $trans->allow_commit();
218
 
219
        // Log slot created event.
220
        $cm = get_coursemodule_from_instance('quiz', $quiz->id);
221
        $event = \mod_quiz\event\slot_created::create([
222
            'context' => context_module::instance($cm->id),
223
            'objectid' => $this->record->id,
224
            'other' => [
225
                'quizid' => $quiz->id,
226
                'slotnumber' => $this->record->slot,
227
                'page' => $this->record->page
228
            ]
229
        ]);
230
        $event->trigger();
231
    }
232
}