Skip to content

Commit

Permalink
MDL-47084 forum: Return attachments and pluginfiles in get_posts WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 3, 2014
1 parent 6597413 commit 48fb025
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
36 changes: 34 additions & 2 deletions mod/forum/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,29 @@ public static function get_forum_discussion_posts($discussionid, $sortby = "crea
$user = username_load_fields_from_object($user, $post);
$posts[$pid]->userfullname = fullname($user, $canviewfullname);

// Rewrite embedded images URLs.
list($post->message, $post->messageformat) =
external_format_text($post->message, $post->messageformat, $modcontext->id, 'mod_forum', 'post', $post->id);

// List attachments.
if (!empty($post->attachment)) {
$post->attachments = array();

$fs = get_file_storage();
if ($files = $fs->get_area_files($modcontext->id, 'mod_forum', 'attachment', $post->id, "filename", false)) {
foreach ($files as $file) {
$filename = $file->get_filename();

$post->attachments[] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => file_encode_url($CFG->wwwroot.'/webservice/pluginfile.php',
'/'.$modcontext->id.'/mod_forum/attachment/'.$post->id.'/'.$filename)
);
}
}
}

$posts[$pid] = (array) $post;
}

Expand Down Expand Up @@ -503,9 +526,18 @@ public static function get_forum_discussion_posts_returns() {
'mailed' => new external_value(PARAM_INT, 'Mailed?'),
'subject' => new external_value(PARAM_TEXT, 'The post subject'),
'message' => new external_value(PARAM_RAW, 'The post message'),
'messageformat' => new external_value(PARAM_INT, 'The post message format'),
'messageformat' => new external_format_value('message'),
'messagetrust' => new external_value(PARAM_INT, 'Can we trust?'),
'attachment' => new external_value(PARAM_RAW, 'Attachments'),
'attachment' => new external_value(PARAM_RAW, 'Has attachments?'),
'attachments' => new external_multiple_structure(
new external_single_structure(
array (
'filename' => new external_value(PARAM_FILE, 'file name'),
'mimetype' => new external_value(PARAM_RAW, 'mime type'),
'fileurl' => new external_value(PARAM_URL, 'file download url')
)
), 'attachments', VALUE_OPTIONAL
),
'totalscore' => new external_value(PARAM_INT, 'The post message total score'),
'mailnow' => new external_value(PARAM_INT, 'Mail now?'),
'children' => new external_multiple_structure(new external_value(PARAM_INT, 'children post id')),
Expand Down
8 changes: 4 additions & 4 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,14 @@ public function test_mod_forum_get_forum_discussion_posts() {
'id' => $discussion1reply2->id,
'discussion' => $discussion1reply2->discussion,
'parent' => $discussion1reply2->parent,
'userid' => $discussion1reply2->userid,
'userid' => (int) $discussion1reply2->userid,
'created' => $discussion1reply2->created,
'modified' => $discussion1reply2->modified,
'mailed' => $discussion1reply2->mailed,
'subject' => $discussion1reply2->subject,
'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php',
$forum1context->id, 'mod_forum', 'post', $discussion1reply2->id),
'messageformat' => $discussion1reply2->messageformat,
'messageformat' => 1, // This value is usually changed by external_format_text() function.
'messagetrust' => $discussion1reply2->messagetrust,
'attachment' => $discussion1reply2->attachment,
'totalscore' => $discussion1reply2->totalscore,
Expand All @@ -489,14 +489,14 @@ public function test_mod_forum_get_forum_discussion_posts() {
'id' => $discussion1reply1->id,
'discussion' => $discussion1reply1->discussion,
'parent' => $discussion1reply1->parent,
'userid' => $discussion1reply1->userid,
'userid' => (int) $discussion1reply1->userid,
'created' => $discussion1reply1->created,
'modified' => $discussion1reply1->modified,
'mailed' => $discussion1reply1->mailed,
'subject' => $discussion1reply1->subject,
'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php',
$forum1context->id, 'mod_forum', 'post', $discussion1reply1->id),
'messageformat' => $discussion1reply1->messageformat,
'messageformat' => 1, // This value is usually changed by external_format_text() function.
'messagetrust' => $discussion1reply1->messagetrust,
'attachment' => $discussion1reply1->attachment,
'totalscore' => $discussion1reply1->totalscore,
Expand Down

0 comments on commit 48fb025

Please sign in to comment.