Browse Source

Merge pull request #555 from pete1854/AddGetSessionsApiFunction

Added "mod_attendance_get_sessions" API function
40-behatfixes
Dan Marsden 3 years ago
committed by GitHub
parent
commit
c196e93082
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      classes/attendance_webservices_handler.php
  2. 11
      db/services.php
  3. 37
      externallib.php
  4. 35
      tests/externallib_test.php

20
classes/attendance_webservices_handler.php

@ -158,4 +158,24 @@ class attendance_handler {
$DB->update_record('attendance_sessions', $attendancesession); $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;
}
} }

11
db/services.php

@ -67,13 +67,19 @@ $functions = array(
'description' => 'Method that retrieves the session data', 'description' => 'Method that retrieves the session data',
'type' => 'read', 'type' => 'read',
), ),
'mod_attendance_update_user_status' => array( 'mod_attendance_update_user_status' => array(
'classname' => 'mod_attendance_external', 'classname' => 'mod_attendance_external',
'methodname' => 'update_user_status', 'methodname' => 'update_user_status',
'classpath' => 'mod/attendance/externallib.php', 'classpath' => 'mod/attendance/externallib.php',
'description' => 'Method that updates the user status in a session.', 'description' => 'Method that updates the user status in a session.',
'type' => 'write', '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_remove_session',
'mod_attendance_get_courses_with_today_sessions', 'mod_attendance_get_courses_with_today_sessions',
'mod_attendance_get_session', 'mod_attendance_get_session',
'mod_attendance_update_user_status' 'mod_attendance_update_user_status',
'mod_attendance_get_sessions'
), ),
'restrictedusers' => 0, 'restrictedusers' => 0,
'enabled' => 1, 'enabled' => 1,

37
externallib.php

@ -543,4 +543,41 @@ class mod_attendance_external extends external_api {
public static function update_user_status_returns() { public static function update_user_status_returns() {
return new external_value(PARAM_TEXT, 'Http code'); 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']);
}
} }

35
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('\core\event\calendar_event_created', $events[0]);
$this->assertInstanceOf('\mod_attendance\event\session_added', $events[1]); $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']));
}
}
}
}
}
} }

Loading…
Cancel
Save