Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/user/ -> view.php (source)

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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      print_error('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          echo $OUTPUT->header();
  69          echo $OUTPUT->confirm(get_string('guestcantaccessprofiles', 'error'),
  70                                get_login_url(),
  71                                $CFG->wwwroot);
  72          echo $OUTPUT->footer();
  73          die;
  74      }
  75  }
  76  
  77  $PAGE->set_course($course);
  78  $PAGE->set_pagetype('course-view-' . $course->format);  // To get the blocks exactly like the course.
  79  $PAGE->add_body_class('path-user');                     // So we can style it independently.
  80  $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  81  
  82  // Set the Moodle docs path explicitly because the default behaviour
  83  // of inhereting the pagetype will lead to an incorrect docs location.
  84  $PAGE->set_docs_path('user/profile');
  85  
  86  $isparent = false;
  87  
  88  if (!$currentuser and !$user->deleted
  89    and $DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))
  90    and has_capability('moodle/user:viewdetails', $usercontext)) {
  91      // TODO: very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
  92      //       this way they may access the profile where they get overview of grades and child activity in course,
  93      //       please note this is just a guess!
  94      require_login();
  95      $isparent = true;
  96      $PAGE->navigation->set_userid_for_parent_checks($id);
  97  } else {
  98      // Normal course.
  99      require_login($course);
 100      // What to do with users temporary accessing this course? should they see the details?
 101  }
 102  
 103  $strpersonalprofile = get_string('personalprofile');
 104  $strparticipants = get_string("participants");
 105  $struser = get_string("user");
 106  
 107  $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $coursecontext));
 108  
 109  // Now test the actual capabilities and enrolment in course.
 110  if ($currentuser) {
 111      if (!is_viewing($coursecontext) && !is_enrolled($coursecontext)) {
 112          // Need to have full access to a course to see the rest of own info.
 113          $referer = get_local_referer(false);
 114          if (!empty($referer)) {
 115              redirect($referer, get_string('notenrolled', '', $fullname));
 116          }
 117          echo $OUTPUT->header();
 118          echo $OUTPUT->heading(get_string('notenrolled', '', $fullname));
 119          echo $OUTPUT->footer();
 120          die;
 121      }
 122  
 123  } else {
 124      // Somebody else.
 125      $PAGE->set_title("$strpersonalprofile: ");
 126      $PAGE->set_heading("$strpersonalprofile: ");
 127  
 128      // Check to see if the user can see this user's profile.
 129      if (!user_can_view_profile($user, $course, $usercontext) && !$isparent) {
 130          print_error('cannotviewprofile');
 131      }
 132  
 133      if (!is_enrolled($coursecontext, $user->id)) {
 134          // TODO: the only potential problem is that managers and inspectors might post in forum, but the link
 135          //       to profile would not work - maybe a new capability - moodle/user:freely_acessile_profile_for_anybody
 136          //       or test for course:inspect capability.
 137          if (has_capability('moodle/role:assign', $coursecontext)) {
 138              $PAGE->navbar->add($fullname);
 139              $notice = get_string('notenrolled', '', $fullname);
 140          } else {
 141              $PAGE->navbar->add($struser);
 142              $notice = get_string('notenrolledprofile', '', $fullname);
 143          }
 144          $referer = get_local_referer(false);
 145          if (!empty($referer)) {
 146              redirect($referer, $notice);
 147          }
 148          echo $OUTPUT->header();
 149          echo $OUTPUT->heading($notice);
 150          echo $OUTPUT->footer();
 151          exit;
 152      }
 153  
 154      if (!isloggedin() or isguestuser()) {
 155          // Do not use require_login() here because we might have already used require_login($course).
 156          redirect(get_login_url());
 157      }
 158  }
 159  
 160  $PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
 161  $PAGE->set_heading($course->fullname);
 162  $PAGE->set_pagelayout('mypublic');
 163  
 164  // Locate the users settings in the settings navigation and force it open.
 165  // This MUST be done after we've set up the page as it is going to cause theme and output to initialise.
 166  if (!$currentuser) {
 167      $PAGE->navigation->extend_for_user($user);
 168      if ($node = $PAGE->settingsnav->get('userviewingsettings'.$user->id)) {
 169          $node->forceopen = true;
 170      }
 171  } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
 172      $node->forceopen = true;
 173  }
 174  if ($node = $PAGE->settingsnav->get('courseadmin')) {
 175      $node->forceopen = false;
 176  }
 177  
 178  echo $OUTPUT->header();
 179  
 180  echo '<div class="userprofile">';
 181  $headerinfo = array('heading' => fullname($user), 'user' => $user, 'usercontext' => $usercontext);
 182  echo $OUTPUT->context_header($headerinfo, 2);
 183  
 184  if ($user->deleted) {
 185      echo $OUTPUT->heading(get_string('userdeleted'));
 186      if (!has_capability('moodle/user:update', $coursecontext)) {
 187          echo $OUTPUT->footer();
 188          die;
 189      }
 190  }
 191  
 192  // OK, security out the way, now we are showing the user.
 193  // Trigger a user profile viewed event.
 194  profile_view($user, $coursecontext, $course);
 195  
 196  $hiddenfields = [];
 197  if (!has_capability('moodle/user:viewhiddendetails', $coursecontext)) {
 198      $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
 199  }
 200  if ($user->description && !isset($hiddenfields['description'])) {
 201      echo '<div class="description">';
 202      if (!empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $id))) {
 203          echo get_string('profilenotshown', 'moodle');
 204      } else {
 205          if ($courseid == SITEID) {
 206              $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $usercontext->id, 'user', 'profile', null);
 207          } else {
 208              // We have to make a little detour thought the course context to verify the access control for course profile.
 209              $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $coursecontext->id, 'user', 'profile', $user->id);
 210          }
 211          $options = array('overflowdiv' => true);
 212          echo format_text($user->description, $user->descriptionformat, $options);
 213      }
 214      echo '</div>'; // Description class.
 215  }
 216  
 217  // Render custom blocks.
 218  $renderer = $PAGE->get_renderer('core_user', 'myprofile');
 219  $tree = core_user\output\myprofile\manager::build_tree($user, $currentuser, $course);
 220  echo $renderer->render($tree);
 221  
 222  echo '</div>';  // Userprofile class.
 223  
 224  echo $OUTPUT->footer();