From c349f489d9579ecfc60e2c22aca4ff2e06f869a5 Mon Sep 17 00:00:00 2001 From: Peter Rundqvist Date: Fri, 3 Sep 2021 12:26:17 +0200 Subject: [PATCH 1/5] Added "mod_attendance_get_sessions" API function --- classes/attendance_webservices_handler.php | 20 +++++++ db/services.php | 11 +++- externallib.php | 61 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/classes/attendance_webservices_handler.php b/classes/attendance_webservices_handler.php index c961362..2439bc7 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..6a9b7cb 100644 --- a/externallib.php +++ b/externallib.php @@ -543,4 +543,65 @@ 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() { + $statuses = array('id' => new external_value(PARAM_INT, 'Status id.'), + 'attendanceid' => new external_value(PARAM_INT, 'Attendance id.'), + 'acronym' => new external_value(PARAM_TEXT, 'Status acronym.'), + 'description' => new external_value(PARAM_TEXT, 'Status description.'), + 'grade' => new external_value(PARAM_FLOAT, 'Status grade.'), + 'visible' => new external_value(PARAM_INT, 'Status visibility.'), + 'deleted' => new external_value(PARAM_INT, 'informs if this session was deleted.'), + 'setnumber' => new external_value(PARAM_INT, 'Set number.')); + + $users = array('id' => new external_value(PARAM_INT, 'User id.'), + 'firstname' => new external_value(PARAM_TEXT, 'User first name.'), + 'lastname' => new external_value(PARAM_TEXT, 'User last name.')); + + $attendancelog = array('studentid' => new external_value(PARAM_INT, 'Student id.'), + 'statusid' => new external_value(PARAM_TEXT, 'Status id (last time).'), + 'remarks' => new external_value(PARAM_TEXT, 'Last remark.'), + 'id' => new external_value(PARAM_TEXT, 'log id.')); + + $session = self::get_session_structure(); + $session['courseid'] = new external_value(PARAM_INT, 'Course moodle id.'); + $session['statuses'] = new external_multiple_structure(new external_single_structure($statuses)); + $session['attendance_log'] = new external_multiple_structure(new external_single_structure($attendancelog)); + $session['users'] = new external_multiple_structure(new external_single_structure($users)); + + return new external_multiple_structure(new external_single_structure(($session))); + } + + /** + * 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']); + } } From 0f45b350f74d3edecae62f934f332790284f197e Mon Sep 17 00:00:00 2001 From: Peter Rundqvist Date: Fri, 3 Sep 2021 14:28:40 +0200 Subject: [PATCH 2/5] Returned redundant code --- externallib.php | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/externallib.php b/externallib.php index 6a9b7cb..0adabfc 100644 --- a/externallib.php +++ b/externallib.php @@ -563,31 +563,7 @@ class mod_attendance_external extends external_api { * @return external_multiple_structure */ public static function get_sessions_returns() { - $statuses = array('id' => new external_value(PARAM_INT, 'Status id.'), - 'attendanceid' => new external_value(PARAM_INT, 'Attendance id.'), - 'acronym' => new external_value(PARAM_TEXT, 'Status acronym.'), - 'description' => new external_value(PARAM_TEXT, 'Status description.'), - 'grade' => new external_value(PARAM_FLOAT, 'Status grade.'), - 'visible' => new external_value(PARAM_INT, 'Status visibility.'), - 'deleted' => new external_value(PARAM_INT, 'informs if this session was deleted.'), - 'setnumber' => new external_value(PARAM_INT, 'Set number.')); - - $users = array('id' => new external_value(PARAM_INT, 'User id.'), - 'firstname' => new external_value(PARAM_TEXT, 'User first name.'), - 'lastname' => new external_value(PARAM_TEXT, 'User last name.')); - - $attendancelog = array('studentid' => new external_value(PARAM_INT, 'Student id.'), - 'statusid' => new external_value(PARAM_TEXT, 'Status id (last time).'), - 'remarks' => new external_value(PARAM_TEXT, 'Last remark.'), - 'id' => new external_value(PARAM_TEXT, 'log id.')); - - $session = self::get_session_structure(); - $session['courseid'] = new external_value(PARAM_INT, 'Course moodle id.'); - $session['statuses'] = new external_multiple_structure(new external_single_structure($statuses)); - $session['attendance_log'] = new external_multiple_structure(new external_single_structure($attendancelog)); - $session['users'] = new external_multiple_structure(new external_single_structure($users)); - - return new external_multiple_structure(new external_single_structure(($session))); + return new external_multiple_structure(self::get_session_returns()); } /** From a781fb8fef5bda5c714f822ca6de82a65dd33478 Mon Sep 17 00:00:00 2001 From: Peter Rundqvist Date: Mon, 6 Sep 2021 09:00:12 +0200 Subject: [PATCH 3/5] Added unit test "test_get_sessions" --- tests/externallib_test.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/externallib_test.php b/tests/externallib_test.php index e7262e8..a97c9c3 100644 --- a/tests/externallib_test.php +++ b/tests/externallib_test.php @@ -471,4 +471,40 @@ 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'])); + } + } + } + } + + } } From 7721cce204b41f130288dc7b0dc503c2e59fdc98 Mon Sep 17 00:00:00 2001 From: Peter Rundqvist Date: Tue, 7 Sep 2021 09:12:49 +0200 Subject: [PATCH 4/5] Fixed codechecker flagged errors --- classes/attendance_webservices_handler.php | 2 +- tests/externallib_test.php | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/classes/attendance_webservices_handler.php b/classes/attendance_webservices_handler.php index 2439bc7..d0c0d7a 100644 --- a/classes/attendance_webservices_handler.php +++ b/classes/attendance_webservices_handler.php @@ -172,7 +172,7 @@ class attendance_handler { $sessionsinfo = array(); - foreach($sessions as $session) { + foreach ($sessions as $session) { $sessionsinfo[$session->id] = self::get_session($session->id); } diff --git a/tests/externallib_test.php b/tests/externallib_test.php index a97c9c3..3a70d8a 100644 --- a/tests/externallib_test.php +++ b/tests/externallib_test.php @@ -479,21 +479,21 @@ class mod_attendance_external_testcase extends externallib_advanced_testcase { $courseswithsessions = external_api::clean_returnvalue(mod_attendance_external::get_courses_with_today_sessions_returns(), $courseswithsessions); - foreach($courseswithsessions as $course) { + foreach ($courseswithsessions as $course) { $attendanceinstances = $course['attendance_instances']; - foreach($attendanceinstances as $attendanceinstance) { + foreach ($attendanceinstances as $attendanceinstance) { $sessionsinfo = $attendanceinstance['today_sessions']; - foreach($sessionsinfo as $sessioninfo) { + 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) { + foreach ($sessions as $session) { $sessiontocompareagainst = attendance_handler::get_session($session['id']); $sessiontocompareagainst = external_api::clean_returnvalue(mod_attendance_external::get_session_returns(), $sessiontocompareagainst); @@ -504,7 +504,6 @@ class mod_attendance_external_testcase extends externallib_advanced_testcase { } } } - } - + } } } From b84db111a17ffb9c4d1fee0e2435673b53d349c9 Mon Sep 17 00:00:00 2001 From: Peter Rundqvist Date: Wed, 8 Sep 2021 10:56:49 +0200 Subject: [PATCH 5/5] Fixed codechecker reported issues --- externallib.php | 2 +- tests/externallib_test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/externallib.php b/externallib.php index 0adabfc..d7cbfb6 100644 --- a/externallib.php +++ b/externallib.php @@ -576,7 +576,7 @@ class mod_attendance_external extends external_api { $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 3a70d8a..18e7ee0 100644 --- a/tests/externallib_test.php +++ b/tests/externallib_test.php @@ -504,6 +504,6 @@ class mod_attendance_external_testcase extends externallib_advanced_testcase { } } } - } + } } }