From c184b2d284e3d7b7ca61792ba4f62a6b2b4f8f1c Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 7 Feb 2023 13:26:55 +0000 Subject: [PATCH] MDL-68347 course: trigger event if user was created during restore --- backup/moodle2/restore_stepslib.php | 2 +- backup/util/dbops/restore_dbops.class.php | 6 +++- course/tests/backup/restore_test.php | 36 +++++++++++++++++++++++ lib/classes/event/user_created.php | 28 +++++++++++++++++- lib/upgrade.txt | 2 ++ 5 files changed, 71 insertions(+), 3 deletions(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index b1b9768079a..aca0c086e5d 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -1142,7 +1142,7 @@ class restore_create_included_users extends restore_execution_step { protected function define_execution() { restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), - $this->task->get_userid(), $this->task->get_progress()); + $this->task->get_userid(), $this->task->get_progress(), $this->task->get_courseid()); } } diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 9042c4f4fde..fa85101ebc0 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -1172,9 +1172,10 @@ abstract class restore_dbops { * @param string $restoreid Restore ID * @param int $userid Default userid for files * @param \core\progress\base $progress Object used for progress tracking + * @param int $courseid Course ID */ public static function create_included_users($basepath, $restoreid, $userid, - \core\progress\base $progress) { + \core\progress\base $progress, int $courseid = 0) { global $CFG, $DB; require_once($CFG->dirroot.'/user/profile/lib.php'); $progress->start_progress('Creating included users'); @@ -1296,6 +1297,9 @@ abstract class restore_dbops { } } + // Trigger event that user was created. + \core\event\user_created::create_from_user_id_on_restore($newuserid, $restoreid, $courseid)->trigger(); + // Process tags if (core_tag_tag::is_enabled('core', 'user') && isset($user->tags)) { // If enabled in server and present in backup. $tags = array(); diff --git a/course/tests/backup/restore_test.php b/course/tests/backup/restore_test.php index 550504007a9..85bf9004d24 100644 --- a/course/tests/backup/restore_test.php +++ b/course/tests/backup/restore_test.php @@ -388,6 +388,42 @@ class restore_test extends \advanced_testcase { $this->assertEquals($startdate, $c2->startdate); } + public function test_restore_course_with_users() { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + $dg = $this->getDataGenerator(); + + // Create a user and a course, enrol user in the course. Backup this course. + $startdate = mktime(12, 0, 0, 7, 1, 2016); // 01-Jul-2016. + $u1 = $dg->create_user(['firstname' => 'Olivia']); + $c1 = $dg->create_course(['shortname' => 'SN', 'fullname' => 'FN', 'startdate' => $startdate, + 'summary' => 'DESC', 'summaryformat' => FORMAT_MOODLE]); + $dg->enrol_user($u1->id, $c1->id, 'student'); + $backupid = $this->backup_course($c1->id); + + // Delete the course and the user completely. + delete_course($c1, false); + delete_user($u1); + $DB->delete_records('user', ['id' => $u1->id]); + + // Now restore this course, the user will be created and event user_created event will be triggered. + $sink = $this->redirectEvents(); + $c2 = $this->restore_to_new_course($backupid); + $events = $sink->get_events(); + $sink->close(); + + $user = $DB->get_record('user', ['firstname' => 'Olivia'], '*', MUST_EXIST); + $events = array_values(array_filter($events, function(\core\event\base $event) { + return is_a($event, \core\event\user_created::class); + })); + $this->assertEquals(1, count($events)); + $this->assertEquals($user->id, $events[0]->relateduserid); + $this->assertEquals($c2->id, $events[0]->other['courseid']); + $this->assertStringContainsString("during restore of the course with id '{$c2->id}'", + $events[0]->get_description()); + } + public function test_restore_course_info_in_existing_course() { global $DB; $this->resetAfterTest(); diff --git a/lib/classes/event/user_created.php b/lib/classes/event/user_created.php index 2fb3200de7f..9f93dae7c2e 100644 --- a/lib/classes/event/user_created.php +++ b/lib/classes/event/user_created.php @@ -60,7 +60,12 @@ class user_created extends base { * @return string */ public function get_description() { - return "The user with id '$this->userid' created the user with id '$this->objectid'."; + $description = "The user with id '$this->userid' created the user with id '$this->objectid'"; + if (!empty($this->other['restoreid'])) { + $courseid = $this->other['courseid'] ?? 0; + return "{$description} during restore of the course with id '$courseid'."; + } + return "{$description}."; } /** @@ -134,6 +139,27 @@ class user_created extends base { return $event; } + /** + * Create instance of event when user is created during the course restore process. + * + * @param int $userid id of user + * @param string $restoreid + * @param int $courseid + * @return user_created + */ + public static function create_from_user_id_on_restore(int $userid, string $restoreid, + int $courseid): user_created { + $data = [ + 'objectid' => $userid, + 'relateduserid' => $userid, + 'context' => \context_user::instance($userid), + 'other' => ['restoreid' => $restoreid, 'courseid' => $courseid], + ]; + + // Create user_created event. + return self::create($data); + } + public static function get_objectid_mapping() { return array('db' => 'user', 'restore' => 'user'); } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index e4a5232abc6..a4fa5ee5e69 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -62,6 +62,8 @@ information provided here is intended especially for developers. description being incorrectly passed for the $required parameter). A debugging notice will be shown when such cases occur. * The moodle-core-popuphelp YUI modal has been removed. It has not been actively used in Moodle since 3.3. It should be replaced with appropriate ESM/AMD JavaScript. * The moodle-core-tooltip YUI modal has been removed. It should be replaced with appropriate ESM/AMD JavaScript. +* Event \core\event\user_created is now triggered if the user is created during course restore. In this case + $event->other has properties 'restoreid' and 'courseid'. === 4.1 ===