Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
namespace core_course\analytics;
18
 
1441 ariadna 19
use core_analytics\tests\mlbackend_helper_trait;
20
 
1 efrain 21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once(__DIR__ . '/../../../lib/completionlib.php');
25
require_once(__DIR__ . '/../../../completion/criteria/completion_criteria_self.php');
26
require_once(__DIR__ . '/../../../analytics/tests/fixtures/test_target_course_users.php');
27
 
28
/**
29
 * Unit tests for core_course indicators.
30
 *
31
 * @package   core_course
32
 * @category  test
33
 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class indicators_test extends \advanced_testcase {
37
    use mlbackend_helper_trait;
1 efrain 38
 
39
    /**
40
     * test_no_teacher
41
     *
42
     * @return void
43
     */
11 efrain 44
    public function test_no_teacher(): void {
1 efrain 45
        global $DB;
46
 
47
        $this->resetAfterTest(true);
48
 
49
        $course1 = $this->getDataGenerator()->create_course();
50
        $course2 = $this->getDataGenerator()->create_course();
51
        $coursecontext1 = \context_course::instance($course1->id);
52
        $coursecontext2 = \context_course::instance($course2->id);
53
 
54
        $user = $this->getDataGenerator()->create_user();
55
 
56
        $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'student');
57
        $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'teacher');
58
 
59
        $indicator = new \core_course\analytics\indicator\no_teacher();
60
 
61
        $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id);
62
        $data = array(
63
            $course1->id => array(
64
                'context' => $coursecontext1,
65
                'course' => $course1,
66
            ),
67
            $course2->id => array(
68
                'context' => $coursecontext2,
69
                'course' => $course2,
70
            ));
71
        $indicator->add_sample_data($data);
72
 
73
        list($values, $ignored) = $indicator->calculate($sampleids, 'course');
74
        $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
75
        $this->assertEquals($indicator::get_max_value(), $values[$course2->id][0]);
76
    }
77
 
78
    /**
79
     * test_completion_enabled
80
     *
81
     * @return void
82
     */
11 efrain 83
    public function test_completion_enabled(): void {
1 efrain 84
        global $DB;
85
 
86
        $this->resetAfterTest(true);
87
 
88
        $course1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 0));
89
        $course2 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
90
        $course3 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
91
 
92
        // Criteria only for the last one.
93
        $criteriadata = new \stdClass();
94
        $criteriadata->id = $course3->id;
95
        $criteriadata->criteria_self = 1;
96
        $criterion = new \completion_criteria_self();
97
        $criterion->update_config($criteriadata);
98
 
99
        $indicator = new \core_course\analytics\indicator\completion_enabled();
100
 
101
        $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id, $course3->id => $course3->id);
102
        $data = array(
103
            $course1->id => array(
104
                'course' => $course1,
105
            ),
106
            $course2->id => array(
107
                'course' => $course2,
108
            ),
109
            $course3->id => array(
110
                'course' => $course3,
111
            ));
112
        $indicator->add_sample_data($data);
113
 
114
        // Calculate using course samples.
115
        list($values, $ignored) = $indicator->calculate($sampleids, 'course');
116
        $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
117
        $this->assertEquals($indicator::get_min_value(), $values[$course2->id][0]);
118
        $this->assertEquals($indicator::get_max_value(), $values[$course3->id][0]);
119
 
120
        // Calculate using course_modules samples.
121
        $indicator->clear_sample_data();
122
        $data1 = $this->getDataGenerator()->create_module('data', array('course' => $course3->id),
123
                                                             array('completion' => 0));
124
        $data2 = $this->getDataGenerator()->create_module('data', array('course' => $course3->id),
125
                                                             array('completion' => 1));
126
 
127
        $sampleids = array($data1->cmid => $data1->cmid, $data2->cmid => $data2->cmid);
128
        $cm1 = $DB->get_record('course_modules', array('id' => $data1->cmid));
129
        $cm2 = $DB->get_record('course_modules', array('id' => $data2->cmid));
