From 2595d450e0e97e3fe5050f87a2d95eeb8288386f Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 14 Sep 2017 10:03:51 +0800 Subject: [PATCH] MDL-60126 competency: delete content upon user delete --- competency/classes/api.php | 34 ++++++++++++++++++++++++++++++++++ lib/moodlelib.php | 3 +++ 2 files changed, 37 insertions(+) diff --git a/competency/classes/api.php b/competency/classes/api.php index 600f8f823b1..ba2266228fc 100644 --- a/competency/classes/api.php +++ b/competency/classes/api.php @@ -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. * diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 4c6d4b9006a..8f86592e91b 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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.