[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/user/ -> edit.php (source)

   1  <?php // $Id: edit.php,v 1.167.2.12 2008/09/25 10:09:59 mudrd8mz Exp $
   2  
   3      require_once ('../config.php');
   4      require_once($CFG->libdir.'/gdlib.php');
   5      require_once($CFG->dirroot.'/user/edit_form.php');
   6      require_once($CFG->dirroot.'/user/editlib.php');
   7      require_once($CFG->dirroot.'/user/profile/lib.php');
   8  
   9      httpsrequired();
  10  
  11      $userid = optional_param('id', $USER->id, PARAM_INT);    // user id
  12      $course = optional_param('course', SITEID, PARAM_INT);   // course id (defaults to Site)
  13      $cancelemailchange = optional_param('cancelemailchange', false, PARAM_INT);   // course id (defaults to Site)
  14  
  15      if (!$course = get_record('course', 'id', $course)) {
  16          error('Course ID was incorrect');
  17      }
  18  
  19      if ($course->id != SITEID) {
  20          require_login($course);
  21      } else if (!isloggedin()) {
  22          if (empty($SESSION->wantsurl)) {
  23              $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
  24          }
  25          redirect($CFG->httpswwwroot.'/login/index.php');
  26      }
  27  
  28      // Guest can not edit
  29      if (isguestuser()) {
  30          print_error('guestnoeditprofile');
  31      }
  32  
  33      // The user profile we are editing
  34      if (!$user = get_record('user', 'id', $userid)) {
  35          error('User ID was incorrect');
  36      }
  37  
  38      // Guest can not be edited
  39      if (isguestuser($user)) {
  40          print_error('guestnoeditprofile');
  41      }
  42  
  43      // User interests separated by commas
  44      if (!empty($CFG->usetags)) {
  45          require_once($CFG->dirroot.'/tag/lib.php');
  46          $user->interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT);
  47      }
  48  
  49      // remote users cannot be edited
  50      if (is_mnet_remote_user($user)) {
  51          redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
  52      }
  53  
  54      if ($course->id == SITEID) {
  55          $coursecontext = get_context_instance(CONTEXT_SYSTEM);   // SYSTEM context
  56      } else {
  57          $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);   // Course context
  58      }
  59      $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
  60      $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
  61  
  62      // check access control
  63      if ($user->id == $USER->id) {
  64          //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
  65          if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
  66              error('Can not edit own profile, sorry.');
  67          }
  68  
  69      } else {
  70          // teachers, parents, etc.
  71          require_capability('moodle/user:editprofile', $personalcontext);
  72          // no editing of guest user account
  73          if (isguestuser($user->id)) {
  74              print_error('guestnoeditprofileother');
  75          }
  76          // no editing of primary admin!
  77          if (is_primary_admin($user->id)) {
  78              print_error('adminprimarynoedit');
  79          }
  80      }
  81  
  82      if ($user->deleted) {
  83          print_header();
  84          print_heading(get_string('userdeleted'));
  85          print_footer($course);
  86          die;
  87      }
  88  
  89      // Process email change cancellation
  90      if ($cancelemailchange) {
  91          cancel_email_update($user->id);
  92      }
  93  
  94      //load user preferences
  95      useredit_load_preferences($user);
  96  
  97      //Load custom profile fields data
  98      profile_load_data($user);
  99  
 100  
 101      //create form
 102      $userform = new user_edit_form();
 103      if (empty($user->country)) {
 104          // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
 105          unset($user->country);
 106      }
 107      $userform->set_data($user);
 108  
 109      $email_changed = false;
 110  
 111      if ($usernew = $userform->get_data()) {
 112  
 113          add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
 114  
 115          $email_changed_html = '';
 116  
 117          if ($CFG->emailchangeconfirmation) {
 118              // Handle change of email carefully for non-trusted users
 119              if ($user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
 120                  $a = new stdClass();
 121                  $a->newemail = $usernew->preference_newemail = $usernew->email;
 122                  $usernew->preference_newemailkey = random_string(20);
 123                  $usernew->preference_newemailattemptsleft = 3;
 124                  $a->oldemail = $usernew->email = $user->email;
 125  
 126                  $email_changed_html = print_box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice', true);
 127                  $email_changed_html .= print_continue("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id", true);
 128                  $email_changed = true;
 129              }
 130          }
 131  
 132          $authplugin = get_auth_plugin($user->auth);
 133  
 134          $usernew->timemodified = time();
 135  
 136          if (!update_record('user', $usernew)) {
 137              error('Error updating user record');
 138          }
 139  
 140          // pass a true $userold here
 141          if (! $authplugin->user_update($user, $userform->get_data(false))) {
 142              // auth update failed, rollback for moodle
 143              update_record('user', addslashes_object($user));
 144              error('Failed to update user data on external auth: '.$user->auth.
 145                      '. See the server logs for more details.');
 146          }
 147  
 148          //update preferences
 149          useredit_update_user_preference($usernew);
 150  
 151          //update interests
 152          if (!empty($CFG->usetags)) {
 153              useredit_update_interests($usernew, $usernew->interests);
 154          }
 155  
 156          //update user picture
 157          if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
 158              useredit_update_picture($usernew, $userform);
 159          }
 160  
 161          // update mail bounces
 162          useredit_update_bounces($user, $usernew);
 163  
 164          /// update forum track preference
 165          useredit_update_trackforums($user, $usernew);
 166  
 167          // save custom profile fields data
 168          profile_save_data($usernew);
 169  
 170          // If email was changed, send confirmation email now
 171          if ($email_changed && $CFG->emailchangeconfirmation) {
 172              $temp_user = fullclone($user);
 173              $temp_user->email = $usernew->preference_newemail;
 174              $temp_user->emailstop = NULL;
 175  
 176              $a = new stdClass();
 177              $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
 178              $a->site = $SITE->fullname;
 179              $a->fullname = fullname($user, true);
 180  
 181              $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth', $a);
 182              $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth', $a);
 183  
 184              if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
 185                  die("could not send email!");
 186              }
 187          }
 188  
 189          // reload from db
 190          $usernew = get_record('user', 'id', $user->id);
 191          events_trigger('user_updated', $usernew);
 192  
 193          if ($USER->id == $user->id) {
 194              // Override old $USER session variable if needed
 195              foreach ((array)$usernew as $variable => $value) {
 196                  $USER->$variable = $value;
 197              }
 198          }
 199  
 200          if (!$email_changed || !$CFG->emailchangeconfirmation) {
 201              redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
 202          }
 203      }
 204  
 205  
 206  /// Display page header
 207      $streditmyprofile = get_string('editmyprofile');
 208      $strparticipants  = get_string('participants');
 209      $userfullname     = fullname($user, true);
 210  
 211      $navlinks = array();
 212      if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
 213          $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
 214      }
 215      $navlinks[] = array('name' => $userfullname,
 216                          'link' => "view.php?id=$user->id&amp;course=$course->id",
 217                          'type' => 'misc');
 218      $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
 219      $navigation = build_navigation($navlinks);
 220      print_header("$course->shortname: $streditmyprofile", $course->fullname, $navigation, "");
 221  
 222      /// Print tabs at the top
 223      $showroles = 1;
 224      $currenttab = 'editprofile';
 225      require ('tabs.php');
 226  
 227      if ($email_changed) {
 228          echo $email_changed_html;
 229      } else {
 230      /// Finally display THE form
 231          $userform->display();
 232      }
 233  
 234  /// and proper footer
 235      print_footer($course);
 236  
 237  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7