Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • /user/ -> editlib.php (source)

    Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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   * Loads the given users preferences into the given user object.
      38   *
      39   * @param stdClass $user The user object, modified by reference.
      40   * @param bool $reload
      41   */
      42  function useredit_load_preferences(&$user, $reload=true) {
      43      global $USER;
      44  
      45      if (!empty($user->id)) {
      46          if ($reload and $USER->id == $user->id) {
      47              // Reload preferences in case it was changed in other session.
      48              unset($USER->preference);
      49          }
      50  
      51          if ($preferences = get_user_preferences(null, null, $user->id)) {
      52              foreach ($preferences as $name => $value) {
      53                  $user->{'preference_'.$name} = $value;
      54              }
      55          }
      56      }
      57  }
      58  
      59  /**
      60   * Updates the user preferences for teh given user.
      61   *
      62   * @param stdClass|array $usernew
      63   */
      64  function useredit_update_user_preference($usernew) {
      65      $ua = (array)$usernew;
      66      foreach ($ua as $key => $value) {
      67          if (strpos($key, 'preference_') === 0) {
      68              $name = substr($key, strlen('preference_'));
      69              set_user_preference($name, $value, $usernew->id);
      70          }
      71      }
      72  }
      73  
      74  /**
      75   * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
      76   *
      77   * @global moodle_database $DB
      78   * @param stdClass $usernew An object that contains some information about the user being updated
      79   * @param moodleform $userform The form that was submitted to edit the form
      80   * @param array $filemanageroptions
      81   * @return bool True if the user was updated, false if it stayed the same.
      82   */
      83  function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
      84      global $CFG, $DB;
      85      require_once("$CFG->libdir/gdlib.php");
      86  
      87      $context = context_user::instance($usernew->id, MUST_EXIST);
      88      $user = $DB->get_record('user', array('id' => $usernew->id), 'id, picture', MUST_EXIST);
      89  
      90      $newpicture = $user->picture;
      91      // Get file_storage to process files.
      92      $fs = get_file_storage();
      93      if (!empty($usernew->deletepicture)) {
      94          // The user has chosen to delete the selected users picture.
      95          $fs->delete_area_files($context->id, 'user', 'icon'); // Drop all images in area.
      96          $newpicture = 0;
      97  
      98      } else {
      99          // Save newly uploaded file, this will avoid context mismatch for newly created users.
     100          file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
     101          if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
     102              // Get file which was uploaded in draft area.
     103              foreach ($iconfiles as $file) {
     104                  if (!$file->is_directory()) {
     105                      break;
     106                  }
     107              }
     108              // Copy file to temporary location and the send it for processing icon.
     109              if ($iconfile = $file->copy_content_to_temp()) {
     110                  // There is a new image that has been uploaded.
     111                  // Process the new image and set the user to make use of it.
     112                  // NOTE: Uploaded images always take over Gravatar.
     113                  $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
     114                  // Delete temporary file.
     115                  @unlink($iconfile);
     116                  // Remove uploaded file.
     117                  $fs->delete_area_files($context->id, 'user', 'newicon');
     118              } else {
     119                  // Something went wrong while creating temp file.
     120                  // Remove uploaded file.
     121                  $fs->delete_area_files($context->id, 'user', 'newicon');
     122                  return false;
     123              }
     124          }
     125      }
     126  
     127      if ($newpicture != $user->picture) {
     128          $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
     129          return true;
     130      } else {
     131          return false;
     132      }
     133  }
     134  
     135  /**
     136   * Updates the user email bounce + send counts when the user is edited.
     137   *
     138   * @param stdClass $user The current user object.
     139   * @param stdClass $usernew The updated user object.
     140   */
     141  function useredit_update_bounces($user, $usernew) {
     142      if (!isset($usernew->email)) {
     143          // Locked field.
     144          return;
     145      }
     146      if (!isset($user->email) || $user->email !== $usernew->email) {
     147          set_bounce_count($usernew, true);
     148          set_send_count($usernew, true);
     149      }
     150  }
     151  
     152  /**
     153   * Updates the forums a user is tracking when the user is edited.
     154   *
     155   * @param stdClass $user The original user object.
     156   * @param stdClass $usernew The updated user object.
     157   */
     158  function useredit_update_trackforums($user, $usernew) {
     159      global $CFG;
     160      if (!isset($usernew->trackforums)) {
     161          // Locked field.
     162          return;
     163      }
     164      if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
     165          require_once($CFG->dirroot.'/mod/forum/lib.php');
     166          forum_tp_delete_read_records($usernew->id);
     167      }
     168  }
     169  
     170  /**
     171   * Updates a users interests.
     172   *
     173   * @param stdClass $user
     174   * @param array $interests
     175   */
     176  function useredit_update_interests($user, $interests) {
     177      tag_set('user', $user->id, $interests, 'core', context_user::instance($user->id)->id);
     178  }
     179  
     180  /**
     181   * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc.
     182   *
     183   * @param moodleform $mform
     184   * @param array|null $editoroptions
     185   * @param array|null $filemanageroptions
     186   */
     187  function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
     188      global $CFG, $USER, $DB;
     189  
     190      $user = $DB->get_record('user', array('id' => $USER->id));
     191      useredit_load_preferences($user, false);
     192  
     193      $strrequired = get_string('required');
     194  
     195      // Add the necessary names.
     196      foreach (useredit_get_required_name_fields() as $fullname) {
     197          $mform->addElement('text', $fullname,  get_string($fullname),  'maxlength="100" size="30"');
     198          $mform->addRule($fullname, $strrequired, 'required', null, 'client');
     199          $mform->setType($fullname, PARAM_NOTAGS);
     200      }
     201  
     202      $enabledusernamefields = useredit_get_enabled_name_fields();
     203      // Add the enabled additional name fields.
     204      foreach ($enabledusernamefields as $addname) {
     205          $mform->addElement('text', $addname,  get_string($addname), 'maxlength="100" size="30"');
     206          $mform->setType($addname, PARAM_NOTAGS);
     207      }
     208  
     209      // Do not show email field if change confirmation is pending.
     210      if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
     211          $notice = get_string('emailchangepending', 'auth', $user);
     212          $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
     213                  . get_string('emailchangecancel', 'auth') . '</a>';
     214          $mform->addElement('static', 'emailpending', get_string('email'), $notice);
     215      } else {
     216          $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
     217          $mform->addRule('email', $strrequired, 'required', null, 'client');
     218          $mform->setType('email', PARAM_RAW_TRIMMED);
     219      }
     220  
     221      $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
     222      $mform->setType('city', PARAM_TEXT);
     223      if (!empty($CFG->defaultcity)) {
     224          $mform->setDefault('city', $CFG->defaultcity);
     225      }
     226  
     227      $choices = get_string_manager()->get_list_of_countries();
     228      $choices = array('' => get_string('selectacountry') . '...') + $choices;
     229      $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
     230      if (!empty($CFG->country)) {
     231          $mform->setDefault('country', $CFG->country);
     232      }
     233  
     234      $choices = get_list_of_timezones();
     235      $choices['99'] = get_string('serverlocaltime');
     236      if ($CFG->forcetimezone != 99) {
     237          $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
     238      } else {
     239          $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
     240          $mform->setDefault('timezone', '99');
     241      }
     242  
     243      // Multi-Calendar Support - see MDL-18375.
     244      $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
     245      // We do not want to show this option unless there is more than one calendar type to display.
     246      if (count($calendartypes) > 1) {
     247          $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
     248          $mform->setDefault('calendartype', $CFG->calendartype);
     249      }
     250  
     251      if (!empty($CFG->allowuserthemes)) {
     252          $choices = array();
     253          $choices[''] = get_string('default');
     254          $themes = get_list_of_themes();
     255          foreach ($themes as $key => $theme) {
     256              if (empty($theme->hidefromselector)) {
     257                  $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
     258              }
     259          }
     260          $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
     261      }
     262  
     263      $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
     264      $mform->setType('description_editor', PARAM_CLEANHTML);
     265      $mform->addHelpButton('description_editor', 'userdescription');
     266  
     267      $mform->addElement('header', 'moodle_userpreferences', get_string('preferences'));
     268      useredit_shared_definition_preferences($user, $mform, $editoroptions, $filemanageroptions);
     269  
     270      if (empty($USER->newadminuser)) {
     271          $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
     272          $mform->setExpanded('moodle_picture', true);
     273  
     274          if (!empty($CFG->enablegravatar)) {
     275              $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
     276          }
     277  
     278          $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
     279  
     280          $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
     281          $mform->setDefault('deletepicture', 0);
     282  
     283          $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
     284          $mform->addHelpButton('imagefile', 'newpicture');
     285  
     286          $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
     287          $mform->setType('imagealt', PARAM_TEXT);
     288  
     289      }
     290  
     291      // Display user name fields that are not currenlty enabled here if there are any.
     292      $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
     293      if (count($disabledusernamefields) > 0) {
     294          $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
     295          foreach ($disabledusernamefields as $allname) {
     296              $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
     297              $mform->setType($allname, PARAM_NOTAGS);
     298          }
     299      }
     300  
     301      if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
     302          $mform->addElement('header', 'moodle_interests', get_string('interests'));
     303          $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
     304          $mform->addHelpButton('interests', 'interestslist');
     305      }
     306  
     307      // Moodle optional fields.
     308      $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
     309  
     310      $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
     311      $mform->setType('url', PARAM_URL);
     312  
     313      $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
     314      $mform->setType('icq', PARAM_NOTAGS);
     315  
     316      $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
     317      $mform->setType('skype', PARAM_NOTAGS);
     318  
     319      $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
     320      $mform->setType('aim', PARAM_NOTAGS);
     321  
     322      $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
     323      $mform->setType('yahoo', PARAM_NOTAGS);
     324  
     325      $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
     326      $mform->setType('msn', PARAM_NOTAGS);
     327  
     328      $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
     329      $mform->setType('idnumber', PARAM_NOTAGS);
     330  
     331      $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
     332      $mform->setType('institution', PARAM_TEXT);
     333  
     334      $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
     335      $mform->setType('department', PARAM_TEXT);
     336  
     337      $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
     338      $mform->setType('phone1', PARAM_NOTAGS);
     339  
     340      $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
     341      $mform->setType('phone2', PARAM_NOTAGS);
     342  
     343      $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
     344      $mform->setType('address', PARAM_TEXT);
     345  }
     346  
     347  /**
     348   * Adds user preferences elements to user edit form.
     349   *
     350   * @param stdClass $user
     351   * @param moodleform $mform
     352   * @param array|null $editoroptions
     353   * @param array|null $filemanageroptions
     354   */
     355  function useredit_shared_definition_preferences($user, &$mform, $editoroptions = null, $filemanageroptions = null) {
     356      global $CFG;
     357  
     358      $choices = array();
     359      $choices['0'] = get_string('emaildisplayno');
     360      $choices['1'] = get_string('emaildisplayyes');
     361      $choices['2'] = get_string('emaildisplaycourse');
     362      $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
     363      $mform->setDefault('maildisplay', $CFG->defaultpreference_maildisplay);
     364  
     365      $choices = array();
     366      $choices['0'] = get_string('textformat');
     367      $choices['1'] = get_string('htmlformat');
     368      $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
     369      $mform->setDefault('mailformat', $CFG->defaultpreference_mailformat);
     370  
     371      if (!empty($CFG->allowusermailcharset)) {
     372          $choices = array();
     373          $charsets = get_list_of_charsets();
     374          if (!empty($CFG->sitemailcharset)) {
     375              $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
     376          } else {
     377              $choices['0'] = get_string('site').' (UTF-8)';
     378          }
     379          $choices = array_merge($choices, $charsets);
     380          $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
     381      }
     382  
     383      $choices = array();
     384      $choices['0'] = get_string('emaildigestoff');
     385      $choices['1'] = get_string('emaildigestcomplete');
     386      $choices['2'] = get_string('emaildigestsubjects');
     387      $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
     388      $mform->setDefault('maildigest', $CFG->defaultpreference_maildigest);
     389      $mform->addHelpButton('maildigest', 'emaildigest');
     390  
     391      $choices = array();
     392      $choices['1'] = get_string('autosubscribeyes');
     393      $choices['0'] = get_string('autosubscribeno');
     394      $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
     395      $mform->setDefault('autosubscribe', $CFG->defaultpreference_autosubscribe);
     396  
     397      if (!empty($CFG->forum_trackreadposts)) {
     398          $choices = array();
     399          $choices['0'] = get_string('trackforumsno');
     400          $choices['1'] = get_string('trackforumsyes');
     401          $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
     402          $mform->setDefault('trackforums', $CFG->defaultpreference_trackforums);
     403      }
     404  
     405      $editors = editors_get_enabled();
     406      if (count($editors) > 1) {
     407          $choices = array('' => get_string('defaulteditor'));
     408          $firsteditor = '';
     409          foreach (array_keys($editors) as $editor) {
     410              if (!$firsteditor) {
     411                  $firsteditor = $editor;
     412              }
     413              $choices[$editor] = get_string('pluginname', 'editor_' . $editor);
     414          }
     415          $mform->addElement('select', 'preference_htmleditor', get_string('textediting'), $choices);
     416          $mform->setDefault('preference_htmleditor', '');
     417      } else {
     418          // Empty string means use the first chosen text editor.
     419          $mform->addElement('hidden', 'preference_htmleditor');
     420          $mform->setDefault('preference_htmleditor', '');
     421          $mform->setType('preference_htmleditor', PARAM_PLUGIN);
     422      }
     423  
     424      $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
     425      $mform->setDefault('lang', $CFG->lang);
     426  
     427  }
     428  
     429  /**
     430   * Return required user name fields for forms.
     431   *
     432   * @return array required user name fields in order according to settings.
     433   */
     434  function useredit_get_required_name_fields() {
     435      global $CFG;
     436  
     437      // Get the name display format.
     438      $nameformat = $CFG->fullnamedisplay;
     439  
     440      // Names that are required fields on user forms.
     441      $necessarynames = array('firstname', 'lastname');
     442      $languageformat = get_string('fullnamedisplay');
     443  
     444      // Check that the language string and the $nameformat contain the necessary names.
     445      foreach ($necessarynames as $necessaryname) {
     446          $pattern = "/$necessaryname\b/";
     447          if (!preg_match($pattern, $languageformat)) {
     448              // If the language string has been altered then fall back on the below order.
     449              $languageformat = 'firstname lastname';
     450          }
     451          if (!preg_match($pattern, $nameformat)) {
     452              // If the nameformat doesn't contain the necessary name fields then use the languageformat.
     453              $nameformat = $languageformat;
     454          }
     455      }
     456  
     457      // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
     458      $necessarynames = order_in_string($necessarynames, $nameformat);
     459      return $necessarynames;
     460  }
     461  
     462  /**
     463   * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
     464   *
     465   * @return array Enabled user name fields.
     466   */
     467  function useredit_get_enabled_name_fields() {
     468      global $CFG;
     469  
     470      // Get all of the other name fields which are not ranked as necessary.
     471      $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
     472      // Find out which additional name fields are actually being used from the fullnamedisplay setting.
     473      $enabledadditionalusernames = array();
     474      foreach ($additionalusernamefields as $enabledname) {
     475          if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
     476              $enabledadditionalusernames[] = $enabledname;
     477          }
     478      }
     479  
     480      // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
     481      $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay);
     482      return $enabledadditionalusernames;
     483  }
     484  
     485  /**
     486   * Gets user name fields not enabled from the setting fullnamedisplay.
     487   *
     488   * @param array $enabledadditionalusernames Current enabled additional user name fields.
     489   * @return array Disabled user name fields.
     490   */
     491  function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
     492      // If we don't have enabled additional user name information then go and fetch it (try to avoid).
     493      if (!isset($enabledadditionalusernames)) {
     494          $enabledadditionalusernames = useredit_get_enabled_name_fields();
     495      }
     496  
     497      // These are the additional fields that are not currently enabled.
     498      $nonusednamefields = array_diff(get_all_user_name_fields(),
     499              array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
     500      return $nonusednamefields;
     501  }
    

    Search This Site: