[ Index ]

PHP Cross Reference of moodle-2.8

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1