From dbe59d1bb7846603ce48b937d3ea9b96ca486e31 Mon Sep 17 00:00:00 2001 From: Morgan Harris Date: Mon, 18 Feb 2019 08:02:06 +1100 Subject: [PATCH] Allow group sessions in webservices (#364) * Enable groups for web sservices This small change enables webservice users to see group-based attendance sessions * Tests for group-based web services Add a test for web services to test group-based sessions --- classes/attendance_webservices_handler.php | 3 +- tests/attendance_webservices_test.php | 38 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/classes/attendance_webservices_handler.php b/classes/attendance_webservices_handler.php index 0b0d079..58f3ff1 100644 --- a/classes/attendance_webservices_handler.php +++ b/classes/attendance_webservices_handler.php @@ -109,7 +109,8 @@ class attendance_handler { $session->courseid = $DB->get_field('attendance', 'course', array('id' => $session->attendanceid)); $session->statuses = attendance_get_statuses($session->attendanceid, true, $session->statusset); $coursecontext = context_course::instance($session->courseid); - $session->users = get_enrolled_users($coursecontext, 'mod/attendance:canbelisted', 0, 'u.id, u.firstname, u.lastname'); + $session->users = get_enrolled_users($coursecontext, 'mod/attendance:canbelisted', + $session->groupid, 'u.id, u.firstname, u.lastname'); $session->attendance_log = array(); if ($attendancelog = $DB->get_records('attendance_log', array('sessionid' => $sessionid), diff --git a/tests/attendance_webservices_test.php b/tests/attendance_webservices_test.php index 4712c15..b60960a 100644 --- a/tests/attendance_webservices_test.php +++ b/tests/attendance_webservices_test.php @@ -46,6 +46,8 @@ class attendance_webservices_tests extends advanced_testcase { /** @var stdClass */ protected $teacher; /** @var array */ + protected $students; + /** @var array */ protected $sessions; /** @@ -95,9 +97,11 @@ class attendance_webservices_tests extends advanced_testcase { /** Creating 10 students and 1 teacher. */ protected function create_and_enrol_users() { + $this->students = array(); for ($i = 0; $i < 10; $i++) { $student = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student->id, $this->course->id, 5); // Enrol as student. + $this->students[] = $student; } $this->teacher = $this->getDataGenerator()->create_user(); @@ -136,6 +140,40 @@ class attendance_webservices_tests extends advanced_testcase { $this->assertEquals(count($sessioninfo->users), 10); } + public function test_get_session_with_group() { + $this->resetAfterTest(true); + + // Create a group in our course, and add some students to it. + $group = new stdClass(); + $group->courseid = $this->course->id; + $group = $this->getDataGenerator()->create_group($group); + + for ($i = 0; $i < 5; $i++) { + $member = new stdClass; + $member->groupid = $group->id; + $member->userid = $this->students[$i]->id; + $this->getDataGenerator()->create_group_member($member); + } + + // Add a session that's identical to the first, but with a group. + $session = $this->sessions[0]; + $session->groupid = $group->id; + $session->sessdate += 3600; // Make sure it appears second in the list. + $this->attendance->add_sessions($this->sessions); + + $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); + + $course = array_pop($courseswithsessions); + $attendanceinstance = array_pop($course->attendance_instances); + $session = array_pop($attendanceinstance['today_sessions']); + + $sessioninfo = attendance_handler::get_session($session->id); + + $this->assertEquals($session->id, $sessioninfo->id); + $this->assertEquals($group->id, $sessioninfo->groupid); + $this->assertEquals(count($sessioninfo->users), 5); + } + public function test_update_user_status() { $this->resetAfterTest(true);