MDL-65176 analytics: target for students who don't get the minimum grade
[moodle.git] / lib / tests / targets_test.php
blob025073126bac753cd6330828b8671157a7130def
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.
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/>.
17 /**
18 * Unit tests for core targets.
20 * @package core
21 * @category analytics
22 * @copyright 2019 Victor Deniz <victor@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/completion/criteria/completion_criteria.php');
30 require_once($CFG->dirroot . '/completion/criteria/completion_criteria_activity.php');
31 require_once($CFG->dirroot . '/lib/grade/grade_item.php');
32 require_once($CFG->dirroot . '/lib/grade/grade_grade.php');
33 require_once($CFG->dirroot . '/lib/grade/grade_category.php');
34 require_once($CFG->dirroot . '/lib/grade/constants.php');
36 /**
37 * Unit tests for core targets.
39 * @package core
40 * @category analytics
41 * @copyright 2019 Victor Deniz <victor@moodle.com>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class core_analytics_targets_testcase extends advanced_testcase {
46 /**
47 * Provides course params for the {@link self::test_core_target_course_completion_analysable()} method.
49 * @return array
51 public function analysable_provider() {
53 $now = new DateTime("now", core_date::get_server_timezone_object());
54 $year = $now->format('Y');
55 $month = $now->format('m');
57 return [
58 'coursenotyetstarted' => [
59 'params' => [
60 'enablecompletion' => 1,
61 'startdate' => mktime(0, 0, 0, 10, 24, $year + 1)
63 'isvalid' => get_string('coursenotyetstarted')
65 'coursenostudents' => [
66 'params' => [
67 'enablecompletion' => 1,
68 'startdate' => mktime(0, 0, 0, 10, 24, $year - 2),
69 'enddate' => mktime(0, 0, 0, 10, 24, $year - 1)
71 'isvalid' => get_string('nocoursestudents')
73 'coursenosections' => [
74 'params' => [
75 'enablecompletion' => 1,
76 'format' => 'social',
77 'students' => true
79 'isvalid' => get_string('nocoursesections')
81 'coursenoendtime' => [
82 'params' => [
83 'enablecompletion' => 1,
84 'format' => 'topics',
85 'enddate' => 0,
86 'students' => true
88 'isvalid' => get_string('nocourseendtime')
90 'courseendbeforestart' => [
91 'params' => [
92 'enablecompletion' => 1,
93 'enddate' => mktime(0, 0, 0, 10, 23, $year - 2),
94 'students' => true
96 'isvalid' => get_string('errorendbeforestart', 'analytics')
98 'coursetoolong' => [
99 'params' => [
100 'enablecompletion' => 1,
101 'startdate' => mktime(0, 0, 0, 10, 24, $year - 2),
102 'enddate' => mktime(0, 0, 0, 10, 23, $year),
103 'students' => true
105 'isvalid' => get_string('coursetoolong', 'analytics')
107 'coursealreadyfinished' => [
108 'params' => [
109 'enablecompletion' => 1,
110 'startdate' => mktime(0, 0, 0, 10, 24, $year - 2),
111 'enddate' => mktime(0, 0, 0, 10, 23, $year - 1),
112 'students' => true
114 'isvalid' => get_string('coursealreadyfinished'),
115 'fortraining' => false
117 'coursenotyetfinished' => [
118 'params' => [
119 'enablecompletion' => 1,
120 'startdate' => mktime(0, 0, 0, $month - 1, 24, $year),
121 'enddate' => mktime(0, 0, 0, $month + 2, 23, $year),
122 'students' => true
124 'isvalid' => get_string('coursenotyetfinished')
126 'coursenocompletion' => [
127 'params' => [
128 'enablecompletion' => 0,
129 'startdate' => mktime(0, 0, 0, $month - 2, 24, $year),
130 'enddate' => mktime(0, 0, 0, $month - 1, 23, $year),
131 'students' => true
133 'isvalid' => get_string('completionnotenabledforcourse', 'completion')
139 * Provides enrolment params for the {@link self::test_core_target_course_completion_samples()} method.
141 * @return array
143 public function sample_provider() {
144 $now = time();
145 return [
146 'enrolmentendbeforecourse' => [
147 'coursestart' => $now,
148 'courseend' => $now + (WEEKSECS * 8),
149 'timestart' => $now,
150 'timeend' => $now - DAYSECS,
151 'isvalid' => false
153 'enrolmenttoolong' => [
154 'coursestart' => $now,
155 'courseend' => $now + (WEEKSECS * 8),
156 'timestart' => $now - (YEARSECS + (WEEKSECS * 8)),
157 'timeend' => $now + (WEEKSECS * 8),
158 'isvalid' => false
160 'enrolmentstartaftercourse' => [
161 'coursestart' => $now,
162 'courseend' => $now + (WEEKSECS * 8),
163 'timestart' => $now + (WEEKSECS * 9),
164 'timeend' => $now + (WEEKSECS * 10),
165 'isvalid' => false
171 * Test the conditions of a valid analysable, both common and specific to this target (course_completion).
173 * @dataProvider analysable_provider
174 * @param mixed $courseparams Course data
175 * @param true|string $isvalid True when analysable is valid, string when it is not
176 * @param boolean $fortraining True if the course is for training the model
178 public function test_core_target_course_completion_analysable($courseparams, $isvalid, $fortraining = true) {
179 global $DB;
181 $this->resetAfterTest(true);
183 try {
184 $course = $this->getDataGenerator()->create_course($courseparams);
185 } catch (moodle_exception $e) {
186 $course = $this->getDataGenerator()->create_course();
187 $courserecord = $courseparams;
188 $courserecord['id'] = $course->id;
189 unset($courserecord['students']);
191 $DB->update_record_raw('course', $courserecord);
192 $course = get_course($course->id);
194 $user = $this->getDataGenerator()->create_user();
196 if (!empty($courseparams['enablecompletion'])) {
197 $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'completion' => 1]);
198 $cm = get_coursemodule_from_id('assign', $assign->cmid);
200 $criteriadata = (object) [
201 'id' => $course->id,
202 'criteria_activity' => [
203 $cm->id => 1
206 $criterion = new completion_criteria_activity();
207 $criterion->update_config($criteriadata);
210 $target = new \core\analytics\target\course_completion();
212 // Test valid analysables.
214 if (!empty($courseparams['students'])) {
215 // Enroll user in course.
216 $this->getDataGenerator()->enrol_user($user->id, $course->id);
219 $analysable = new \core_analytics\course($course);
220 $this->assertEquals($isvalid, $target->is_valid_analysable($analysable, $fortraining));
224 * Test the conditions of a valid sample, both common and specific to this target (course_completion).
226 * @dataProvider sample_provider
227 * @param int $coursestart Course start date
228 * @param int $courseend Course end date
229 * @param int $timestart Enrol start date
230 * @param int $timeend Enrol end date
231 * @param boolean $isvalid True when sample is valid, false when it is not
233 public function test_core_target_course_completion_samples($coursestart, $courseend, $timestart, $timeend, $isvalid) {
235 $this->resetAfterTest(true);
237 $courserecord = new stdClass();
238 $courserecord->startdate = $coursestart;
239 $courserecord->enddate = $courseend;
241 $user = $this->getDataGenerator()->create_user();
242 $course = $this->getDataGenerator()->create_course($courserecord);
243 $this->getDataGenerator()->enrol_user($user->id, $course->id, null, 'manual', $timestart, $timeend);
245 $target = new \core\analytics\target\course_completion();
246 $analyser = new \core\analytics\analyser\student_enrolments(1, $target, [], [], []);
247 $analysable = new \core_analytics\course($course);
249 $class = new ReflectionClass('\core\analytics\analyser\student_enrolments');
250 $method = $class->getMethod('get_all_samples');
251 $method->setAccessible(true);
253 list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
254 $target->add_sample_data($samplesdata);
255 $sampleid = reset($sampleids);
257 $this->assertEquals($isvalid, $target->is_valid_sample($sampleid, $analysable));
261 * Setup user, framework, competencies and course competencies.
263 protected function setup_competencies_environment() {
264 $this->resetAfterTest(true);
265 $now = time();
266 $this->setAdminUser();
267 $dg = $this->getDataGenerator();
268 $lpg = $dg->get_plugin_generator('core_competency');
270 $course = $dg->create_course(array('startdate' => $now - WEEKSECS, 'enddate' => $now - DAYSECS));
271 $coursenocompetencies = $dg->create_course(array('startdate' => $now - WEEKSECS, 'enddate' => $now - DAYSECS));
273 $u1 = $dg->create_user();
274 $this->getDataGenerator()->enrol_user($u1->id, $course->id);
275 $this->getDataGenerator()->enrol_user($u1->id, $coursenocompetencies->id);
276 $f1 = $lpg->create_framework();
277 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
278 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
279 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
280 $c4 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
281 $cc1 = $lpg->create_course_competency(array('competencyid' => $c1->get('id'), 'courseid' => $course->id,
282 'ruleoutcome' => \core_competency\course_competency::OUTCOME_NONE));
283 $cc2 = $lpg->create_course_competency(array('competencyid' => $c2->get('id'), 'courseid' => $course->id,
284 'ruleoutcome' => \core_competency\course_competency::OUTCOME_EVIDENCE));
285 $cc3 = $lpg->create_course_competency(array('competencyid' => $c3->get('id'), 'courseid' => $course->id,
286 'ruleoutcome' => \core_competency\course_competency::OUTCOME_RECOMMEND));
287 $cc4 = $lpg->create_course_competency(array('competencyid' => $c4->get('id'), 'courseid' => $course->id,
288 'ruleoutcome' => \core_competency\course_competency::OUTCOME_COMPLETE));
290 return array(
291 'course' => $course,
292 'coursenocompetencies' => $coursenocompetencies,
293 'user' => $u1,
294 'course_competencies' => array($cc1, $cc2, $cc3, $cc4)
299 * Test the specific conditions of a valid analysable for the course_competencies target.
301 public function test_core_target_course_competencies_analysable() {
303 $data = $this->setup_competencies_environment();
305 $analysable = new \core_analytics\course($data['course']);
306 $target = new \core\analytics\target\course_competencies();
308 $this->assertTrue($target->is_valid_analysable($analysable));
310 $analysable = new \core_analytics\course($data['coursenocompetencies']);
311 $this->assertEquals(get_string('nocompetenciesincourse', 'tool_lp'), $target->is_valid_analysable($analysable));
315 * Test the target value calculation.
317 public function test_core_target_course_competencies_calculate() {
319 $data = $this->setup_competencies_environment();
321 $target = new \core\analytics\target\course_competencies();
322 $analyser = new \core\analytics\analyser\student_enrolments(1, $target, [], [], []);
323 $analysable = new \core_analytics\course($data['course']);
325 $class = new ReflectionClass('\core\analytics\analyser\student_enrolments');
326 $method = $class->getMethod('get_all_samples');
327 $method->setAccessible(true);
329 list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
330 $target->add_sample_data($samplesdata);
331 $sampleid = reset($sampleids);
333 $class = new ReflectionClass('\core\analytics\target\course_competencies');
334 $method = $class->getMethod('calculate_sample');
335 $method->setAccessible(true);
337 // Method calculate_sample() returns 1 when the user has not achieved all the competencies assigned to the course.
338 $this->assertEquals(1, $method->invoke($target, $sampleid, $analysable));
340 // Grading of all the competences assigned to the course, in such way that the user achieves them all.
341 foreach ($data['course_competencies'] as $competency) {
342 \core_competency\api::grade_competency_in_course($data['course']->id, $data['user']->id,
343 $competency->get('competencyid'), 3, 'Unit test');
345 // Method calculate_sample() returns 0 when the user has achieved all the competencies assigned to the course.
346 $this->assertEquals(0, $method->invoke($target, $sampleid, $analysable));
350 * Test the specific conditions of a valid analysable for the course_gradetopass target.
352 public function test_core_target_course_gradetopass_analysable() {
353 global $DB;
355 $this->resetAfterTest(true);
356 $now = time();
358 $dg = $this->getDataGenerator();
360 // Course without grade to pass set.
361 $course1 = $dg->create_course(array('startdate' => $now - WEEKSECS, 'enddate' => $now - DAYSECS));
362 $student1 = $dg->create_user();
363 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
364 $dg->enrol_user($student1->id, $course1->id, $studentrole->id);
366 $analysable = new \core_analytics\course($course1);
367 $target = new \core\analytics\target\course_gradetopass();
368 $this->assertEquals(get_string('gradetopassnotset', 'course'), $target->is_valid_analysable($analysable));
370 // Set grade to pass.
371 $courseitem = grade_item::fetch_course_item($course1->id);
372 $courseitem->gradepass = 50;
373 $DB->update_record('grade_items', $courseitem);
374 // Since the grade to pass value is cached in the target, a new one it is instanciated.
375 $target = new \core\analytics\target\course_gradetopass();
376 $this->assertTrue($target->is_valid_analysable($analysable));
381 * Test the target value calculation of the course_gradetopass target.
383 public function test_core_target_course_gradetopass_calculate() {
384 global $DB;
386 $this->resetAfterTest(true);
388 $dg = $this->getDataGenerator();
389 $course1 = $dg->create_course();
390 // Set grade to pass.
391 $student1 = $dg->create_user();
392 $student2 = $dg->create_user();
393 $student3 = $dg->create_user();
394 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
395 $dg->enrol_user($student1->id, $course1->id, $studentrole->id);
396 $dg->enrol_user($student2->id, $course1->id, $studentrole->id);
397 $dg->enrol_user($student3->id, $course1->id, $studentrole->id);
399 $courseitem = grade_item::fetch_course_item($course1->id);
400 // Student1 fails.
401 $courseitem->update_final_grade($student1->id, 30);
402 // Student2 pass.
403 $courseitem->update_final_grade($student2->id, 60);
404 // Student 3 has no grade.
406 $courseitem->gradepass = 50;
407 $DB->update_record('grade_items', $courseitem);
409 $target = new \core\analytics\target\course_gradetopass();
410 $analyser = new \core\analytics\analyser\student_enrolments(1, $target, [], [], []);
411 $analysable = new \core_analytics\course($course1);
413 $class = new ReflectionClass('\core\analytics\analyser\student_enrolments');
414 $method = $class->getMethod('get_all_samples');
415 $method->setAccessible(true);
417 list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
418 $target->add_sample_data($samplesdata);
420 // Users in array $sampleids are sorted by user id, so student1 is the first sample.
421 $sampleid = reset($sampleids);
423 $class = new ReflectionClass('\core\analytics\target\course_gradetopass');
424 $method = $class->getMethod('calculate_sample');
425 $method->setAccessible(true);
427 // Method calculate_sample() returns 1 when the user has not successfully graded to pass the course.
428 $this->assertEquals(1, $method->invoke($target, $sampleid, $analysable));
430 // Student2.
431 $sampleid = next($sampleids);
433 // Method calculate_sample() returns 0 when the user has successfully graded to pass the course.
434 $this->assertEquals(0, $method->invoke($target, $sampleid, $analysable));
436 // Student3.
437 $sampleid = next($sampleids);
439 // Method calculate_sample() returns 1 when the user has not been graded.
440 $this->assertEquals(1, $method->invoke($target, $sampleid, $analysable));