diff --git a/admin/settings/users.php b/admin/settings/users.php index 8534c4565ab..cf782b2980c 100644 --- a/admin/settings/users.php +++ b/admin/settings/users.php @@ -126,6 +126,7 @@ if ($hassiteconfig $temp->add(new admin_setting_configmulticheckbox('extrauserselectorfields', get_string('extrauserselectorfields', 'admin'), get_string('configextrauserselectorfields', 'admin'), array('email' => '1'), array('email' => get_string('email'), 'idnumber' => get_string('idnumber'), 'username' => get_string('username'), ))); + $temp->add(new admin_setting_configcheckbox('enablegravatar', get_string('enablegravatar', 'admin'), get_string('enablegravatar_help', 'admin'), 0)); } $ADMIN->add('roles', $temp); diff --git a/lang/en/admin.php b/lang/en/admin.php index d5c1e20d0d5..ea523e044e6 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -469,6 +469,8 @@ $string['enablecourseajax_desc'] = 'Allow AJAX when editing main course pages. N $string['enablecourserequests'] = 'Enable course requests'; $string['enabledevicedetection'] = 'Enable device detection'; $string['enableglobalsearch'] = 'Enable global search'; +$string['enablegravatar'] = 'Enable Gravatar'; +$string['enablegravatar_help'] = 'When enabled Moodle will attempt to fetch a user profile picture from Gravatar if the user has not uploaded an image.'; $string['enablegroupmembersonly'] = 'Enable group members only'; $string['enablehtmlpurifier'] = 'Enable HTML Purifier'; $string['enablemobilewebservice'] = 'Enable mobile web service'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 4615a99a2ed..a51008a8807 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -749,6 +749,7 @@ See the Moodle License information page for full details: http://docs.moodle.org/dev/License'; $string['grade'] = 'Grade'; $string['grades'] = 'Grades'; +$string['gravatarenabled'] = 'Gravatar has been enabled for this site. If you don\'t upload a profile picture Moodle will attempt to load a profile picture for you from Gravatar.'; $string['group'] = 'Group'; $string['groupadd'] = 'Add new group'; $string['groupaddusers'] = 'Add selected to group'; diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index f822ef9c150..3a1dc0794ba 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -270,7 +270,7 @@ class user_picture implements renderable { * @return moodle_url */ public function get_url(moodle_page $page, renderer_base $renderer = null) { - global $CFG; + global $CFG, $FULLME; if (is_null($renderer)) { $renderer = $page->get_renderer('core'); @@ -298,16 +298,14 @@ class user_picture implements renderable { $size = (int)$this->size; } - if ($this->user->picture == 1) { - // The user has uploaded their own profile pic. In this case we will - // check that a profile pic file does actually exist - $fs = get_file_storage(); - $context = get_context_instance(CONTEXT_USER, $this->user->id); - if (!$fs->file_exists($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) { - if (!$fs->file_exists($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) { - return $renderer->pix_url('u/'.$filename); - } - } + // First we need to determine whether the user has uploaded a profile + // picture of not. + $fs = get_file_storage(); + $context = get_context_instance(CONTEXT_USER, $this->user->id); + $hasuploadedfile = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', $filename.'/.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')); + + $imageurl = $renderer->pix_url('u/'.$filename); + if ($hasuploadedfile && $this->user->picture == 1) { $path = '/'; if (clean_param($page->theme->name, PARAM_THEME) == $page->theme->name) { // We append the theme name to the file path if we have it so that @@ -316,20 +314,27 @@ class user_picture implements renderable { // picture for the correct theme. $path .= $page->theme->name.'/'; } - return moodle_url::make_pluginfile_url($context->id, 'user', 'icon', NULL, $path, $filename); - - } else if ($this->user->picture == 2) { - // This is just VERY basic support for gravatar to give the actual - // implementor a headstart in what to do. - if ($size < 1 || $size > 500) { + // Set the image URL to the URL for the uploaded file. + $imageurl = moodle_url::make_pluginfile_url($context->id, 'user', 'icon', NULL, $path, $filename); + } else if (!empty($CFG->enablegravatar)) { + // Normalise the size variable to acceptable bounds + if ($size < 1 || $size > 512) { $size = 35; } + // Hash the users email address $md5 = md5(strtolower(trim($this->user->email))); - $default = urlencode($this->pix_url('u/'.$filename)->out(false)); - return "http://www.gravatar.com/avatar/{$md5}?s={$size}&d={$default}"; + // Build a gravatar URL with what we know. + // If the currently requested page is https then we'll return an + // https gravatar page. + if (strpos($FULLME, 'https://') === 0) { + $imageurl = new moodle_url("https://secure.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $imageurl->out(false))); + } else { + $imageurl = new moodle_url("http://www.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $imageurl->out(false))); + } } - return $renderer->pix_url('u/'.$filename); + // Return the URL that has been generated. + return $imageurl; } } diff --git a/user/edit_form.php b/user/edit_form.php index d22784857a1..e0290896307 100644 --- a/user/edit_form.php +++ b/user/edit_form.php @@ -73,11 +73,19 @@ class user_edit_form extends moodleform { // print picture if (!empty($CFG->gdversion)) { - $image_el =& $mform->getElement('currentpicture'); - if ($user and $user->picture) { - $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID, 'size'=>64))); + $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST); + $fs = get_file_storage(); + $hasuploadedpicture = (!$fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') && !$fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg')); + if (!empty($user->picture) && $hasuploadedpicture) { + $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64)); } else { - $image_el->setValue(get_string('none')); + $imagevalue = get_string('none'); + } + $imageelement = $mform->getElement('currentpicture'); + $imageelement->setValue($imagevalue); + + if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) { + $mform->removeElement('deletepicture'); } } diff --git a/user/editadvanced_form.php b/user/editadvanced_form.php index 10f6b456bfb..0beb65eff9c 100644 --- a/user/editadvanced_form.php +++ b/user/editadvanced_form.php @@ -101,11 +101,23 @@ class user_editadvanced_form extends moodleform { // print picture if (!empty($CFG->gdversion) and empty($USER->newadminuser)) { - $image_el =& $mform->getElement('currentpicture'); - if ($user and $user->picture) { - $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID))); + if ($user) { + $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST); + $fs = get_file_storage(); + $hasuploadedpicture = (!$fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') && !$fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg')); + if (!empty($user->picture) && $hasuploadedpicture) { + $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64)); + } else { + $imagevalue = get_string('none'); + } } else { - $image_el->setValue(get_string('none')); + $imagevalue = get_string('none'); + } + $imageelement = $mform->getElement('currentpicture'); + $imageelement->setValue($imagevalue); + + if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) { + $mform->removeElement('deletepicture'); } } diff --git a/user/editlib.php b/user/editlib.php index af8e537e067..168b5f09a40 100644 --- a/user/editlib.php +++ b/user/editlib.php @@ -33,23 +33,48 @@ function useredit_update_user_preference($usernew) { } } -function useredit_update_picture(&$usernew, $userform) { +/** + * 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"); - $fs = get_file_storage(); $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST); - - if (isset($usernew->deletepicture) and $usernew->deletepicture) { + // 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 - $DB->set_field('user', 'picture', 0, array('id'=>$usernew->id)); - + $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)) { - $DB->set_field('user', 'picture', 1, array('id'=>$usernew->id)); + $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; } function useredit_update_bounces($user, $usernew) { @@ -237,6 +262,10 @@ function useredit_shared_definition(&$mform, $editoroptions = null) { if (!empty($CFG->gdversion) and empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_picture', get_string('pictureofuser')); + if (!empty($CFG->enablegravatar)) { + $mform->addElement('static', 'gravatarenabled', get_string('gravatarenabled')); + } + $mform->addElement('static', 'currentpicture', get_string('currentpicture')); $mform->addElement('checkbox', 'deletepicture', get_string('delete'));