Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 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_analytics;
18
 
19
use advanced_testcase;
20
use ReflectionClass;
21
use ReflectionMethod;
22
use stdClass;
23
 
24
/**
25
 * Unit tests for activities completed by classification.
26
 *
27
 * @package   core_analytics
28
 * @covers    \core_analytics\local\indicator\community_of_inquiry_activity
29
 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
30
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
final class community_of_inquiry_activities_completed_by_test extends advanced_testcase {
33
 
34
    /**
35
     * availability_levels
36
     *
37
     * @return array
38
     */
39
    public static function availability_levels(): array {
40
        return array(
41
            'activity' => array('activity'),
42
            'section' => array('section'),
43
        );
44
    }
45
 
46
    /**
47
     * test_get_activities_with_availability
48
     *
49
     * @dataProvider availability_levels
50
     * @param string $availabilitylevel
51
     * @return void
52
     */
53
    public function test_get_activities_with_availability($availabilitylevel) {
54
 
55
        list($course, $stu1) = $this->setup_course();
56
 
57
        // Forum1 is ignored as section 0 does not count.
58
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
59
 
60
        $modinfo = get_fast_modinfo($course, $stu1->id);
61
        $cm = $modinfo->get_cm($forum->cmid);
62
 
63
        if ($availabilitylevel === 'activity') {
64
            $availabilityinfo = new \core_availability\info_module($cm);
65
        } else if ($availabilitylevel === 'section') {
66
            $availabilityinfo = new \core_availability\info_section($cm->get_modinfo()->get_section_info($cm->sectionnum));
67
        } else {
68
            $this->fail('Unsupported availability level');
69
        }
70
 
71
        $fromtime = strtotime('2015-10-22 00:00:00 GMT');
72
        $untiltime = strtotime('2015-10-24 00:00:00 GMT');
73
        $structure = (object)array('op' => '|', 'show' => true, 'c' => array(
74
                (object)array('type' => 'date', 'd' => '<', 't' => $untiltime),
75
                (object)array('type' => 'date', 'd' => '>=', 't' => $fromtime)
76
        ));
77
 
78
        $method = new ReflectionMethod($availabilityinfo, 'set_in_database');
79
        $method->invoke($availabilityinfo, json_encode($structure));
80
 
81
        $this->setUser($stu1);
82
 
83
        // Reset modinfo we also want coursemodinfo cache definition to be cleared.
84
        get_fast_modinfo($course, $stu1->id, true);
85
        rebuild_course_cache($course->id, true);
86
 
87
        $modinfo = get_fast_modinfo($course, $stu1->id);
88
 
89
        $cm = $modinfo->get_cm($forum->cmid);
90
 
91
        $course = new \core_analytics\course($course);
92
        list($indicator, $method) = $this->instantiate_indicator('mod_forum', $course);
93
 
94
        // Condition from after provided end time.
95
        $this->assertCount(0, $method->invoke($indicator, strtotime('2015-10-20 00:00:00 GMT'),
96
            strtotime('2015-10-21 00:00:00 GMT'), $stu1));
97
 
98
        // Condition until before provided start time
99
        $this->assertCount(0, $method->invoke($indicator, strtotime('2015-10-25 00:00:00 GMT'),
100
            strtotime('2015-10-26 00:00:00 GMT'), $stu1));
101
 
102
        // Condition until after provided end time.
103
        $this->assertCount(0, $method->invoke($indicator, strtotime('2015-10-22 00:00:00 GMT'),
104
            strtotime('2015-10-23 00:00:00 GMT'), $stu1));
105
 
106
        // Condition until after provided start time and before provided end time.
107
        $this->assertCount(1, $method->invoke($indicator, strtotime('2015-10-22 00:00:00 GMT'),
108
            strtotime('2015-10-25 00:00:00 GMT'), $stu1));
109
    }
110
 
111
    /**
112
     * test_get_activities_with_weeks
113
     *
114
     * @return void
115
     */
116
    public function test_get_activities_with_weeks() {
117
 
118
        $startdate = gmmktime('0', '0', '0', 10, 24, 2015);
119
        $record = array(
120
            'format' => 'weeks',
121
            'numsections' => 4,
122
            'startdate' => $startdate,
123
        );
124
 
125
        list($course, $stu1) = $this->setup_course($record);
126
 
127
        // Forum1 is ignored as section 0 does not count.
128
        $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
129
            array('section' => 0));
130
        $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
131
            array('section' => 1));
132
        $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
133
            array('section' => 2));
134
        $forum4 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
135
            array('section' => 4));
136
        $forum5 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
137
            array('section' => 4));
138
 
139
        $course = new \core_analytics\course($course);
