Proyectos de Subversion Moodle

Rev

Rev 11 | | 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 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
 */
1441 ariadna 37
final class quiz_question_version_test extends \advanced_testcase {
1 efrain 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
     */
11 efrain 58
    public function test_quiz_questions_for_changed_versions(): void {
1 efrain 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);
1441 ariadna 100
        $this->assertDebuggingCalled();
1 efrain 101
        // We don't want the current version.
102
        $selectversions = [];
103
        foreach ($versions as $version) {
104
            if ($version->version === $slot->version) {
105
                continue;
106
            }
107
            $selectversions [$version->version] = $version;
108
        }
109
        // Change to version 1.
110
        submit_question_version::execute($slot->id, (int)$selectversions[1]->version);
111
        $quizobj->preload_questions();
112
        $quizobj->load_questions();
113
        $questions = $quizobj->get_questions();
114
        $question = reset($questions);
115
        $this->assertEquals(1, $question->version);
116
        $this->assertEquals('This is the first version', $question->name);
117
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
118
        $slots = $structure->get_slots();
119
        $slot = reset($slots);
120
        $this->assertEquals(1, $slot->version);
121
        // Change to version 2.
122
        submit_question_version::execute($slot->id, $selectversions[2]->version);
123
        $quizobj->preload_questions();
124
        $quizobj->load_questions();
125
        $questions = $quizobj->get_questions();
126
        $question = reset($questions);
127
        $this->assertEquals(2, $question->version);
128
        $this->assertEquals('This is the second version', $question->name);
129
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
130
        $slots = $structure->get_slots();
131
        $slot = reset($slots);
132
        $this->assertEquals(2, $slot->version);
133
    }
134
 
135
    /**
136
     * Test if changing the version of the slot changes the attempts.
137
     */
11 efrain 138
    public function test_quiz_question_attempts_with_changed_version(): void {
1 efrain 139
        $this->resetAfterTest();
140
        $quiz = $this->create_test_quiz($this->course);
141
        // Test for questions from a different context.
142
        $context = \context_module::instance(get_coursemodule_from_instance("quiz", $quiz->id, $this->course->id)->id);
143
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
144
        // Create a couple of questions.
145
        $cat = $questiongenerator->create_question_category(['contextid' => $context->id]);
146
        $numq = $questiongenerator->create_question('numerical', null,
147
            ['category' => $cat->id, 'name' => 'This is the first version']);
148
        // Create two version.
149
        $questiongenerator->update_question($numq, null, ['name' => 'This is the second version']);
150
        $questiongenerator->update_question($numq, null, ['name' => 'This is the third version']);
151
        quiz_add_quiz_question($numq->id, $quiz);
152
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student);
153
        $this->assertEquals('This is the third version', $attemptobj->get_question_attempt(1)->get_question()->name);
154
        // Create the quiz object.
155
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
156
        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
157
        $slots = $structure->get_slots();
158
        $slot = reset($slots);
159
        // Now change the version using the external service.
160
        $versions = qbank_helper::get_version_options($slot->questionid);
1441 ariadna 161
        $this->assertDebuggingCalled();
1 efrain 162
        // We dont want the current version.
163
        $selectversions = [];
164
        foreach ($versions as $version) {
165
            if ($version->version === $slot->version) {
166
                continue;
167
            }
168
            $selectversions [$version->version] = $version;
169
        }
170
        // Change to version 1.
171
        $this->expectException('moodle_exception');
172
        submit_question_version::execute($slot->id, (int)$selectversions[1]->version);
173
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student, 2);
174
        $this->assertEquals('This is the first version', $attemptobj->get_question_attempt(1)->get_question()->name);
175
        // Change to version 2.
176
        submit_question_version::execute($slot->id, (int)$selectversions[2]->version);
177
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student, 3);
178
        $this->assertEquals('This is the second version', $attemptobj->get_question_attempt(1)->get_question()->name);
179
        // Create another version.
180
        $questiongenerator->update_question($numq, null, ['name' => 'This is the latest version']);
181
        // Change to always latest.
182
        submit_question_version::execute($slot->id, 0);
183
        [, , $attemptobj] = $this->attempt_quiz($quiz, $this->student, 4);
184
        $this->assertEquals('This is the latest version', $attemptobj->get_question_attempt(1)->get_question()->name);
185
    }
186
 
187
    public function test_get_version_information_for_questions_in_attempt(): void {
188
        $this->resetAfterTest();
189
        /** @var \mod_quiz_generator $quizgenerator */
190
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
191
        /** @var \core_question_generator $questiongenerator */
192
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
193
 
194
        // Make two categories, each with a question.
195
        $coursecontext = \context_course::instance($this->course->id);
196
        $cat = $questiongenerator->create_question_category(
197
            ['name' => 'Non-random questions', 'context' => $coursecontext->id]);
198
        $randomcat = $questiongenerator->create_question_category(
199
            ['name' => 'Random questions', 'context' => $coursecontext->id]);
200
        $q1 = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
201
        $q2 = $questiongenerator->create_question('truefalse', null, ['category' => $randomcat->id]);
202
 
203
        // Make the quiz, adding q1, and a random question from randomcat.
204
        $quiz = $quizgenerator->create_instance([
205
            'course' => $this->course->id,
206
            'grade' => 100.0,
207
            'sumgrades' => 2,
208
            'canredoquestions' => 1,
209
            'preferredbehaviour' => 'immediatefeedback',
210
        ]);
211
        $quizobj = quiz_settings::create($quiz->id);
212
        quiz_add_quiz_question($q1->id, $quiz);
213
        $structure = $quizobj->get_structure();
214
        $structure->add_random_questions(0, 1, [
215
            'filter' => [
216
                'category' => [
217
                    'jointype' => condition::JOINTYPE_DEFAULT,
218
                    'values' => [$randomcat->id],
219
                    'filteroptions' => ['includesubcategories' => false],
220
                ],
221
            ],
222
        ]);
223
 
224
        // Student starts attempt.
225
        $quizobj = quiz_settings::create($quiz->id);
226
        $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
227
        $attemptobj = quiz_attempt::create($attempt->id);
228
 
229
        // Answer both questions.
230
        $postdata = $questiongenerator->get_simulated_post_data_for_questions_in_usage(
231
            $attemptobj->get_question_usage(),
232
            [1 => 'True', 2 => 'False'],
233
            true,
234
        );
235
        $attemptobj->process_submitted_actions(time(), false, $postdata);
236
 
237
        // Redo both questions - need to re-create attemptobj each time.
238
        $attemptobj = quiz_attempt::create($attempt->id);
239
        $attemptobj->process_redo_question(1, time());
240
        $attemptobj = quiz_attempt::create($attempt->id);
241
        $attemptobj->process_redo_question(2, time());
242
 
243
        // Edit both questions to make a second version.
244
        $questiongenerator->update_question($q1);
245
        $questiongenerator->update_question($q2);
246
 
247
        // Finally! call the method we want to test.
248
        $versioninfo = qbank_helper::get_version_information_for_questions_in_attempt(
249
            $attemptobj->get_attempt(), $attemptobj->get_context());
250
 
251
        // Verify - all questions should now want to be V2 for various reasons.
252
        $this->assertEquals(2, $versioninfo[1]->newversion);
253
        $this->assertEquals(2, $versioninfo[2]->newversion);
254
        $this->assertEquals(2, $versioninfo[3]->newversion);
255
        $this->assertEquals(2, $versioninfo[4]->newversion);
256
    }
257
}