Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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;
18
 
19
use core_question\local\bank\condition;
20
use mod_quiz\external\submit_question_version;
21
use mod_quiz\question\bank\qbank_helper;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
require_once(__DIR__ . '/quiz_question_helper_test_trait.php');
26
 
27
/**
28
 * Question versions test for quiz.
29
 *
30
 * @package    mod_quiz
31
 * @category   test
32
 * @copyright  2021 Catalyst IT Australia Pty Ltd
33
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @covers \mod_quiz\question\bank\qbank_helper
36
 */
37
class quiz_question_version_test extends \advanced_testcase {
38
    use \quiz_question_helper_test_trait;
39
 
40
    /** @var \stdClass user record. */
41
    protected $student;
42
 
43
    /**
44
     * Called before every test.
45
     */
46
    public function setUp(): void {
47
        global $USER;
48
        parent::setUp();
49
        $this->setAdminUser();
50
        $this->course = $this->getDataGenerator()->create_course();
51
        $this->student = $this->getDataGenerator()->create_user();
52
        $this->user = $USER;
53
    }
54
 
55
    /**
56
     * Test the quiz question data for changed version in the slots.
57
     */
58
    public function test_quiz_questions_for_changed_versions() {
59
        $this->resetAfterTest();
60
        $quiz = $this->create_test_quiz($this->course);
61
        // Test for questions from a different context.
62
        $context = \context_module::instance(get_coursemodule_from_instance("quiz", $quiz->id, $this->course->id)->id);
63
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
64
        // Create a couple of questions.
65
        $cat = $questiongenerator->create_question_category(['contextid' => $context->id]);
66
        $numq = $questiongenerator->create_question('essay', null,
67
            ['category' => $cat->id, 'name' => 'This is the first version']);
68
        // Create two version.
69
        $questiongenerator->update_question($numq, null, ['name' => 'This is the second version']);
70
        $questiongenerator->update_question($numq, null, ['name' => 'This is the third version']);
71
        quiz_add_quiz_question($numq->id, $quiz);
72
        // Create the quiz object.
73
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
74
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
75
        $slots = $structure->get_slots();
76
        $slot = reset($slots);
77
        // Test that the version added is 'always latest'.
78
        $this->assertEquals(3, $slot->version);
79
        $quizobj->preload_questions();
80
        $quizobj->load_questions();
81
        $questions = $quizobj->get_questions();
82
        $question = reset($questions);
83
        $this->assertEquals(3, $question->version);
84
        $this->assertEquals('This is the third version', $question->name);
85
        // Create another version.
86
        $questiongenerator->update_question($numq, null, ['name' => 'This is the latest version']);
87
        // Check that 'Always latest is working'.
88
        $quizobj->preload_questions();
89
        $quizobj->load_questions();
90
        $questions = $quizobj->get_questions();
91
        $question = reset($questions);
92
        $this->assertEquals(4, $question->version);
93
        $this->assertEquals('This is the latest version', $question->name);
94
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
95
        $slots = $structure->get_slots();
96
        $slot = reset($slots);
97
        $this->assertEquals(4, $slot->version);
98
        // Now change the version using the external service.
99
        $versions = qbank_helper::get_version_options($slot->questionid);
100
        // We don't want the current version.
101
        $selectversions = [];
102
        foreach ($versions as $version) {
103
            if ($version->version === $slot->version) {
104
                continue;
105
            }
106
            $selectversions [$version->version] = $version;
107
        }
108
        // Change to version 1.
109
        submit_question_version::execute($slot->id, (int)$selectversions[1]->version);
110
        $quizobj->preload_questions();
111
        $quizobj->load_questions();
112
        $questions = $quizobj->get_questions();
113
        $question = reset($questions);
114
        $this->assertEquals(1, $question->version);
115
        $this->assertEquals('This is the first version', $question->name);
116
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
117
        $slots = $structure->get_slots();
118
        $slot = reset($slots);
119
        $this->assertEquals(1, $slot->version);
120
        // Change to version 2.
121
        submit_question_version::execute($slot->id, $selectversions[2]->version);
122
        $quizobj->preload_questions();
123
        $quizobj->load_questions();
124
        $questions = $quizobj->get_questions();
125
        $question = reset($questions);
126
        $this->assertEquals(2, $question->version);
127
        $this->assertEquals('This is the second version', $question->name);
128
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
129
        $slots = $structure->get_slots();
130
        $slot = reset($slots);
131
        $this->assertEquals(2, $slot->version);
132
    }
133
 
134
    /**
135
     * Test if changing the version of the slot changes the attempts.
136
     */
137
    public function test_quiz_question_attempts_with_changed_version() {
138
        $this->resetAfterTest();
139
        $quiz = $this->create_test_quiz($this->course);
140
        // Test for questions from a different context.
141
        $context = \context_module::instance(get_coursemodule_from_instance("quiz", $quiz->id, $this->course->id)->id);
142
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
143
        // Create a couple of questions.
144
        $cat = $questiongenerator->create_question_category(['contextid' => $context->id]);
145
        $numq = $questiongenerator->create_question('numerical', null,
146
            ['category' => $cat->id, 'name' => 'This is the first version']);
147
        // Create two version.
148
        $questiongenerator->update_question($numq, null, ['name' => 'This is the second version']);
149
        $questiongenerator->update_question($numq, null, ['name' => 'This is the third version']);
150
        quiz_add_quiz_question($numq->id, $quiz);
151
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student);
152
        $this->assertEquals('This is the third version', $attemptobj->get_question_attempt(1)->get_question()->name);
153
        // Create the quiz object.
154
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
155
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
156
        $slots = $structure->get_slots();
157
        $slot = reset($slots);
158
        // Now change the version using the external service.
159
        $versions = qbank_helper::get_version_options($slot->questionid);
160
        // We dont want the current version.
161
        $selectversions = [];
162
        foreach ($versions as $version) {
163
            if ($version->version === $slot->version) {
164
                continue;
165
            }
166
            $selectversions [$version->version] = $version;
167
        }
168
        // Change to version 1.
169
        $this->expectException('moodle_exception');
170
        submit_question_version::execute($slot->id, (int)$selectversions[1]->version);
171
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student, 2);
172
        $this->assertEquals('This is the first version', $attemptobj->get_question_attempt(1)->get_question()->name);
