MDL-45544 events: Add event user_profile_viewed trigger to user/profile.php.
authorSimey Lameze <lameze@gmail.com>
Wed, 21 May 2014 02:11:12 +0000 (10:11 +0800)
committerSimey Lameze <lameze@gmail.com>
Mon, 26 May 2014 04:51:40 +0000 (12:51 +0800)
lib/classes/event/user_profile_viewed.php
lib/tests/events_test.php
user/profile.php

index de88628..ec11331 100644 (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;
     }
 }
index ca0e178..06fbdb6 100644 (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);
+    }
 }
index e30bc8d..88fb9a7 100644 (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">';