MDL-42556 Events: passing modified user info to user_deleted event

Deleting user modifies few fields and leave rest as un-modified
So passing modified data to event, to keep it small
This commit is contained in:
Rajesh Taneja 2013-10-28 11:48:40 +08:00
parent 68291f2d57
commit abedc1a84c
3 changed files with 51 additions and 9 deletions

View File

@ -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.');
}
}
}
}

View File

@ -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

View File

@ -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();