|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once("../config.php"); 00027 require_once($CFG->dirroot.'/user/profile/lib.php'); 00028 require_once($CFG->dirroot.'/tag/lib.php'); 00029 require_once($CFG->libdir . '/filelib.php'); 00030 00031 $id = optional_param('id', 0, PARAM_INT); // user id 00032 $courseid = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site) 00033 00034 if (empty($id)) { // See your own profile by default 00035 require_login(); 00036 $id = $USER->id; 00037 } 00038 00039 if ($courseid == SITEID) { // Since Moodle 2.0 all site-level profiles are shown by profile.php 00040 redirect($CFG->wwwroot.'/user/profile.php?id='.$id); // Immediate redirect 00041 } 00042 00043 $PAGE->set_url('/user/view.php', array('id'=>$id,'course'=>$courseid)); 00044 00045 $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST); 00046 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); 00047 $currentuser = ($user->id == $USER->id); 00048 00049 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00050 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); 00051 $usercontext = get_context_instance(CONTEXT_USER, $user->id, IGNORE_MISSING); 00052 00053 // Require login first 00054 if (isguestuser($user)) { 00055 // can not view profile of guest - thre is nothing to see there 00056 print_error('invaliduserid'); 00057 } 00058 00059 if (!empty($CFG->forceloginforprofiles)) { 00060 require_login(); // we can not log in to course due to the parent hack below 00061 } 00062 00063 $PAGE->set_context($coursecontext); 00064 $PAGE->set_course($course); 00065 $PAGE->set_pagetype('course-view-' . $course->format); // To get the blocks exactly like the course 00066 $PAGE->add_body_class('path-user'); // So we can style it independently 00067 $PAGE->set_other_editing_capability('moodle/course:manageactivities'); 00068 00069 $isparent = false; 00070 00071 if (!$currentuser and !$user->deleted 00072 and $DB->record_exists('role_assignments', array('userid'=>$USER->id, 'contextid'=>$usercontext->id)) 00073 and has_capability('moodle/user:viewdetails', $usercontext)) { 00074 // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in, 00075 // this way they may access the profile where they get overview of grades and child activity in course, 00076 // please note this is just a guess! 00077 require_login(); 00078 $isparent = true; 00079 $PAGE->navigation->set_userid_for_parent_checks($id); 00080 } else { 00081 // normal course 00082 require_login($course); 00083 // what to do with users temporary accessing this course? should they see the details? 00084 } 00085 00086 $strpersonalprofile = get_string('personalprofile'); 00087 $strparticipants = get_string("participants"); 00088 $struser = get_string("user"); 00089 00090 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext)); 00091 00093 if ($currentuser) { 00094 // me 00095 if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) { // Need to have full access to a course to see the rest of own info 00096 echo $OUTPUT->header(); 00097 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname)); 00098 if (!empty($_SERVER['HTTP_REFERER'])) { 00099 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']); 00100 } 00101 echo $OUTPUT->footer(); 00102 die; 00103 } 00104 00105 } else { 00106 // somebody else 00107 $PAGE->set_title("$strpersonalprofile: "); 00108 $PAGE->set_heading("$strpersonalprofile: "); 00109 00110 // check course level capabilities 00111 if (!has_capability('moodle/user:viewdetails', $coursecontext) && // normal enrolled user or mnager 00112 ($user->deleted or !has_capability('moodle/user:viewdetails', $usercontext))) { // usually parent 00113 print_error('cannotviewprofile'); 00114 } 00115 00116 if (!is_enrolled($coursecontext, $user->id)) { 00117 // TODO: the only potential problem is that managers and inspectors might post in forum, but the link 00118 // to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody 00119 // or test for course:inspect capability 00120 if (has_capability('moodle/role:assign', $coursecontext)) { 00121 $PAGE->navbar->add($fullname); 00122 echo $OUTPUT->header(); 00123 echo $OUTPUT->heading(get_string('notenrolled', '', $fullname)); 00124 } else { 00125 echo $OUTPUT->header(); 00126 $PAGE->navbar->add($struser); 00127 echo $OUTPUT->heading(get_string('notenrolledprofile')); 00128 } 00129 if (!empty($_SERVER['HTTP_REFERER'])) { 00130 echo $OUTPUT->continue_button($_SERVER['HTTP_REFERER']); 00131 } 00132 echo $OUTPUT->footer(); 00133 exit; 00134 } 00135 00136 // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group 00137 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and $course->groupmodeforce 00138 and !has_capability('moodle/site:accessallgroups', $coursecontext) and !has_capability('moodle/site:accessallgroups', $coursecontext, $user->id)) { 00139 if (!isloggedin() or isguestuser()) { 00140 // do not use require_login() here because we might have already used require_login($course) 00141 redirect(get_login_url()); 00142 } 00143 $mygroups = array_keys(groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid, 'g.id, g.name')); 00144 $usergroups = array_keys(groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name')); 00145 if (!array_intersect($mygroups, $usergroups)) { 00146 print_error("groupnotamember", '', "../course/view.php?id=$course->id"); 00147 } 00148 } 00149 } 00150 00151 00153 00154 if (!$currentuser) { 00155 $PAGE->navigation->extend_for_user($user); 00156 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) { 00157 $node->forceopen = true; 00158 } 00159 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) { 00160 $node->forceopen = true; 00161 } 00162 if ($node = $PAGE->settingsnav->get('courseadmin')) { 00163 $node->forceopen = false; 00164 } 00165 00166 $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname"); 00167 $PAGE->set_heading($course->fullname); 00168 $PAGE->set_pagelayout('standard'); 00169 echo $OUTPUT->header(); 00170 00171 echo '<div class="userprofile">'; 00172 00173 echo $OUTPUT->heading(fullname($user).' ('.format_string($course->shortname, true, array('context' => $coursecontext)).')'); 00174 00175 if ($user->deleted) { 00176 echo $OUTPUT->heading(get_string('userdeleted')); 00177 if (!has_capability('moodle/user:update', $coursecontext)) { 00178 echo $OUTPUT->footer(); 00179 die; 00180 } 00181 } 00182 00184 00185 add_to_log($course->id, "user", "view", "view.php?id=$user->id&course=$course->id", "$user->id"); 00186 00188 if (has_capability('moodle/user:viewhiddendetails', $coursecontext)) { 00189 $hiddenfields = array(); 00190 } else { 00191 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); 00192 } 00193 00194 if (is_mnet_remote_user($user)) { 00195 $sql = "SELECT h.id, h.name, h.wwwroot, 00196 a.name as application, a.display_name 00197 FROM {mnet_host} h, {mnet_application} a 00198 WHERE h.id = ? AND h.applicationid = a.id"; 00199 00200 $remotehost = $DB->get_record_sql($sql, array($user->mnethostid)); 00201 $a = new stdclass(); 00202 $a->remotetype = $remotehost->display_name; 00203 $a->remotename = $remotehost->name; 00204 $a->remoteurl = $remotehost->wwwroot; 00205 00206 echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo'); 00207 } 00208 00209 echo '<div class="userprofilebox clearfix"><div class="profilepicture">'; 00210 echo $OUTPUT->user_picture($user, array('size'=>100)); 00211 echo '</div>'; 00212 00213 // Print the description 00214 echo '<div class="descriptionbox"><div class="description">'; 00215 if ($user->description && !isset($hiddenfields['description'])) { 00216 if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$id))) { 00217 echo get_string('profilenotshown', 'moodle'); 00218 } else { 00219 if ($courseid == SITEID) { 00220 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null); 00221 } else { 00222 // we have to make a little detour thought the course context to verify the access control for course profile 00223 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id); 00224 } 00225 $options = array('overflowdiv'=>true); 00226 echo format_text($user->description, $user->descriptionformat, $options); 00227 } 00228 } 00229 echo '</div>'; 00230 00231 00232 // Print all the little details in a list 00233 00234 echo '<table class="list" summary="">'; 00235 00236 //checks were performed above that ensure that if we've got to here either the user 00237 //is viewing their own profile ($USER->id == $user->id) or $user is enrolled in the course 00238 if ($currentuser 00239 or $user->maildisplay == 1 //allow everyone to see email address 00240 or ($user->maildisplay == 2 && is_enrolled($coursecontext, $USER)) //fellow course members can see email. Already know $user is enrolled 00241 or has_capability('moodle/course:useremail', $coursecontext)) { 00242 print_row(get_string("email").":", obfuscate_mailto($user->email, '')); 00243 } 00244 00245 // Show last time this user accessed this course 00246 if (!isset($hiddenfields['lastaccess'])) { 00247 if ($lastaccess = $DB->get_record('user_lastaccess', array('userid'=>$user->id, 'courseid'=>$course->id))) { 00248 $datestring = userdate($lastaccess->timeaccess)." (".format_time(time() - $lastaccess->timeaccess).")"; 00249 } else { 00250 $datestring = get_string("never"); 00251 } 00252 print_row(get_string("lastaccess").":", $datestring); 00253 } 00254 00255 // Show roles in this course 00256 if ($rolestring = get_user_roles_in_course($id, $course->id)) { 00257 print_row(get_string('roles').':', $rolestring); 00258 } 00259 00260 // Show groups this user is in 00261 if (!isset($hiddenfields['groups'])) { 00262 $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext); 00263 if ($usergroups = groups_get_all_groups($course->id, $user->id)) { 00264 $groupstr = ''; 00265 foreach ($usergroups as $group){ 00266 if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) { 00267 if (!groups_is_member($group->id, $user->id)) { 00268 continue; 00269 } 00270 } 00271 00272 if ($course->groupmode != NOGROUPS) { 00273 $groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&group='.$group->id.'">'.format_string($group->name).'</a>,'; 00274 } else { 00275 $groupstr .= ' '.format_string($group->name); // the user/index.php shows groups only when course in group mode 00276 } 00277 } 00278 if ($groupstr !== '') { 00279 print_row(get_string("group").":", rtrim($groupstr, ', ')); 00280 } 00281 } 00282 } 00283 00284 // Show other courses they may be in 00285 if (!isset($hiddenfields['mycourses'])) { 00286 if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) { 00287 $shown = 0; 00288 $courselisting = ''; 00289 foreach ($mycourses as $mycourse) { 00290 if ($mycourse->category) { 00291 $ccontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);; 00292 $cfullname = format_string($mycourse->fullname, true, array('context' => $ccontext)); 00293 if ($mycourse->id != $course->id){ 00294 $class = ''; 00295 if ($mycourse->visible == 0) { 00296 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) { 00297 continue; 00298 } 00299 $class = 'class="dimmed"'; 00300 } 00301 $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$mycourse->id}\" $class >" 00302 . $cfullname . "</a>, "; 00303 } else { 00304 $courselisting .= $cfullname . ", "; 00305 $PAGE->navbar->add($cfullname); 00306 } 00307 } 00308 $shown++; 00309 if ($shown >= 20) { 00310 $courselisting .= "..."; 00311 break; 00312 } 00313 } 00314 print_row(get_string('courseprofiles').':', rtrim($courselisting,', ')); 00315 } 00316 } 00317 00318 if (!isset($hiddenfields['suspended'])) { 00319 if ($user->suspended) { 00320 print_row('', get_string('suspended', 'auth')); 00321 } 00322 } 00323 00324 echo "</table></div></div>"; 00325 00326 // Print messaging link if allowed 00327 if (isloggedin() && has_capability('moodle/site:sendmessage', $usercontext) 00328 && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) { 00329 echo '<div class="messagebox">'; 00330 echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>'; 00331 echo '</div>'; 00332 } 00333 00334 if ($currentuser || has_capability('moodle/user:viewdetails', $usercontext) || has_coursecontact_role($id)) { 00335 echo '<div class="fullprofilelink">'; 00336 echo html_writer::link($CFG->wwwroot.'/user/profile.php?id='.$id, get_string('fullprofile')); 00337 echo '</div>'; 00338 } 00339 00341 00343 00345 00347 00348 00349 00350 echo '</div>'; // userprofile class 00351 00352 echo $OUTPUT->footer(); 00353 00355 00356 function print_row($left, $right) { 00357 echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n"; 00358 } 00359 00360