173
        // Change to version 2.
174
        submit_question_version::execute($slot->id, (int)$selectversions[2]->version);
175
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student, 3);
176
        $this->assertEquals('This is the second version', $attemptobj->get_question_attempt(1)->get_question()->name);
177
        // Create another version.
178
        $questiongenerator->update_question($numq, null, ['name' => 'This is the latest version']);
179
        // Change to always latest.
180
        submit_question_version::execute($slot->id, 0);
181
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student, 4);
182
        $this->assertEquals('This is the latest version', $attemptobj->get_question_attempt(1)->get_question()->name);
183
    }
184
 
185
    public function test_get_version_information_for_questions_in_attempt(): void {
186
        $this->resetAfterTest();
187
        /** @var \mod_quiz_generator $quizgenerator */
188
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
189
        /** @var \core_question_generator $questiongenerator */
190
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
191
 
192
        // Make two categories, each with a question.
193
        $coursecontext = \context_course::instance($this->course->id);
194
        $cat = $questiongenerator->create_question_category(
195
            ['name' => 'Non-random questions', 'context' => $coursecontext->id]);
196
        $randomcat = $questiongenerator->create_question_category(
197
            ['name' => 'Random questions', 'context' => $coursecontext->id]);
198
        $q1 = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
199
        $q2 = $questiongenerator->create_question('truefalse', null, ['category' => $randomcat->id]);
200
 
201
        // Make the quiz, adding q1, and a random question from randomcat.
202
        $quiz = $quizgenerator->create_instance([
203
            'course' => $this->course->id,
204
            'grade' => 100.0,
205
            'sumgrades' => 2,
206
            'canredoquestions' => 1,
207
            'preferredbehaviour' => 'immediatefeedback',
208
        ]);
209
        $quizobj = quiz_settings::create($quiz->id);
210
        quiz_add_quiz_question($q1->id, $quiz);
211
        $structure = $quizobj->get_structure();
212
        $structure->add_random_questions(0, 1, [
213
            'filter' => [
214
                'category' => [
215
                    'jointype' => condition::JOINTYPE_DEFAULT,
216
                    'values' => [$randomcat->id],
217
                    'filteroptions' => ['includesubcategories' => false],
218
                ],
219
            ],
220
        ]);
221
 
222
        // Student starts attempt.
223
        $quizobj = quiz_settings::create($quiz->id);
224
        $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
225
        $attemptobj = quiz_attempt::create($attempt->id);
226
 
227
        // Answer both questions.
228
        $postdata = $questiongenerator->get_simulated_post_data_for_questions_in_usage(
229
            $attemptobj->get_question_usage(),
230
            [1 => 'True', 2 => 'False'],
231
            true,
232
        );
233
        $attemptobj->process_submitted_actions(time(), false, $postdata);
234
 
235
        // Redo both questions - need to re-create attemptobj each time.
236
        $attemptobj = quiz_attempt::create($attempt->id);
237
        $attemptobj->process_redo_question(1, time());
238
        $attemptobj = quiz_attempt::create($attempt->id);
239
        $attemptobj->process_redo_question(2, time());
240
 
241
        // Edit both questions to make a second version.
242
        $questiongenerator->update_question($q1);
243
        $questiongenerator->update_question($q2);
244
 
245
        // Finally! call the method we want to test.
246
        $versioninfo = qbank_helper::get_version_information_for_questions_in_attempt(
247
            $attemptobj->get_attempt(), $attemptobj->get_context());
248
 
249
        // Verify - all questions should now want to be V2 for various reasons.
250
        $this->assertEquals(2, $versioninfo[1]->newversion);
251
        $this->assertEquals(2, $versioninfo[2]->newversion);
252
        $this->assertEquals(2, $versioninfo[3]->newversion);
253
        $this->assertEquals(2, $versioninfo[4]->newversion);
254
    }
255
}