Skip to content

Commit

Permalink
Merge branch 'MDL-65252-master-fix' of git://github.com/lameze/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta authored and stronk7 committed Jul 8, 2021
2 parents b452bab + 8fd7e5e commit 3b0e7c6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
9 changes: 7 additions & 2 deletions mod/forum/classes/local/builders/exported_posts.php
Expand Up @@ -117,13 +117,15 @@ public function __construct(
* @param forum_entity[] $forums A list of all forums that each of the $discussions belong to
* @param discussion_entity[] $discussions A list of all discussions that each of the $posts belong to
* @param post_entity[] $posts The list of posts to export.
* @param bool $includeinlineattachments Whether inline attachments should be included or not.
* @return stdClass[] List of exported posts in the same order as the $posts array.
*/
public function build(
stdClass $user,
array $forums,
array $discussions,
array $posts
array $posts,
bool $includeinlineattachments = false
) : array {
// Format the forums and discussion to make them more easily accessed later.
$forums = array_reduce($forums, function($carry, $forum) {
Expand All @@ -142,7 +144,10 @@ public function build(
$authorsbyid = $this->get_authors_for_posts($posts);
$authorcontextids = $this->get_author_context_ids(array_keys($authorsbyid));
$attachmentsbypostid = $this->get_attachments_for_posts($groupedposts);
$inlineattachments = $this->get_inline_attachments_for_posts($groupedposts);
$inlineattachments = [];
if ($includeinlineattachments) {
$inlineattachments = $this->get_inline_attachments_for_posts($groupedposts);
}
$groupsbycourseandauthorid = $this->get_author_groups_from_posts($groupedposts);
$tagsbypostid = $this->get_tags_from_posts($posts);
$ratingbypostid = $this->get_ratings_from_posts($user, $groupedposts);
Expand Down
1 change: 1 addition & 0 deletions mod/forum/classes/local/exporters/post.php
Expand Up @@ -274,6 +274,7 @@ protected static function define_other_properties() {
'type' => $attachmentdefinition
],
'messageinlinefiles' => [
'optional' => true,
'multiple' => true,
'type' => stored_file_exporter::read_properties_definition(),
],
Expand Down
9 changes: 6 additions & 3 deletions mod/forum/externallib.php
Expand Up @@ -170,9 +170,10 @@ public static function get_forums_by_courses_returns() {
* @param int $discussionid
* @param string $sortby
* @param string $sortdirection
* @param bool $includeinlineattachments Whether inline attachments should be included or not.
* @return array
*/
public static function get_discussion_posts(int $discussionid, ?string $sortby, ?string $sortdirection) {
public static function get_discussion_posts(int $discussionid, ?string $sortby, ?string $sortdirection, bool $includeinlineattachments = false) {
global $USER;
// Validate the parameter.
$params = self::validate_parameters(self::get_discussion_posts_parameters(), [
Expand Down Expand Up @@ -225,7 +226,7 @@ public static function get_discussion_posts(int $discussionid, ?string $sortby,
$legacydatamapper = mod_forum\local\container::get_legacy_data_mapper_factory();

return [
'posts' => $postbuilder->build($USER, [$forum], [$discussion], $posts),
'posts' => $postbuilder->build($USER, [$forum], [$discussion], $posts, $includeinlineattachments),
'forumid' => $discussion->get_forum_id(),
'courseid' => $discussion->get_course_id(),
'ratinginfo' => \core_rating\external\util::get_rating_info(
Expand All @@ -248,7 +249,9 @@ public static function get_discussion_posts_parameters() {
return new external_function_parameters ([
'discussionid' => new external_value(PARAM_INT, 'The ID of the discussion from which to fetch posts.', VALUE_REQUIRED),
'sortby' => new external_value(PARAM_ALPHA, 'Sort by this element: id, created or modified', VALUE_DEFAULT, 'created'),
'sortdirection' => new external_value(PARAM_ALPHA, 'Sort direction: ASC or DESC', VALUE_DEFAULT, 'DESC')
'sortdirection' => new external_value(PARAM_ALPHA, 'Sort direction: ASC or DESC', VALUE_DEFAULT, 'DESC'),
'includeinlineattachments' => new external_value(PARAM_BOOL, 'Whether inline attachments should be included or not', VALUE_DEFAULT,
false),
]);
}

Expand Down
87 changes: 79 additions & 8 deletions mod/forum/tests/externallib_test.php
Expand Up @@ -593,7 +593,7 @@ public function test_mod_forum_get_discussion_posts() {
);

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

Expand Down Expand Up @@ -733,6 +733,81 @@ public function test_mod_forum_get_discussion_posts_deleted() {
}
}

/**
* Test get forum posts returns inline attachments.
*/
public function test_mod_forum_get_discussion_posts_inline_attachments() {
global $CFG;

$this->resetAfterTest(true);

// Create a course and enrol some users in it.
$course = self::getDataGenerator()->create_course();

// Create users.
$user = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);


// Set the first created user to the test user.
self::setUser($user);

// Create test data.
$forum = self::getDataGenerator()->create_module('forum', (object) [
'course' => $course->id,
]);

// Create a file in a draft area for inline attachments.
$draftidinlineattach = file_get_unused_draft_itemid();
$draftidattach = file_get_unused_draft_itemid();
self::setUser($user);
$usercontext = context_user::instance($user->id);
$filepath = '/';
$filearea = 'draft';
$component = 'user';
$filenameimg = 'fakeimage.png';
$filerecordinline = [
'contextid' => $usercontext->id,
'component' => $component,
'filearea' => $filearea,
'itemid' => $draftidinlineattach,
'filepath' => $filepath,
'filename' => $filenameimg,
];
$fs = get_file_storage();
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');

// Create discussion.
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
"/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
'" alt="inlineimage">.';
$options = [
[
'name' => 'inlineattachmentsid',
'value' => $draftidinlineattach
],
[
'name' => 'attachmentsid',
'value' => $draftidattach
]
];
$discussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject', $dummytext,
-1, $options);

$posts = mod_forum_external::get_discussion_posts($discussion['discussionid'], 'modified', 'DESC');
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
$post = $posts['posts'][0];
$this->assertCount(0, $post['messageinlinefiles']);
$this->assertEmpty($post['messageinlinefiles']);

$posts = mod_forum_external::get_discussion_posts($discussion['discussionid'], 'modified', 'DESC',
true);
$posts = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $posts);
$post = $posts['posts'][0];
$this->assertCount(1, $post['messageinlinefiles']);
$this->assertEquals('fakeimage.png', $post['messageinlinefiles'][0]['filename']);
}

/**
* Test get forum posts (qanda forum)
*/
Expand Down Expand Up @@ -2574,9 +2649,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() {
'wordcount' => null,
'author' => $exporteduser2,
'attachments' => [],
'messageinlinefiles' => [
0 => $this->get_expected_attachment($file1),
],
'messageinlinefiles' => [],
'tags' => [],
'html' => [
'rating' => null,
Expand Down Expand Up @@ -2719,9 +2792,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() {
'wordcount' => null,
'author' => $exporteduser2,
'attachments' => [],
'messageinlinefiles' => [
0 => $this->get_expected_attachment($file2),
],
'messageinlinefiles' => [],
'tags' => [],
'html' => [
'rating' => null,
Expand Down Expand Up @@ -3139,7 +3210,7 @@ public function test_update_discussion_post_post() {
$this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id, $cm));

// Get the post from WS.
$result = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'DESC');
$result = mod_forum_external::get_discussion_posts($discussion->id, 'modified', 'DESC', true);
$result = external_api::clean_returnvalue(mod_forum_external::get_discussion_posts_returns(), $result);
$found = false;
foreach ($result['posts'] as $post) {
Expand Down

0 comments on commit 3b0e7c6

Please sign in to comment.