|
| 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_assign; |
| 18 | + |
| 19 | +use mod_assign_test_generator; |
| 20 | + |
| 21 | +defined('MOODLE_INTERNAL') || die(); |
| 22 | + |
| 23 | +global $CFG; |
| 24 | +require_once(__DIR__ . '/../locallib.php'); |
| 25 | +require_once($CFG->dirroot . '/mod/assign/tests/generator.php'); |
| 26 | + |
| 27 | +/** |
| 28 | + * Unit tests for revoking attempts in mod/assign/locallib.php. |
| 29 | + * |
| 30 | + * @package mod_assign |
| 31 | + * @category test |
| 32 | + * @copyright 2024 Adrian Greeve |
| 33 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 34 | + */ |
| 35 | +class revokeattempt_test extends \advanced_testcase { |
| 36 | + use mod_assign_test_generator; |
| 37 | + |
| 38 | + protected function revoke_attempt_dataprovider() { |
| 39 | + return [ |
| 40 | + // Attempt single submission no additional grading, no feedback. |
| 41 | + 'Single attempt' => [ |
| 42 | + 'type' => 'single', |
| 43 | + 'grade' => false, |
| 44 | + 'feedback' => false, |
| 45 | + 'activitycompletion' => 'none' |
| 46 | + ], |
| 47 | + // Attempt single submission, additional grade, no feedback. |
| 48 | + 'Single attempt with grade' => [ |
| 49 | + 'type' => 'single', |
| 50 | + 'grade' => true, |
| 51 | + 'feedback' => false, |
| 52 | + 'activitycompletion' => 'none' |
| 53 | + ], |
| 54 | + // Attempt single submission, additional grade, feedback. |
| 55 | + 'Single attempt with grade and feedback' => [ |
| 56 | + 'type' => 'single', |
| 57 | + 'grade' => true, |
| 58 | + 'feedback' => true, |
| 59 | + 'activitycompletion' => 'none' |
| 60 | + ], |
| 61 | + // Attempt single submission no additional grading, no feedback, activity was complete, now is not. |
| 62 | + 'Single attempt with past completed activity' => [ |
| 63 | + 'type' => 'single', |
| 64 | + 'grade' => false, |
| 65 | + 'feedback' => false, |
| 66 | + 'activitycompletion' => 'past' |
| 67 | + ], |
| 68 | + // Attempt single submission, additional grade, no feedback, activity was not complete, now is. |
| 69 | + 'Single attempt with future completed activity' => [ |
| 70 | + 'type' => 'single', |
| 71 | + 'grade' => true, |
| 72 | + 'feedback' => false, |
| 73 | + 'activitycompletion' => 'future' |
| 74 | + ], |
| 75 | + // Group attempt, no additional grading, no feedback. |
| 76 | + 'Group attempt' => [ |
| 77 | + 'type' => 'group', |
| 78 | + 'grade' => false, |
| 79 | + 'feedback' => false, |
| 80 | + 'activitycompletion' => 'none' |
| 81 | + ], |
| 82 | + // Group attempt, additional grade, no feedback. |
| 83 | + 'Group attempt with grade' => [ |
| 84 | + 'type' => 'group', |
| 85 | + 'grade' => true, |
| 86 | + 'feedback' => false, |
| 87 | + 'activitycompletion' => 'none' |
| 88 | + ], |
| 89 | + // Group attempt, additional grade, feedback. |
| 90 | + 'Group attempt with grade and feedback' => [ |
| 91 | + 'type' => 'group', |
| 92 | + 'grade' => true, |
| 93 | + 'feedback' => true, |
| 94 | + 'activitycompletion' => 'none' |
| 95 | + ], |
| 96 | + // Group attempt, no additional grading, no feedback, activity was complete, now is not. |
| 97 | + 'Group attempt with past completed activity' => [ |
| 98 | + 'type' => 'group', |
| 99 | + 'grade' => false, |
| 100 | + 'feedback' => false, |
| 101 | + 'activitycompletion' => 'past' |
| 102 | + ], |
| 103 | + // Group attempt, additional grade, no feedback, activity was not complete, now is. |
| 104 | + 'Group attempt with future completed activity' => [ |
| 105 | + 'type' => 'group', |
| 106 | + 'grade' => true, |
| 107 | + 'feedback' => false, |
| 108 | + 'activitycompletion' => 'future' |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Test revoking an assignment attempt |
| 115 | + * |
| 116 | + * @dataProvider revoke_attempt_dataprovider |
| 117 | + */ |
| 118 | + public function test_revoke_attempt($type, $grade, $feedback, $activitycompletion) { |
| 119 | + $this->resetAfterTest(); |
| 120 | + |
| 121 | + $generator = $this->getDataGenerator(); |
| 122 | + $course = $generator->create_course(); |
| 123 | + // user creation to be moved. |
| 124 | + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); |
| 125 | + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); |
| 126 | + |
| 127 | + $assign = $this->create_assignment($generator, $type, $course); |
| 128 | + // Add a submission. |
| 129 | + $this->add_submission($student, $assign, $onlinetext = 'First attempt submission'); |
| 130 | + $this->submit_for_grading($student, $assign); |
| 131 | + |
| 132 | + $teacher->ignoresesskey = true; |
| 133 | + $this->setUser($teacher); |
| 134 | + |
| 135 | + $initialfeedback = 'Feedback for first attempt'; |
| 136 | + $data = [ |
| 137 | + 'assignfeedbackcomments_editor' => ['text' => $initialfeedback, 'format' => 1] |
| 138 | + ]; |
| 139 | + $this->mark_submission($teacher, $assign, $student, 45.0, $data); |
| 140 | + |
| 141 | + $assign->testable_process_add_attempt($student->id); |
| 142 | + if ($grade) { |
| 143 | + if ($feedback) { |
| 144 | + $data = [ |
| 145 | + 'assignfeedbackcomments_editor' => ['text' => 'Feedback!', 'format' => 1] |
| 146 | + ]; |
| 147 | + } else { |
| 148 | + $data = [ |
| 149 | + 'assignfeedbackcomments_editor' => ['text' => '', 'format' => 1] |
| 150 | + ]; |
| 151 | + } |
| 152 | + $this->mark_submission($teacher, $assign, $student, 51.0, $data, 1); |
| 153 | + $gradeinfo = $assign->get_user_grade($student->id, true); |
| 154 | + $this->assertEquals(51.0, $gradeinfo->grade); |
| 155 | + } |
| 156 | + |
| 157 | + $assign->revoke_attempt($student->id); |
| 158 | + $gradeinfo = $assign->get_user_grade($student->id, true); |
| 159 | + $this->assertEquals(45.0, $gradeinfo->grade); |
| 160 | + } |
| 161 | + |
| 162 | + protected function create_assignment($generator, $type, $course) { |
| 163 | + $data = [ |
| 164 | + 'assignsubmission_onlinetext_enabled' => 1, |
| 165 | + 'assignfeedback_comments_enabled' => 1, |
| 166 | + ]; |
| 167 | + |
| 168 | + $assign = $this->create_instance($course, $data); |
| 169 | + return $assign; |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments