* @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;
}
/**
* @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));
}
/**
* @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;
}
}
$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);
+ }
}
$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">';