140
        list($indicator, $method) = $this->instantiate_indicator('mod_forum', $course);
141
 
142
        $this->setUser($stu1);
143
 
144
        $first = $startdate;
145
        $second = $startdate + WEEKSECS;
146
        $third = $startdate + (WEEKSECS * 2);
147
        $forth = $startdate + (WEEKSECS * 3);
148
        $this->assertCount(2, $method->invoke($indicator, $first, $first + WEEKSECS, $stu1));
149
        $this->assertCount(1, $method->invoke($indicator, $second, $second + WEEKSECS, $stu1));
150
        $this->assertCount(0, $method->invoke($indicator, $third, $third + WEEKSECS, $stu1));
151
        $this->assertCount(2, $method->invoke($indicator, $forth, $forth + WEEKSECS, $stu1));
152
    }
153
 
154
    /**
155
     * test_get_activities_by_section
156
     *
157
     * @return void
158
     */
159
    public function test_get_activities_by_section() {
160
 
161
        // This makes debugging easier, sorry WA's +8 :).
162
        $this->setTimezone('UTC');
163
 
164
        // 1 year.
165
        $startdate = gmmktime('0', '0', '0', 10, 24, 2015);
166
        $enddate = gmmktime('0', '0', '0', 10, 24, 2016);
167
        $numsections = 12;
168
        $record = array(
169
            'format' => 'topics',
170
            'numsections' => $numsections,
171
            'startdate' => $startdate,
172
            'enddate' => $enddate
173
        );
174
 
175
        list($course, $stu1) = $this->setup_course($record);
176
 
177
        // Forum1 is ignored as section 0 does not count.
178
        $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
179
            array('section' => 0));
180
        $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
181
            array('section' => 1));
182
        $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
183
            array('section' => 4));
184
        $forum4 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
185
            array('section' => 8));
186
        $forum5 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
187
            array('section' => 10));
188
        $forum6 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
189
            array('section' => 12));
190
 
191
        $course = new \core_analytics\course($course);
192
        list($indicator, $method) = $this->instantiate_indicator('mod_forum', $course);
193
 
194
        $this->setUser($stu1);
195
 
196
        // Split the course in quarters.
197
        $duration = ($enddate - $startdate) / 4;
198
        $first = $startdate;
199
        $second = $startdate + $duration;
200
        $third = $startdate + ($duration * 2);
201
        $forth = $startdate + ($duration * 3);
202
        $this->assertCount(1, $method->invoke($indicator, $first, $first + $duration, $stu1));
203
        $this->assertCount(1, $method->invoke($indicator, $second, $second + $duration, $stu1));
204
        $this->assertCount(1, $method->invoke($indicator, $third, $third + $duration, $stu1));
205
        $this->assertCount(2, $method->invoke($indicator, $forth, $forth + $duration, $stu1));
206
 
207
        // Split the course in as many parts as sections.
208
        $duration = ($enddate - $startdate) / $numsections;
209
        for ($i = 1; $i <= $numsections; $i++) {
210
            // The -1 because section 1 start represents the course start.
211
            $timeranges[$i] = $startdate + ($duration * ($i - 1));
212
        }
213
        $this->assertCount(1, $method->invoke($indicator, $timeranges[1], $timeranges[1] + $duration, $stu1));
214
        $this->assertCount(1, $method->invoke($indicator, $timeranges[4], $timeranges[4] + $duration, $stu1));
215
        $this->assertCount(1, $method->invoke($indicator, $timeranges[8], $timeranges[8] + $duration, $stu1));
216
        $this->assertCount(1, $method->invoke($indicator, $timeranges[10], $timeranges[10] + $duration, $stu1));
217
        $this->assertCount(1, $method->invoke($indicator, $timeranges[12], $timeranges[12] + $duration, $stu1));
218
 
219
        // Nothing here.
220
        $this->assertCount(0, $method->invoke($indicator, $timeranges[2], $timeranges[2] + $duration, $stu1));
221
        $this->assertCount(0, $method->invoke($indicator, $timeranges[3], $timeranges[3] + $duration, $stu1));
222
    }
223
 
224
    /**
225
     * test_get_activities_with_specific_restrictions
226
     *
227
     * @return void
228
     */
