[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/user/ -> editlib.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   * This file contains function used when editing a users profile and preferences.
  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  /**
  26   * Cancels the requirement for a user to update their email address.
  27   *
  28   * @param int $userid
  29   */
  30  function cancel_email_update($userid) {
  31      unset_user_preference('newemail', $userid);
  32      unset_user_preference('newemailkey', $userid);
  33      unset_user_preference('newemailattemptsleft', $userid);
  34  }
  35  
  36  /**
  37   * Performs the common access checks and page setup for all
  38   * user preference pages.
  39   *
  40   * @param int $userid The user id to edit taken from the page params.
  41   * @param int $courseid The optional course id if we came from a course context.
  42   * @return array containing the user and course records.
  43   */
  44  function useredit_setup_preference_page($userid, $courseid) {
  45      global $PAGE, $SESSION, $DB, $CFG, $OUTPUT, $USER;
  46  
  47      // Guest can not edit.
  48      if (isguestuser()) {
  49          print_error('guestnoeditprofile');
  50      }
  51  
  52      if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  53          print_error('invalidcourseid');
  54      }
  55  
  56      if ($course->id != SITEID) {
  57          require_login($course);
  58      } else if (!isloggedin()) {
  59          if (empty($SESSION->wantsurl)) {
  60              $SESSION->wantsurl = $CFG->httpswwwroot.'/user/preferences.php';
  61          }
  62          redirect(get_login_url());
  63      } else {
  64          $PAGE->set_context(context_system::instance());
  65      }
  66  
  67      // The user profile we are editing.
  68      if (!$user = $DB->get_record('user', array('id' => $userid))) {
  69          print_error('invaliduserid');
  70      }
  71  
  72      // Guest can not be edited.
  73      if (isguestuser($user)) {
  74          print_error('guestnoeditprofile');
  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      $systemcontext   = context_system::instance();
  87      $personalcontext = context_user::instance($user->id);
  88  
  89      // Check access control.
  90      if ($user->id == $USER->id) {
  91          // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
  92          if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
  93              print_error('cannotedityourprofile');
  94          }
  95  
  96      } else {
  97          // Teachers, parents, etc.
  98          require_capability('moodle/user:editprofile', $personalcontext);
  99  
 100          // No editing of primary admin!
 101          if (is_siteadmin($user) and !is_siteadmin($USER)) {  // Only admins may edit other admins.
 102              print_error('useradmineditadmin');
 103          }
 104      }
 105  
 106      if ($user->deleted) {
 107          echo $OUTPUT->header();
 108          echo $OUTPUT->heading(get_string('userdeleted'));
 109          echo $OUTPUT->footer();
 110          die;
 111      }
 112  
 113      $PAGE->set_pagelayout('admin');
 114      $PAGE->set_context($personalcontext);
 115      if ($USER->id != $user->id) {
 116          $PAGE->navigation->extend_for_user($user);
 117      } else {
 118          if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
 119              $node->force_open();
 120          }
 121      }
 122  
 123      return array($user, $course);
 124  }
 125  
 126  /**
 127   * Loads the given users preferences into the given user object.
 128   *
 129   * @param stdClass $user The user object, modified by reference.
 130   * @param bool $reload
 131   */
 132  function useredit_load_preferences(&$user, $reload=true) {
 133      global $USER;
 134  
 135      if (!empty($user->id)) {
 136          if ($reload and $USER->id == $user->id) {
 137              // Reload preferences in case it was changed in other session.
 138              unset($USER->preference);
 139          }
 140  
 141          if ($preferences = get_user_preferences(null, null, $user->id)) {
 142              foreach ($preferences as $name => $value) {
 143                  $user->{'preference_'.$name} = $value;
 144              }
 145          }
 146      }
 147  }
 148  
 149  /**
 150   * Updates the user preferences for teh given user.
 151   *
 152   * @param stdClass|array $usernew
 153   */
 154  function useredit_update_user_preference($usernew) {
 155      $ua = (array)$usernew;
 156      foreach ($ua as $key => $value) {
 157          if (strpos($key, 'preference_') === 0) {
 158              $name = substr($key, strlen('preference_'));
 159              set_user_preference($name, $value, $usernew->id);
 160          }
 161      }
 162  }
 163  
 164  /**
 165   * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
 166   *
 167   * @deprecated since Moodle 3.2 MDL-51789 - please use core_user::update_picture() instead.
 168   * @todo MDL-54858 This will be deleted in Moodle 3.6.
 169   * @see core_user::update_picture()
 170   *
 171   * @global moodle_database $DB
 172   * @param stdClass $usernew An object that contains some information about the user being updated
 173   * @param moodleform $userform The form that was submitted to edit the form (unused)
 174   * @param array $filemanageroptions
 175   * @return bool True if the user was updated, false if it stayed the same.
 176   */
 177  function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
 178      debugging('useredit_update_picture() is deprecated. Please use core_user::update_picture() instead.', DEBUG_DEVELOPER);
 179      return core_user::update_picture($usernew, $filemanageroptions);
 180  }
 181  
 182  /**
 183   * Updates the user email bounce + send counts when the user is edited.
 184   *
 185   * @param stdClass $user The current user object.
 186   * @param stdClass $usernew The updated user object.
 187   */
 188  function useredit_update_bounces($user, $usernew) {
 189      if (!isset($usernew->email)) {
 190          // Locked field.
 191          return;
 192      }
 193      if (!isset($user->email) || $user->email !== $usernew->email) {
 194          set_bounce_count($usernew, true);
 195          set_send_count($usernew, true);
 196      }
 197  }
 198  
 199  /**
 200   * Updates the forums a user is tracking when the user is edited.
 201   *
 202   * @param stdClass $user The original user object.
 203   * @param stdClass $usernew The updated user object.
 204   */
 205  function useredit_update_trackforums($user, $usernew) {
 206      global $CFG;
 207      if (!isset($usernew->trackforums)) {
 208          // Locked field.
 209          return;
 210      }
 211      if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
 212          require_once($CFG->dirroot.'/mod/forum/lib.php');
 213          forum_tp_delete_read_records($usernew->id);
 214      }
 215  }
 216  
 217  /**
 218   * Updates a users interests.
 219   *
 220   * @param stdClass $user
 221   * @param array $interests
 222   */
 223  function useredit_update_interests($user, $interests) {
 224      core_tag_tag::set_item_tags('core', 'user', $user->id,
 225              context_user::instance($user->id), $interests);
 226  }
 227  
 228  /**
 229   * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc.
 230   *
 231   * @param moodleform $mform
 232   * @param array $editoroptions
 233   * @param array $filemanageroptions
 234   * @param stdClass $user
 235   */
 236  function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions, $user) {
 237      global $CFG, $USER, $DB;
 238  
 239      if ($user->id > 0) {
 240          useredit_load_preferences($user, false);
 241      }
 242  
 243      $strrequired = get_string('required');
 244      $stringman = get_string_manager();
 245  
 246      // Add the necessary names.
 247      foreach (useredit_get_required_name_fields() as $fullname) {
 248          $mform->addElement('text', $fullname,  get_string($fullname),  'maxlength="100" size="30"');
 249          if ($stringman->string_exists('missing'.$fullname, 'core')) {
 250              $strmissingfield = get_string('missing'.$fullname, 'core');
 251          } else {
 252              $strmissingfield = $strrequired;
 253          }
 254          $mform->addRule($fullname, $strmissingfield, 'required', null, 'client');
 255          $mform->setType($fullname, PARAM_NOTAGS);
 256      }
 257  
 258      $enabledusernamefields = useredit_get_enabled_name_fields();
 259      // Add the enabled additional name fields.
 260      foreach ($enabledusernamefields as $addname) {
 261          $mform->addElement('text', $addname,  get_string($addname), 'maxlength="100" size="30"');
 262          $mform->setType($addname, PARAM_NOTAGS);
 263      }
 264  
 265      // Do not show email field if change confirmation is pending.
 266      if ($user->id > 0 and !empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
 267          $notice = get_string('emailchangepending', 'auth', $user);
 268          $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
 269                  . get_string('emailchangecancel', 'auth') . '</a>';
 270          $mform->addElement('static', 'emailpending', get_string('email'), $notice);
 271      } else {
 272          $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
 273          $mform->addRule('email', $strrequired, 'required', null, 'client');
 274          $mform->setType('email', PARAM_RAW_TRIMMED);
 275      }
 276  
 277      $choices = array();
 278      $choices['0'] = get_string('emaildisplayno');
 279      $choices['1'] = get_string('emaildisplayyes');
 280      $choices['2'] = get_string('emaildisplaycourse');
 281      $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
 282      $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
 283  
 284      $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
 285      $mform->setType('city', PARAM_TEXT);
 286      if (!empty($CFG->defaultcity)) {
 287          $mform->setDefault('city', $CFG->defaultcity);
 288      }
 289  
 290      $choices = get_string_manager()->get_list_of_countries();
 291      $choices = array('' => get_string('selectacountry') . '...') + $choices;
 292      $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
 293      if (!empty($CFG->country)) {
 294          $mform->setDefault('country', core_user::get_property_default('country'));
 295      }
 296  
 297      if (isset($CFG->forcetimezone) and $CFG->forcetimezone != 99) {
 298          $choices = core_date::get_list_of_timezones($CFG->forcetimezone);
 299          $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
 300          $mform->addElement('hidden', 'timezone');
 301          $mform->setType('timezone', core_user::get_property_type('timezone'));
 302      } else {
 303          $choices = core_date::get_list_of_timezones($user->timezone, true);
 304          $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
 305      }
 306  
 307      // Multi-Calendar Support - see MDL-18375.
 308      $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
 309      // We do not want to show this option unless there is more than one calendar type to display.
 310      if (count($calendartypes) > 1) {
 311          $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
 312          $mform->setDefault('calendartype', $CFG->calendartype);
 313      }
 314  
 315      if (!empty($CFG->allowuserthemes)) {
 316          $choices = array();
 317          $choices[''] = get_string('default');
 318          $themes = get_list_of_themes();
 319          foreach ($themes as $key => $theme) {
 320              if (empty($theme->hidefromselector)) {
 321                  $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
 322              }
 323          }
 324          $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
 325      }
 326  
 327      $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
 328      $mform->setType('description_editor', PARAM_CLEANHTML);
 329      $mform->addHelpButton('description_editor', 'userdescription');
 330  
 331      if (empty($USER->newadminuser)) {
 332          $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
 333          $mform->setExpanded('moodle_picture', true);
 334  
 335          if (!empty($CFG->enablegravatar)) {
 336              $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
 337          }
 338  
 339          $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
 340  
 341          $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
 342          $mform->setDefault('deletepicture', 0);
 343  
 344          $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
 345          $mform->addHelpButton('imagefile', 'newpicture');
 346  
 347          $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
 348          $mform->setType('imagealt', PARAM_TEXT);
 349  
 350      }
 351  
 352      // Display user name fields that are not currenlty enabled here if there are any.
 353      $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
 354      if (count($disabledusernamefields) > 0) {
 355          $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
 356          foreach ($disabledusernamefields as $allname) {
 357              $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
 358              $mform->setType($allname, PARAM_NOTAGS);
 359          }
 360      }
 361  
 362      if (core_tag_tag::is_enabled('core', 'user') and empty($USER->newadminuser)) {
 363          $mform->addElement('header', 'moodle_interests', get_string('interests'));
 364          $mform->addElement('tags', 'interests', get_string('interestslist'),
 365              array('itemtype' => 'user', 'component' => 'core'));
 366          $mform->addHelpButton('interests', 'interestslist');
 367      }
 368  
 369      // Moodle optional fields.
 370      $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
 371  
 372      $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
 373      $mform->setType('url', core_user::get_property_type('url'));
 374  
 375      $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
 376      $mform->setType('icq', core_user::get_property_type('icq'));
 377  
 378      $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
 379      $mform->setType('skype', core_user::get_property_type('skype'));
 380  
 381      $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
 382      $mform->setType('aim', core_user::get_property_type('aim'));
 383  
 384      $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
 385      $mform->setType('yahoo', core_user::get_property_type('yahoo'));
 386  
 387      $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
 388      $mform->setType('msn', core_user::get_property_type('msn'));
 389  
 390      $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
 391      $mform->setType('idnumber', core_user::get_property_type('idnumber'));
 392  
 393      $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
 394      $mform->setType('institution', core_user::get_property_type('institution'));
 395  
 396      $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
 397      $mform->setType('department', core_user::get_property_type('department'));
 398  
 399      $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
 400      $mform->setType('phone1', core_user::get_property_type('phone1'));
 401  
 402      $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
 403      $mform->setType('phone2', core_user::get_property_type('phone2'));
 404  
 405      $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
 406      $mform->setType('address', core_user::get_property_type('address'));
 407  }
 408  
 409  /**
 410   * Return required user name fields for forms.
 411   *
 412   * @return array required user name fields in order according to settings.
 413   */
 414  function useredit_get_required_name_fields() {
 415      global $CFG;
 416  
 417      // Get the name display format.
 418      $nameformat = $CFG->fullnamedisplay;
 419  
 420      // Names that are required fields on user forms.
 421      $necessarynames = array('firstname', 'lastname');
 422      $languageformat = get_string('fullnamedisplay');
 423  
 424      // Check that the language string and the $nameformat contain the necessary names.
 425      foreach ($necessarynames as $necessaryname) {
 426          $pattern = "/$necessaryname\b/";
 427          if (!preg_match($pattern, $languageformat)) {
 428              // If the language string has been altered then fall back on the below order.
 429              $languageformat = 'firstname lastname';
 430          }
 431          if (!preg_match($pattern, $nameformat)) {
 432              // If the nameformat doesn't contain the necessary name fields then use the languageformat.
 433              $nameformat = $languageformat;
 434          }
 435      }
 436  
 437      // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
 438      $necessarynames = order_in_string($necessarynames, $nameformat);
 439      return $necessarynames;
 440  }
 441  
 442  /**
 443   * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
 444   *
 445   * @return array Enabled user name fields.
 446   */
 447  function useredit_get_enabled_name_fields() {
 448      global $CFG;
 449  
 450      // Get all of the other name fields which are not ranked as necessary.
 451      $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
 452      // Find out which additional name fields are actually being used from the fullnamedisplay setting.
 453      $enabledadditionalusernames = array();
 454      foreach ($additionalusernamefields as $enabledname) {
 455          if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
 456              $enabledadditionalusernames[] = $enabledname;
 457          }
 458      }
 459  
 460      // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
 461      $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay);
 462      return $enabledadditionalusernames;
 463  }
 464  
 465  /**
 466   * Gets user name fields not enabled from the setting fullnamedisplay.
 467   *
 468   * @param array $enabledadditionalusernames Current enabled additional user name fields.
 469   * @return array Disabled user name fields.
 470   */
 471  function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
 472      // If we don't have enabled additional user name information then go and fetch it (try to avoid).
 473      if (!isset($enabledadditionalusernames)) {
 474          $enabledadditionalusernames = useredit_get_enabled_name_fields();
 475      }
 476  
 477      // These are the additional fields that are not currently enabled.
 478      $nonusednamefields = array_diff(get_all_user_name_fields(),
 479              array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
 480      return $nonusednamefields;
 481  }


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