Merge branch 'MDL-60126-master-3' of git://github.com/mihailges/moodle

This commit is contained in:
Andrew Nicols 2020-02-04 10:33:19 +08:00
commit 7be4a3fba1
5 changed files with 124 additions and 1 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

@ -207,4 +207,47 @@ class core_competency_hooks_testcase extends advanced_testcase {
$this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t1->get('id'))));
$this->assertEquals(0, \core_competency\template_cohort::count_records(array('templateid' => $t2->get('id'))));
}
public function test_hook_user_deleted() {
$this->resetAfterTest();
$dg = $this->getDataGenerator();
$ccg = $dg->get_plugin_generator('core_competency');
$u1 = $dg->create_user();
$framework = $ccg->create_framework();
$comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
$comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
$c1 = $dg->create_course();
$cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
$cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
$assign1a = $dg->create_module('assign', ['course' => $c1]);
$assign1b = $dg->create_module('assign', ['course' => $c1]);
$cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
$cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
$ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
'userid' => $u1->id]);
$ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
'userid' => $u1->id]);
$c2 = $dg->create_course();
$cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
$cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
$assign2a = $dg->create_module('assign', ['course' => $c2]);
$assign2b = $dg->create_module('assign', ['course' => $c2]);
$cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
$cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
$ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
'userid' => $u1->id]);
$ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
'userid' => $u1->id]);
reset_course_userdata((object) ['id' => $c1->id, 'reset_competency_ratings' => true]);
delete_user($u1);
// Assert the records don't exist anymore.
$this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));
}
}

View File

@ -2172,5 +2172,48 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2020011700.02);
}
if ($oldversion < 2020013000.01) {
global $DB;
// Delete any associated files.
$fs = get_file_storage();
$sql = "SELECT cuc.id, cuc.userid
FROM {competency_usercomp} cuc
LEFT JOIN {user} u ON cuc.userid = u.id
WHERE u.deleted = 1";
$usercompetencies = $DB->get_records_sql($sql);
foreach ($usercompetencies as $usercomp) {
$DB->delete_records('competency_evidence', ['usercompetencyid' => $usercomp->id]);
$DB->delete_records('competency_usercompcourse', ['userid' => $usercomp->userid]);
$DB->delete_records('competency_usercompplan', ['userid' => $usercomp->userid]);
$DB->delete_records('competency_usercomp', ['userid' => $usercomp->userid]);
}
$sql = "SELECT cue.id, cue.userid
FROM {competency_userevidence} cue
LEFT JOIN {user} u ON cue.userid = u.id
WHERE u.deleted = 1";
$userevidences = $DB->get_records_sql($sql);
foreach ($userevidences as $userevidence) {
$DB->delete_records('competency_userevidencecomp', ['userevidenceid' => $userevidence->id]);
$DB->delete_records('competency_userevidence', ['id' => $userevidence->id]);
$context = context_user::instance($userevidence->userid);
$fs->delete_area_files($context->id, 'core_competency', 'userevidence', $userevidence->id);
}
$sql = "SELECT cp.id
FROM {competency_plan} cp
LEFT JOIN {user} u ON cp.userid = u.id
WHERE u.deleted = 1";
$userplans = $DB->get_records_sql($sql);
foreach ($userplans as $userplan) {
$DB->delete_records('competency_plancomp', ['planid' => $userplan->id]);
$DB->delete_records('competency_plan', ['id' => $userplan->id]);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2020013000.01);
}
return true;
}

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.

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2020013000.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2020013000.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '3.9dev (Build: 20200130)'; // Human-friendly version name