mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
MDL-68347 course: trigger event if user was created during restore
This commit is contained in:
parent
8503f2cfd8
commit
c184b2d284
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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');
|
||||
}
|
||||
|
@ -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 ===
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user