Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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_workshop\courseformat;
18
 
19
use core_courseformat\local\overview\overviewfactory;
20
 
21
/**
22
 * Tests for Workshop overview integration.
23
 *
24
 * @covers \mod_workshop\course\overview
25
 * @package    mod_workshop
26
 * @category   test
27
 * @copyright  2025 Ferran Recio <ferran@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
final class overview_test extends \advanced_testcase {
31
    #[\Override]
32
    public static function setUpBeforeClass(): void {
33
        global $CFG;
34
        require_once($CFG->dirroot . '/mod/workshop/locallib.php');
35
        parent::setUpBeforeClass();
36
    }
37
 
38
    /**
39
     * Test get_grade_item_names method.
40
     *
41
     * @dataProvider data_provider_get_grade_item_names
42
     * @covers ::get_grade_item_names
43
     * @param string $user
44
     * @param bool $expectempty
45
     * @param bool $hassubmission
46
     * @param bool $hasassesment
47
     */
48
    public function test_get_grade_item_names(
49
        string $user,
50
        bool $expectempty,
51
        bool $hassubmission,
52
        bool $hasassesment,
53
    ): void {
54
        $this->resetAfterTest();
55
        $this->setAdminUser();
56
 
57
        $course = $this->getDataGenerator()->create_course();
58
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
59
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
60
 
61
        $activity = $this->getDataGenerator()->create_module(
62
            'workshop',
63
            ['course' => $course->id],
64
        );
65
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
66
 
67
        // Set up a generator to create content.
68
        /** @var \mod_workshop_generator $generator */
69
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
70
 
71
        // Add grading.
72
        $workshopitems = array_values(
73
            \grade_item::fetch_all([
74
                'itemtype' => 'mod',
75
                'itemmodule' => 'workshop',
76
                'iteminstance' => (int) $activity->id,
77
                'courseid' => $course->id,
78
                ['grade' => 100.0],
79
            ])
80
        );
81
 
82
        // Workshop stores in number 1 the assessment grade and in number 0 the submission grade.
83
        $gradeitems = [];
84
        foreach ($workshopitems as $workshopitem) {
85
            if ($workshopitem->itemnumber == 0) {
86
                $gradeitems['submission'] = $workshopitem;
87
            } else {
88
                $gradeitems['assessment'] = $workshopitem;
89
            }
90
        }
91
 
92
        $expectedsubmissions = '-';
93
        if ($hassubmission) {
94
            $submissionid = $generator->create_submission(
95
                $activity->id,
96
                $student->id,
97
                ['title' => 'My custom title', 'grade' => 85.00000],
98
            );
99
            $gradegrade = new \grade_grade();
100
            $gradegrade->itemid = $gradeitems['submission']->id;
101
            $gradegrade->userid = (int) $student->id;
102
            $gradegrade->rawgrade = 77;
103
            $gradegrade->finalgrade = 77;
104
            $gradegrade->insert();
105
            $expectedsubmissions = '77.00000';
106
        }
107
 
108
        $expectedassessments = '-';
109
        if ($hasassesment) {
110
            $generator->create_assessment(
111
                $submissionid,
112
                $student->id,
113
                ['weight' => 3, 'grade' => 95.00000],
114
            );
115
            $gradegrade = new \grade_grade();
116
            $gradegrade->itemid = $gradeitems['assessment']->id;
117
            $gradegrade->userid = (int) $student->id;
118
            $gradegrade->rawgrade = 88;
119
            $gradegrade->finalgrade = 88;
120
            $gradegrade->insert();
121
            $expectedassessments = '88.00000';
122
        }
123
 
124
        $currentuser = ($user == 'teacher') ? $teacher : $student;
125
        $this->setUser($currentuser);
126
 
127
        $items = overviewfactory::create($cm)->get_grades_overviews();
128
 
129
        // Students should not see item.
130
        if ($expectempty) {
131
            $this->assertEmpty($items);
132
            return;
133
        }
134
 
135
        $this->assertEquals(get_string('overview_submission_grade', 'mod_workshop'), $items[0]->get_name());
136
        $this->assertEquals($expectedsubmissions, $items[0]->get_value());
137
 
138
        $this->assertEquals(get_string('overview_assessment_grade', 'mod_workshop'), $items[1]->get_name());
139
        $this->assertEquals($expectedassessments, $items[1]->get_value());
140
    }
141
 
142
    /**
143
     * Data provider for test_get_grade_item_names.
144
     *
145
     * @return array
146
     */
