Browse Source

Fix #348 - tidy up user notifications when user deleted.

MOODLE_36_STABLE
Dan Marsden 6 years ago
parent
commit
2a2acd50ae
  1. 29
      classes/privacy/provider.php
  2. 15
      classes/task/notify.php
  3. 29
      lib.php

29
classes/privacy/provider.php

@ -244,12 +244,6 @@ final class provider implements
$inparams $inparams
); );
// Delete all warnings.
$DB->delete_records_select(
'attendance_warning_done',
"notifyid $insql",
$inparams
);
$DB->delete_records_select( $DB->delete_records_select(
'attendance_warning_done', 'attendance_warning_done',
"userid $insql", "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. * @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) { 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. // Get all warnings because the user could have their ID listed in the thirdpartyemails column as a comma delimited string.
$warnings = $DB->get_records( $warnings = $DB->get_records(
@ -435,24 +430,7 @@ final class provider implements
return; return;
} }
// Update the third party emails list for all the relevant warnings. attendance_remove_user_from_thirdpartyemails($warnings, $userid);
$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);
}
// Delete any record of the user being notified. // Delete any record of the user being notified.
list($warningssql, $warningsparams) = $DB->get_in_or_equal(array_keys($warnings), SQL_PARAMS_NAMED); 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 string $path The path in the export (relative to the current context).
* @param array $attendances Array of attendances to export the logs for. * @param array $attendances Array of attendances to export the logs for.
*/ */
*/
private static function export_attendance_logs(string $path, array $attendances) { private static function export_attendance_logs(string $path, array $attendances) {
$attendancesbycontextid = self::group_by_property($attendances, 'contextid'); $attendancesbycontextid = self::group_by_property($attendances, 'contextid');

15
classes/task/notify.php

@ -25,6 +25,7 @@
namespace mod_attendance\task; namespace mod_attendance\task;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/attendance/lib.php');
require_once($CFG->dirroot . '/mod/attendance/locallib.php'); require_once($CFG->dirroot . '/mod/attendance/locallib.php');
/** /**
* Task class * 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. // 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)) { if (!empty($record->thirdpartyemails)) {
$sendto = explode(',', $record->thirdpartyemails); $sendto = explode(',', $record->thirdpartyemails);
$record->percent = round($record->percent * 100)."%"; $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. // Probably an extra comma in the thirdpartyusers field.
continue; 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. // Check user is allowed to receive warningemails.
if (has_capability('mod/attendance:warningemails', $context, $senduser)) { if (has_capability('mod/attendance:warningemails', $context, $senduser)) {
if (empty($thirdpartynotifications[$senduser])) { if (empty($thirdpartynotifications[$senduser])) {
@ -138,6 +143,16 @@ class notify extends \core\task\scheduled_task {
if (!empty($thirdpartynotifications)) { if (!empty($thirdpartynotifications)) {
foreach ($thirdpartynotifications as $sendid => $notifications) { foreach ($thirdpartynotifications as $sendid => $notifications) {
$user = $DB->get_record('user', array('id' => $sendid)); $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(); $from = \core_user::get_noreply_user();
$oldforcelang = force_current_language($user->lang); $oldforcelang = force_current_language($user->lang);

29
lib.php

@ -499,3 +499,32 @@ function attendance_print_settings_tabs($selected = 'settings') {
return $tabmenu; 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);
}
}
Loading…
Cancel
Save