From 982acd769333a76709f88430ba617a15161a2b09 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Wed, 24 Oct 2018 14:38:12 +1300 Subject: [PATCH] Add new functions for 3.6 GDPR class. --- classes/privacy/provider.php | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index d8c6e22..7aeb9fa 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -117,6 +117,46 @@ final class provider implements ); } + /** + * Get the list of users who have data within a context. + * + * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination. + */ + public static function get_users_in_context(userlist $userlist) { + $context = $userlist->get_context(); + + if (!is_a($context, \context_module::class)) { + return; + } + + $sql = "SELECT al.studentid + FROM {course_modules} cm + JOIN {modules} m ON cm.module = m.id AND m.name = 'attendance' + JOIN {attendance} a ON cm.instance = a.id + JOIN {attendance_sessions} asess ON asess.attendanceid = a.id + JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel + JOIN {attendance_log} al ON asess.id = al.sessionid + WHERE ctx.id = :contextid"; + + $params = [ + 'contextlevel' => CONTEXT_MODULE, + 'contextid' => $context->id, + ]; + + $userlist->add_from_sql('userid', $sql, $params); + + $sql = "SELECT al.takenby + FROM {course_modules} cm + JOIN {modules} m ON cm.module = m.id AND m.name = 'attendance' + JOIN {attendance} a ON cm.instance = a.id + JOIN {attendance_sessions} asess ON asess.attendanceid = a.id + JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel + JOIN {attendance_log} al ON asess.id = al.sessionid + WHERE ctx.id = :contextid"; + + $userlist->add_from_sql('userid', $sql, $params); + + } /** * Delete all data for all users in the specified context. * @@ -179,6 +219,52 @@ final class provider implements } } + /** + * Delete multiple users within a single context. + * + * @param approved_userlist $userlist The approved context and user information to delete information for. + */ + public static function delete_data_for_users(approved_userlist $userlist) { + global $DB; + $context = $userlist->get_context(); + + if (!is_a($context, \context_module::class)) { + return; + } + + // Prepare SQL to gather all completed IDs. + $userids = $userlist->get_userids(); + list($insql, $inparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED); + + // Delete records where user was marked as attending. + $DB->delete_records_select( + 'attendance_log', + "studentid $insql", + $inparams + ); + + // Delete all warnings. + $DB->delete_records_select( + 'attendance_warning_done', + "notifyid $insql", + $inparams + ); + $DB->delete_records_select( + 'attendance_warning_done', + "userid $insql", + $inparams + ); + + // Now for teachers remove relation for marking. + $DB->set_field_select( + 'attendance_log', + 'takenby', + 2, + "takenby $insql", + $inparams); + + } + /** * Export all user data for the specified user, in the specified contexts. *