Merge branch 'MDL-70169-39' of git://github.com/lameze/moodle into MOODLE_39_STABLE
[moodle.git] / user / view.php
blob921a28c309d620c8dd6d443b14d3c192e129474c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Display profile for a particular user
20 * @package core_user
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../config.php");
26 require_once($CFG->dirroot.'/user/profile/lib.php');
27 require_once($CFG->dirroot.'/user/lib.php');
28 require_once($CFG->libdir . '/filelib.php');
29 require_once($CFG->libdir . '/badgeslib.php');
31 $id = optional_param('id', 0, PARAM_INT); // User id.
32 $courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site).
33 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
35 // See your own profile by default.
36 if (empty($id)) {
37 require_login();
38 $id = $USER->id;
41 if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php.
42 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect.
45 $PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
47 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
48 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
49 $currentuser = ($user->id == $USER->id);
51 $systemcontext = context_system::instance();
52 $coursecontext = context_course::instance($course->id);
53 $usercontext = context_user::instance($user->id, IGNORE_MISSING);
55 // Check we are not trying to view guest's profile.
56 if (isguestuser($user)) {
57 // Can not view profile of guest - thre is nothing to see there.
58 print_error('invaliduserid');
61 $PAGE->set_context($coursecontext);
63 if (!empty($CFG->forceloginforprofiles)) {
64 require_login(); // We can not log in to course due to the parent hack below.
66 // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
67 if (isguestuser()) {
68 echo $OUTPUT->header();
69 echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
70 get_login_url(),
71 $CFG->wwwroot);
72 echo $OUTPUT->footer();
73 die;
77 $PAGE->set_course($course);
78 $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course.
79 $PAGE->add_body_class('path-user'); // So we can style it independently.
80 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
82 // Set the Moodle docs path explicitly because the default behaviour
83 // of inhereting the pagetype will lead to an incorrect docs location.
84 $PAGE->set_docs_path('user/profile');
86 $isparent = false;
88 if (!$currentuser and !$user->deleted
89 and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
90 and has_capability('moodle/user:viewdetails', $usercontext)) {
91 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
92 // this way they may access the profile where they get overview of grades and child activity in course,
93 // please note this is just a guess!
94 require_login();
95 $isparent = true;
96 $PAGE->navigation->set_userid_for_parent_checks($id);
97 } else {
98 // Normal course.
99 require_login($course);
100 // What to do with users temporary accessing this course? should they see the details?
103 $strpersonalprofile = get_string('personalprofile');
104 $strparticipants = get_string("participants");
105 $struser = get_string("user");
107 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
109 // Now test the actual capabilities and enrolment in course.
110 if ($currentuser) {
111 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
112 // Need to have full access to a course to see the rest of own info.
113 $referer = get_local_referer(false);
114 if (!empty($referer)) {
115 redirect($referer, get_string('notenrolled', '', $fullname));
117 echo $OUTPUT->header();
118 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
119 echo $OUTPUT->footer();
120 die;
123 } else {
124 // Somebody else.
125 $PAGE->set_title("$strpersonalprofile: ");
126 $PAGE->set_heading("$strpersonalprofile: ");
128 // Check to see if the user can see this user's profile.
129 if (!user_can_view_profile($user, $course, $usercontext) && !$isparent) {
130 print_error('cannotviewprofile');
133 if (!is_enrolled($coursecontext, $user->id)) {
134 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
135 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
136 // or test for course:inspect capability.
137 if (has_capability('moodle/role:assign', $coursecontext)) {
138 $PAGE->navbar->add($fullname);
139 $notice = get_string('notenrolled', '', $fullname);
140 } else {
141 $PAGE->navbar->add($struser);
142 $notice = get_string('notenrolledprofile', '', $fullname);
144 $referer = get_local_referer(false);
145 if (!empty($referer)) {
146 redirect($referer, $notice);
148 echo $OUTPUT->header();
149 echo $OUTPUT->heading($notice);
150 echo $OUTPUT->footer();
151 exit;
154 if (!isloggedin() or isguestuser()) {
155 // Do not use require_login() here because we might have already used require_login($course).
156 redirect(get_login_url());
160 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
161 $PAGE->set_heading($course->fullname);
162 $PAGE->set_pagelayout('mypublic');
164 // Locate the users settings in the settings navigation and force it open.
165 // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
166 if (!$currentuser) {
167 $PAGE->navigation->extend_for_user($user);
168 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
169 $node->forceopen = true;
171 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
172 $node->forceopen = true;
174 if ($node = $PAGE->settingsnav->get('courseadmin')) {
175 $node->forceopen = false;
178 echo $OUTPUT->header();
180 echo '<div class="userprofile">';
181 $headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
182 echo $OUTPUT->context_header($headerinfo, 2);
184 if ($user->deleted) {
185 echo $OUTPUT->heading(get_string('userdeleted'));
186 if (!has_capability('moodle/user:update', $coursecontext)) {
187 echo $OUTPUT->footer();
188 die;
192 // OK, security out the way, now we are showing the user.
193 // Trigger a user profile viewed event.
194 profile_view($user, $coursecontext, $course);
196 if ($user->description && !isset($hiddenfields['description'])) {
197 echo '<div class="description">';
198 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
199 echo get_string('profilenotshown', 'moodle');
200 } else {
201 if ($courseid == SITEID) {
202 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
203 } else {
204 // We have to make a little detour thought the course context to verify the access control for course profile.
205 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
207 $options = array('overflowdiv' => true);
208 echo format_text($user->description, $user->descriptionformat, $options);
210 echo '</div>'; // Description class.
213 // Render custom blocks.
214 $renderer = $PAGE->get_renderer('core_user', 'myprofile');
215 $tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
216 echo $renderer->render($tree);
218 echo '</div>'; // Userprofile class.
220 echo $OUTPUT->footer();