PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/user/view.php

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