diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index a9571ee..48a33d6 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -244,12 +244,6 @@ final class provider implements $inparams ); - // Delete all warnings. - $DB->delete_records_select( - 'attendance_warning_done', - "notifyid $insql", - $inparams - ); $DB->delete_records_select( 'attendance_warning_done', "userid $insql", @@ -423,7 +417,8 @@ final class provider implements * @param int $attendanceid The id of the attendance instance to remove the relevant warnings from. */ private static function delete_user_from_attendance_warnings_log(int $userid, int $attendanceid) { - global $DB; + global $DB, $CFG; + require_once($CFG->dirroot.'/mod/attendance/lib.php'); // Get all warnings because the user could have their ID listed in the thirdpartyemails column as a comma delimited string. $warnings = $DB->get_records( @@ -435,24 +430,7 @@ final class provider implements return; } - // Update the third party emails list for all the relevant warnings. - $updatedwarnings = array_map( - function(stdClass $warning) use ($userid) : stdClass { - $warning->thirdpartyemails = implode(',', array_diff(explode(',', $warning->thirdpartyemails), [$userid])); - return $warning; - }, - array_filter( - $warnings, - function (stdClass $warning) use ($userid) : bool { - return in_array($userid, explode(',', $warning->thirdpartyemails)); - } - ) - ); - - // Sadly need to update each individually, no way to bulk update as all the thirdpartyemails field can be different. - foreach ($updatedwarnings as $updatedwarning) { - $DB->update_record('attendance_warning', $updatedwarning); - } + attendance_remove_user_from_thirdpartyemails($warnings, $userid); // Delete any record of the user being notified. list($warningssql, $warningsparams) = $DB->get_in_or_equal(array_keys($warnings), SQL_PARAMS_NAMED); @@ -531,6 +509,7 @@ final class provider implements * @param string $path The path in the export (relative to the current context). * @param array $attendances Array of attendances to export the logs for. */ + */ private static function export_attendance_logs(string $path, array $attendances) { $attendancesbycontextid = self::group_by_property($attendances, 'contextid'); diff --git a/classes/task/notify.php b/classes/task/notify.php index b52abe9..e857410 100644 --- a/classes/task/notify.php +++ b/classes/task/notify.php @@ -25,6 +25,7 @@ namespace mod_attendance\task; defined('MOODLE_INTERNAL') || die(); +require_once($CFG->dirroot . '/mod/attendance/lib.php'); require_once($CFG->dirroot . '/mod/attendance/locallib.php'); /** * Task class @@ -103,6 +104,7 @@ class notify extends \core\task\scheduled_task { } } // Only send one warning to this user from each attendance in this run. - flag any higher percent notifications as sent. + $thirdpartyusers = array(); if (!empty($record->thirdpartyemails)) { $sendto = explode(',', $record->thirdpartyemails); $record->percent = round($record->percent * 100)."%"; @@ -112,6 +114,9 @@ class notify extends \core\task\scheduled_task { // Probably an extra comma in the thirdpartyusers field. continue; } + // Create array of the warnings this user will recieve in case we need to clean up. + $thirdpartyusers[$senduser][] = $record->notifyid; + // Check user is allowed to receive warningemails. if (has_capability('mod/attendance:warningemails', $context, $senduser)) { if (empty($thirdpartynotifications[$senduser])) { @@ -138,6 +143,16 @@ class notify extends \core\task\scheduled_task { if (!empty($thirdpartynotifications)) { foreach ($thirdpartynotifications as $sendid => $notifications) { $user = $DB->get_record('user', array('id' => $sendid)); + if (empty($user) || !empty($user->deleted)) { + // Clean this user up and remove from the notification list. + $warnings = $DB->get_records_list('attendance_warning', 'id', $thirdpartyusers[$sendid]); + if (!empty($warnings)) { + attendance_remove_user_from_thirdpartyemails($warnings, $sendid); + } + // Don't send and skip to next notification. + continue; + } + $from = \core_user::get_noreply_user(); $oldforcelang = force_current_language($user->lang); diff --git a/lib.php b/lib.php index 0660b0d..752e295 100644 --- a/lib.php +++ b/lib.php @@ -498,4 +498,33 @@ function attendance_print_settings_tabs($selected = 'settings') { ob_end_clean(); return $tabmenu; +} + +/** + * Helper function to remove a user from the thirdpartyemails record of the attendance_warning table. + * + * @param array $warnings - list of warnings to parse. + * @param int $userid - User id of user to remove. + */ +function attendance_remove_user_from_thirdpartyemails($warnings, $userid) { + global $DB; + + // Update the third party emails list for all the relevant warnings. + $updatedwarnings = array_map( + function(stdClass $warning) use ($userid) : stdClass { + $warning->thirdpartyemails = implode(',', array_diff(explode(',', $warning->thirdpartyemails), [$userid])); + return $warning; + }, + array_filter( + $warnings, + function (stdClass $warning) use ($userid) : bool { + return in_array($userid, explode(',', $warning->thirdpartyemails)); + } + ) + ); + + // Sadly need to update each individually, no way to bulk update as all the thirdpartyemails field can be different. + foreach ($updatedwarnings as $updatedwarning) { + $DB->update_record('attendance_warning', $updatedwarning); + } } \ No newline at end of file