2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for course.
20 * @package core_analytics
21 * @copyright 2016 David Monllaó {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Unit tests for course.
30 * @package core_analytics
31 * @copyright 2016 David Monllaó {@link http://www.davidmonllao.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_analytics_course_testcase extends advanced_testcase {
36 public function setUp() {
39 $this->course = $this->getDataGenerator()->create_course();
40 $this->stu1 = $this->getDataGenerator()->create_user();
41 $this->stu2 = $this->getDataGenerator()->create_user();
42 $this->both = $this->getDataGenerator()->create_user();
43 $this->editingteacher = $this->getDataGenerator()->create_user();
44 $this->teacher = $this->getDataGenerator()->create_user();
46 $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
47 $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
48 $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
51 $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
52 $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
53 $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
54 $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
55 $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
56 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
59 set_config('studentroles', $this->studentroleid, 'analytics');
60 set_config('teacherroles', $this->editingteacherroleid . ',' . $this->teacherroleid, 'analytics');
66 public function test_users() {
69 $this->resetAfterTest(true);
71 $courseman = new \core_analytics\course($this->course->id);
72 $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
73 $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
74 $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
76 // Distinct is applied
77 $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
78 $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
82 * Course validation tests.
86 public function test_course_validation() {
89 $this->resetAfterTest(true);
91 $courseman = new \core_analytics\course($this->course->id);
92 $this->assertFalse($courseman->was_started());
93 $this->assertFalse($courseman->is_finished());
94 $this->assertFalse($courseman->is_valid());
96 // Nothing should change when assigning as teacher.
97 for ($i = 0; $i < 10; $i++) {
98 $user = $this->getDataGenerator()->create_user();
99 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
101 $courseman = new \core_analytics\course($this->course->id);
102 $this->assertFalse($courseman->is_valid());
104 // More students now.
105 for ($i = 0; $i < 10; $i++) {
106 $user = $this->getDataGenerator()->create_user();
107 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
109 $courseman = new \core_analytics\course($this->course->id);
110 $this->assertFalse($courseman->is_valid());
112 // Valid start date unknown end date.
113 $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
114 $DB->update_record('course', $this->course);
115 $courseman = new \core_analytics\course($this->course->id);
116 $this->assertTrue($courseman->was_started());
117 $this->assertFalse($courseman->is_finished());
118 $this->assertFalse($courseman->is_valid());
120 // Valid start and end date.
121 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
122 $DB->update_record('course', $this->course);
123 $courseman = new \core_analytics\course($this->course->id);
124 $this->assertTrue($courseman->was_started());
125 $this->assertTrue($courseman->is_finished());
126 $this->assertTrue($courseman->is_valid());
128 // Valid start and ongoing course.
129 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
130 $DB->update_record('course', $this->course);
131 $courseman = new \core_analytics\course($this->course->id);
132 $this->assertTrue($courseman->was_started());
133 $this->assertFalse($courseman->is_finished());
134 $this->assertFalse($courseman->is_valid());
138 * Get the minimum time that is considered valid according to guess_end logic.
143 protected function time_greater_than($time) {
144 return $time - (WEEKSECS * 2);
148 * Get the maximum time that is considered valid according to guess_end logic.
153 protected function time_less_than($time) {
154 return $time + (WEEKSECS * 2);
162 * @param int $courseid
165 protected function generate_log($time, $userid = false, $courseid = false) {
168 if (empty($userid)) {
169 $userid = $this->stu1->id;
171 if (empty($courseid)) {
172 $courseid = $this->course->id;
175 $context = context_course::instance($courseid);
177 'eventname' => '\\core\\event\\course_viewed',
178 'component' => 'core',
179 'action' => 'viewed',
180 'target' => 'course',
181 'objecttable' => 'course',
182 'objectid' => $courseid,
184 'edulevel' => \core\event\base::LEVEL_PARTICIPATING,
185 'contextid' => $context->id,
186 'contextlevel' => $context->contextlevel,
187 'contextinstanceid' => $context->instanceid,
189 'courseid' => $courseid,
190 'relateduserid' => null,
193 'timecreated' => $time,
196 $DB->insert_record('logstore_standard_log', $obj);