PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/user/view.php

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