weekly release 2.1dev
[moodle.git] / user / view.php
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.
14 //
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
20  *
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
24  */
26 require_once("../config.php");
27 require_once($CFG->dirroot.'/user/profile/lib.php');
28 require_once($CFG->dirroot.'/tag/lib.php');
29 require_once($CFG->libdir . '/filelib.php');
31 $id        = optional_param('id', 0, PARAM_INT);   // user id
32 $courseid  = optional_param('course', SITEID, PARAM_INT);   // course id (defaults to Site)
34 if (empty($id)) {            // See your own profile by default
35     require_login();
36     $id = $USER->id;
37 }
39 if ($courseid == SITEID) {   // Since Moodle 2.0 all site-level profiles are shown by profile.php
40     redirect($CFG->wwwroot.'/user/profile.php?id='.$id);  // Immediate redirect
41 }
43 $PAGE->set_url('/user/view.php', array('id'=>$id,'course'=>$courseid));
45 $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST);
46 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
47 $currentuser = ($user->id == $USER->id);
49 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
50 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
51 $usercontext   = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
53 // Require login first
54 if (isguestuser($user)) {
55     // can not view profile of guest - thre is nothing to see there
56     print_error('invaliduserid');
57 }
59 if (!empty($CFG->forceloginforprofiles)) {
60     require_login(); // we can not log in to course due to the parent hack bellow
61     if (isguestuser()) {
62         $SESSION->wantsurl = $PAGE->url->out(false);
63         redirect(get_login_url());
64     }
65 }
67 $PAGE->set_context($coursecontext);
68 $PAGE->set_course($course);
69 $PAGE->set_pagetype('course-view-' . $course->format);  // To get the blocks exactly like the course
70 $PAGE->add_body_class('path-user');                     // So we can style it independently
71 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
73 $isparent = false;
75 if (!$currentuser
76   and $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id))
77   and has_capability('moodle/user:viewdetails', $usercontext)) {
78     // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
79     //       this way they may access the profile where they get overview of grades and child activity in course,
80     //       please note this is just a guess!
81     require_login();
82     $isparent = true;
83 } else {
84     // normal course
85     require_login($course);
86     // what to do with users temporary accessing this course? should they see the details?
87 }
89 $strpersonalprofile = get_string('personalprofile');
90 $strparticipants = get_string("participants");
91 $struser = get_string("user");
93 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
95 /// Now test the actual capabilities and enrolment in course
96 if ($currentuser) {
97     // me
98     if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) { // Need to have full access to a course to see the rest of own info
99         echo $OUTPUT->header();
100         echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
101         if (!empty($_SERVER['HTTP_REFERER'])) {
102             echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
103         }
104         echo $OUTPUT->footer();
105         die;
106     }
108 } else {
109     // somebody else
110     $PAGE->set_title("$strpersonalprofile: ");
111     $PAGE->set_heading("$strpersonalprofile: ");
113     // check course level capabilities
114     if (!has_capability('moodle/user:viewdetails', $coursecontext) && // normal enrolled user or mnager
115         !has_capability('moodle/user:viewdetails', $usercontext)) {   // usually parent
116         print_error('cannotviewprofile');
117     }
119     if (!is_enrolled($coursecontext, $user->id)) {
120         // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
121         //       to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
122         //       or test for course:inspect capability
123         if (has_capability('moodle/role:assign', $coursecontext)) {
124             $PAGE->navbar->add($fullname);
125             echo $OUTPUT->header();
126             echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
127         } else {
128             echo $OUTPUT->header();
129             $PAGE->navbar->add($struser);
130             echo $OUTPUT->heading(get_string('notenrolledprofile'));
131         }
132         if (!empty($_SERVER['HTTP_REFERER'])) {
133             echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']);
134         }
135         echo $OUTPUT->footer();
136         exit;
137     }
139     // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
140     if (groups_get_course_groupmode($course) == SEPARATEGROUPS and $course->groupmodeforce
141       and !has_capability('moodle/site:accessallgroups', $coursecontext) and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)) {
142         if (!isloggedin() or isguestuser()) {
143             // do not use require_login() here because we might have already used require_login($course)
144             redirect(get_login_url());
145         }
146         $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name'));
147         $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name'));
148         if (!array_intersect($mygroups, $usergroups)) {
149             print_error("groupnotamember", '', "../course/view.php?id=$course->id");
150         }
151     }
155 /// We've established they can see the user's name at least, so what about the rest?
157 if (!$currentuser) {
158     $PAGE->navigation->extend_for_user($user);
159     if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
160         $node->forceopen = true;
161     }
162 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
163     $node->forceopen = true;
165 if ($node = $PAGE->settingsnav->get('courseadmin')) {
166     $node->forceopen = false;
169 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
170 $PAGE->set_heading($course->fullname);
171 $PAGE->set_pagelayout('standard');
172 echo $OUTPUT->header();
174 echo '<div class="userprofile">';
176 echo $OUTPUT->heading(fullname($user).' ('.$course->shortname.')');
178 if ($user->deleted) {
179     echo $OUTPUT->heading(get_string('userdeleted'));
180     if (!has_capability('moodle/user:update', $coursecontext)) {
181         echo $OUTPUT->footer();
182         die;
183     }
186 /// OK, security out the way, now we are showing the user
188 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id");
190 /// Get the hidden field list
191 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
192     $hiddenfields = array();
193 } else {
194     $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
197 if (is_mnet_remote_user($user)) {
198     $sql = "SELECT h.id, h.name, h.wwwroot,
199                    a.name as application, a.display_name
200               FROM {mnet_host} h, {mnet_application} a
201              WHERE h.id = ? AND h.applicationid = a.id";
203     $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
204     $a = new stdclass();
205     $a->remotetype = $remotehost->display_name;
206     $a->remotename = $remotehost->name;
207     $a->remoteurl  = $remotehost->wwwroot;
209     echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
212 echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
213 echo $OUTPUT->user_picture($user, array('size'=>100));
214 echo '</div>';
216 // Print the description
217 echo '<div class="descriptionbox"><div class="description">';
218 if ($user->description && !isset($hiddenfields['description'])) {
219     if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) {
220         echo get_string('profilenotshown', 'moodle');
221     } else {
222         if ($courseid == SITEID) {
223             $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
224         } else {
225             // we have to make a little detour thought the course context to verify the access control for course profile
226             $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
227         }
228         $options = array('overflowdiv'=>true);
229         echo format_text($user->description, $user->descriptionformat, $options);
230     }
232 echo '</div>';
235 // Print all the little details in a list
237 echo '<table class="list" summary="">';
239 //checks were performed above that ensure that if we've got to here either the user
240 //is viewing their own profile ($USER->id == $user->id) or $user is enrolled in the course
241 if ($currentuser
242    or $user->maildisplay == 1 //allow everyone to see email address
243    or ($user->maildisplay == 2 && is_enrolled($coursecontext, $USER)) //fellow course members can see email. Already know $user is enrolled
244    or has_capability('moodle/course:useremail', $coursecontext)) {
245     print_row(get_string("email").":", obfuscate_mailto($user->email, ''));
248 // Show last time this user accessed this course
249 if (!isset($hiddenfields['lastaccess'])) {
250     if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) {
251         $datestring = userdate($lastaccess->timeaccess)."&nbsp; (".format_time(time() - $lastaccess->timeaccess).")";
252     } else {
253         $datestring = get_string("never");
254     }
255     print_row(get_string("lastaccess").":", $datestring);
258 // Show roles in this course
259 if ($rolestring = get_user_roles_in_course($id, $course->id)) {
260     print_row(get_string('roles').':', $rolestring);
263 // Show groups this user is in
264 if (!isset($hiddenfields['groups'])) {
265     $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext);
266     if ($usergroups = groups_get_all_groups($course->id, $user->id)) {
267         $groupstr = '';
268         foreach ($usergroups as $group){
269             if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) {
270                 if (!groups_is_member($group->id, $user->id)) {
271                     continue;
272                 }
273             }
275             if ($course->groupmode != NOGROUPS) {
276                 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$group->id.'">'.format_string($group->name).'</a>,';
277             } else {
278                 $groupstr .= ' '.format_string($group->name); // the user/index.php shows groups only when course in group mode
279             }
280         }
281         if ($groupstr !== '') {
282             print_row(get_string("group").":", rtrim($groupstr, ', '));
283         }
284     }
287 // Show other courses they may be in
288 if (!isset($hiddenfields['mycourses'])) {
289     if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
290         $shown = 0;
291         $courselisting = '';
292         foreach ($mycourses as $mycourse) {
293             if ($mycourse->category) {
294                 if ($mycourse->id != $course->id){
295                     $class = '';
296                     if ($mycourse->visible == 0) {
297                         $ccontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
298                         if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
299                             continue;
300                         }
301                         $class = 'class="dimmed"';
302                     }
303                     $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >"
304                         . format_string($mycourse->fullname) . "</a>, ";
305                 } else {
306                     $courselisting .= format_string($mycourse->fullname) . ", ";
307                     $PAGE->navbar->add($mycourse->fullname);
308                 }
309             }
310             $shown++;
311             if ($shown >= 20) {
312                 $courselisting .= "...";
313                 break;
314             }
315         }
316         print_row(get_string('courseprofiles').':', rtrim($courselisting,', '));
317     }
320 echo "</table></div></div>";
322 // Print messaging link if allowed
323 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext)
324     && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
325     echo '<div class="messagebox">';
326     echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
327     echo '</div>';
330 if ($currentuser || has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($id)) {
331     echo '<div class="fullprofilelink">';
332     echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile'));
333     echo '</div>';
336 /// TODO Add more useful overview info for teachers here, see below
338 /// Show links to notes made about this student (must click to display, for privacy)
340 /// Recent comments made in this course
342 /// Recent blogs associated with this course and items in it
346 echo '</div>';  // userprofile class
348 echo $OUTPUT->footer();
350 /// Functions ///////
352 function print_row($left, $right) {
353     echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";