Example #1
0
/**
 * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
 *
 * @deprecated since Moodle 3.2 MDL-51789 - please use core_user::update_picture() instead.
 * @todo MDL-54858 This will be deleted in Moodle 3.6.
 * @see core_user::update_picture()
 *
 * @global moodle_database $DB
 * @param stdClass $usernew An object that contains some information about the user being updated
 * @param moodleform $userform The form that was submitted to edit the form (unused)
 * @param array $filemanageroptions
 * @return bool True if the user was updated, false if it stayed the same.
 */
function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array())
{
    debugging('useredit_update_picture() is deprecated. Please use core_user::update_picture() instead.', DEBUG_DEVELOPER);
    return core_user::update_picture($usernew, $filemanageroptions);
}
Example #2
0
 /**
  * Update or delete the user picture in the site
  *
  * @param  int  $draftitemid id of the user draft file to use as image
  * @param  bool $delete      if we should delete the user picture
  * @param  int $userid       id of the user, 0 for current user
  * @return array warnings and success status
  * @since Moodle 3.2
  * @throws moodle_exception
  */
 public static function update_picture($draftitemid, $delete = false, $userid = 0)
 {
     global $CFG, $USER, $PAGE;
     $params = self::validate_parameters(self::update_picture_parameters(), array('draftitemid' => $draftitemid, 'delete' => $delete, 'userid' => $userid));
     $context = context_system::instance();
     self::validate_context($context);
     if (!empty($CFG->disableuserimages)) {
         throw new moodle_exception('userimagesdisabled', 'admin');
     }
     if (empty($params['userid']) or $params['userid'] == $USER->id) {
         $user = $USER;
         require_capability('moodle/user:editownprofile', $context);
     } else {
         $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
         core_user::require_active_user($user);
         $personalcontext = context_user::instance($user->id);
         require_capability('moodle/user:editprofile', $personalcontext);
         if (is_siteadmin($user) and !is_siteadmin($USER)) {
             // Only admins may edit other admins.
             throw new moodle_exception('useradmineditadmin');
         }
     }
     // Load the appropriate auth plugin.
     $userauth = get_auth_plugin($user->auth);
     if (is_mnet_remote_user($user) or !$userauth->can_edit_profile() or $userauth->edit_profile_url()) {
         throw new moodle_exception('noprofileedit', 'auth');
     }
     $filemanageroptions = array('maxbytes' => $CFG->maxbytes, 'subdirs' => 0, 'maxfiles' => 1, 'accepted_types' => 'web_image');
     $user->deletepicture = $params['delete'];
     $user->imagefile = $params['draftitemid'];
     $success = core_user::update_picture($user, $filemanageroptions);
     $result = array('success' => $success, 'warnings' => array());
     if ($success) {
         $userpicture = new user_picture(core_user::get_user($user->id));
         $userpicture->size = 1;
         // Size f1.
         $result['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
     }
     return $result;
 }
Example #3
0
 // Pass a true old $user here.
 if (!$authplugin->user_update($user, $usernew)) {
     // Auth update failed.
     print_error('cannotupdateprofile');
 }
 // Update user with new profile data.
 user_update_user($usernew, false, false);
 // Update preferences.
 useredit_update_user_preference($usernew);
 // Update interests.
 if (isset($usernew->interests)) {
     useredit_update_interests($usernew, $usernew->interests);
 }
 // Update user picture.
 if (empty($CFG->disableuserimages)) {
     core_user::update_picture($usernew, $filemanageroptions);
 }
 // Update mail bounces.
 useredit_update_bounces($user, $usernew);
 // Update forum track preference.
 useredit_update_trackforums($user, $usernew);
 // Save custom profile fields data.
 profile_save_data($usernew);
 // Trigger event.
 \core\event\user_updated::create_from_userid($user->id)->trigger();
 // If email was changed and confirmation is required, send confirmation email now to the new address.
 if ($emailchanged && $CFG->emailchangeconfirmation) {
     $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
     $tempuser->email = $usernew->preference_newemail;
     $a = new stdClass();
     $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
Example #4
0
 /**
  * Update users
  *
  * @param array $users
  * @return null
  * @since Moodle 2.2
  */
 public static function update_users($users)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . "/user/lib.php";
     require_once $CFG->dirroot . "/user/profile/lib.php";
     // Required for customfields related function.
     // Ensure the current user is allowed to run this function.
     $context = context_system::instance();
     require_capability('moodle/user:update', $context);
     self::validate_context($context);
     $params = self::validate_parameters(self::update_users_parameters(), array('users' => $users));
     $filemanageroptions = array('maxbytes' => $CFG->maxbytes, 'subdirs' => 0, 'maxfiles' => 1, 'accepted_types' => 'web_image');
     $transaction = $DB->start_delegated_transaction();
     foreach ($params['users'] as $user) {
         user_update_user($user, true, false);
         // Update user picture if it was specified for this user.
         if (empty($CFG->disableuserimages) && isset($user['userpicture'])) {
             $userobject = (object) $user;
             $userobject->deletepicture = null;
             if ($user['userpicture'] == 0) {
                 $userobject->deletepicture = true;
             } else {
                 $userobject->imagefile = $user['userpicture'];
             }
             core_user::update_picture($userobject, $filemanageroptions);
         }
         // Update user custom fields.
         if (!empty($user['customfields'])) {
             foreach ($user['customfields'] as $customfield) {
                 // Profile_save_data() saves profile file it's expecting a user with the correct id,
                 // and custom field to be named profile_field_"shortname".
                 $user["profile_field_" . $customfield['type']] = $customfield['value'];
             }
             profile_save_data((object) $user);
         }
         // Trigger event.
         \core\event\user_updated::create_from_userid($user['id'])->trigger();
         // Preferences.
         if (!empty($user['preferences'])) {
             foreach ($user['preferences'] as $preference) {
                 set_user_preference($preference['type'], $preference['value'], $user['id']);
             }
         }
     }
     $transaction->allow_commit();
     return null;
 }