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

This commit is contained in:
Simey Lameze
2014-05-26 14:10:59 +08:00
parent 4a9dcf3f17
commit 7874aa6eb7
3 changed files with 70 additions and 5 deletions
+12 -5
View File
@@ -53,8 +53,9 @@ class user_profile_viewed extends base {
* @return string
*/
public function get_description() {
return 'User ' . $this->userid . ' viewed the profile for user ' . $this->relateduserid . ' in the course ' .
$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;
}
/**
@@ -63,7 +64,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));
}
/**
@@ -72,7 +76,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;
}
}
+49
View File
@@ -732,4 +732,53 @@ class core_event_testcase extends advanced_testcase {
$this->assertSame($event->get_data(), $data);
}
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);
// 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);
}
}
+9
View File
@@ -199,6 +199,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();