From b0ad512f9531cb94c647002e60a4deaaf5c10303 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 21 May 2014 10:11:12 +0800 Subject: [PATCH] MDL-45544 events: Add event user_profile_viewed trigger to user/profile.php. --- lib/classes/event/user_profile_viewed.php | 17 +++++--- lib/tests/events_test.php | 51 +++++++++++++++++++++++ user/profile.php | 9 ++++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/lib/classes/event/user_profile_viewed.php b/lib/classes/event/user_profile_viewed.php index de886281dc1..ec11331546b 100644 --- a/lib/classes/event/user_profile_viewed.php +++ b/lib/classes/event/user_profile_viewed.php @@ -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; } } diff --git a/lib/tests/events_test.php b/lib/tests/events_test.php index ca0e178d9de..06fbdb69e93 100644 --- a/lib/tests/events_test.php +++ b/lib/tests/events_test.php @@ -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); + } } diff --git a/user/profile.php b/user/profile.php index e30bc8d72bd..88fb9a78f3b 100644 --- a/user/profile.php +++ b/user/profile.php @@ -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 '
';