147
    public static function data_provider_get_grade_item_names(): array {
148
        return [
149
            'student with submissions' => [
150
                'user' => 'student',
151
                'expectempty' => false,
152
                'hassubmission' => true,
153
                'hasassesment' => false,
154
            ],
155
            'student with assessments' => [
156
                'user' => 'student',
157
                'expectempty' => false,
158
                'hassubmission' => true,
159
                'hasassesment' => true,
160
            ],
161
            'teacher' => [
162
                'user' => 'teacher',
163
                'expectempty' => true,
164
                'hassubmission' => false,
165
                'hasassesment' => false,
166
            ],
167
        ];
168
    }
169
 
170
    /**
171
     * Test get_extra_phase_overview method.
172
     *
173
     * @covers ::get_extra_phase_overview
174
     * @dataProvider data_provider_get_extra_phase_overview
175
     * @param string $user
176
     * @param int $currentphase
177
     */
178
    public function test_get_extra_phase_overview(string $user, int $currentphase): void {
179
        $this->resetAfterTest();
180
        $this->setAdminUser();
181
 
182
        $course = $this->getDataGenerator()->create_course();
183
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
184
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
185
 
186
        $activity = $this->getDataGenerator()->create_module(
187
            'workshop',
188
            ['course' => $course->id],
189
        );
190
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
191
 
192
        $manager = new \workshop($activity, $cm, $course, $cm->context);
193
        $manager->switch_phase($currentphase);
194
 
195
        $currentuser = ($user == 'teacher') ? $teacher : $student;
196
        $this->setUser($currentuser);
197
 
198
        $overview = overviewfactory::create($cm);
199
        $reflection = new \ReflectionClass($overview);
200
        $method = $reflection->getMethod('get_extra_phase_overview');
201
        $method->setAccessible(true);
202
        $item = $method->invoke($overview);
203
 
204
        $this->assertEquals(get_string('phase', 'mod_workshop'), $item->get_name());
205
        $this->assertEquals($currentphase, $item->get_value());
206
    }
207
 
208
    /**
209
     * Data provider for test_get_extra_phase_overview.
210
     *
211
     * @return array
212
     */
213
    public static function data_provider_get_extra_phase_overview(): array {
214
        return [
215
            'teacher setup phase' => [
216
                'user' => 'teacher',
217
                'currentphase' => \workshop::PHASE_SETUP,
218
            ],
219
            'student setup phase' => [
220
                'user' => 'student',
221
                'currentphase' => \workshop::PHASE_SETUP,
222
            ],
223
            'teacher submission phase' => [
224
                'user' => 'teacher',
225
                'currentphase' => \workshop::PHASE_SUBMISSION,
226
            ],
227
            'student submission phase' => [
228
                'user' => 'student',
229
                'currentphase' => \workshop::PHASE_SUBMISSION,
230
            ],
231
            'teacher assessment phase' => [
232
                'user' => 'teacher',
233
                'currentphase' => \workshop::PHASE_ASSESSMENT,
234
            ],
235
            'student assessment phase' => [
236
                'user' => 'student',
237
                'currentphase' => \workshop::PHASE_ASSESSMENT,
238
            ],
239
            'teacher evaluation phase' => [
240
                'user' => 'teacher',
241
                'currentphase' => \workshop::PHASE_EVALUATION,
242
            ],
243
            'student evaluation phase' => [
244
                'user' => 'student',
245
                'currentphase' => \workshop::PHASE_EVALUATION,
246
            ],
247
            'teacher closed phase' => [
248
                'user' => 'teacher',
249
                'currentphase' => \workshop::PHASE_CLOSED,
250
            ],
251
            'student closed phase' => [
252
                'user' => 'student',
253
                'currentphase' => \workshop::PHASE_CLOSED,
254
            ],
255
        ];
256
    }
257
 
258
    /**
259
     * Test get_extra_deadline_overview method.
260
     *
261
     * @covers ::get_extra_deadline_overview
262
     * @dataProvider data_provider_get_extra_deadline_overview
263
     * @param string $user
264
     * @param int $currentphase
265
     * @param int $submissionend
266
     * @param int $assessmentend
267
     * @param int|null $expectedincrement null if the item should be null.
268
     */
