MDL-50837 mod_scorm: Fix availability checks
[moodle.git] / user / view.php
blobf2cdcd669ee16820457f7a9a54845c83c5e51a48
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.'/tag/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 echo $OUTPUT->header();
114 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
115 $referer = get_local_referer(false);
116 if (!empty($referer)) {
117 echo $OUTPUT->continue_button($referer);
119 echo $OUTPUT->footer();
120 die;
123 } else {
124 // Somebody else.
125 $PAGE->set_title("$strpersonalprofile: ");
126 $PAGE->set_heading("$strpersonalprofile: ");
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.
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 groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group.
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) {
162 if (!isloggedin() or isguestuser()) {
163 // Do not use require_login() here because we might have already used require_login($course).
164 redirect(get_login_url());
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)) {
169 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
174 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
175 $PAGE->set_heading($course->fullname);
176 $PAGE->set_pagelayout('standard');
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.
180 if (!$currentuser) {
181 $PAGE->navigation->extend_for_user($user);
182 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
183 $node->forceopen = true;
185 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
186 $node->forceopen = true;
188 if ($node = $PAGE->settingsnav->get('courseadmin')) {
189 $node->forceopen = false;
192 echo $OUTPUT->header();
194 echo '<div class="userprofile">';
195 $headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
196 echo $OUTPUT->context_header($headerinfo, 2);
198 if ($user->deleted) {
199 echo $OUTPUT->heading(get_string('userdeleted'));
200 if (!has_capability('moodle/user:update', $coursecontext)) {
201 echo $OUTPUT->footer();
202 die;
206 // OK, security out the way, now we are showing the user.
207 // Trigger a user profile viewed event.
208 profile_view($user, $coursecontext, $course);
210 if ($user->description && !isset($hiddenfields['description'])) {
211 echo '<div class="description">';
212 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
213 echo get_string('profilenotshown', 'moodle');
214 } else {
215 if ($courseid == SITEID) {
216 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
217 } else {
218 // We have to make a little detour thought the course context to verify the access control for course profile.
219 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
221 $options = array('overflowdiv' => true);
222 echo format_text($user->description, $user->descriptionformat, $options);
224 echo '</div>'; // Description class.
227 // Render custom blocks.
228 $renderer = $PAGE->get_renderer('core_user', 'myprofile');
229 $tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
230 echo $renderer->render($tree);
232 echo '</div>'; // Userprofile class.
234 echo $OUTPUT->footer();