From 71bc7f15aa7aec7073c3b3c0810b72c021e73549 Mon Sep 17 00:00:00 2001 From: Morgan Harris Date: Fri, 1 Mar 2019 10:53:14 +1100 Subject: [PATCH] Webservice: allow multiple instances in one course (#372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [WS] Allow multiple instances in same course You can now retrieve multiple instances in the one course * Add test for multiple instances Also cleaned up a lot of test code… there was a bunch of stuff doing nothing just sitting in the file. All the stuff with getting the module and the context… that’s all handled by the data generator. * satisfy the linter bloody thing --- classes/attendance_webservices_handler.php | 4 +- tests/attendance_webservices_test.php | 46 ++++++++++++++-------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/classes/attendance_webservices_handler.php b/classes/attendance_webservices_handler.php index 58f3ff1..c961362 100644 --- a/classes/attendance_webservices_handler.php +++ b/classes/attendance_webservices_handler.php @@ -51,7 +51,9 @@ class attendance_handler { $context = context_course::instance($attendance->course); if (has_capability('mod/attendance:takeattendances', $context, $userid)) { $course = $usercourses[$attendance->course]; - $course->attendance_instance = array(); + if (!isset($course->attendance_instance)) { + $course->attendance_instance = array(); + } $att = new stdClass(); $att->id = $attendance->id; diff --git a/tests/attendance_webservices_test.php b/tests/attendance_webservices_test.php index 7f7f834..3e7ed0b 100644 --- a/tests/attendance_webservices_test.php +++ b/tests/attendance_webservices_test.php @@ -59,20 +59,7 @@ class attendance_webservices_tests extends advanced_testcase { $this->category = $this->getDataGenerator()->create_category(); $this->course = $this->getDataGenerator()->create_course(array('category' => $this->category->id)); - $record = new stdClass(); - $record->course = $this->course->id; - $record->name = "Attendance"; - $record->grade = 100; - - $DB->insert_record('attendance', $record); - - $this->getDataGenerator()->create_module('attendance', array('course' => $this->course->id)); - - $moduleid = $DB->get_field('modules', 'id', array('name' => 'attendance')); - $cm = $DB->get_record('course_modules', array('course' => $this->course->id, 'module' => $moduleid)); - $context = context_course::instance($this->course->id); - $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST); - $this->attendance = new mod_attendance_structure($att, $cm, $this->course, $context); + $this->attendance = $this->create_attendance(); $this->create_and_enrol_users(); @@ -96,6 +83,14 @@ class attendance_webservices_tests extends advanced_testcase { $this->attendance->add_sessions($this->sessions); } + private function create_attendance() { + global $DB; + $att = $this->getDataGenerator()->create_module('attendance', array('course' => $this->course->id)); + $cm = $DB->get_record('course_modules', array('id' => $att->cmid)); + unset($att->cmid); + return new mod_attendance_structure($att, $cm, $this->course); + } + /** Creating 10 students and 1 teacher. */ protected function create_and_enrol_users() { $this->students = array(); @@ -125,6 +120,25 @@ class attendance_webservices_tests extends advanced_testcase { $this->assertEquals(count($attendanceinstance['today_sessions']), 2); } + public function test_get_courses_with_today_sessions_multiple_instances() { + $this->resetAfterTest(true); + + // Make another attendance. + $second = $this->create_attendance(); + + // Just add the same session. + $secondsession = clone $this->sessions[0]; + $secondsession->sessdate += 3600; + + $second->add_sessions([$secondsession]); + + $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); + $this->assertTrue(is_array($courseswithsessions)); + $this->assertEquals(count($courseswithsessions), 1); + $course = array_pop($courseswithsessions); + $this->assertEquals(count($course->attendance_instances), 2); + } + public function test_get_session() { $this->resetAfterTest(true); @@ -157,10 +171,10 @@ class attendance_webservices_tests extends advanced_testcase { } // Add a session that's identical to the first, but with a group. - $session = $this->sessions[0]; + $session = clone $this->sessions[0]; $session->groupid = $group->id; $session->sessdate += 3600; // Make sure it appears second in the list. - $this->attendance->add_sessions($this->sessions); + $this->attendance->add_sessions([$session]); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id);