Merge branch 'mdl-53133-m30' of git://github.com/deraadt/moodle into MOODLE_30_STABLE
[moodle.git] / user / view.php
blob5040d088d4f9f3f1c7fa6e3a7b20013b7fcd7dcf
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->dirroot.'/tag/lib.php');
29 require_once($CFG->libdir . '/filelib.php');
30 require_once($CFG->libdir . '/badgeslib.php');
32 $id = optional_param('id', 0, PARAM_INT); // User id.
33 $courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site).
34 $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
36 // See your own profile by default.
37 if (empty($id)) {
38 require_login();
39 $id = $USER->id;
42 if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php.
43 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect.
46 $PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
48 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
49 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
50 $currentuser = ($user->id == $USER->id);
52 $systemcontext = context_system::instance();
53 $coursecontext = context_course::instance($course->id);
54 $usercontext = context_user::instance($user->id, IGNORE_MISSING);
56 // Check we are not trying to view guest's profile.
57 if (isguestuser($user)) {
58 // Can not view profile of guest - thre is nothing to see there.
59 print_error('invaliduserid');
62 $PAGE->set_context($coursecontext);
64 if (!empty($CFG->forceloginforprofiles)) {
65 require_login(); // We can not log in to course due to the parent hack below.
67 // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
68 if (isguestuser()) {
69 echo $OUTPUT->header();
70 echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
71 get_login_url(),
72 $CFG->wwwroot);
73 echo $OUTPUT->footer();
74 die;
78 $PAGE->set_course($course);
79 $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course.
80 $PAGE->add_body_class('path-user'); // So we can style it independently.
81 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
83 // Set the Moodle docs path explicitly because the default behaviour
84 // of inhereting the pagetype will lead to an incorrect docs location.
85 $PAGE->set_docs_path('user/profile');
87 $isparent = false;
89 if (!$currentuser and !$user->deleted
90 and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
91 and has_capability('moodle/user:viewdetails', $usercontext)) {
92 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
93 // this way they may access the profile where they get overview of grades and child activity in course,
94 // please note this is just a guess!
95 require_login();
96 $isparent = true;
97 $PAGE->navigation->set_userid_for_parent_checks($id);
98 } else {
99 // Normal course.
100 require_login($course);
101 // What to do with users temporary accessing this course? should they see the details?
104 $strpersonalprofile = get_string('personalprofile');
105 $strparticipants = get_string("participants");
106 $struser = get_string("user");
108 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
110 // Now test the actual capabilities and enrolment in course.
111 if ($currentuser) {
112 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
113 // Need to have full access to a course to see the rest of own info.
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
116 $referer = get_local_referer(false);
117 if (!empty($referer)) {
118 echo $OUTPUT->continue_button($referer);
120 echo $OUTPUT->footer();
121 die;
124 } else {
125 // Somebody else.
126 $PAGE->set_title("$strpersonalprofile: ");
127 $PAGE->set_heading("$strpersonalprofile: ");
129 // Check to see if the user can see this user's profile.
130 if (!user_can_view_profile($user, $course, $usercontext) && !$isparent) {
131 print_error('cannotviewprofile');
134 if (!is_enrolled($coursecontext, $user->id)) {
135 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
136 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
137 // or test for course:inspect capability.
138 if (has_capability('moodle/role:assign', $coursecontext)) {
139 $PAGE->navbar->add($fullname);
140 echo $OUTPUT->header();
141 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
142 } else {
143 echo $OUTPUT->header();
144 $PAGE->navbar->add($struser);
145 echo $OUTPUT->heading(get_string('notenrolledprofile'));
147 $referer = get_local_referer(false);
148 if (!empty($referer)) {
149 echo $OUTPUT->continue_button($referer);
151 echo $OUTPUT->footer();
152 exit;
155 if (!isloggedin() or isguestuser()) {
156 // Do not use require_login() here because we might have already used require_login($course).
157 redirect(get_login_url());
161 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
162 $PAGE->set_heading($course->fullname);
163 $PAGE->set_pagelayout('standard');
165 // Locate the users settings in the settings navigation and force it open.
166 // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
167 if (!$currentuser) {
168 $PAGE->navigation->extend_for_user($user);
169 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
170 $node->forceopen = true;
172 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
173 $node->forceopen = true;
175 if ($node = $PAGE->settingsnav->get('courseadmin')) {
176 $node->forceopen = false;
179 echo $OUTPUT->header();
181 echo '<div class="userprofile">';
182 $headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
183 echo $OUTPUT->context_header($headerinfo, 2);
185 if ($user->deleted) {
186 echo $OUTPUT->heading(get_string('userdeleted'));
187 if (!has_capability('moodle/user:update', $coursecontext)) {
188 echo $OUTPUT->footer();
189 die;
193 // OK, security out the way, now we are showing the user.
194 // Trigger a user profile viewed event.
195 profile_view($user, $coursecontext, $course);
197 if ($user->description && !isset($hiddenfields['description'])) {
198 echo '<div class="description">';
199 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
200 echo get_string('profilenotshown', 'moodle');
201 } else {
202 if ($courseid == SITEID) {
203 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
204 } else {
205 // We have to make a little detour thought the course context to verify the access control for course profile.
206 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
208 $options = array('overflowdiv' => true);
209 echo format_text($user->description, $user->descriptionformat, $options);
211 echo '</div>'; // Description class.
214 // Render custom blocks.
215 $renderer = $PAGE->get_renderer('core_user', 'myprofile');
216 $tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
217 echo $renderer->render($tree);
219 echo '</div>'; // Userprofile class.
221 echo $OUTPUT->footer();