130
        $data = array(
131
            $cm1->id => array(
132
                'course' => $course3,
133
                'course_modules' => $cm1,
134
            ),
135
            $cm2->id => array(
136
                'course' => $course3,
137
                'course_modules' => $cm2,
138
            ));
139
        $indicator->add_sample_data($data);
140
 
141
        list($values, $ignored) = $indicator->calculate($sampleids, 'course_modules');
142
        $this->assertEquals($indicator::get_min_value(), $values[$cm1->id][0]);
143
        $this->assertEquals($indicator::get_max_value(), $values[$cm2->id][0]);
144
    }
145
 
146
    /**
147
     * test_potential_cognitive
148
     *
149
     * @return void
150
     */
11 efrain 151
    public function test_potential_cognitive(): void {
1 efrain 152
        global $DB;
153
 
154
        $this->resetAfterTest(true);
155
 
156
        $course1 = $this->getDataGenerator()->create_course();
157
 
158
        $course2 = $this->getDataGenerator()->create_course();
159
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course2->id));
160
 
161
        $course3 = $this->getDataGenerator()->create_course();
162
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id));
163
        $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course3->id));
164
 
165
        $course4 = $this->getDataGenerator()->create_course();
166
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course4->id));
167
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course4->id));
168
 
169
        $indicator = new \core_course\analytics\indicator\potential_cognitive_depth();
170
 
171
        $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id, $course3->id => $course3->id,
172
            $course4->id => $course4->id);
173
        $data = array(
174
            $course1->id => array(
175
                'course' => $course1,
176
            ),
177
            $course2->id => array(
178
                'course' => $course2,
179
            ),
180
            $course3->id => array(
181
                'course' => $course3,
182
            ),
183
            $course4->id => array(
184
                'course' => $course4,
185
            ));
186
        $indicator->add_sample_data($data);
187
 
188
        list($values, $ignored) = $indicator->calculate($sampleids, 'course');
189
        $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
190
 
191
        // General explanation about the points, the max level is 5 so level 1 is -1, level 2 is -0.5, level 3 is 0,
192
        // level 4 is 0.5 and level 5 is 1.
193
 
194
        // Page cognitive is level 1 (the lower one).
195
        $this->assertEquals($indicator::get_min_value(), $values[$course2->id][0]);
196
 
197
        // The maximum cognitive depth level is 5, assign level is 5 therefore the potential cognitive depth is the max.
198
        $this->assertEquals($indicator::get_max_value(), $values[$course3->id][0]);
199
 
200
        // Forum level is 4.
201
        $this->assertEquals(0.5, $values[$course4->id][0]);
202
 
203
        // Calculate using course_modules samples.
204
        $course5 = $this->getDataGenerator()->create_course();
205
        $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course5->id));
206
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course5->id));
207
 
208
        $sampleids = array($assign->cmid => $assign->cmid, $forum->cmid => $forum->cmid);
209
        $cm1 = $DB->get_record('course_modules', array('id' => $assign->cmid));
210
        $cm2 = $DB->get_record('course_modules', array('id' => $forum->cmid));
211
        $data = array(
212
            $cm1->id => array(
213
                'course' => $course5,
214
                'course_modules' => $cm1,
215
            ),
216
            $cm2->id => array(
217
                'course' => $course5,
218
                'course_modules' => $cm2,
219
            ));
220
        $indicator->clear_sample_data();
221
        $indicator->add_sample_data($data);
222
 
223
        list($values, $ignored) = $indicator->calculate($sampleids, 'course_modules');
224
        // Assign level is 5, the maximum level.
225
        $this->assertEquals($indicator::get_max_value(), $values[$cm1->id][0]);
226
        // Forum level is 4.
227
        $this->assertEquals(0.5, $values[$cm2->id][0]);
228
 
229
    }
230
 
231
    /**
232
     * test_potential_social
233
     *
234
     * @return void
235
     */
