forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_test.php
286 lines (236 loc) · 12.2 KB
/
progress_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_completion;
use completion_completion;
/**
* Test completion progress API.
*
* @package core_completion
* @category test
* @copyright 2017 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class progress_test extends \advanced_testcase {
/**
* Test setup.
*/
public function setUp(): void {
global $CFG;
$CFG->enablecompletion = true;
$this->resetAfterTest();
}
/**
* Tests that the course progress percentage is returned correctly when we have only activity completion.
*/
public function test_course_progress_percentage_with_just_activities() {
global $DB;
// Add a course that supports completion.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
// Enrol a user in the course.
$user = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Add four activities that use completion.
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
array('completion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
$this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
// Add an activity that does *not* use completion.
$this->getDataGenerator()->create_module('assign', array('course' => $course->id));
// Mark two of them as completed for a user.
$cmassign = get_coursemodule_from_id('assign', $assign->cmid);
$cmdata = get_coursemodule_from_id('data', $data->cmid);
$completion = new \completion_info($course);
$completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
$completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
// Check we have received valid data.
// Note - only 4 out of the 5 activities support completion, and the user has completed 2 of those.
$this->assertEquals('50', \core_completion\progress::get_course_progress_percentage($course, $user->id));
}
/**
* Tests that the course progress percentage is returned correctly when we have a course and activity completion.
*/
public function test_course_progress_percentage_with_activities_and_course() {
global $DB;
// Add a course that supports completion.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
// Enrol a user in the course.
$user = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Add four activities that use completion.
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
array('completion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
$this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
// Add an activity that does *not* use completion.
$this->getDataGenerator()->create_module('assign', array('course' => $course->id));
// Mark two of them as completed for a user.
$cmassign = get_coursemodule_from_id('assign', $assign->cmid);
$cmdata = get_coursemodule_from_id('data', $data->cmid);
$completion = new \completion_info($course);
$completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
$completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
// Now, mark the course as completed.
$ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
$ccompletion->mark_complete();
// Check we have received valid data.
// The course completion takes priority, so should return 100.
$this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
}
/**
* Tests that the course progress percentage is returned correctly for various grade to pass settings
*
* @covers \core_completion\progress::get_course_progress_percentage.
*/
public function test_course_progress_percentage_completion_state() {
global $DB, $CFG;
require_once("{$CFG->dirroot}/completion/criteria/completion_criteria_activity.php");
// Add a course that supports completion.
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
// Enrol a user in the course.
$teacher = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
// Add three activities that use completion.
/** @var \mod_assign_generator $assigngenerator */
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$assign['passgragepassed'] = $assigngenerator->create_instance([
'course' => $course->id,
'completion' => COMPLETION_ENABLED,
'completionusegrade' => 1,
'gradepass' => 50,
'completionpassgrade' => 1
]);
$assign['passgragefailed'] = $assigngenerator->create_instance([
'course' => $course->id,
'completion' => COMPLETION_ENABLED,
'completionusegrade' => 1,
'gradepass' => 50,
'completionpassgrade' => 1
]);
$assign['passgragenotused'] = $assigngenerator->create_instance([
'course' => $course->id,
'completion' => COMPLETION_ENABLED,
'completionusegrade' => 1,
'gradepass' => 50,
]);
$assign['nograde'] = $assigngenerator->create_instance([
'course' => $course->id,
'completion' => COMPLETION_ENABLED,
]);
$c = new \completion_info($course);
foreach ($assign as $item) {
$cmassing = get_coursemodule_from_id('assign', $item->cmid);
// Add activity completion criteria.
$criteriadata = new \stdClass();
$criteriadata->id = $course->id;
$criteriadata->criteria_activity = [];
// Some activities.
$criteriadata->criteria_activity[$cmassing->id] = 1;
$criterion = new \completion_criteria_activity();
$criterion->update_config($criteriadata);
}
$this->setUser($teacher);
foreach ($assign as $key => $item) {
$cm = get_coursemodule_from_instance('assign', $item->id);
// Mark user completions.
$completion = new \stdClass();
$completion->coursemoduleid = $cm->id;
$completion->timemodified = time();
$completion->viewed = COMPLETION_NOT_VIEWED;
$completion->overrideby = null;
if ($key == 'passgragepassed') {
$completion->id = 0;
$completion->completionstate = COMPLETION_COMPLETE_PASS;
$completion->userid = $user->id;
$c->internal_set_data($cm, $completion, true);
} else if ($key == 'passgragefailed') {
$completion->id = 0;
$completion->completionstate = COMPLETION_COMPLETE_FAIL;
$completion->userid = $user->id;
$c->internal_set_data($cm, $completion, true);
} else if ($key == 'passgragenotused') {
$completion->id = 0;
$completion->completionstate = COMPLETION_COMPLETE;
$completion->userid = $user->id;
$c->internal_set_data($cm, $completion, true);
} else if ($key == 'nograde') {
$completion->id = 0;
$completion->completionstate = COMPLETION_COMPLETE;
$completion->userid = $user->id;
$c->internal_set_data($cm, $completion, true);
}
}
// Run course completions cron.
\core_completion\api::mark_course_completions_activity_criteria();
// Check we have received valid data.
// Only assign2 is not completed.
$this->assertEquals('75', \core_completion\progress::get_course_progress_percentage($course, $user->id));
}
/**
* Tests that the course progress returns null when the course does not support it.
*/
public function test_course_progress_course_not_using_completion() {
// Create a course that does not use completion.
$course = $this->getDataGenerator()->create_course();
// Check that the result was null.
$this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
}
/**
* Tests that the course progress returns null when there are no activities that support it.
*/
public function test_course_progress_no_activities_using_completion() {
// Create a course that does support completion.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
// Add an activity that does *not* support completion.
$this->getDataGenerator()->create_module('assign', array('course' => $course->id));
// Check that the result was null.
$this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
}
/**
* Tests that the course progress returns null for a not tracked for completion user in a course.
*/
public function test_course_progress_not_tracked_user() {
global $DB;
// Add a course that supports completion.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
// Enrol a user in the course.
$user = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Now, mark the course as completed.
$ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
$ccompletion->mark_complete();
// The course completion should return 100.
$this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
// Now make the user's role to be not tracked for completion.
unassign_capability('moodle/course:isincompletionreports', $studentrole->id);
// Check that the result is null now.
$this->assertNull(\core_completion\progress::get_course_progress_percentage($course, $user->id));
}
}