269
    public function test_get_extra_deadline_overview(
270
        string $user,
271
        int $currentphase,
272
        int $submissionend,
273
        int $assessmentend,
274
        ?int $expectedincrement,
275
    ): void {
276
        $this->resetAfterTest();
277
        $this->setAdminUser();
278
 
279
        $course = $this->getDataGenerator()->create_course();
280
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
281
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
282
 
283
        $current = $this->mock_clock_with_frozen()->time();
284
 
285
        $activity = $this->getDataGenerator()->create_module(
286
            'workshop',
287
            [
288
                'course' => $course->id,
289
                'submissionend' => $current + $submissionend,
290
                'assessmentend' => $current + $assessmentend,
291
            ],
292
        );
293
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
294
 
295
        $manager = new \workshop($activity, $cm, $course, $cm->context);
296
        $manager->switch_phase($currentphase);
297
 
298
        $currentuser = ($user == 'teacher') ? $teacher : $student;
299
        $this->setUser($currentuser);
300
 
301
        $overview = overviewfactory::create($cm);
302
        $reflection = new \ReflectionClass($overview);
303
        $method = $reflection->getMethod('get_extra_deadline_overview');
304
        $method->setAccessible(true);
305
        $item = $method->invoke($overview);
306
 
307
        $this->assertEquals(get_string('deadline', 'mod_workshop'), $item->get_name());
308
 
309
        if ($expectedincrement === null) {
310
            $this->assertNull($item->get_value());
311
            return;
312
        }
313
 
314
        $this->assertEquals($current + $expectedincrement, $item->get_value());
315
    }
316
 
317
    /**
318
     * Data provider for test_get_extra_phase_overview.
319
     *
320
     * @return array
321
     */
322
    public static function data_provider_get_extra_deadline_overview(): array {
323
        $submissionend = 3600;
324
        $assessmentend = 7200;
325
        return [
326
            'teacher setup phase' => [
327
                'user' => 'teacher',
328
                'currentphase' => \workshop::PHASE_SETUP,
329
                'submissionend' => $submissionend,
330
                'assessmentend' => $assessmentend,
331
                'expectedincrement' => null,
332
            ],
333
            'student setup phase' => [
334
                'user' => 'student',
335
                'currentphase' => \workshop::PHASE_SETUP,
336
                'submissionend' => $submissionend,
337
                'assessmentend' => $assessmentend,
338
                'expectedincrement' => null,
339
            ],
340
            'teacher submission phase' => [
341
                'user' => 'teacher',
342
                'currentphase' => \workshop::PHASE_SUBMISSION,
343
                'submissionend' => $submissionend,
344
                'assessmentend' => $assessmentend,
345
                'expectedincrement' => $submissionend,
346
            ],
347
            'student submission phase' => [
348
                'user' => 'student',
349
                'currentphase' => \workshop::PHASE_SUBMISSION,
350
                'submissionend' => $submissionend,
351
                'assessmentend' => $assessmentend,
352
                'expectedincrement' => $submissionend,
353
            ],
354
            'teacher assessment phase' => [
355
                'user' => 'teacher',
356
                'currentphase' => \workshop::PHASE_ASSESSMENT,
357
                'submissionend' => $submissionend,
358
                'assessmentend' => $assessmentend,
359
                'expectedincrement' => $assessmentend,
360
            ],
361
            'student assessment phase' => [
362
                'user' => 'student',
363
                'currentphase' => \workshop::PHASE_ASSESSMENT,
364
                'submissionend' => $submissionend,
365
                'assessmentend' => $assessmentend,
366
                'expectedincrement' => $assessmentend,
367
            ],
368
            'teacher evaluation phase' => [
369
                'user' => 'teacher',
370
                'currentphase' => \workshop::PHASE_EVALUATION,
371
                'submissionend' => $submissionend,
372
                'assessmentend' => $assessmentend,
373
                'expectedincrement' => null,
374
            ],
375
            'student evaluation phase' => [
376
                'user' => 'student',
377
                'currentphase' => \workshop::PHASE_EVALUATION,
378
                'submissionend' => $submissionend,
379
                'assessmentend' => $assessmentend,
380
                'expectedincrement' => null,
381
            ],
382
            'teacher closed phase' => [
383
                'user' => 'teacher',
384
                'currentphase' => \workshop::PHASE_CLOSED,
385
                'submissionend' => $submissionend,
386
                'assessmentend' => $assessmentend,
387
                'expectedincrement' => null,
388
            ],
389
            'student closed phase' => [
390
                'user' => 'student',
391
                'currentphase' => \workshop::PHASE_CLOSED,
392
                'submissionend' => $submissionend,
393
                'assessmentend' => $assessmentend,
394
                'expectedincrement' => null,
395
            ],
396
        ];
397
    }
