MDL-59030 analytics: Social breadth accepting more than level 2
[moodle.git] / analytics / tests / course_activities_test.php
CommitLineData
ff656bae
DM
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 * 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 */
24
25defined('MOODLE_INTERNAL') || die();
26
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 */
34class core_analytics_course_activities_testcase extends advanced_testcase {
35
413f19bc
DM
36 /**
37 * availability_levels
38 *
39 * @return array
40 */
ff656bae
DM
41 public function availability_levels() {
42 return array(
43 'activity' => array('activity'),
44 'section' => array('section'),
45 );
46 }
47
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) {
56
57 list($course, $stu1) = $this->setup_course();
58
413f19bc 59 // Forum1 is ignored as section 0 does not count.
ff656bae
DM
60 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
61
62 $courseman = new \core_analytics\course($course);
63
64 $modinfo = get_fast_modinfo($course, $stu1->id);
65 $cm = $modinfo->get_cm($forum->cmid);
66
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 }
74
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 ));
81
82 $method = new ReflectionMethod($availabilityinfo, 'set_in_database');
83 $method->setAccessible(true);
84 $method->invoke($availabilityinfo, json_encode($structure));
85
86 $this->setUser($stu1);
87
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);
91
92 $modinfo = get_fast_modinfo($course, $stu1->id);
93
94 $cm = $modinfo->get_cm($forum->cmid);
95
96 // Condition from after provided end time.
413f19bc
DM
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));
ff656bae
DM
99
100 // Condition until before provided start time
413f19bc
DM
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));
ff656bae
DM
103
104 // Condition until after provided end time.
413f19bc
DM
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));
ff656bae
DM
107
108 // Condition until after provided start time and before provided end time.
413f19bc
DM
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));
ff656bae
DM
111 }
112
413f19bc
DM
113 /**
114 * test_get_activities_with_weeks
115 *
116 * @return void
117 */
ff656bae
DM
118 public function test_get_activities_with_weeks() {
119
120 $startdate = gmmktime('0', '0', '0', 10, 24, 2015);
121 $record = array(
122 'format' => 'weeks',
123 'numsections' => 4,
124 'startdate' => $startdate,
125 );
126
127 list($course, $stu1) = $this->setup_course($record);
128
413f19bc 129 // Forum1 is ignored as section 0 does not count.
ff656bae
DM
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));
140
141 $courseman = new \core_analytics\course($course);
142
143 $this->setUser($stu1);
144
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 }
154
413f19bc
DM
155 /**
156 * test_get_activities_by_section
157 *
158 * @return void
159 */
ff656bae
DM
160 public function test_get_activities_by_section() {
161
413f19bc 162 // This makes debugging easier, sorry WA's +8 :).
ff656bae
DM
163 $this->setTimezone('UTC');
164
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 );
175
176 list($course, $stu1) = $this->setup_course($record);
177
413f19bc 178 // Forum1 is ignored as section 0 does not count.
ff656bae
DM
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));
191
192 $courseman = new \core_analytics\course($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, $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));
206
207 // Split the course in as many parts as sections.
208 $duration = ($enddate - $startdate) / $numsections;
413f19bc
DM
209 for ($i = 1; $i <= $numsections; $i++) {
210 // The -1 because section 1 start represents the course start.
ff656bae
DM
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));
218
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 }
223
413f19bc
DM
224 /**
225 * setup_course
226 *
227 * @param stdClass $courserecord
228 * @return array
229 */
ff656bae
DM
230 protected function setup_course($courserecord = null) {
231 global $CFG;
232
233 $this->resetAfterTest(true);
234
235 $this->setAdminUser();
236
237 $CFG->enableavailability = true;
238
239 $course = $this->getDataGenerator()->create_course($courserecord);
240 $stu1 = $this->getDataGenerator()->create_user();
241 $this->getDataGenerator()->enrol_user($stu1->id, $course->id, 'student');
242
243 return array($course, $stu1);
244 }
245
246}