Skip to content

Commit

Permalink
Merge branch 'MDL-49763-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Apr 15, 2015
2 parents 7068541 + 81f810d commit 8c1444e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
39 changes: 27 additions & 12 deletions mod/forum/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,15 @@ public static function get_forum_discussion_posts($discussionid, $sortby = "crea
$user->id = $post->userid;
$user = username_load_fields_from_object($user, $post);
$post->userfullname = fullname($user, $canviewfullname);
$post->userpictureurl = moodle_url::make_webservice_pluginfile_url(
context_user::instance($user->id)->id, 'user', 'icon', null, '/', 'f1')->out(false);

// 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) =
Expand Down Expand Up @@ -702,21 +709,29 @@ public static function get_forum_discussions_paginated($forumid, $sortby = 'time
$user->id = $discussion->userid;
$user = username_load_fields_from_object($user, $discussion);
$discussion->userfullname = fullname($user, $canviewfullname);
$discussion->userpictureurl = moodle_url::make_pluginfile_url(
context_user::instance($user->id)->id, 'user', 'icon', null, '/', 'f1');
// Fix the pluginfile.php link.
$discussion->userpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php",
$discussion->userpictureurl);

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

$usermodified = new stdclass();
$usermodified->id = $discussion->usermodified;
$usermodified = username_load_fields_from_object($usermodified, $discussion, 'um');
$discussion->usermodifiedfullname = fullname($usermodified, $canviewfullname);
$discussion->usermodifiedpictureurl = moodle_url::make_pluginfile_url(
context_user::instance($usermodified->id)->id, 'user', 'icon', null, '/', 'f1');
// Fix the pluginfile.php link.
$discussion->usermodifiedpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php",
$discussion->usermodifiedpictureurl);

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

// Rewrite embedded images URLs.
list($discussion->message, $discussion->messageformat) =
Expand Down
19 changes: 11 additions & 8 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,18 @@ public function test_mod_forum_get_forum_discussion_posts() {
// So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
$this->getDataGenerator()->enrol_user($user3->id, $course1->id);

// Delete one user, to test that we still receive posts by this user.
delete_user($user3);

// Create what we expect to be returned when querying the discussion.
$expectedposts = array(
'posts' => array(),
'warnings' => array(),
);

$userpictureurl = moodle_url::make_webservice_pluginfile_url(
context_user::instance($discussion1reply2->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false);
// Empty picture since it's a user deleted (user3).
$userpictureurl = '';

$expectedposts['posts'][] = array(
'id' => $discussion1reply2->id,
Expand Down Expand Up @@ -576,6 +578,9 @@ public function test_mod_forum_get_forum_discussions_paginated() {
}
$enrol->enrol_user($instance1, $user1->id);

// Delete one user.
delete_user($user4);

// Assign capabilities to view discussions for forum 1.
$cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
Expand All @@ -585,13 +590,11 @@ 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_pluginfile_url(
$userpictureurl = moodle_url::make_webservice_pluginfile_url(
context_user::instance($user1->id)->id, 'user', 'icon', null, '/', 'f1');
$userpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php", $userpictureurl);

$usermodifiedpictureurl = moodle_url::make_pluginfile_url(
context_user::instance($user4->id)->id, 'user', 'icon', null, '/', 'f1');
$usermodifiedpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php", $usermodifiedpictureurl);
// We expect an empty URL since we deleted the user4.
$usermodifiedpictureurl = '';

$expecteddiscussions = array(
'id' => $discussion1->firstpost,
Expand Down
12 changes: 9 additions & 3 deletions rating/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,19 @@ public static function get_item_ratings($contextlevel, $instanceid, $component,
if ($rating->rating > $maxrating) {
$rating->rating = $maxrating;
}
$usercontext = context_user::instance($rating->userid);
$profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1');

$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);
}

$result = array();
$result['id'] = $rating->id;
$result['userid'] = $rating->userid;
$result['userpictureurl'] = $profileimageurl->out(false);
$result['userpictureurl'] = $profileimageurl;
$result['userfullname'] = fullname($rating);
$result['rating'] = $scalemenu[$rating->rating];
$result['timemodified'] = $rating->timemodified;
Expand Down
3 changes: 3 additions & 0 deletions rating/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public function test_get_item_ratings() {
$rating2->timemodified = time() + 1;
$rating2->id = $DB->insert_record('rating', $rating2);

// Delete teacher2, we must still receive the ratings.
delete_user($teacher2);

// Teachers can see all the ratings.
$this->setUser($teacher1);

Expand Down

0 comments on commit 8c1444e

Please sign in to comment.