MDL-60126 competency: delete content upon user delete

This commit is contained in:
Simey Lameze 2017-09-14 10:03:51 +08:00 committed by Mihail Geshoski
parent cb38ab1e39
commit 2595d450e0
2 changed files with 37 additions and 0 deletions

View File

@ -4802,6 +4802,40 @@ class api {
$DB->delete_records(template_cohort::TABLE, array('cohortid' => $cohort->id));
}
/**
* Action to perform when a user is deleted.
*
* @param int $userid The user id.
*/
public static function hook_user_deleted($userid) {
global $DB;
$usercompetencies = $DB->get_records(user_competency::TABLE, ['userid' => $userid], '', 'id');
foreach ($usercompetencies as $usercomp) {
$DB->delete_records(evidence::TABLE, ['usercompetencyid' => $usercomp->id]);
}
$DB->delete_records(user_competency::TABLE, ['userid' => $userid]);
$DB->delete_records(user_competency_course::TABLE, ['userid' => $userid]);
$DB->delete_records(user_competency_plan::TABLE, ['userid' => $userid]);
// Delete any associated files.
$fs = get_file_storage();
$context = context_user::instance($userid);
$userevidences = $DB->get_records(user_evidence::TABLE, ['userid' => $userid], '', 'id');
foreach ($userevidences as $userevidence) {
$DB->delete_records(user_evidence_competency::TABLE, ['userevidenceid' => $userevidence->id]);
$DB->delete_records(user_evidence::TABLE, ['id' => $userevidence->id]);
$fs->delete_area_files($context->id, 'core_competency', 'userevidence', $userevidence->id);
}
$userplans = $DB->get_records(plan::TABLE, ['userid' => $userid], '', 'id');
foreach ($userplans as $userplan) {
$DB->delete_records(plan_competency::TABLE, ['planid' => $userplan->id]);
$DB->delete_records(plan::TABLE, ['id' => $userplan->id]);
}
}
/**
* Manually grade a user competency.
*

View File

@ -4248,6 +4248,9 @@ function delete_user(stdClass $user) {
// This might be slow but it is really needed - modules might do some extra cleanup!
role_unassign_all(array('userid' => $user->id));
// Notify the competency subsystem.
\core_competency\api::hook_user_deleted($user->id);
// Now do a brute force cleanup.
// Delete all user events and subscription events.