Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/user/ -> view.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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  /**
  18   * Display profile for a particular user
  19   *
  20   * @package core_user
  21   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once("../config.php");
  26  require_once($CFG->dirroot.'/user/profile/lib.php');
  27  require_once($CFG->dirroot.'/user/lib.php');
  28  require_once($CFG->libdir . '/filelib.php');
  29  require_once($CFG->libdir . '/badgeslib.php');
  30  
  31  $id             = optional_param('id', 0, PARAM_INT); // User id.
  32  $courseid       = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site).
  33  $showallcourses = optional_param('showallcourses', 0, PARAM_INT);
  34  
  35  // See your own profile by default.
  36  if (empty($id)) {
  37      require_login();
  38      $id = $USER->id;
  39  }
  40  
  41  if ($courseid == SITEID) {   // Since Moodle 2.0 all site-level profiles are shown by profile.php.
  42      redirect($CFG->wwwroot.'/user/profile.php?id='.$id);  // Immediate redirect.
  43  }
  44  
  45  $PAGE->set_url('/user/view.php', array('id' => $id, 'course' => $courseid));
  46  
  47  $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
  48  $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  49  $currentuser = ($user->id == $USER->id);
  50  
  51  $systemcontext = context_system::instance();
  52  $coursecontext = context_course::instance($course->id);
  53  $usercontext   = context_user::instance($user->id, IGNORE_MISSING);
  54  
  55  // Check we are not trying to view guest's profile.
  56  if (isguestuser($user)) {
  57      // Can not view profile of guest - thre is nothing to see there.
  58      throw new \moodle_exception('invaliduserid');
  59  }
  60  
  61  $PAGE->set_context($coursecontext);
  62  
  63  if (!empty($CFG->forceloginforprofiles)) {
  64      require_login(); // We can not log in to course due to the parent hack below.
  65  
  66      // Guests do not have permissions to view anyone's profile if forceloginforprofiles is set.
  67      if (isguestuser()) {
  68          $PAGE->set_secondary_navigation(false);
  69          echo $OUTPUT->header();
  70          echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
  71                                get_login_url(),
  72                                $CFG->wwwroot);
  73          echo $OUTPUT->footer();
  74          die;
  75      }
  76  }
  77  
  78  $PAGE->set_course($course);
  79  $PAGE->set_pagetype('course-view-' . $course->format);  // To get the blocks exactly like the course.
  80  $PAGE->add_body_class('path-user');                     // So we can style it independently.
  81  $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  82  
  83  // Set the Moodle docs path explicitly because the default behaviour
  84  // of inhereting the pagetype will lead to an incorrect docs location.
  85  $PAGE->set_docs_path('user/profile');
  86  
  87  $isparent = false;
  88  
  89  if (!$currentuser and !$user->deleted
  90    and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
  91    and has_capability('moodle/user:viewdetails', $usercontext)) {
  92      // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
  93      //       this way they may access the profile where they get overview of grades and child activity in course,
  94      //       please note this is just a guess!
  95      require_login();
  96      $isparent = true;
  97      $PAGE->navigation->set_userid_for_parent_checks($id);
  98  } else {
  99      // Normal course.
 100      require_login($course);
 101      // What to do with users temporary accessing this course? should they see the details?
 102  }
 103  
 104  $strpersonalprofile = get_string('personalprofile');
 105  $strparticipants = get_string("participants");
 106  $struser = get_string("user");
 107  
 108  $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
 109  
 110  // Now test the actual capabilities and enrolment in course.
 111  if ($currentuser) {
 112      if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
 113          // Need to have full access to a course to see the rest of own info.
 114          $referer = get_local_referer(false);
 115          if (!empty($referer)) {
 116              redirect($referer, get_string('notenrolled', '', $fullname));
 117          }
 118          echo $OUTPUT->header();
 119          echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
 120          echo $OUTPUT->footer();
 121          die;
 122      }
 123  
 124  } else {
 125      // Somebody else.
 126      $PAGE->set_title("$strpersonalprofile: ");
 127      $PAGE->set_heading("$strpersonalprofile: ");
 128  
 129      // Check to see if the user can see this user's profile.
 130      if (!user_can_view_profile($user, $course, $usercontext) && !$isparent) {
 131          throw new \moodle_exception('cannotviewprofile');
 132      }
 133  
 134      if (!is_enrolled($coursecontext, $user->id)) {
 135          // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
 136          //       to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
 137          //       or test for course:inspect capability.
 138          if (has_capability('moodle/role:assign', $coursecontext)) {
 139              $PAGE->navbar->add($fullname);
 140              $notice = get_string('notenrolled', '', $fullname);
 141          } else {
 142              $PAGE->navbar->add($struser);
 143              $notice = get_string('notenrolledprofile', '', $fullname);
 144          }
 145          $referer = get_local_referer(false);
 146          if (!empty($referer)) {
 147              redirect($referer, $notice);
 148          }
 149          echo $OUTPUT->header();
 150          echo $OUTPUT->heading($notice);
 151          echo $OUTPUT->footer();
 152          exit;
 153      }
 154  
 155      if (!isloggedin() or isguestuser()) {
 156          // Do not use require_login() here because we might have already used require_login($course).
 157          redirect(get_login_url());
 158      }
 159  }
 160  
 161  $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
 162  $PAGE->set_heading($course->fullname);
 163  $PAGE->set_pagelayout('mypublic');
 164  $PAGE->add_body_class('limitedwidth');
 165  
 166  // Locate the users settings in the settings navigation and force it open.
 167  // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
 168  if (!$currentuser) {
 169      $PAGE->navigation->extend_for_user($user);
 170      if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
 171          $node->forceopen = true;
 172      }
 173  } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
 174      $node->forceopen = true;
 175  }
 176  if ($node = $PAGE->settingsnav->get('courseadmin')) {
 177      $node->forceopen = false;
 178  }
 179  
 180  echo $OUTPUT->header();
 181  
 182  echo '<div class="userprofile">';
 183  $headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
 184  echo $OUTPUT->context_header($headerinfo, 2);
 185  
 186  if ($user->deleted) {
 187      echo $OUTPUT->heading(get_string('userdeleted'));
 188      if (!has_capability('moodle/user:update', $coursecontext)) {
 189          echo $OUTPUT->footer();
 190          die;
 191      }
 192  }
 193  
 194  // OK, security out the way, now we are showing the user.
 195  // Trigger a user profile viewed event.
 196  profile_view($user, $coursecontext, $course);
 197  
 198  $hiddenfields = [];
 199  if (!has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
 200      $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
 201  }
 202  if ($user->description && !isset($hiddenfields['description'])) {
 203      echo '<div class="description">';
 204      if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
 205          echo get_string('profilenotshown', 'moodle');
 206      } else {
 207          if ($courseid == SITEID) {
 208              $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
 209          } else {
 210              // We have to make a little detour thought the course context to verify the access control for course profile.
 211              $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
 212          }
 213          $options = array('overflowdiv' => true);
 214          echo format_text($user->description, $user->descriptionformat, $options);
 215      }
 216      echo '</div>'; // Description class.
 217  }
 218  
 219  // Render custom blocks.
 220  $renderer = $PAGE->get_renderer('core_user', 'myprofile');
 221  $tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
 222  echo $renderer->render($tree);
 223  
 224  echo '</div>';  // Userprofile class.
 225  
 226  echo $OUTPUT->footer();