398
 
399
    /**
400
     * Test get_extra_submissions_overview and get_extra_assessments_overview methods.
401
     *
402
     * @covers ::get_extra_submissions_overview
403
     * @covers ::get_extra_assessments_overview
404
     * @dataProvider data_provider_get_extra_submissions_overview
405
     * @param string $user
406
     * @param int $currentphase
407
     * @param bool $hasstudentactivity
408
     * @param bool $expectnull
409
     */
410
    public function test_get_extra_submissions_overview(
411
        string $user,
412
        int $currentphase,
413
        bool $hasstudentactivity,
414
        bool $expectnull,
415
    ): void {
416
        $this->resetAfterTest();
417
        $this->setAdminUser();
418
 
419
        $course = $this->getDataGenerator()->create_course();
420
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
421
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
422
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
423
 
424
        $activity = $this->getDataGenerator()->create_module(
425
            'workshop',
426
            ['course' => $course->id],
427
        );
428
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
429
 
430
        // Set up a generator to create content.
431
        /** @var \mod_workshop_generator $generator */
432
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
433
 
434
        // Add grading.
435
        $workshopitems = array_values(
436
            \grade_item::fetch_all([
437
                'itemtype' => 'mod',
438
                'itemmodule' => 'workshop',
439
                'iteminstance' => (int) $activity->id,
440
                'courseid' => $course->id,
441
                ['grade' => 100.0],
442
            ])
443
        );
444
 
445
        // Worksop stores un number 1 the assessment grade and in number 0 the submission grade.
446
        foreach ($workshopitems as $workshopitem) {
447
            if ($workshopitem->itemnumber == 0) {
448
                $gradeitems['submission'] = $workshopitem;
449
            } else {
450
                $gradeitems['assessment'] = $workshopitem;
451
            }
452
        }
453
 
454
        if ($hasstudentactivity) {
455
            // Create some submissions.
456
            $submissionid = $generator->create_submission(
457
                $activity->id,
458
                $student->id,
459
                ['title' => 'My custom title', 'grade' => 85.00000],
460
            );
461
            $generator->create_submission(
462
                $activity->id,
463
                $student2->id,
464
                ['title' => 'My custom title', 'grade' => 65.00000],
465
            );
466
            // Assess one submission.
467
            $generator->create_assessment(
468
                $submissionid,
469
                $student->id,
470
                ['weight' => 3, 'grade' => 95.00000],
471
            );
472
        }
473
 
474
        $manager = new \workshop($activity, $cm, $course, $cm->context);
475
        $manager->switch_phase($currentphase);
476
 
477
        $currentuser = ($user == 'teacher') ? $teacher : $student;
478
        $this->setUser($currentuser);
479
 
480
        $overview = overviewfactory::create($cm);
481
        $reflection = new \ReflectionClass($overview);
482
 
483
        $method = $reflection->getMethod('get_extra_submissions_overview');
484
        $method->setAccessible(true);
485
        $itemsubmissions = $method->invoke($overview);
486
 
487
        $method = $reflection->getMethod('get_extra_assessments_overview');
488
        $method->setAccessible(true);
489
        $itemassessment = $method->invoke($overview);
490
 
491
        if ($expectnull) {
492
            $this->assertNull($itemsubmissions);
493
            $this->assertNull($itemassessment);
494
            return;
495
        }
496
 
497
        $this->assertEquals(get_string('submissions', 'mod_workshop'), $itemsubmissions->get_name());
498
        $expected = ($hasstudentactivity) ? 2 : 0;
499
        $this->assertEquals($expected, $itemsubmissions->get_value());
500
 
501
        $this->assertEquals(get_string('assessments', 'mod_workshop'), $itemassessment->get_name());
502
        $expected = ($hasstudentactivity) ? 1 : 0;
503
        $this->assertEquals($expected, $itemassessment->get_value());
504
    }
505
 
506
    /**
507
     * Data provider for test_get_extra_submissions_overview.
508
     *
509
     * @return array
510
     */
