Skip to content

Commit

Permalink
MDL-49823 webservice: Support gravatar profile images
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 1, 2015
1 parent fd57d68 commit d85bedf
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 105 deletions.
47 changes: 16 additions & 31 deletions message/externallib.php
Expand Up @@ -425,7 +425,7 @@ public static function get_contacts_parameters() {
* @since Moodle 2.5
*/
public static function get_contacts() {
global $CFG;
global $CFG, $PAGE;

// Check if messaging is enabled.
if (!$CFG->messaging) {
Expand All @@ -444,16 +444,11 @@ public static function get_contacts() {
'unread' => $contact->messagecount
);

$usercontext = context_user::instance($contact->id, IGNORE_MISSING);
if ($usercontext) {
$newcontact['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
$newcontact['profileimageurlsmall'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f2')->out(false);
} else {
$newcontact['profileimageurl'] = '';
$newcontact['profileimageurlsmall'] = '';
}
$userpicture = new user_picture($contact);
$userpicture->size = 1; // Size f1.
$newcontact['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
$userpicture->size = 0; // Size f2.
$newcontact['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false);

$allcontacts[$mode][$key] = $newcontact;
}
Expand Down Expand Up @@ -535,7 +530,7 @@ public static function search_contacts_parameters() {
* @since Moodle 2.5
*/
public static function search_contacts($searchtext, $onlymycourses = false) {
global $CFG, $USER;
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/user/lib.php');

// Check if messaging is enabled.
Expand Down Expand Up @@ -581,17 +576,11 @@ public static function search_contacts($searchtext, $onlymycourses = false) {
$user->phone1 = null;
$user->phone2 = null;

$usercontext = context_user::instance($user->id, IGNORE_MISSING);

if ($usercontext) {
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
$newuser['profileimageurlsmall'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f2')->out(false);
} else {
$newuser['profileimageurl'] = '';
$newuser['profileimageurlsmall'] = '';
}
$userpicture = new user_picture($user);
$userpicture->size = 1; // Size f1.
$newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
$userpicture->size = 0; // Size f2.
$newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false);

$user = $newuser;
}
Expand Down Expand Up @@ -863,7 +852,7 @@ public static function get_blocked_users_parameters() {
* @since 2.9
*/
public static function get_blocked_users($userid) {
global $CFG, $USER;
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . "/message/lib.php");

// Warnings array, it can be empty at the end but is mandatory.
Expand Down Expand Up @@ -902,13 +891,9 @@ public static function get_blocked_users($userid) {
'fullname' => fullname($user),
);

$usercontext = context_user::instance($user->id, IGNORE_MISSING);
if ($usercontext) {
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
} else {
$newuser['profileimageurl'] = '';
}
$userpicture = new user_picture($user);
$userpicture->size = 1; // Size f1.
$newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);

$blockedusers[] = $newuser;
}
Expand Down
11 changes: 4 additions & 7 deletions mod/chat/classes/external.php
Expand Up @@ -153,7 +153,7 @@ public static function get_chat_users_parameters() {
* @throws moodle_exception
*/
public static function get_chat_users($chatsid) {
global $DB;
global $DB, $PAGE;

$params = self::validate_parameters(self::get_chat_users_parameters(),
array(
Expand All @@ -180,13 +180,10 @@ public static function get_chat_users($chatsid) {
$returnedusers = array();

foreach ($users as $user) {
$usercontext = context_user::instance($user->id, IGNORE_MISSING);
$profileimageurl = '';

if ($usercontext) {
$profileimageurl = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
}
$userpicture = new user_picture($user);
$userpicture->size = 1; // Size f1.
$profileimageurl = $userpicture->get_url($PAGE)->out(false);

$returnedusers[] = array(
'id' => $user->id,
Expand Down
15 changes: 6 additions & 9 deletions mod/choice/classes/external.php
Expand Up @@ -57,7 +57,7 @@ public static function get_choice_results_parameters() {
* @since Moodle 3.0
*/
public static function get_choice_results($choiceid) {
global $USER;
global $USER, $PAGE;

$params = self::validate_parameters(self::get_choice_results_parameters(), array('choiceid' => $choiceid));

Expand Down Expand Up @@ -96,14 +96,11 @@ public static function get_choice_results($choiceid) {
$response = array();
$response['userid'] = $userresponse->id;
$response['fullname'] = fullname($userresponse, $fullnamecap);
$usercontext = context_user::instance($userresponse->id, IGNORE_MISSING);
if ($usercontext) {
$profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null,
'/', 'f1')->out(false);
} else {
$profileimageurl = '';
}
$response['profileimageurl'] = $profileimageurl;

$userpicture = new user_picture($userresponse);
$userpicture->size = 1; // Size f1.
$response['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);

// Add optional properties.
foreach (array('answerid', 'timemodified') as $field) {
if (property_exists($userresponse, 'answerid')) {
Expand Down
49 changes: 20 additions & 29 deletions mod/forum/externallib.php
Expand Up @@ -384,7 +384,7 @@ public static function get_forum_discussion_posts_parameters() {
* @since Moodle 2.7
*/
public static function get_forum_discussion_posts($discussionid, $sortby = "created", $sortdirection = "DESC") {
global $CFG, $DB, $USER;
global $CFG, $DB, $USER, $PAGE;

$posts = array();
$warnings = array();
Expand Down Expand Up @@ -475,20 +475,15 @@ public static function get_forum_discussion_posts($discussionid, $sortby = "crea
$post->children = array();
}

$userpicture = new user_picture($post);
$userpicture->size = 1; // Size f1.
$post->userpictureurl = $userpicture->get_url($PAGE)->out(false);

$user = new stdclass();
$user->id = $post->userid;
$user = username_load_fields_from_object($user, $post);
$post->userfullname = fullname($user, $canviewfullname);

// We can have post written by users that are deleted. In this case, those users don't have a valid context.
$usercontext = context_user::instance($user->id, IGNORE_MISSING);
if ($usercontext) {
$post->userpictureurl = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
} else {
$post->userpictureurl = '';
}

// Rewrite embedded images URLs.
list($post->message, $post->messageformat) =
external_format_text($post->message, $post->messageformat, $modcontext->id, 'mod_forum', 'post', $post->id);
Expand Down Expand Up @@ -603,7 +598,7 @@ public static function get_forum_discussions_paginated_parameters() {
*/
public static function get_forum_discussions_paginated($forumid, $sortby = 'timemodified', $sortdirection = 'DESC',
$page = -1, $perpage = 0) {
global $CFG, $DB, $USER;
global $CFG, $DB, $USER, $PAGE;

require_once($CFG->dirroot . "/mod/forum/lib.php");

Expand Down Expand Up @@ -696,34 +691,30 @@ public static function get_forum_discussions_paginated($forumid, $sortby = 'time
$discussion->numreplies = (int) $replies[$discussion->discussion]->replies;
}

$picturefields = explode(',', user_picture::fields());

// Load user objects from the results of the query.
$user = new stdclass();
$user->id = $discussion->userid;
$user = username_load_fields_from_object($user, $discussion);
$user = username_load_fields_from_object($user, $discussion, null, $picturefields);
// Preserve the id, it can be modified by username_load_fields_from_object.
$user->id = $discussion->userid;
$discussion->userfullname = fullname($user, $canviewfullname);

// We can have post written by users that are deleted. In this case, those users don't have a valid context.
$usercontext = context_user::instance($user->id, IGNORE_MISSING);
if ($usercontext) {
$discussion->userpictureurl = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
} else {
$discussion->userpictureurl = '';
}
$userpicture = new user_picture($user);
$userpicture->size = 1; // Size f1.
$discussion->userpictureurl = $userpicture->get_url($PAGE)->out(false);

$usermodified = new stdclass();
$usermodified->id = $discussion->usermodified;
$usermodified = username_load_fields_from_object($usermodified, $discussion, 'um');
$usermodified = username_load_fields_from_object($usermodified, $discussion, 'um', $picturefields);
// Preserve the id (it can be overwritten due to the prefixed $picturefields).
$usermodified->id = $discussion->usermodified;
$discussion->usermodifiedfullname = fullname($usermodified, $canviewfullname);

// We can have post written by users that are deleted. In this case, those users don't have a valid context.
$usercontext = context_user::instance($usermodified->id, IGNORE_MISSING);
if ($usercontext) {
$discussion->usermodifiedpictureurl = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
} else {
$discussion->usermodifiedpictureurl = '';
}
$userpicture = new user_picture($usermodified);
$userpicture->size = 1; // Size f1.
$discussion->usermodifiedpictureurl = $userpicture->get_url($PAGE)->out(false);

// Rewrite embedded images URLs.
list($discussion->message, $discussion->messageformat) =
Expand Down
3 changes: 2 additions & 1 deletion mod/forum/lib.php
Expand Up @@ -2679,7 +2679,8 @@ function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost=
$umfields = "";
$umtable = "";
} else {
$umfields = ', ' . get_all_user_name_fields(true, 'um', null, 'um');
$umfields = ', ' . get_all_user_name_fields(true, 'um', null, 'um') . ', um.email AS umemail, um.picture AS umpicture,
um.imagealt AS umimagealt';
$umtable = " LEFT JOIN {user} um ON (d.usermodified = um.id)";
}

Expand Down
44 changes: 27 additions & 17 deletions mod/forum/tests/externallib_test.php
Expand Up @@ -383,7 +383,7 @@ public function test_mod_forum_get_forum_discussions() {
* Test get forum posts
*/
public function test_mod_forum_get_forum_discussion_posts() {
global $CFG;
global $CFG, $PAGE;

$this->resetAfterTest(true);

Expand Down Expand Up @@ -451,9 +451,7 @@ public function test_mod_forum_get_forum_discussion_posts() {
'warnings' => array(),
);

// Empty picture since it's a user deleted (user3).
$userpictureurl = '';

// User pictures are initially empty, we should get the links once the external function is called.
$expectedposts['posts'][] = array(
'id' => $discussion1reply2->id,
'discussion' => $discussion1reply2->discussion,
Expand All @@ -474,12 +472,9 @@ public function test_mod_forum_get_forum_discussion_posts() {
'canreply' => true,
'postread' => false,
'userfullname' => fullname($user3),
'userpictureurl' => $userpictureurl
'userpictureurl' => ''
);

$userpictureurl = moodle_url::make_webservice_pluginfile_url(
context_user::instance($discussion1reply1->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false);

$expectedposts['posts'][] = array(
'id' => $discussion1reply1->id,
'discussion' => $discussion1reply1->discussion,
Expand All @@ -500,14 +495,23 @@ public function test_mod_forum_get_forum_discussion_posts() {
'canreply' => true,
'postread' => false,
'userfullname' => fullname($user2),
'userpictureurl' => $userpictureurl
'userpictureurl' => ''
);

// Test a discussion with two additional posts (total 3 posts).
$posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertEquals(3, count($posts['posts']));

// Generate here the pictures because we need to wait to the external function to init the theme.
$userpicture = new user_picture($user3);
$userpicture->size = 1; // Size f1.
$expectedposts['posts'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);

$userpicture = new user_picture($user2);
$userpicture->size = 1; // Size f1.
$expectedposts['posts'][1]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);

// Unset the initial discussion post.
array_pop($posts['posts']);
$this->assertEquals($expectedposts, $posts);
Expand Down Expand Up @@ -589,7 +593,7 @@ public function test_mod_forum_get_forum_discussion_posts_qanda() {
* Test get forum discussions paginated
*/
public function test_mod_forum_get_forum_discussions_paginated() {
global $USER, $CFG, $DB;
global $USER, $CFG, $DB, $PAGE;

$this->resetAfterTest(true);

Expand Down Expand Up @@ -663,12 +667,8 @@ public function test_mod_forum_get_forum_discussions_paginated() {
// Create what we expect to be returned when querying the forums.

$post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
$userpictureurl = moodle_url::make_webservice_pluginfile_url(
context_user::instance($user1->id)->id, 'user', 'icon', null, '/', 'f1');

// We expect an empty URL since we deleted the user4.
$usermodifiedpictureurl = '';

// User pictures are initially empty, we should get the links once the external function is called.
$expecteddiscussions = array(
'id' => $discussion1->firstpost,
'name' => $discussion1->name,
Expand All @@ -692,8 +692,8 @@ public function test_mod_forum_get_forum_discussions_paginated() {
'mailnow' => $post1->mailnow,
'userfullname' => fullname($user1),
'usermodifiedfullname' => fullname($user4),
'userpictureurl' => $userpictureurl,
'usermodifiedpictureurl' => $usermodifiedpictureurl,
'userpictureurl' => '',
'usermodifiedpictureurl' => '',
'numreplies' => 3,
'numunread' => 0
);
Expand All @@ -705,6 +705,16 @@ public function test_mod_forum_get_forum_discussions_paginated() {
'discussions' => array($expecteddiscussions),
'warnings' => array()
);

// Wait the theme to be loaded (the external_api call does that) to generate the user profiles.
$userpicture = new user_picture($user1);
$userpicture->size = 1; // Size f1.
$expectedreturn['discussions'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);

$userpicture = new user_picture($user4);
$userpicture->size = 1; // Size f1.
$expectedreturn['discussions'][0]['usermodifiedpictureurl'] = $userpicture->get_url($PAGE)->out(false);

$this->assertEquals($expectedreturn, $discussions);

// Call without required view discussion capability.
Expand Down
13 changes: 5 additions & 8 deletions rating/classes/external.php
Expand Up @@ -75,7 +75,7 @@ public static function get_item_ratings_parameters() {
* @since Moodle 2.9
*/
public static function get_item_ratings($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $sort) {
global $USER;
global $USER, $PAGE;

$warnings = array();

Expand Down Expand Up @@ -146,13 +146,10 @@ public static function get_item_ratings($contextlevel, $instanceid, $component,
$rating->rating = $maxrating;
}

$profileimageurl = '';
// We can have ratings from deleted users. In this case, those users don't have a valid context.
$usercontext = context_user::instance($rating->userid, IGNORE_MISSING);
if ($usercontext) {
$profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null,
'/', 'f1')->out(false);
}
// The rating object has all the required fields for generating the picture url.
$userpicture = new user_picture($rating);
$userpicture->size = 1; // Size f1.
$profileimageurl = $userpicture->get_url($PAGE)->out(false);

$result = array();
$result['id'] = $rating->id;
Expand Down

0 comments on commit d85bedf

Please sign in to comment.