MDL-63854 competencies, themes: misplaced dropdown arrows
[moodle.git] / course / tests / indicators_test.php
blob1749a13a576455bc4ba2e949d31a639bb94e1680
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_course indicators.
20 * @package core_course
21 * @category analytics
22 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once(__DIR__ . '/../../lib/completionlib.php');
30 require_once(__DIR__ . '/../../completion/criteria/completion_criteria_self.php');
32 /**
33 * Unit tests for core_course indicators.
35 * @package core_course
36 * @category analytics
37 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_course_indicators_testcase extends advanced_testcase {
42 /**
43 * test_no_teacher
45 * @return void
47 public function test_no_teacher() {
48 global $DB;
50 $this->resetAfterTest(true);
52 $course1 = $this->getDataGenerator()->create_course();
53 $course2 = $this->getDataGenerator()->create_course();
54 $coursecontext1 = \context_course::instance($course1->id);
55 $coursecontext2 = \context_course::instance($course2->id);
57 $user = $this->getDataGenerator()->create_user();
59 $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'student');
60 $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'teacher');
62 $indicator = new \core_course\analytics\indicator\no_teacher();
64 $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id);
65 $data = array(
66 $course1->id => array(
67 'context' => $coursecontext1,
68 'course' => $course1,
70 $course2->id => array(
71 'context' => $coursecontext2,
72 'course' => $course2,
73 ));
74 $indicator->add_sample_data($data);
76 list($values, $ignored) = $indicator->calculate($sampleids, 'course');
77 $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
78 $this->assertEquals($indicator::get_max_value(), $values[$course2->id][0]);
81 /**
82 * test_completion_enabled
84 * @return void
86 public function test_completion_enabled() {
87 global $DB;
89 $this->resetAfterTest(true);
91 $course1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 0));
92 $course2 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
93 $course3 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
95 // Criteria only for the last one.
96 $criteriadata = new stdClass();
97 $criteriadata->id = $course3->id;
98 $criteriadata->criteria_self = 1;
99 $criterion = new completion_criteria_self();
100 $criterion->update_config($criteriadata);
102 $indicator = new \core_course\analytics\indicator\completion_enabled();
104 $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id, $course3->id => $course3->id);
105 $data = array(
106 $course1->id => array(
107 'course' => $course1,
109 $course2->id => array(
110 'course' => $course2,
112 $course3->id => array(
113 'course' => $course3,
115 $indicator->add_sample_data($data);
117 // Calculate using course samples.
118 list($values, $ignored) = $indicator->calculate($sampleids, 'course');
119 $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
120 $this->assertEquals($indicator::get_min_value(), $values[$course2->id][0]);
121 $this->assertEquals($indicator::get_max_value(), $values[$course3->id][0]);
123 // Calculate using course_modules samples.
124 $indicator->clear_sample_data();
125 $data1 = $this->getDataGenerator()->create_module('data', array('course' => $course3->id),
126 array('completion' => 0));
127 $data2 = $this->getDataGenerator()->create_module('data', array('course' => $course3->id),
128 array('completion' => 1));
130 $sampleids = array($data1->cmid => $data1->cmid, $data2->cmid => $data2->cmid);
131 $cm1 = $DB->get_record('course_modules', array('id' => $data1->cmid));
132 $cm2 = $DB->get_record('course_modules', array('id' => $data2->cmid));
133 $data = array(
134 $cm1->id => array(
135 'course' => $course3,
136 'course_modules' => $cm1,
138 $cm2->id => array(
139 'course' => $course3,
140 'course_modules' => $cm2,
142 $indicator->add_sample_data($data);
144 list($values, $ignored) = $indicator->calculate($sampleids, 'course_modules');
145 $this->assertEquals($indicator::get_min_value(), $values[$cm1->id][0]);
146 $this->assertEquals($indicator::get_max_value(), $values[$cm2->id][0]);
150 * test_potential_cognitive
152 * @return void
154 public function test_potential_cognitive() {
155 global $DB;
157 $this->resetAfterTest(true);
159 $course1 = $this->getDataGenerator()->create_course();
161 $course2 = $this->getDataGenerator()->create_course();
162 $page = $this->getDataGenerator()->create_module('page', array('course' => $course2->id));
164 $course3 = $this->getDataGenerator()->create_course();
165 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id));
166 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course3->id));
168 $course4 = $this->getDataGenerator()->create_course();
169 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course4->id));
170 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course4->id));
172 $indicator = new \core_course\analytics\indicator\potential_cognitive_depth();
174 $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id, $course3->id => $course3->id,
175 $course4->id => $course4->id);
176 $data = array(
177 $course1->id => array(
178 'course' => $course1,
180 $course2->id => array(
181 'course' => $course2,
183 $course3->id => array(
184 'course' => $course3,
186 $course4->id => array(
187 'course' => $course4,
189 $indicator->add_sample_data($data);
191 list($values, $ignored) = $indicator->calculate($sampleids, 'course');
192 $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
194 // General explanation about the points, the max level is 5 so level 1 is -1, level 2 is -0.5, level 3 is 0,
195 // level 4 is 0.5 and level 5 is 1.
197 // Page cognitive is level 1 (the lower one).
198 $this->assertEquals($indicator::get_min_value(), $values[$course2->id][0]);
200 // The maximum cognitive depth level is 5, assign level is 5 therefore the potential cognitive depth is the max.
201 $this->assertEquals($indicator::get_max_value(), $values[$course3->id][0]);
203 // Forum level is 4.
204 $this->assertEquals(0.5, $values[$course4->id][0]);
206 // Calculate using course_modules samples.
207 $course5 = $this->getDataGenerator()->create_course();
208 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course5->id));
209 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course5->id));
211 $sampleids = array($assign->cmid => $assign->cmid, $forum->cmid => $forum->cmid);
212 $cm1 = $DB->get_record('course_modules', array('id' => $assign->cmid));
213 $cm2 = $DB->get_record('course_modules', array('id' => $forum->cmid));
214 $data = array(
215 $cm1->id => array(
216 'course' => $course5,
217 'course_modules' => $cm1,
219 $cm2->id => array(
220 'course' => $course5,
221 'course_modules' => $cm2,
223 $indicator->clear_sample_data();
224 $indicator->add_sample_data($data);
226 list($values, $ignored) = $indicator->calculate($sampleids, 'course_modules');
227 // Assign level is 5, the maximum level.
228 $this->assertEquals($indicator::get_max_value(), $values[$cm1->id][0]);
229 // Forum level is 4.
230 $this->assertEquals(0.5, $values[$cm2->id][0]);
235 * test_potential_social
237 * @return void
239 public function test_potential_social() {
240 global $DB;
242 $this->resetAfterTest(true);
244 $course1 = $this->getDataGenerator()->create_course();
246 $course2 = $this->getDataGenerator()->create_course();
247 $page = $this->getDataGenerator()->create_module('page', array('course' => $course2->id));
249 $course3 = $this->getDataGenerator()->create_course();
250 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id));
251 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course3->id));
253 $course4 = $this->getDataGenerator()->create_course();
254 $page = $this->getDataGenerator()->create_module('page', array('course' => $course4->id));
255 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course4->id));
257 $indicator = new \core_course\analytics\indicator\potential_social_breadth();
259 $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id, $course3->id => $course3->id,
260 $course4->id => $course4->id);
261 $data = array(
262 $course1->id => array(
263 'course' => $course1,
265 $course2->id => array(
266 'course' => $course2,
268 $course3->id => array(
269 'course' => $course3,
271 $course4->id => array(
272 'course' => $course4,
274 $indicator->add_sample_data($data);
276 list($values, $ignored) = $indicator->calculate($sampleids, 'course');
277 $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
279 // General explanation about the points, the max level is 2 so level 1 is -1, level 2 is 1.
281 // Page social is level 1 (the lower level).
282 $this->assertEquals($indicator::get_min_value(), $values[$course2->id][0]);
284 // Forum is level 2 and assign is level 2.
285 $this->assertEquals($indicator::get_max_value(), $values[$course3->id][0]);
287 // Page is level 1 and assign is level 2, so potential level is the max one.
288 $this->assertEquals($indicator::get_max_value(), $values[$course4->id][0]);
290 // Calculate using course_modules samples.
291 $course5 = $this->getDataGenerator()->create_course();
292 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course5->id));
293 $page = $this->getDataGenerator()->create_module('page', array('course' => $course5->id));
295 $sampleids = array($assign->cmid => $assign->cmid, $page->cmid => $page->cmid);
296 $cm1 = $DB->get_record('course_modules', array('id' => $assign->cmid));
297 $cm2 = $DB->get_record('course_modules', array('id' => $page->cmid));
298 $data = array(
299 $cm1->id => array(
300 'course' => $course5,
301 'course_modules' => $cm1,
303 $cm2->id => array(
304 'course' => $course5,
305 'course_modules' => $cm2,
307 $indicator->clear_sample_data();
308 $indicator->add_sample_data($data);
310 list($values, $ignored) = $indicator->calculate($sampleids, 'course_modules');
311 // Assign social is level 2 (the max level).
312 $this->assertEquals($indicator::get_max_value(), $values[$cm1->id][0]);
313 // Page social is level 1 (the lower level).
314 $this->assertEquals($indicator::get_min_value(), $values[$cm2->id][0]);