/** * Updates the provided users profile picture based upon the expected fields * returned from the edit or edit_advanced forms. * * @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 * @return bool True if the user was updated, false if it stayed the same. */ function useredit_update_picture(stdClass $usernew, moodleform $userform) { global $CFG, $DB; require_once "{$CFG->libdir}/gdlib.php"; $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST); $user = $DB->get_record('user', array('id' => $usernew->id), 'id, picture', MUST_EXIST); $newpicture = $user->picture; if (!empty($usernew->deletepicture)) { // The user has chosen to delete the selected users picture $fs = get_file_storage(); $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area $newpicture = 0; } else { if ($iconfile = $userform->save_temp_file('imagefile')) { // There is a new image that has been uploaded // Process the new image and set the user to make use of it. // NOTE: Uploaded images always take over Gravatar $newpicture = (int) process_new_icon($context, 'user', 'icon', 0, $iconfile); // Delete the file that has now been processed @unlink($iconfile); } } if ($newpicture != $user->picture) { $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id)); return true; } else { return false; } }
/** * Updates the provided users profile picture based upon the expected fields * returned from the edit or edit_advanced forms. * * @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 * @return bool True if the user was updated, false if it stayed the same. */ function useredit_update_picture(stdClass $usernew, moodleform $userform) { global $CFG, $DB; require_once("$CFG->libdir/gdlib.php"); $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST); // This will hold the value to set to the user's picture field at the end of // this function $picturetouse = null; if (!empty($usernew->deletepicture)) { // The user has chosen to delete the selected users picture $fs = get_file_storage(); $fs->delete_area_files($context->id, 'user', 'icon'); // drop all areas $picturetouse = 0; } else if ($iconfile = $userform->save_temp_file('imagefile')) { // There is a new image that has been uploaded // Process the new image and set the user to make use of it. // NOTE: This may be overridden by Gravatar if (process_new_icon($context, 'user', 'icon', 0, $iconfile)) { $picturetouse = 1; } // Delete the file that has now been processed @unlink($iconfile); } // If we have a picture to set we can now do so. Note this will still be NULL // unless the user has changed their picture or caused a change by selecting // to delete their picture or use gravatar if (!is_null($picturetouse)) { $DB->set_field('user', 'picture', $picturetouse, array('id' => $usernew->id)); return true; } return false; }