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

This commit is contained in:
Eloy Lafuente (stronk7) 2015-04-07 17:29:15 +02:00
commit 522eff0d30
7 changed files with 189 additions and 21 deletions

View file

@ -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()
)
);
}
}
/**