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 |
/**
|
|
|
18 |
* Trait for course completion creation in unit tests
|
|
|
19 |
*
|
|
|
20 |
* @package core_completion
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2018 Adrian Greeve <adriangreeve.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
global $CFG;
|
|
|
29 |
|
|
|
30 |
require_once($CFG->dirroot . '/completion/criteria/completion_criteria.php');
|
|
|
31 |
require_once($CFG->dirroot . '/completion/criteria/completion_criteria_activity.php');
|
|
|
32 |
require_once($CFG->dirroot . '/completion/criteria/completion_criteria_role.php');
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Trait for unit tests and completion.
|
|
|
36 |
*
|
|
|
37 |
* @copyright 2018 Adrian Greeve <adriangreeve.com>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
trait completion_creation {
|
|
|
41 |
|
|
|
42 |
/** @var stdClass The course object. */
|
|
|
43 |
public $course;
|
|
|
44 |
|
|
|
45 |
/** @var context The course context object. */
|
|
|
46 |
public $coursecontext;
|
|
|
47 |
|
|
|
48 |
/** @var stdClass The course module object */
|
|
|
49 |
public $cm;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Create completion information.
|
|
|
53 |
*/
|
|
|
54 |
public function create_course_completion() {
|
|
|
55 |
$this->resetAfterTest();
|
|
|
56 |
|
|
|
57 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
58 |
$coursecontext = context_course::instance($course->id);
|
|
|
59 |
|
|
|
60 |
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'completion' => 1]);
|
|
|
61 |
$modulecontext = context_module::instance($assign->cmid);
|
|
|
62 |
$cm = get_coursemodule_from_id('assign', $assign->cmid);
|
|
|
63 |
|
|
|
64 |
// Set completion rules.
|
|
|
65 |
$completion = new \completion_info($course);
|
|
|
66 |
|
|
|
67 |
$criteriadata = (object) [
|
|
|
68 |
'id' => $course->id,
|
|
|
69 |
'criteria_activity' => [
|
|
|
70 |
$cm->id => 1
|
|
|
71 |
]
|
|
|
72 |
];
|
|
|
73 |
$criterion = new \completion_criteria_activity();
|
|
|
74 |
$criterion->update_config($criteriadata);
|
|
|
75 |
|
|
|
76 |
$criteriadata = (object) [
|
|
|
77 |
'id' => $course->id,
|
|
|
78 |
'criteria_role' => [3 => 3]
|
|
|
79 |
];
|
|
|
80 |
$criterion = new \completion_criteria_role();
|
|
|
81 |
$criterion->update_config($criteriadata);
|
|
|
82 |
|
|
|
83 |
// Handle overall aggregation.
|
|
|
84 |
$aggdata = array(
|
|
|
85 |
'course' => $course->id,
|
|
|
86 |
'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY
|
|
|
87 |
);
|
|
|
88 |
$aggregation = new \completion_aggregation($aggdata);
|
|
|
89 |
$aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
|
|
|
90 |
$aggregation->save();
|
|
|
91 |
$aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
|
|
|
92 |
$aggregation = new \completion_aggregation($aggdata);
|
|
|
93 |
$aggregation->setMethod(COMPLETION_AGGREGATION_ANY);
|
|
|
94 |
$aggregation->save();
|
|
|
95 |
|
|
|
96 |
// Set variables for access in tests.
|
|
|
97 |
$this->course = $course;
|
|
|
98 |
$this->coursecontext = $coursecontext;
|
|
|
99 |
$this->cm = $cm;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Complete some of the course completion criteria.
|
|
|
104 |
*
|
|
|
105 |
* @param stdClass $user The user object
|
|
|
106 |
* @param bool $modulecompletion If true will complete the activity module completion thing.
|
|
|
107 |
*/
|
|
|
108 |
public function complete_course($user, $modulecompletion = true) {
|
|
|
109 |
$this->getDataGenerator()->enrol_user($user->id, $this->course->id, 'student');
|
|
|
110 |
$completion = new \completion_info($this->course);
|
|
|
111 |
$criteriacompletions = $completion->get_completions($user->id, COMPLETION_CRITERIA_TYPE_ROLE);
|
|
|
112 |
$criteria = completion_criteria::factory(['id' => 3, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ROLE]);
|
|
|
113 |
foreach ($criteriacompletions as $ccompletion) {
|
|
|
114 |
$criteria->complete($ccompletion);
|
|
|
115 |
}
|
|
|
116 |
if ($modulecompletion) {
|
|
|
117 |
// Set activity as complete.
|
|
|
118 |
$completion->update_state($this->cm, COMPLETION_COMPLETE, $user->id);
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|