MDL-59030 analytics: Social breadth accepting more than level 2
[moodle.git] / analytics / tests / course_activities_test.php
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/>.
17 /**
18  * Unit tests for course activities.
19  *
20  * @package   core_analytics
21  * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Unit tests for course activities
29  *
30  * @package   core_analytics
31  * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
32  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  */
34 class core_analytics_course_activities_testcase extends advanced_testcase {
36     /**
37      * availability_levels
38      *
39      * @return array
40      */
41     public function availability_levels() {
42         return array(
43             'activity' => array('activity'),
44             'section' => array('section'),
45         );
46     }
48     /**
49      * test_get_activities_with_availability
50      *
51      * @dataProvider availability_levels
52      * @param string $availabilitylevel
53      * @return void
54      */
55     public function test_get_activities_with_availability($availabilitylevel) {
57         list($course, $stu1) = $this->setup_course();
59         // Forum1 is ignored as section 0 does not count.
60         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
62         $courseman = new \core_analytics\course($course);
64         $modinfo = get_fast_modinfo($course, $stu1->id);
65         $cm = $modinfo->get_cm($forum->cmid);
67         if ($availabilitylevel === 'activity') {
68             $availabilityinfo = new \core_availability\info_module($cm);
69         } else if ($availabilitylevel === 'section') {
70             $availabilityinfo = new \core_availability\info_section($cm->get_modinfo()->get_section_info($cm->sectionnum));
71         } else {
72             $this->fail('Unsupported availability level');
73         }
75         $fromtime = strtotime('2015-10-22 00:00:00 GMT');
76         $untiltime = strtotime('2015-10-24 00:00:00 GMT');
77         $structure = (object)array('op' => '|', 'show' => true, 'c' => array(
78                 (object)array('type' => 'date', 'd' => '<', 't' => $untiltime),
79                 (object)array('type' => 'date', 'd' => '>=', 't' => $fromtime)
80         ));
82         $method = new ReflectionMethod($availabilityinfo, 'set_in_database');
83         $method->setAccessible(true);
84         $method->invoke($availabilityinfo, json_encode($structure));
86         $this->setUser($stu1);
88         // Reset modinfo we also want coursemodinfo cache definition to be cleared.
89         get_fast_modinfo($course, $stu1->id, true);
90         rebuild_course_cache($course->id, true);
92         $modinfo = get_fast_modinfo($course, $stu1->id);
94         $cm = $modinfo->get_cm($forum->cmid);
96         // Condition from after provided end time.
97         $this->assertCount(0, $courseman->get_activities('forum', strtotime('2015-10-20 00:00:00 GMT'),
98             strtotime('2015-10-21 00:00:00 GMT'), $stu1));
100         // Condition until before provided start time
101         $this->assertCount(0, $courseman->get_activities('forum', strtotime('2015-10-25 00:00:00 GMT'),
102             strtotime('2015-10-26 00:00:00 GMT'), $stu1));
104         // Condition until after provided end time.
105         $this->assertCount(0, $courseman->get_activities('forum', strtotime('2015-10-22 00:00:00 GMT'),
106             strtotime('2015-10-23 00:00:00 GMT'), $stu1));
108         // Condition until after provided start time and before provided end time.
109         $this->assertCount(1, $courseman->get_activities('forum', strtotime('2015-10-22 00:00:00 GMT'),
110             strtotime('2015-10-25 00:00:00 GMT'), $stu1));
111     }
113     /**
114      * test_get_activities_with_weeks
115      *
116      * @return void
117      */
118     public function test_get_activities_with_weeks() {
120         $startdate = gmmktime('0', '0', '0', 10, 24, 2015);
121         $record = array(
122             'format' => 'weeks',
123             'numsections' => 4,
124             'startdate' => $startdate,
125         );
127         list($course, $stu1) = $this->setup_course($record);
129         // Forum1 is ignored as section 0 does not count.
130         $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
131             array('section' => 0));
132         $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
133             array('section' => 1));
134         $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
135             array('section' => 2));
136         $forum4 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
137             array('section' => 4));
138         $forum5 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
139             array('section' => 4));
141         $courseman = new \core_analytics\course($course);
143         $this->setUser($stu1);
145         $first = $startdate;
146         $second = $startdate + WEEKSECS;
147         $third = $startdate + (WEEKSECS * 2);
148         $forth = $startdate + (WEEKSECS * 3);
149         $this->assertCount(1, $courseman->get_activities('forum', $first, $first + WEEKSECS, $stu1));
150         $this->assertCount(1, $courseman->get_activities('forum', $second, $second + WEEKSECS, $stu1));
151         $this->assertCount(0, $courseman->get_activities('forum', $third, $third + WEEKSECS, $stu1));
152         $this->assertCount(2, $courseman->get_activities('forum', $forth, $forth + WEEKSECS, $stu1));
153     }
155     /**
156      * test_get_activities_by_section
157      *
158      * @return void
159      */
160     public function test_get_activities_by_section() {
162         // This makes debugging easier, sorry WA's +8 :).
163         $this->setTimezone('UTC');
165         // 1 year.
166         $startdate = gmmktime('0', '0', '0', 10, 24, 2015);
167         $enddate = gmmktime('0', '0', '0', 10, 24, 2016);
168         $numsections = 12;
169         $record = array(
170             'format' => 'topics',
171             'numsections' => $numsections,
172             'startdate' => $startdate,
173             'enddate' => $enddate
174         );
176         list($course, $stu1) = $this->setup_course($record);
178         // Forum1 is ignored as section 0 does not count.
179         $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
180             array('section' => 0));
181         $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
182             array('section' => 1));
183         $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
184             array('section' => 4));
185         $forum4 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
186             array('section' => 8));
187         $forum5 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
188             array('section' => 10));
189         $forum6 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
190             array('section' => 12));
192         $courseman = new \core_analytics\course($course);
194         $this->setUser($stu1);
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, $courseman->get_activities('forum', $first, $first + $duration, $stu1));
203         $this->assertCount(1, $courseman->get_activities('forum', $second, $second + $duration, $stu1));
204         $this->assertCount(1, $courseman->get_activities('forum', $third, $third + $duration, $stu1));
205         $this->assertCount(2, $courseman->get_activities('forum', $forth, $forth + $duration, $stu1));
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, $courseman->get_activities('forum', $timeranges[1], $timeranges[1] + $duration, $stu1));
214         $this->assertCount(1, $courseman->get_activities('forum', $timeranges[4], $timeranges[4] + $duration, $stu1));
215         $this->assertCount(1, $courseman->get_activities('forum', $timeranges[8], $timeranges[8] + $duration, $stu1));
216         $this->assertCount(1, $courseman->get_activities('forum', $timeranges[10], $timeranges[10] + $duration, $stu1));
217         $this->assertCount(1, $courseman->get_activities('forum', $timeranges[12], $timeranges[12] + $duration, $stu1));
219         // Nothing here.
220         $this->assertCount(0, $courseman->get_activities('forum', $timeranges[2], $timeranges[2] + $duration, $stu1));
221         $this->assertCount(0, $courseman->get_activities('forum', $timeranges[3], $timeranges[3] + $duration, $stu1));
222     }
224     /**
225      * setup_course
226      *
227      * @param stdClass $courserecord
228      * @return array
229      */
230     protected function setup_course($courserecord = null) {
231         global $CFG;
233         $this->resetAfterTest(true);
235         $this->setAdminUser();
237         $CFG->enableavailability = true;
239         $course = $this->getDataGenerator()->create_course($courserecord);
240         $stu1 = $this->getDataGenerator()->create_user();
241         $this->getDataGenerator()->enrol_user($stu1->id, $course->id, 'student');
243         return array($course, $stu1);
244     }