Skip to content

Commit

Permalink
MDL-60126 competency: delete content upon user delete
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze authored and Mihail Geshoski committed Feb 4, 2020
1 parent cb38ab1 commit 2595d45
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions competency/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4802,6 +4802,40 @@ public static function hook_cohort_deleted(\stdClass $cohort) {
$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.
*
Expand Down
3 changes: 3 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2595d45

Please sign in to comment.