MDL-21695 adding help and link strings
[moodle.git] / user / view.php
blobc18f9283138dbd336435dfdca4302b4094776c82
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Display profile for a particular user
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
26 require_once("../config.php");
27 require_once($CFG->dirroot.'/user/profile/lib.php');
28 require_once($CFG->dirroot.'/tag/lib.php');
30 $id = optional_param('id', 0, PARAM_INT); // user id
31 $courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
32 $enable = optional_param('enable', 0, PARAM_BOOL); // enable email
33 $disable = optional_param('disable', 0, PARAM_BOOL); // disable email
35 if (empty($id)) { // See your own profile by default
36 require_login();
37 $id = $USER->id;
40 if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php
41 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect
44 $url = new moodle_url('/user/view.php', array('id'=>$id,'course'=>$courseid));
45 $PAGE->set_url($url);
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 = get_context_instance(CONTEXT_SYSTEM);
52 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
53 $usercontext = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
55 // Require login first
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);
62 $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course
63 $PAGE->add_body_class('path-user'); // So we can style it independently
64 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
66 $isparent = false;
68 if (!$currentuser
69 and $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id))
70 and has_capability('moodle/user:viewdetails', $usercontext)) {
71 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
72 // this way they may access the profile where they get overview of grades and child activity in course,
73 // please note this is just a guess!
74 require_login();
75 $isparent = true;
77 } else {
78 // normal course
79 require_login($course);
80 // what to do with users temporary accessing this course? shoudl they see the details?
84 $strpersonalprofile = get_string('personalprofile');
85 $strparticipants = get_string("participants");
86 $struser = get_string("user");
88 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
90 /// Now test the actual capabilities and enrolment in course
91 if ($currentuser) {
92 // me
93 if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) { // Need to have full access to a course to see the rest of own info
94 echo $OUTPUT->header();
95 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
96 if (!empty($_SERVER['HTTP_REFERER'])) {
97 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
99 echo $OUTPUT->footer();
100 die;
103 } else {
104 // somebody else
105 $PAGE->set_title("$strpersonalprofile: ");
106 $PAGE->set_heading("$strpersonalprofile: ");
108 // check course level capabilities
109 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // normal enrolled user or mnager
110 !has_capability('moodle/user:viewdetails', $usercontext)) { // usually parent
111 print_error('cannotviewprofile');
114 if (!is_enrolled($coursecontext, $user->id)) {
115 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
116 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
117 // or test for course:inspect capability
118 if (has_capability('moodle/role:assign', $coursecontext)) {
119 $PAGE->navbar->add($fullname);
120 echo $OUTPUT->header();
121 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
122 } else {
123 echo $OUTPUT->header();
124 $PAGE->navbar->add($struser);
125 echo $OUTPUT->heading(get_string('notenrolledprofile'));
127 if (!empty($_SERVER['HTTP_REFERER'])) {
128 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
130 echo $OUTPUT->footer();
131 exit;
134 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
135 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and $course->groupmodeforce
136 and !has_capability('moodle/site:accessallgroups', $coursecontext) and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)) {
137 if (!isloggedin() or isguestuser()) {
138 // do not use require_login() here because we might have already used require_login($course)
139 redirect(get_login_url());
141 $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name'));
142 $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name'));
143 if (!array_intersect($mygroups, $usergroups)) {
144 print_error("groupnotamember", '', "../course/view.php?id=$course->id");
150 /// We've established they can see the user's name at least, so what about the rest?
152 if (!$currentuser) {
153 $PAGE->navigation->extend_for_user($user);
154 if ($node = $PAGE->settingsnav->get('userviewingsettings')) {
155 $node->forceopen = true;
157 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
158 $node->forceopen = true;
160 if ($node = $PAGE->settingsnav->get('courseadmin')) {
161 $node->forceopen = false;
164 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
165 $PAGE->set_heading($course->fullname);
166 $PAGE->set_pagelayout('standard');
167 echo $OUTPUT->header();
169 echo '<div class="userprofile">';
171 echo $OUTPUT->heading(fullname($user).' ('.$course->shortname.')');
173 if ($user->deleted) {
174 echo $OUTPUT->heading(get_string('userdeleted'));
175 if (!has_capability('moodle/user:update', $coursecontext)) {
176 echo $OUTPUT->footer();
177 die;
181 /// OK, security out the way, now we are showing the user
183 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
185 /// Get the hidden field list
186 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
187 $hiddenfields = array();
188 } else {
189 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
192 if (is_mnet_remote_user($user)) {
193 $sql = "
194 SELECT DISTINCT h.id, h.name, h.wwwroot,
195 a.name as application, a.display_name
196 FROM {mnet_host} h, {mnet_application} a
197 WHERE h.id = ? AND h.applicationid = a.id
198 ORDER BY a.display_name, h.name";
200 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
202 echo '<p class="errorboxcontent">'.get_string('remoteappuser', $remotehost->application)." <br />\n";
203 if ($currentuser) {
204 if ($remotehost->application =='moodle') {
205 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/user/edit.php\">{$remotehost->name}</a> ".get_string('editremoteprofile')." </p>\n";
206 } else {
207 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a> ".get_string('gotoyourserver')." </p>\n";
209 } else {
210 echo "Remote {$remotehost->display_name}: <a href=\"{$remotehost->wwwroot}/\">{$remotehost->name}</a></p>\n";
214 echo '<div class="profilepicture">';
215 echo $OUTPUT->user_picture($user, array('size'=>100));
216 echo '</div>';
218 // Print the description
219 echo '<div class="description">';
220 if ($user->description && !isset($hiddenfields['description'])) {
221 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) {
222 echo get_string('profilenotshown', 'moodle');
223 } else {
224 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user_profile', $id);
225 echo format_text($user->description, $user->descriptionformat);
228 echo '</div>';
231 // Print all the little details in a list
233 echo '<table class="list" summary="">';
235 // Show last time this user accessed this course
236 if (!isset($hiddenfields['lastaccess'])) {
237 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) {
238 $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
239 } else {
240 $datestring = get_string("never");
242 print_row(get_string("lastaccess").":", $datestring);
245 // Show roles in this course
246 if ($rolestring = get_user_roles_in_course($id, $course->id)) {
247 print_row(get_string('roles').':', $rolestring);
250 // Show groups this user is in
251 if (!isset($hiddenfields['groups'])) {
252 if ($course->groupmode != SEPARATEGROUPS or has_capability('moodle/site:accessallgroups', $coursecontext)) {
253 if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
254 $groupstr = '';
255 foreach ($usergroups as $group){
256 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
258 print_row(get_string("group").":", rtrim($groupstr, ', '));
263 // Show other courses they may be in
264 if (!isset($hiddenfields['mycourses'])) {
265 if ($mycourses = get_my_courses($user->id, 'visible DESC,sortorder ASC', null, false, 21)) {
266 $shown = 0;
267 $courselisting = '';
268 foreach ($mycourses as $mycourse) {
269 if ($mycourse->category) {
270 if ($mycourse->id != $course->id){
271 $class = '';
272 if ($mycourse->visible == 0) {
273 // get_my_courses will filter courses $USER cannot see
274 // if we get one with visible 0 it just means it's hidden
275 // ... but not from $USER
276 $class = 'class="dimmed"';
278 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
279 . format_string($mycourse->fullname) . "</a>, ";
280 } else {
281 $courselisting .= format_string($mycourse->fullname) . ", ";
282 $PAGE->navbar->add($mycourse->fullname);
285 $shown++;
286 if ($shown >= 20) {
287 $courselisting .= "...";
288 break;
291 print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
295 echo "</table>";
297 echo '<div class="fullprofilelink">';
298 echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile'));
299 echo '</div>';
301 /// TODO Add more useful overview info for teachers here, see below
303 /// Show links to notes made about this student (must click to display, for privacy)
305 /// Recent comments made in this course
307 /// Recent blogs associated with this course and items in it
311 echo '</div>'; // userprofile class
313 echo $OUTPUT->footer();
315 /// Functions ///////
317 function print_row($left, $right) {
318 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";