[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/user/ -> edit.php (source)

   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   * Allows you to edit a users profile
  19   *
  20   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core_user
  23   */
  24  
  25  require_once('../config.php');
  26  require_once($CFG->libdir.'/gdlib.php');
  27  require_once($CFG->dirroot.'/user/edit_form.php');
  28  require_once($CFG->dirroot.'/user/editlib.php');
  29  require_once($CFG->dirroot.'/user/profile/lib.php');
  30  require_once($CFG->dirroot.'/user/lib.php');
  31  
  32  // HTTPS is required in this page when $CFG->loginhttps enabled.
  33  $PAGE->https_required();
  34  
  35  $userid = optional_param('id', $USER->id, PARAM_INT);    // User id.
  36  $course = optional_param('course', SITEID, PARAM_INT);   // Course id (defaults to Site).
  37  $returnto = optional_param('returnto', null, PARAM_ALPHA);  // Code determining where to return to after save.
  38  $cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT);   // Course id (defaults to Site).
  39  
  40  $PAGE->set_url('/user/edit.php', array('course' => $course, 'id' => $userid));
  41  
  42  if (!$course = $DB->get_record('course', array('id' => $course))) {
  43      print_error('invalidcourseid');
  44  }
  45  
  46  if ($course->id != SITEID) {
  47      require_login($course);
  48  } else if (!isloggedin()) {
  49      if (empty($SESSION->wantsurl)) {
  50          $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
  51      }
  52      redirect(get_login_url());
  53  } else {
  54      $PAGE->set_context(context_system::instance());
  55  }
  56  
  57  // Guest can not edit.
  58  if (isguestuser()) {
  59      print_error('guestnoeditprofile');
  60  }
  61  
  62  // The user profile we are editing.
  63  if (!$user = $DB->get_record('user', array('id' => $userid))) {
  64      print_error('invaliduserid');
  65  }
  66  
  67  // Guest can not be edited.
  68  if (isguestuser($user)) {
  69      print_error('guestnoeditprofile');
  70  }
  71  
  72  // User interests separated by commas.
  73  $user->interests = core_tag_tag::get_item_tags_array('core', 'user', $user->id);
  74  
  75  // Remote users cannot be edited.
  76  if (is_mnet_remote_user($user)) {
  77      if (user_not_fully_set_up($user)) {
  78          $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $user->mnethostid));
  79          print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
  80      }
  81      redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
  82  }
  83  
  84  // Load the appropriate auth plugin.
  85  $userauth = get_auth_plugin($user->auth);
  86  
  87  if (!$userauth->can_edit_profile()) {
  88      print_error('noprofileedit', 'auth');
  89  }
  90  
  91  if ($editurl = $userauth->edit_profile_url()) {
  92      // This internal script not used.
  93      redirect($editurl);
  94  }
  95  
  96  if ($course->id == SITEID) {
  97      $coursecontext = context_system::instance();   // SYSTEM context.
  98  } else {
  99      $coursecontext = context_course::instance($course->id);   // Course context.
 100  }
 101  $systemcontext   = context_system::instance();
 102  $personalcontext = context_user::instance($user->id);
 103  
 104  // Check access control.
 105  if ($user->id == $USER->id) {
 106      // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
 107      if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
 108          print_error('cannotedityourprofile');
 109      }
 110  
 111  } else {
 112      // Teachers, parents, etc.
 113      require_capability('moodle/user:editprofile', $personalcontext);
 114      // No editing of guest user account.
 115      if (isguestuser($user->id)) {
 116          print_error('guestnoeditprofileother');
 117      }
 118      // No editing of primary admin!
 119      if (is_siteadmin($user) and !is_siteadmin($USER)) {  // Only admins may edit other admins.
 120          print_error('useradmineditadmin');
 121      }
 122  }
 123  
 124  if ($user->deleted) {
 125      echo $OUTPUT->header();
 126      echo $OUTPUT->heading(get_string('userdeleted'));
 127      echo $OUTPUT->footer();
 128      die;
 129  }
 130  
 131  $PAGE->set_pagelayout('admin');
 132  $PAGE->set_context($personalcontext);
 133  if ($USER->id != $user->id) {
 134      $PAGE->navigation->extend_for_user($user);
 135  } else {
 136      if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
 137          $node->force_open();
 138      }
 139  }
 140  
 141  // Process email change cancellation.
 142  if ($cancelemailchange) {
 143      cancel_email_update($user->id);
 144  }
 145  
 146  // Load user preferences.
 147  useredit_load_preferences($user);
 148  
 149  // Load custom profile fields data.
 150  profile_load_data($user);
 151  
 152  
 153  // Prepare the editor and create form.
 154  $editoroptions = array(
 155      'maxfiles'   => EDITOR_UNLIMITED_FILES,
 156      'maxbytes'   => $CFG->maxbytes,
 157      'trusttext'  => false,
 158      'forcehttps' => false,
 159      'context'    => $personalcontext
 160  );
 161  
 162  $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
 163  // Prepare filemanager draft area.
 164  $draftitemid = 0;
 165  $filemanagercontext = $editoroptions['context'];
 166  $filemanageroptions = array('maxbytes'       => $CFG->maxbytes,
 167                               'subdirs'        => 0,
 168                               'maxfiles'       => 1,
 169                               'accepted_types' => 'web_image');
 170  file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
 171  $user->imagefile = $draftitemid;
 172  // Create form.
 173  $userform = new user_edit_form(new moodle_url($PAGE->url, array('returnto' => $returnto)), array(
 174      'editoroptions' => $editoroptions,
 175      'filemanageroptions' => $filemanageroptions,
 176      'user' => $user));
 177  
 178  $emailchanged = false;
 179  
 180  if ($usernew = $userform->get_data()) {
 181  
 182      // Deciding where to send the user back in most cases.
 183      if ($returnto === 'profile') {
 184          if ($course->id != SITEID) {
 185              $returnurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
 186          } else {
 187              $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id));
 188          }
 189      } else {
 190          $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id));
 191      }
 192  
 193      $emailchangedhtml = '';
 194  
 195      if ($CFG->emailchangeconfirmation) {
 196          // Users with 'moodle/user:update' can change their email address immediately.
 197          // Other users require a confirmation email.
 198          if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
 199              $a = new stdClass();
 200              $a->newemail = $usernew->preference_newemail = $usernew->email;
 201              $usernew->preference_newemailkey = random_string(20);
 202              $usernew->preference_newemailattemptsleft = 3;
 203              $a->oldemail = $usernew->email = $user->email;
 204  
 205              $emailchangedhtml = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
 206              $emailchangedhtml .= $OUTPUT->continue_button($returnurl);
 207              $emailchanged = true;
 208          }
 209      }
 210  
 211      $authplugin = get_auth_plugin($user->auth);
 212  
 213      $usernew->timemodified = time();
 214  
 215      // Description editor element may not exist!
 216      if (isset($usernew->description_editor) && isset($usernew->description_editor['format'])) {
 217          $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
 218      }
 219  
 220      // Pass a true old $user here.
 221      if (!$authplugin->user_update($user, $usernew)) {
 222          // Auth update failed.
 223          print_error('cannotupdateprofile');
 224      }
 225  
 226      // Update user with new profile data.
 227      user_update_user($usernew, false, false);
 228  
 229      // Update preferences.
 230      useredit_update_user_preference($usernew);
 231  
 232      // Update interests.
 233      if (isset($usernew->interests)) {
 234          useredit_update_interests($usernew, $usernew->interests);
 235      }
 236  
 237      // Update user picture.
 238      if (empty($CFG->disableuserimages)) {
 239          core_user::update_picture($usernew, $filemanageroptions);
 240      }
 241  
 242      // Update mail bounces.
 243      useredit_update_bounces($user, $usernew);
 244  
 245      // Update forum track preference.
 246      useredit_update_trackforums($user, $usernew);
 247  
 248      // Save custom profile fields data.
 249      profile_save_data($usernew);
 250  
 251      // Trigger event.
 252      \core\event\user_updated::create_from_userid($user->id)->trigger();
 253  
 254      // If email was changed and confirmation is required, send confirmation email now to the new address.
 255      if ($emailchanged && $CFG->emailchangeconfirmation) {
 256          $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
 257          $tempuser->email = $usernew->preference_newemail;
 258  
 259          $a = new stdClass();
 260          $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
 261          $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
 262          $a->fullname = fullname($tempuser, true);
 263  
 264          $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
 265          $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
 266  
 267          // Email confirmation directly rather than using messaging so they will definitely get an email.
 268          $supportuser = core_user::get_support_user();
 269          if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
 270              die("could not send email!");
 271          }
 272      }
 273  
 274      // Reload from db, we need new full name on this page if we do not redirect.
 275      $user = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
 276  
 277      if ($USER->id == $user->id) {
 278          // Override old $USER session variable if needed.
 279          foreach ((array)$user as $variable => $value) {
 280              if ($variable === 'description' or $variable === 'password') {
 281                  // These are not set for security nad perf reasons.
 282                  continue;
 283              }
 284              $USER->$variable = $value;
 285          }
 286          // Preload custom fields.
 287          profile_load_custom_fields($USER);
 288      }
 289  
 290      if (is_siteadmin() and empty($SITE->shortname)) {
 291          // Fresh cli install - we need to finish site settings.
 292          redirect(new moodle_url('/admin/index.php'));
 293      }
 294  
 295      if (!$emailchanged || !$CFG->emailchangeconfirmation) {
 296          redirect($returnurl);
 297      }
 298  }
 299  
 300  // Make sure we really are on the https page when https login required.
 301  $PAGE->verify_https_required();
 302  
 303  
 304  // Display page header.
 305  $streditmyprofile = get_string('editmyprofile');
 306  $strparticipants  = get_string('participants');
 307  $userfullname     = fullname($user, true);
 308  
 309  $PAGE->set_title("$course->shortname: $streditmyprofile");
 310  $PAGE->set_heading($userfullname);
 311  
 312  echo $OUTPUT->header();
 313  echo $OUTPUT->heading($userfullname);
 314  
 315  if ($emailchanged) {
 316      echo $emailchangedhtml;
 317  } else {
 318      // Finally display THE form.
 319      $userform->display();
 320  }
 321  
 322  // And proper footer.
 323  echo $OUTPUT->footer();
 324  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1