MDL-50653 user: forgotten closing div tag on the page
[moodle.git] / user / view.php
CommitLineData
ce221eb5 1<?php
ce221eb5 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.
13//
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/>.
16
17/**
18 * Display profile for a particular user
19 *
a2ed6e69 20 * @package core_user
ce221eb5 21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
ce221eb5 23 */
24
25require_once("../config.php");
26require_once($CFG->dirroot.'/user/profile/lib.php');
27require_once($CFG->dirroot.'/tag/lib.php');
99d19c13 28require_once($CFG->libdir . '/filelib.php');
8da433f0 29require_once($CFG->libdir . '/badgeslib.php');
ce221eb5 30
9594f50a
JO
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);
ce221eb5 34
a2ed6e69
SH
35// See your own profile by default.
36if (empty($id)) {
ce221eb5 37 require_login();
38 $id = $USER->id;
39}
f9903ed0 40
a2ed6e69
SH
41if ($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.
ce221eb5 43}
03d9401e 44
a2ed6e69 45$PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
e41ddc4b 46
a2ed6e69
SH
47$user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
48$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
03d9401e 49$currentuser = ($user->id == $USER->id);
f9903ed0 50
43731030
FM
51$systemcontext = context_system::instance();
52$coursecontext = context_course::instance($course->id);
53$usercontext = context_user::instance($user->id, IGNORE_MISSING);
1deff123 54
7120494e 55// Check we are not trying to view guest's profile.
4f0c2d00 56if (isguestuser($user)) {
a2ed6e69 57 // Can not view profile of guest - thre is nothing to see there.
4f0c2d00 58 print_error('invaliduserid');
ce221eb5 59}
bad59bc0 60
7120494e
AD
61$PAGE->set_context($coursecontext);
62
81b58cc2 63if (!empty($CFG->forceloginforprofiles)) {
3ecc63e9
FM
64 require_login(); // We can not log in to course due to the parent hack below.
65
66 // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
67 if (isguestuser()) {
7120494e
AD
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;
3ecc63e9 74 }
81b58cc2
PS
75}
76
f5c1e621 77$PAGE->set_course($course);
a2ed6e69
SH
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.
03d9401e 80$PAGE->set_other_editing_capability('moodle/course:manageactivities');
cb4f6179 81
1e72465c
VR
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');
85
4f0c2d00 86$isparent = false;
0be6f678 87
589071ba 88if (!$currentuser and !$user->deleted
a2ed6e69 89 and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
4f0c2d00
PS
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!
ce221eb5 94 require_login();
4f0c2d00 95 $isparent = true;
870815fa 96 $PAGE->navigation->set_userid_for_parent_checks($id);
4f0c2d00 97} else {
a2ed6e69 98 // Normal course.
4f0c2d00 99 require_login($course);
a2ed6e69 100 // What to do with users temporary accessing this course? should they see the details?
ce221eb5 101}
f9903ed0 102
ce221eb5 103$strpersonalprofile = get_string('personalprofile');
104$strparticipants = get_string("participants");
105$struser = get_string("user");
f9903ed0 106
ce221eb5 107$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
f1603208 108
a2ed6e69 109// Now test the actual capabilities and enrolment in course.
4f0c2d00 110if ($currentuser) {
a2ed6e69
SH
111 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
112 // Need to have full access to a course to see the rest of own info.
4f0c2d00
PS
113 echo $OUTPUT->header();
114 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
b2687a05
FM
115 $referer = clean_param($_SERVER['HTTP_REFERER'], PARAM_LOCALURL);
116 if (!empty($referer)) {
117 echo $OUTPUT->continue_button($referer);
4f0c2d00
PS
118 }
119 echo $OUTPUT->footer();
120 die;
121 }
caa8363f 122
4f0c2d00 123} else {
a2ed6e69 124 // Somebody else.
ce221eb5 125 $PAGE->set_title("$strpersonalprofile: ");
126 $PAGE->set_heading("$strpersonalprofile: ");
caa8363f 127
a2ed6e69
SH
128 // Check course level capabilities.
129 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // Normal enrolled user or mnager.
130 ($user->deleted or !has_capability('moodle/user:viewdetails', $usercontext))) { // Usually parent.
03d9401e
MD
131 print_error('cannotviewprofile');
132 }
4f0c2d00 133
03d9401e
MD
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
a2ed6e69 137 // or test for course:inspect capability.
03d9401e
MD
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'));
ce221eb5 146 }
b2687a05
FM
147 $referer = clean_param($_SERVER['HTTP_REFERER'], PARAM_LOCALURL);
148 if (!empty($referer)) {
149 echo $OUTPUT->continue_button($referer);
fa22fd5f 150 }
03d9401e
MD
151 echo $OUTPUT->footer();
152 exit;
ce221eb5 153 }
499e78b4 154
a2ed6e69 155 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group.
fbd846be
FM
156 // Except when we are a parent, in which case we would not be in any group.
157 if (groups_get_course_groupmode($course) == SEPARATEGROUPS
158 and $course->groupmodeforce
159 and !has_capability('moodle/site:accessallgroups', $coursecontext)
160 and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)
161 and !$isparent) {
4f0c2d00 162 if (!isloggedin() or isguestuser()) {
a2ed6e69 163 // Do not use require_login() here because we might have already used require_login($course).
4f0c2d00
PS
164 redirect(get_login_url());
165 }
166 $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name'));
167 $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name'));
168 if (!array_intersect($mygroups, $usergroups)) {
ce221eb5 169 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
dd780a3b 170 }
171 }
ce221eb5 172}
0be6f678 173
8b03c251
SH
174$PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
175$PAGE->set_heading($course->fullname);
176$PAGE->set_pagelayout('standard');
f5ecf2e9 177
8b03c251
SH
178// Locate the users settings in the settings navigation and force it open.
179// This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
87c215de
SH
180if (!$currentuser) {
181 $PAGE->navigation->extend_for_user($user);
5d07e957 182 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
87c215de
SH
183 $node->forceopen = true;
184 }
185} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
186 $node->forceopen = true;
187}
188if ($node = $PAGE->settingsnav->get('courseadmin')) {
189 $node->forceopen = false;
190}
191
ce221eb5 192echo $OUTPUT->header();
99cca847
MD
193
194echo '<div class="userprofile">';
45367bdf 195$headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
283ea788 196echo $OUTPUT->context_header($headerinfo, 2);
b1d530d2 197
ce221eb5 198if ($user->deleted) {
199 echo $OUTPUT->heading(get_string('userdeleted'));
200 if (!has_capability('moodle/user:update', $coursecontext)) {
201 echo $OUTPUT->footer();
202 die;
bb09fb11 203 }
ce221eb5 204}
bb09fb11 205
c76318d3
MN
206// OK, security out the way, now we are showing the user.
207// Trigger a user profile viewed event.
d1b23838 208profile_view($user, $coursecontext, $course);
c76318d3 209
ce221eb5 210if ($user->description && !isset($hiddenfields['description'])) {
bffb1c15 211 echo '<div class="description">';
a2ed6e69 212 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
99cca847 213 echo get_string('profilenotshown', 'moodle');
ce221eb5 214 } else {
64f93798
PS
215 if ($courseid == SITEID) {
216 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
217 } else {
a2ed6e69 218 // We have to make a little detour thought the course context to verify the access control for course profile.
64f93798
PS
219 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
220 }
a2ed6e69 221 $options = array('overflowdiv' => true);
367a75fa 222 echo format_text($user->description, $user->descriptionformat, $options);
f9903ed0 223 }
bffb1c15 224 echo '</div>'; // Description class.
ce221eb5 225}
3de34ccf 226
b19cc4ef
AA
227// Render custom blocks.
228$renderer = $PAGE->get_renderer('core_user', 'myprofile');
229$tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
230echo $renderer->render($tree);
99cca847 231
a2ed6e69 232echo '</div>'; // Userprofile class.
3de34ccf 233
4e516138 234echo $OUTPUT->footer();