229
    public function test_get_activities_with_specific_restrictions() {
230
 
231
        list($course, $stu1) = $this->setup_course();
232
 
233
        $end = strtotime('2015-10-24 00:00:00 GMT');
234
 
235
        // 1 with time close, one without.
236
        $params = array('course' => $course->id);
237
        $assign1 = $this->getDataGenerator()->create_module('assign', $params);
238
        $params['duedate'] = $end;
239
        $assign2 = $this->getDataGenerator()->create_module('assign', $params);
240
 
241
        // 1 with time close, one without.
242
        $params = array('course' => $course->id);
243
        $choice1 = $this->getDataGenerator()->create_module('choice', $params);
244
        $params['timeclose'] = $end;
245
        $choice1 = $this->getDataGenerator()->create_module('choice', $params);
246
 
247
        // 1 with time close, one without.
248
        $params = array('course' => $course->id);
249
        $data1 = $this->getDataGenerator()->create_module('data', $params);
250
        $params['timeavailableto'] = $end;
251
        $data1 = $this->getDataGenerator()->create_module('data', $params);
252
 
253
        // 1 with time close, one without.
254
        $params = array('course' => $course->id);
255
        $feedback1 = $this->getDataGenerator()->create_module('feedback', $params);
256
        $params['timeclose'] = $end;
257
        $feedback1 = $this->getDataGenerator()->create_module('feedback', $params);
258
 
259
        // 1 with time close, one without.
260
        $params = array('course' => $course->id);
261
        $lesson1 = $this->getDataGenerator()->create_module('lesson', $params);
262
        $params['deadline'] = $end;
263
        $lesson1 = $this->getDataGenerator()->create_module('lesson', $params);
264
 
265
        // 1 with time close, one without.
266
        $params = array('course' => $course->id);
267
        $quiz1 = $this->getDataGenerator()->create_module('quiz', $params);
268
        $params['timeclose'] = $end;
269
        $quiz1 = $this->getDataGenerator()->create_module('quiz', $params);
270
 
271
        // 1 with time close, one without.
272
        $params = array('course' => $course->id);
273
        $scorm1 = $this->getDataGenerator()->create_module('scorm', $params);
274
        $params['timeclose'] = $end;
275
        $scorm1 = $this->getDataGenerator()->create_module('scorm', $params);
276
 
277
        // 1 with time close, one without.
278
        $params = array('course' => $course->id);
279
        $workshop1 = $this->getDataGenerator()->create_module('workshop', $params);
280
        $params['submissionend'] = $end;
281
        $workshop1 = $this->getDataGenerator()->create_module('workshop', $params);
282
 
283
        $course = new \core_analytics\course($course);
284
 
285
        $activitytypes = array('mod_assign', 'mod_choice', 'mod_data', 'mod_feedback', 'mod_lesson',
286
            'mod_quiz', 'mod_scorm', 'mod_workshop');
287
        foreach ($activitytypes as $activitytype) {
288
 
289
            list($indicator, $method) = $this->instantiate_indicator($activitytype, $course);
290
 
291
            $message = $activitytype . ' activity type returned activities do not match expected size';
292
            $this->assertCount(0, $method->invoke($indicator, strtotime('2015-10-20 00:00:00 GMT'),
293
                strtotime('2015-10-21 00:00:00 GMT'), $stu1), $message);
294
 
295
            $this->assertCount(0, $method->invoke($indicator, strtotime('2015-10-25 00:00:00 GMT'),
296
                strtotime('2015-10-26 00:00:00 GMT'), $stu1), $message);
297
 
298
            $this->assertCount(1, $method->invoke($indicator, strtotime('2015-10-22 00:00:00 GMT'),
299
                strtotime('2015-10-25 00:00:00 GMT'), $stu1), $message);
300
        }
301
    }
302
 
303
    /**
304
     * setup_course
305
     *
306
     * @param stdClass $courserecord
307
     * @return array
308
     */
309
    protected function setup_course($courserecord = null) {
310
        global $CFG;
311
 
312
        $this->resetAfterTest(true);
313
 
314
        $this->setAdminUser();
315
 
316
        $CFG->enableavailability = true;
317
 
318
        $course = $this->getDataGenerator()->create_course($courserecord);
319
        $stu1 = $this->getDataGenerator()->create_user();
320
        $this->getDataGenerator()->enrol_user($stu1->id, $course->id, 'student');
321
 
322
        return array($course, $stu1);
323
    }
324
 
325
    /**
326
     * Returns the module cognitive depth indicator and the reflection method.
327
     *
328
     * @param string $modulename
329
     * @param \core_analytics\course $course
330
     * @return array
331
     */
332
    private function instantiate_indicator($modulename, \core_analytics\course $course) {
333
 
334
        $classname = '\\' . $modulename . '\analytics\indicator\cognitive_depth';
335
        $indicator = new $classname();
336
        $class = new ReflectionClass($indicator);
337
 
338
        $property = $class->getProperty('course');
339
        $property->setValue($indicator, $course);
340
 
341
        $method = new ReflectionMethod($indicator, 'get_activities');
342
 
343
        return array($indicator, $method);
344
    }
345
 
346
}