511
    public static function data_provider_get_extra_submissions_overview(): array {
512
        return [
513
            'teacher setup phase without activity' => [
514
                'user' => 'teacher',
515
                'currentphase' => \workshop::PHASE_SETUP,
516
                'hasstudentactivity' => false,
517
                'expectnull' => false,
518
            ],
519
            'student setup phase without activity' => [
520
                'user' => 'student',
521
                'currentphase' => \workshop::PHASE_SETUP,
522
                'hasstudentactivity' => false,
523
                'expectnull' => true,
524
            ],
525
            'teacher submission phase without activity' => [
526
                'user' => 'teacher',
527
                'currentphase' => \workshop::PHASE_SUBMISSION,
528
                'hasstudentactivity' => false,
529
                'expectnull' => false,
530
            ],
531
            'student submission phase without activity' => [
532
                'user' => 'student',
533
                'currentphase' => \workshop::PHASE_SUBMISSION,
534
                'hasstudentactivity' => false,
535
                'expectnull' => true,
536
            ],
537
            'teacher assessment phase without activity' => [
538
                'user' => 'teacher',
539
                'currentphase' => \workshop::PHASE_ASSESSMENT,
540
                'hasstudentactivity' => false,
541
                'expectnull' => false,
542
            ],
543
            'student assessment phase without activity' => [
544
                'user' => 'student',
545
                'currentphase' => \workshop::PHASE_ASSESSMENT,
546
                'hasstudentactivity' => false,
547
                'expectnull' => true,
548
            ],
549
            'teacher evaluation phase without activity' => [
550
                'user' => 'teacher',
551
                'currentphase' => \workshop::PHASE_EVALUATION,
552
                'hasstudentactivity' => false,
553
                'expectnull' => false,
554
            ],
555
            'student evaluation phase without activity' => [
556
                'user' => 'student',
557
                'currentphase' => \workshop::PHASE_EVALUATION,
558
                'hasstudentactivity' => false,
559
                'expectnull' => true,
560
            ],
561
            'teacher closed phase without activity' => [
562
                'user' => 'teacher',
563
                'currentphase' => \workshop::PHASE_CLOSED,
564
                'hasstudentactivity' => false,
565
                'expectnull' => false,
566
            ],
567
            'student closed phase without activity' => [
568
                'user' => 'student',
569
                'currentphase' => \workshop::PHASE_CLOSED,
570
                'hasstudentactivity' => false,
571
                'expectnull' => true,
572
            ],
573
            // Tests with assessments.
574
            'teacher setup phase with activity' => [
575
                'user' => 'teacher',
576
                'currentphase' => \workshop::PHASE_SETUP,
577
                'hasstudentactivity' => true,
578
                'expectnull' => false,
579
            ],
580
            'student setup phase with activity' => [
581
                'user' => 'student',
582
                'currentphase' => \workshop::PHASE_SETUP,
583
                'hasstudentactivity' => true,
584
                'expectnull' => true,
585
            ],
586
            'teacher submission phase with activity' => [
587
                'user' => 'teacher',
588
                'currentphase' => \workshop::PHASE_SUBMISSION,
589
                'hasstudentactivity' => true,
590
                'expectnull' => false,
591
            ],
592
            'student submission phase with activity' => [
593
                'user' => 'student',
594
                'currentphase' => \workshop::PHASE_SUBMISSION,
595
                'hasstudentactivity' => true,
596
                'expectnull' => true,
597
            ],
598
            'teacher assessment phase with activity' => [
599
                'user' => 'teacher',
600
                'currentphase' => \workshop::PHASE_ASSESSMENT,
601
                'hasstudentactivity' => true,
602
                'expectnull' => false,
603
            ],
604
            'student assessment phase with activity' => [
605
                'user' => 'student',
606
                'currentphase' => \workshop::PHASE_ASSESSMENT,
607
                'hasstudentactivity' => true,
608
                'expectnull' => true,
609
            ],
610
            'teacher evaluation phase with activity' => [
611
                'user' => 'teacher',
612
                'currentphase' => \workshop::PHASE_EVALUATION,
613
                'hasstudentactivity' => true,
614
                'expectnull' => false,
615
            ],
616
            'student evaluation phase with activity' => [
617
                'user' => 'student',
618
                'currentphase' => \workshop::PHASE_EVALUATION,
619
                'hasstudentactivity' => true,
620
                'expectnull' => true,
621
            ],
622
            'teacher closed phase with activity' => [
623
                'user' => 'teacher',
624
                'currentphase' => \workshop::PHASE_CLOSED,
625
                'hasstudentactivity' => true,
626
                'expectnull' => false,
627
            ],
628
            'student closed phase with activity' => [
629
                'user' => 'student',
630
                'currentphase' => \workshop::PHASE_CLOSED,
631
                'hasstudentactivity' => true,
632
                'expectnull' => true,
633
            ],
634
        ];
635
    }
636
}