Skip to content

Commit

Permalink
MDL-21676 user: Implemented support for Gravatar profile pictures
Browse files Browse the repository at this point in the history
Big thanks to the developers who put effort into this improvement:
* Jonathan Robson <[email protected]>
* Jonathan Harker <[email protected]>
  • Loading branch information
Sam Hemelryk authored and stronk7 committed Sep 20, 2011
1 parent 8974d17 commit 4125bdc
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 35 deletions.
1 change: 1 addition & 0 deletions admin/settings/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
$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);
Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@
$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';
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@
http://docs.moodle.org/dev/License';
$string['grade'] = 'Grade';
$string['grades'] = 'Grades';
$string['gravatarenabled'] = '<a href="http://www.gravatar.com/">Gravatar</a> 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';
Expand Down
45 changes: 25 additions & 20 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public static function unalias(stdClass $record, array $extrafields=null, $idali
* @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');
Expand Down Expand Up @@ -298,16 +298,14 @@ public function get_url(moodle_page $page, renderer_base $renderer = null) {
$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
Expand All @@ -316,20 +314,27 @@ public function get_url(moodle_page $page, renderer_base $renderer = null) {
// 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;
}
}

Expand Down
16 changes: 12 additions & 4 deletions user/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ function definition_after_data() {

// 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');
}
}

Expand Down
20 changes: 16 additions & 4 deletions user/editadvanced_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,23 @@ function definition_after_data() {

// 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');
}
}

Expand Down
43 changes: 36 additions & 7 deletions user/editlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit 4125bdc

Please sign in to comment.