11 efrain 236
    public function test_potential_social(): void {
1 efrain 237
        global $DB;
238
 
239
        $this->resetAfterTest(true);
240
 
241
        $course1 = $this->getDataGenerator()->create_course();
242
 
243
        $course2 = $this->getDataGenerator()->create_course();
244
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course2->id));
245
 
246
        $course3 = $this->getDataGenerator()->create_course();
247
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id));
248
        $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course3->id));
249
 
250
        $course4 = $this->getDataGenerator()->create_course();
251
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course4->id));
252
        $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course4->id));
253
 
254
        $indicator = new \core_course\analytics\indicator\potential_social_breadth();
255
 
256
        $sampleids = array($course1->id => $course1->id, $course2->id => $course2->id, $course3->id => $course3->id,
257
            $course4->id => $course4->id);
258
        $data = array(
259
            $course1->id => array(
260
                'course' => $course1,
261
            ),
262
            $course2->id => array(
263
                'course' => $course2,
264
            ),
265
            $course3->id => array(
266
                'course' => $course3,
267
            ),
268
            $course4->id => array(
269
                'course' => $course4,
270
            ));
271
        $indicator->add_sample_data($data);
272
 
273
        list($values, $ignored) = $indicator->calculate($sampleids, 'course');
274
        $this->assertEquals($indicator::get_min_value(), $values[$course1->id][0]);
275
 
276
        // General explanation about the points, the max level is 2 so level 1 is -1, level 2 is 1.
277
 
278
        // Page social is level 1 (the lower level).
279
        $this->assertEquals($indicator::get_min_value(), $values[$course2->id][0]);
280
 
281
        // Forum is level 2 and assign is level 2.
282
        $this->assertEquals($indicator::get_max_value(), $values[$course3->id][0]);
283
 
284
        // Page is level 1 and assign is level 2, so potential level is the max one.
285
        $this->assertEquals($indicator::get_max_value(), $values[$course4->id][0]);
286
 
287
        // Calculate using course_modules samples.
288
        $course5 = $this->getDataGenerator()->create_course();
289
        $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course5->id));
290
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course5->id));
291
 
292
        $sampleids = array($assign->cmid => $assign->cmid, $page->cmid => $page->cmid);
293
        $cm1 = $DB->get_record('course_modules', array('id' => $assign->cmid));
294
        $cm2 = $DB->get_record('course_modules', array('id' => $page->cmid));
295
        $data = array(
296
            $cm1->id => array(
297
                'course' => $course5,
298
                'course_modules' => $cm1,
299
            ),
300
            $cm2->id => array(
301
                'course' => $course5,
302
                'course_modules' => $cm2,
303
            ));
304
        $indicator->clear_sample_data();
305
        $indicator->add_sample_data($data);
306
 
307
        list($values, $ignored) = $indicator->calculate($sampleids, 'course_modules');
308
        // Assign social is level 2 (the max level).
309
        $this->assertEquals($indicator::get_max_value(), $values[$cm1->id][0]);
310
        // Page social is level 1 (the lower level).
311
        $this->assertEquals($indicator::get_min_value(), $values[$cm2->id][0]);
312
    }
313
 
314
    /**
315
     * test_activities_due
316
     *
317
     * @return void
318
     */
11 efrain 319
    public function test_activities_due(): void {
1 efrain 320
        global $DB;
321
 
1441 ariadna 322
        if (!self::is_mlbackend_python_configured()) {
323
            $this->markTestSkipped('mlbackend_python is not configured.');
324
        }
325
 
1 efrain 326
        $this->resetAfterTest(true);
327
        $this->setAdminuser();
328
 
329
        $course1 = $this->getDataGenerator()->create_course();
330
        $user1 = $this->getDataGenerator()->create_user();
331
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
332
 
333
        $target = \core_analytics\manager::get_target('test_target_course_users');
334
        $indicators = array('\core_course\analytics\indicator\activities_due');
335
        foreach ($indicators as $key => $indicator) {
336
            $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
337
        }
338
 
339
        $model = \core_analytics\model::create($target, $indicators);
340
        $model->enable('\core\analytics\time_splitting\single_range');
341
        $model->train();
342
    }
343
}