diff --git a/lib/weblib.php b/lib/weblib.php index 451fe25e82d..7e56c1b1075 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -744,6 +744,32 @@ class moodle_url { } } + /** + * Factory method for creation of url pointing to plugin file. + * This method is the same that make_pluginfile_url but pointing to the webservice pluginfile.php script. + * It should be used only in external functions. + * + * @since 2.8 + * @param int $contextid + * @param string $component + * @param string $area + * @param int $itemid + * @param string $pathname + * @param string $filename + * @param bool $forcedownload + * @return moodle_url + */ + public static function make_webservice_pluginfile_url($contextid, $component, $area, $itemid, $pathname, $filename, + $forcedownload = false) { + global $CFG; + $urlbase = "$CFG->httpswwwroot/webservice/pluginfile.php"; + if ($itemid === null) { + return self::make_file_url($urlbase, "/$contextid/$component/$area".$pathname.$filename, $forcedownload); + } else { + return self::make_file_url($urlbase, "/$contextid/$component/$area/$itemid".$pathname.$filename, $forcedownload); + } + } + /** * Factory method for creation of url pointing to draft file of current user. * diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index b56452bcc97..598993558c4 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -483,8 +483,35 @@ class mod_forum_external extends external_api { } $user = new stdclass(); + $user->id = $post->userid; $user = username_load_fields_from_object($user, $post); - $posts[$pid]->userfullname = fullname($user, $canviewfullname); + $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); + + // 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(); + $fileurl = moodle_url::make_webservice_pluginfile_url( + $modcontext->id, 'mod_forum', 'attachment', $post->id, '/', $filename); + + $post->attachments[] = array( + 'filename' => $filename, + 'mimetype' => $file->get_mimetype(), + 'fileurl' => $fileurl->out(false) + ); + } + } + } $posts[$pid] = (array) $post; } @@ -516,15 +543,25 @@ class mod_forum_external extends external_api { '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')), 'canreply' => new external_value(PARAM_BOOL, 'The user can reply to posts?'), 'postread' => new external_value(PARAM_BOOL, 'The post was read'), - 'userfullname' => new external_value(PARAM_TEXT, 'Post author full name') + 'userfullname' => new external_value(PARAM_TEXT, 'Post author full name'), + 'userpictureurl' => new external_value(PARAM_URL, 'Post author picture.', VALUE_OPTIONAL) ), 'post' ) ), diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 5a72c22f4b3..b2a6a18fd5c 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -482,18 +482,22 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'posts' => array(), 'warnings' => array(), ); + + $userpictureurl = moodle_url::make_webservice_pluginfile_url( + context_user::instance($discussion1reply2->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false); + $expectedposts['posts'][] = array( '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, @@ -501,20 +505,25 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'children' => array(), 'canreply' => true, 'postread' => false, - 'userfullname' => fullname($user3) + 'userfullname' => fullname($user3), + '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, '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, @@ -522,7 +531,8 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'children' => array($discussion1reply2->id), 'canreply' => true, 'postread' => false, - 'userfullname' => fullname($user2) + 'userfullname' => fullname($user2), + 'userpictureurl' => $userpictureurl ); // Test a discussion with two additional posts (total 3 posts).