diff --git a/lib/classes/event/user_deleted.php b/lib/classes/event/user_deleted.php index 545d691a9ea..9457519c535 100644 --- a/lib/classes/event/user_deleted.php +++ b/lib/classes/event/user_deleted.php @@ -58,7 +58,7 @@ class user_deleted extends base { * @return string */ public function get_description() { - $user = (object)$this->other['user']; + $user = $this->get_record_snapshot('user', $this->data['objectid']); return 'User profile deleted for user '.$user->firstname.' '.$user->lastname.' id ('.$user->id.')'; } @@ -77,7 +77,13 @@ class user_deleted extends base { * @return \stdClass user data. */ protected function get_legacy_eventdata() { - return (object)$this->other['user']; + $user = $this->get_record_snapshot('user', $this->data['objectid']); + $user->deleted = 0; + $user->username = $this->data['other']['username']; + $user->email = $this->data['other']['email']; + $user->idnumber = $this->data['other']['idnumber']; + $user->picture = $this->data['other']['picture']; + return $user; } /** @@ -86,8 +92,8 @@ class user_deleted extends base { * @return array */ protected function get_legacy_logdata() { - $user = (object)$this->other['user']; - return array(SITEID, 'user', 'delete', "view.php?id=$user->id", $user->firstname.' '.$user->lastname); + $user = $this->get_record_snapshot('user', $this->data['objectid']); + return array(SITEID, 'user', 'delete', "view.php?id=".$user->id, $user->firstname.' '.$user->lastname); } /** @@ -97,9 +103,29 @@ class user_deleted extends base { * @return void */ protected function validate_data() { - parent::validate_data(); - if (!isset($this->other['user'])) { - throw new \coding_exception('user must be set in $other.'); + global $CFG; + + if ($CFG->debugdeveloper) { + parent::validate_data(); + if (!isset($this->other['username'])) { + throw new \coding_exception('username must be set in $other.'); + } + + if (!isset($this->other['email'])) { + throw new \coding_exception('email must be set in $other.'); + } + + if (!isset($this->other['idnumber'])) { + throw new \coding_exception('idnumber must be set in $other.'); + } + + if (!isset($this->other['picture'])) { + throw new \coding_exception('picture must be set in $other.'); + } + + if (!isset($this->other['mnethostid'])) { + throw new \coding_exception('mnethostid must be set in $other.'); + } } } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 744274ad86f..3977dbee67f 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4131,6 +4131,9 @@ function delete_user(stdClass $user) { return false; } + // Keep user record before updating it, as we have to pass this to user_deleted event. + $olduser = clone $user; + // Keep a copy of user context, we need it for event. $usercontext = context_user::instance($user->id); @@ -4210,10 +4213,16 @@ function delete_user(stdClass $user) { array( 'objectid' => $user->id, 'context' => $usercontext, - 'other' => array('user' => (array)clone $user) + 'other' => array( + 'username' => $user->username, + 'email' => $user->email, + 'idnumber' => $user->idnumber, + 'picture' => $user->picture, + 'mnethostid' => $user->mnethostid + ) ) ); - $event->add_record_snapshot('user', $updateuser); + $event->add_record_snapshot('user', $olduser); $event->trigger(); // We will update the user's timemodified, as it will be passed to the user_deleted event, which diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 48ec5fdb284..54dac60d67d 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -1882,6 +1882,13 @@ class core_moodlelib_testcase extends advanced_testcase { $this->assertEventLegacyData($user, $event); $expectedlogdata = array(SITEID, 'user', 'delete', "view.php?id=$user->id", $user->firstname.' '.$user->lastname); $this->assertEventLegacyLogData($expectedlogdata, $event); + $eventdata = $event->get_data(); + $this->assertSame($eventdata['other']['username'], $user->username); + $this->assertSame($eventdata['other']['email'], $user->email); + $this->assertSame($eventdata['other']['idnumber'], $user->idnumber); + $this->assertSame($eventdata['other']['picture'], $user->picture); + $this->assertSame($eventdata['other']['mnethostid'], $user->mnethostid); + $this->assertEquals($user, $event->get_record_snapshot('user', $event->objectid)); // Try invalid params. $record = new stdClass();