Commit 522eff0d authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

Merge branch 'MDL-49499-master' of git://github.com/jleyva/moodle

parents 63843857 5770f10e
......@@ -512,6 +512,15 @@ $functions = array(
'capabilities' => 'moodle/course:viewparticipants',
),
'core_user_view_user_profile' => array(
'classname' => 'core_user_external',
'methodname' => 'view_user_profile',
'classpath' => 'user/externallib.php',
'description' => 'Simulates the web-interface view of user/view.php and user/profile.php (triggering events).',
'type' => 'write',
'capabilities' => 'moodle/user:viewdetails',
),
// === enrol related functions ===
'core_enrol_get_enrolled_users_with_capability' => array(
......@@ -1106,6 +1115,7 @@ $services = array(
'core_message_mark_message_read',
'core_notes_view_notes',
'mod_forum_view_forum_discussion',
'core_user_view_user_profile',
),
'enabled' => 0,
'restrictedusers' => 0,
......
......@@ -1318,6 +1318,110 @@ class core_user_external extends external_api {
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function view_user_profile_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED),
'courseid' => new external_value(PARAM_INT, 'id of the course, default site course', VALUE_DEFAULT, 0)
)
);
}
/**
* Simulate the /user/index.php and /user/profile.php web interface page triggering events
*
* @param int $userid id of user
* @param int $courseid id of course
* @return array of warnings and status result
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function view_user_profile($userid, $courseid = 0) {
global $CFG, $USER;
require_once($CFG->dirroot . "/user/profile/lib.php");
$params = self::validate_parameters(self::view_user_profile_parameters(),
array(
'userid' => $userid,
'courseid' => $courseid
));
$warnings = array();
if (empty($params['userid'])) {
$params['userid'] = $USER->id;
}
if (empty($params['courseid'])) {
$params['courseid'] = SITEID;
}
$course = get_course($params['courseid']);
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
// Can not view profile of guest - thre is nothing to see there.
throw new moodle_exception('invaliduserid');
}
if ($course->id == SITEID) {
$coursecontext = context_system::instance();;
} else {
$coursecontext = context_course::instance($course->id);
}
self::validate_context($coursecontext);
$currentuser = $USER->id == $user->id;
$usercontext = context_user::instance($user->id);
if (!$currentuser and
!has_capability('moodle/user:viewdetails', $coursecontext) and
!has_capability('moodle/user:viewdetails', $usercontext)) {
throw new moodle_exception('cannotviewprofile');
}
// Case like user/profile.php.
if ($course->id == SITEID) {
profile_view($user, $usercontext);
} else {
// Case like user/view.php.
if (!$currentuser and !is_enrolled($coursecontext, $user->id)) {
throw new moodle_exception('notenrolledprofile');
}
profile_view($user, $coursecontext, $course);
}
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function view_user_profile_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}
}
/**
......
......@@ -225,13 +225,7 @@ if ($currentpage->userid == 0) {
}
// 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();
profile_view($user, $usercontext);
// TODO WORK OUT WHERE THE NAV BAR IS!
echo $OUTPUT->header();
......
......@@ -621,4 +621,33 @@ function profile_load_custom_fields($user) {
$user->profile = (array)profile_user_record($user->id);
}
/**
* Trigger a user profile viewed event.
*
* @param stdClass $user user object
* @param stdClass $context context object (course or user)
* @param stdClass $course course object
* @since Moodle 2.9
*/
function profile_view($user, $context, $course = null) {
$eventdata = array(
'objectid' => $user->id,
'relateduserid' => $user->id,
'context' => $context
);
if (!empty($course)) {
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array(
'courseid' => $course->id,
'courseshortname' => $course->shortname,
'coursefullname' => $course->fullname
);
}
$event = \core\event\user_profile_viewed::create($eventdata);
$event->add_record_snapshot('user', $user);
$event->trigger();
}
......@@ -94,4 +94,47 @@ class core_user_profilelib_testcase extends advanced_testcase {
$this->assertNotNull($formfield);
}
}
/**
* Test profile_view function
*/
public function test_profile_view() {
global $USER;
$this->resetAfterTest();
// Course without sections.
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$user = $this->getDataGenerator()->create_user();
$usercontext = context_user::instance($user->id);
$this->setUser($user);
// Redirect events to the sink, so we can recover them later.
$sink = $this->redirectEvents();
profile_view($user, $context, $course);
$events = $sink->get_events();
$event = reset($events);
// Check the event details are correct.
$this->assertInstanceOf('\core\event\user_profile_viewed', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($course->id, $event->other['courseid']);
$this->assertEquals($course->shortname, $event->other['courseshortname']);
$this->assertEquals($course->fullname, $event->other['coursefullname']);
profile_view($user, $usercontext);
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();
$this->assertInstanceOf('\core\event\user_profile_viewed', $event);
$this->assertEquals($usercontext, $event->get_context());
$this->assertEquals($user->id, $event->relateduserid);
}
}
......@@ -209,19 +209,7 @@ if ($user->deleted) {
// OK, security out the way, now we are showing the user.
// Trigger a user profile viewed event.
$event = \core\event\user_profile_viewed::create(array(
'objectid' => $user->id,
'relateduserid' => $user->id,
'courseid' => $course->id,
'context' => $coursecontext,
'other' => array(
'courseid' => $course->id,
'courseshortname' => $course->shortname,
'coursefullname' => $course->fullname
)
));
$event->add_record_snapshot('user', $user);
$event->trigger();
profile_view($user, $coursecontext, $course);
// Get the hidden field list.
if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
......
......@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015040700.03; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015040200.04; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment