MDL-45544 events: Add event user_profile_viewed trigger to user/profile.php.

This commit is contained in:
Simey Lameze 2014-05-21 10:11:12 +08:00
parent 99627fb3d1
commit b0ad512f95
3 changed files with 72 additions and 5 deletions

View File

@ -68,8 +68,9 @@ class user_profile_viewed extends base {
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the profile for the user with id '$this->relateduserid' in the course " .
"with id '$this->courseid'.";
$desc = "The user with id '$this->userid' viewed the profile for the user with id '$this->relateduserid'";
$desc .= ($this->contextlevel == CONTEXT_COURSE) ? " in the course with id '$this->courseid'." : ".";
return $desc;
}
/**
@ -78,7 +79,10 @@ class user_profile_viewed extends base {
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/user/view.php', array('id' => $this->relateduserid, 'course' => $this->courseid));
if ($this->contextlevel == CONTEXT_COURSE) {
return new \moodle_url('/user/view.php', array('id' => $this->relateduserid, 'course' => $this->courseid));
}
return new \moodle_url('/user/profile.php', array('id' => $this->relateduserid));
}
/**
@ -87,7 +91,10 @@ class user_profile_viewed extends base {
* @return array
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'user', 'view', 'view.php?id=' . $this->relateduserid . '&course=' .
$this->courseid, $this->relateduserid);
if ($this->contextlevel == CONTEXT_COURSE) {
return array($this->courseid, 'user', 'view', 'view.php?id=' . $this->relateduserid . '&course=' .
$this->courseid, $this->relateduserid);
}
return null;
}
}

View File

@ -287,4 +287,55 @@ class core_events_testcase extends advanced_testcase {
$this->assertEquals($url, $event->get_url());
$event->get_name();
}
public function test_user_profile_viewed() {
$this->resetAfterTest();
$this->setAdminUser();
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
// User profile viewed in course context.
$eventparams = array(
'objectid' => $user->id,
'relateduserid' => $user->id,
'courseid' => $course->id,
'context' => $coursecontext,
'other' => array(
'courseid' => $course->id,
'courseshortname' => $course->shortname,
'coursefullname' => $course->fullname
)
);
$event = \core\event\user_profile_viewed::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\core\event\user_profile_viewed', $event);
$log = array($course->id, 'user', 'view', 'view.php?id=' . $user->id . '&course=' . $course->id, $user->id);
$this->assertEventLegacyLogData($log, $event);
$this->assertEventContextNotUsed($event);
// User profile viewed in user context.
$usercontext = context_user::instance($user->id);
$eventparams['context'] = $usercontext;
unset($eventparams['courseid'], $eventparams['other']);
$event = \core\event\user_profile_viewed::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\core\event\user_profile_viewed', $event);
$expected = null;
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}

View File

@ -211,6 +211,15 @@ if ($currentpage->userid == 0) {
$CFG->blockmanagerclass = 'my_syspage_block_manager';
}
// Trigger a user profile viewed event.
$event = \core\event\user_profile_viewed::create(array(
'objectid' => $user->id,
'relateduserid' => $user->id,
'context' => $usercontext
));
$event->add_record_snapshot('user', $user);
$event->trigger();
// TODO WORK OUT WHERE THE NAV BAR IS!
echo $OUTPUT->header();
echo '<div class="userprofile">';