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

/user/profile.php

https://github.com/200896596/moodle
PHP | 371 lines | 261 code | 59 blank | 51 comment | 96 complexity | 5ecb9faedb248531dbdc1d6d43951a1f 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. * Public Profile -- a user's public profile page
  18. *
  19. * - each user can currently have their own page (cloned from system and then customised)
  20. * - users can add any blocks they want
  21. * - the administrators can define a default site public profile for users who have
  22. * not created their own public profile
  23. *
  24. * This script implements the user's view of the public profile, and allows editing
  25. * of the public profile.
  26. *
  27. * @package moodlecore
  28. * @subpackage my
  29. * @copyright 2010 Remote-Learner.net
  30. * @author Hubert Chathi <hubert@remote-learner.net>
  31. * @author Olav Jordan <olav.jordan@remote-learner.net>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. require_once(dirname(__FILE__) . '/../config.php');
  35. require_once($CFG->dirroot . '/my/lib.php');
  36. require_once($CFG->dirroot . '/tag/lib.php');
  37. require_once($CFG->dirroot . '/user/profile/lib.php');
  38. require_once($CFG->libdir.'/filelib.php');
  39. $userid = optional_param('id', 0, PARAM_INT);
  40. $edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
  41. $PAGE->set_url('/user/profile.php', array('id'=>$userid));
  42. if (!empty($CFG->forceloginforprofiles)) {
  43. require_login();
  44. if (isguestuser()) {
  45. $SESSION->wantsurl = $PAGE->url->out(false);
  46. redirect(get_login_url());
  47. }
  48. } else if (!empty($CFG->forcelogin)) {
  49. require_login();
  50. }
  51. $userid = $userid ? $userid : $USER->id; // Owner of the page
  52. $user = $DB->get_record('user', array('id' => $userid));
  53. $currentuser = ($user->id == $USER->id);
  54. $context = $usercontext = get_context_instance(CONTEXT_USER, $userid, MUST_EXIST);
  55. if (!$currentuser &&
  56. !empty($CFG->forceloginforprofiles) &&
  57. !has_capability('moodle/user:viewdetails', $context) &&
  58. !has_coursecontact_role($userid)) {
  59. // Course managers can be browsed at site level. If not forceloginforprofiles, allow access (bug #4366)
  60. $struser = get_string('user');
  61. $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
  62. $PAGE->set_title("$SITE->shortname: $struser"); // Do not leak the name
  63. $PAGE->set_heading("$SITE->shortname: $struser");
  64. $PAGE->set_url('/user/profile.php', array('id'=>$userid));
  65. $PAGE->navbar->add($struser);
  66. echo $OUTPUT->header();
  67. echo $OUTPUT->heading(get_string('usernotavailable', 'error'));
  68. echo $OUTPUT->footer();
  69. exit;
  70. }
  71. // Get the profile page. Should always return something unless the database is broken.
  72. if (!$currentpage = my_get_page($userid, MY_PAGE_PUBLIC)) {
  73. print_error('mymoodlesetup');
  74. }
  75. if (!$currentpage->userid) {
  76. $context = get_context_instance(CONTEXT_SYSTEM); // A trick so that we even see non-sticky blocks
  77. }
  78. $PAGE->set_context($context);
  79. $PAGE->set_pagelayout('mydashboard');
  80. $PAGE->set_pagetype('user-profile');
  81. // Set up block editing capabilities
  82. if (isguestuser()) { // Guests can never edit their profile
  83. $USER->editing = $edit = 0; // Just in case
  84. $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
  85. } else {
  86. if ($currentuser) {
  87. $PAGE->set_blocks_editing_capability('moodle/user:manageownblocks');
  88. } else {
  89. $PAGE->set_blocks_editing_capability('moodle/user:manageblocks');
  90. }
  91. }
  92. if (has_capability('moodle/user:viewhiddendetails', $context)) {
  93. $hiddenfields = array();
  94. } else {
  95. $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
  96. }
  97. // Start setting up the page
  98. $strpublicprofile = get_string('publicprofile');
  99. $PAGE->blocks->add_region('content');
  100. $PAGE->set_subpage($currentpage->id);
  101. $PAGE->set_title(fullname($user).": $strpublicprofile");
  102. $PAGE->set_heading(fullname($user).": $strpublicprofile");
  103. if (!$currentuser) {
  104. $PAGE->navigation->extend_for_user($user);
  105. if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
  106. $node->forceopen = true;
  107. }
  108. } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
  109. $node->forceopen = true;
  110. }
  111. if ($node = $PAGE->settingsnav->get('root')) {
  112. $node->forceopen = false;
  113. }
  114. // Toggle the editing state and switches
  115. if ($PAGE->user_allowed_editing()) {
  116. if ($edit !== null) { // Editing state was specified
  117. $USER->editing = $edit; // Change editing state
  118. if (!$currentpage->userid && $edit) {
  119. // If we are viewing a system page as ordinary user, and the user turns
  120. // editing on, copy the system pages as new user pages, and get the
  121. // new page record
  122. if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PUBLIC, 'user-profile')) {
  123. print_error('mymoodlesetup');
  124. }
  125. $PAGE->set_context($usercontext);
  126. $PAGE->set_subpage($currentpage->id);
  127. }
  128. } else { // Editing state is in session
  129. if ($currentpage->userid) { // It's a page we can edit, so load from session
  130. if (!empty($USER->editing)) {
  131. $edit = 1;
  132. } else {
  133. $edit = 0;
  134. }
  135. } else { // It's a system page and they are not allowed to edit system pages
  136. $USER->editing = $edit = 0; // Disable editing completely, just to be safe
  137. }
  138. }
  139. // Add button for editing page
  140. $params = array('edit' => !$edit);
  141. if (!$currentpage->userid) {
  142. // viewing a system page -- let the user customise it
  143. $editstring = get_string('updatemymoodleon');
  144. $params['edit'] = 1;
  145. } else if (empty($edit)) {
  146. $editstring = get_string('updatemymoodleon');
  147. } else {
  148. $editstring = get_string('updatemymoodleoff');
  149. }
  150. $url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
  151. $button = $OUTPUT->single_button($url, $editstring);
  152. $PAGE->set_button($button);
  153. } else {
  154. $USER->editing = $edit = 0;
  155. }
  156. // HACK WARNING! This loads up all this page's blocks in the system context
  157. if ($currentpage->userid == 0) {
  158. $CFG->blockmanagerclass = 'my_syspage_block_manager';
  159. }
  160. // TODO WORK OUT WHERE THE NAV BAR IS!
  161. echo $OUTPUT->header();
  162. echo '<div class="userprofile">';
  163. // Print the standard content of this page, the basic profile info
  164. echo $OUTPUT->heading(fullname($user));
  165. if (is_mnet_remote_user($user)) {
  166. $sql = "SELECT h.id, h.name, h.wwwroot,
  167. a.name as application, a.display_name
  168. FROM {mnet_host} h, {mnet_application} a
  169. WHERE h.id = ? AND h.applicationid = a.id";
  170. $remotehost = $DB->get_record_sql($sql, array($user->mnethostid));
  171. $a = new stdclass();
  172. $a->remotetype = $remotehost->display_name;
  173. $a->remotename = $remotehost->name;
  174. $a->remoteurl = $remotehost->wwwroot;
  175. echo $OUTPUT->box(get_string('remoteuserinfo', 'mnet', $a), 'remoteuserinfo');
  176. }
  177. echo '<div class="userprofilebox clearfix"><div class="profilepicture">';
  178. echo $OUTPUT->user_picture($user, array('size'=>100));
  179. echo '</div>';
  180. echo '<div class="descriptionbox"><div class="description">';
  181. // Print the description
  182. if ($user->description && !isset($hiddenfields['description'])) {
  183. if (!empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid'=>$user->id))) {
  184. echo get_string('profilenotshown', 'moodle');
  185. } else {
  186. $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
  187. $options = array('overflowdiv'=>true);
  188. echo format_text($user->description, $user->descriptionformat, $options);
  189. }
  190. }
  191. echo '</div>';
  192. // Print all the little details in a list
  193. echo '<table class="list" summary="">';
  194. // Show idnumber
  195. if ($user->idnumber && $user->auth == 'cas' && has_capability('moodle/user:viewalldetails', $context)) {
  196. print_row(get_string("idnumber").":", "$user->idnumber");
  197. }
  198. if (! isset($hiddenfields['country']) && $user->country) {
  199. print_row(get_string('country') . ':', get_string($user->country, 'countries'));
  200. }
  201. if (! isset($hiddenfields['city']) && $user->city) {
  202. print_row(get_string('city') . ':', $user->city);
  203. }
  204. if (has_capability('moodle/user:viewhiddendetails', $context)) {
  205. if ($user->address) {
  206. print_row(get_string("address").":", "$user->address");
  207. }
  208. if ($user->phone1) {
  209. print_row(get_string("phone").":", "$user->phone1");
  210. }
  211. if ($user->phone2) {
  212. print_row(get_string("phone2").":", "$user->phone2");
  213. }
  214. }
  215. if ($currentuser
  216. or $user->maildisplay == 1
  217. or has_capability('moodle/course:useremail', $context)
  218. or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
  219. print_row(get_string("email").":", obfuscate_mailto($user->email, ''));
  220. }
  221. if ($user->url && !isset($hiddenfields['webpage'])) {
  222. $url = $user->url;
  223. if (strpos($user->url, '://') === false) {
  224. $url = 'http://'. $url;
  225. }
  226. print_row(get_string("webpage") .":", '<a href="'.s($url).'">'.s($user->url).'</a>');
  227. }
  228. if ($user->icq && !isset($hiddenfields['icqnumber'])) {
  229. print_row(get_string('icqnumber').':',"<a href=\"http://web.icq.com/wwp?uin=".urlencode($user->icq)."\">".s($user->icq)." <img src=\"http://web.icq.com/whitepages/online?icq=".urlencode($user->icq)."&amp;img=5\" alt=\"\" /></a>");
  230. }
  231. if ($user->skype && !isset($hiddenfields['skypeid'])) {
  232. print_row(get_string('skypeid').':','<a href="callto:'.urlencode($user->skype).'">'.s($user->skype).
  233. ' <img src="http://mystatus.skype.com/smallicon/'.urlencode($user->skype).'" alt="'.get_string('status').'" '.
  234. ' /></a>');
  235. }
  236. if ($user->yahoo && !isset($hiddenfields['yahooid'])) {
  237. print_row(get_string('yahooid').':', '<a href="http://edit.yahoo.com/config/send_webmesg?.target='.urlencode($user->yahoo).'&amp;.src=pg">'.s($user->yahoo)." <img src=\"http://opi.yahoo.com/online?u=".urlencode($user->yahoo)."&m=g&t=0\" alt=\"\"></a>");
  238. }
  239. if ($user->aim && !isset($hiddenfields['aimid'])) {
  240. print_row(get_string('aimid').':', '<a href="aim:goim?screenname='.urlencode($user->aim).'">'.s($user->aim).'</a>');
  241. }
  242. if ($user->msn && !isset($hiddenfields['msnid'])) {
  243. print_row(get_string('msnid').':', s($user->msn));
  244. }
  245. /// Print the Custom User Fields
  246. profile_display_fields($user->id);
  247. if (!isset($hiddenfields['mycourses'])) {
  248. if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
  249. $shown=0;
  250. $courselisting = '';
  251. foreach ($mycourses as $mycourse) {
  252. if ($mycourse->category) {
  253. $class = '';
  254. if ($mycourse->visible == 0) {
  255. $ccontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
  256. if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
  257. continue;
  258. }
  259. $class = 'class="dimmed"';
  260. }
  261. $courselisting .= "<a href=\"{$CFG->wwwroot}/user/view.php?id={$user->id}&amp;course={$mycourse->id}\" $class >" . format_string($mycourse->fullname) . "</a>, ";
  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['firstaccess'])) {
  273. if ($user->firstaccess) {
  274. $datestring = userdate($user->firstaccess)."&nbsp; (".format_time(time() - $user->firstaccess).")";
  275. } else {
  276. $datestring = get_string("never");
  277. }
  278. print_row(get_string("firstaccess").":", $datestring);
  279. }
  280. if (!isset($hiddenfields['lastaccess'])) {
  281. if ($user->lastaccess) {
  282. $datestring = userdate($user->lastaccess)."&nbsp; (".format_time(time() - $user->lastaccess).")";
  283. } else {
  284. $datestring = get_string("never");
  285. }
  286. print_row(get_string("lastaccess").":", $datestring);
  287. }
  288. /// Printing tagged interests
  289. if (!empty($CFG->usetags)) {
  290. if ($interests = tag_get_tags_csv('user', $user->id) ) {
  291. print_row(get_string('interests') .": ", $interests);
  292. }
  293. }
  294. echo "</table></div></div>";
  295. echo $OUTPUT->blocks_for_region('content');
  296. // Print messaging link if allowed
  297. if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
  298. && !empty($CFG->messaging) && !isguestuser() && !isguestuser($user) && ($USER->id != $user->id)) {
  299. echo '<div class="messagebox">';
  300. echo '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'">'.get_string('messageselectadd').'</a>';
  301. echo '</div>';
  302. }
  303. if ($CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) && $currentuser) { // Show user object
  304. echo '<br /><br /><hr />';
  305. echo $OUTPUT->heading('DEBUG MODE: User session variables');
  306. print_object($USER);
  307. }
  308. echo '</div>'; // userprofile class
  309. echo $OUTPUT->footer();
  310. function print_row($left, $right) {
  311. echo "\n<tr><td class=\"label c0\">$left</td><td class=\"info c1\">$right</td></tr>\n";
  312. }