diff --git a/classes/attendance_webservices_handler.php b/classes/attendance_webservices_handler.php index c961362..d0c0d7a 100644 --- a/classes/attendance_webservices_handler.php +++ b/classes/attendance_webservices_handler.php @@ -158,4 +158,24 @@ class attendance_handler { $DB->update_record('attendance_sessions', $attendancesession); } } + + /** + * For this attendance instance, returns all sessions. + * + * @param int $attendanceid + * @return mixed + */ + public static function get_sessions($attendanceid) { + global $DB; + + $sessions = $DB->get_records('attendance_sessions', array('attendanceid' => $attendanceid), 'id ASC'); + + $sessionsinfo = array(); + + foreach ($sessions as $session) { + $sessionsinfo[$session->id] = self::get_session($session->id); + } + + return $sessionsinfo; + } } diff --git a/db/services.php b/db/services.php index 6cc2584..4e0f049 100644 --- a/db/services.php +++ b/db/services.php @@ -67,13 +67,19 @@ $functions = array( 'description' => 'Method that retrieves the session data', 'type' => 'read', ), - 'mod_attendance_update_user_status' => array( 'classname' => 'mod_attendance_external', 'methodname' => 'update_user_status', 'classpath' => 'mod/attendance/externallib.php', 'description' => 'Method that updates the user status in a session.', 'type' => 'write', + ), + 'mod_attendance_get_sessions' => array( + 'classname' => 'mod_attendance_external', + 'methodname' => 'get_sessions', + 'classpath' => 'mod/attendance/externallib.php', + 'description' => 'Method that retrieves the sessions in an attendance instance.', + 'type' => 'read', ) ); @@ -88,7 +94,8 @@ $services = array( 'mod_attendance_remove_session', 'mod_attendance_get_courses_with_today_sessions', 'mod_attendance_get_session', - 'mod_attendance_update_user_status' + 'mod_attendance_update_user_status', + 'mod_attendance_get_sessions' ), 'restrictedusers' => 0, 'enabled' => 1, diff --git a/externallib.php b/externallib.php index 163a880..d7cbfb6 100644 --- a/externallib.php +++ b/externallib.php @@ -543,4 +543,41 @@ class mod_attendance_external extends external_api { public static function update_user_status_returns() { return new external_value(PARAM_TEXT, 'Http code'); } + + /** + * Get sessions params. + * + * @return external_function_parameters + */ + public static function get_sessions_parameters() { + return new external_function_parameters( + array( + 'attendanceid' => new external_value(PARAM_INT, 'Attendance id.', VALUE_REQUIRED), + ) + ); + } + + /** + * Describes get_sessions return values. + * + * @return external_multiple_structure + */ + public static function get_sessions_returns() { + return new external_multiple_structure(self::get_session_returns()); + } + + /** + * Get sessions. + * + * @param int $attendanceid + */ + public static function get_sessions($attendanceid) { + global $DB; + + $params = self::validate_parameters(self::get_sessions_parameters(), array( + 'attendanceid' => $attendanceid, + )); + + return attendance_handler::get_sessions($params['attendanceid']); + } } diff --git a/tests/externallib_test.php b/tests/externallib_test.php index e7262e8..18e7ee0 100644 --- a/tests/externallib_test.php +++ b/tests/externallib_test.php @@ -471,4 +471,39 @@ class mod_attendance_external_testcase extends externallib_advanced_testcase { $this->assertInstanceOf('\core\event\calendar_event_created', $events[0]); $this->assertInstanceOf('\mod_attendance\event\session_added', $events[1]); } + + public function test_get_sessions() { + $this->resetAfterTest(true); + + $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); + $courseswithsessions = external_api::clean_returnvalue(mod_attendance_external::get_courses_with_today_sessions_returns(), + $courseswithsessions); + + foreach ($courseswithsessions as $course) { + + $attendanceinstances = $course['attendance_instances']; + + foreach ($attendanceinstances as $attendanceinstance) { + + $sessionsinfo = $attendanceinstance['today_sessions']; + + foreach ($sessionsinfo as $sessioninfo) { + + $sessions = attendance_handler::get_sessions($sessioninfo['attendanceid']); + $sessions = external_api::clean_returnvalue(mod_attendance_external::get_sessions_returns(), + $sessions); + + foreach ($sessions as $session) { + $sessiontocompareagainst = attendance_handler::get_session($session['id']); + $sessiontocompareagainst = external_api::clean_returnvalue(mod_attendance_external::get_session_returns(), + $sessiontocompareagainst); + + $this->assertEquals($this->attendance->id, $session['attendanceid']); + $this->assertEquals($sessiontocompareagainst['id'], $session['id']); + $this->assertEquals(count($session['users']), count($sessiontocompareagainst['users'])); + } + } + } + } + } }