From da1be050bc2e412092c70ff966f8e3b566d97dcd Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 29 Sep 2014 15:07:04 +0200 Subject: [PATCH] MDL-47084 forum: New moodle_url method make_webservice_pluginfile_url --- lib/weblib.php | 26 ++++++++++++++++++++++++++ mod/forum/externallib.php | 12 +++++------- mod/forum/tests/externallib_test.php | 12 ++++-------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/weblib.php b/lib/weblib.php index 3ab7691da2b..488bfc2b510 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -730,6 +730,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 182f7c3e754..7090d943999 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -473,11 +473,8 @@ class mod_forum_external extends external_api { $user->id = $post->userid; $user = username_load_fields_from_object($user, $post); $post->userfullname = fullname($user, $canviewfullname); - $post->userpictureurl = moodle_url::make_pluginfile_url( - context_user::instance($user->id)->id, 'user', 'icon', null, '/', 'f1'); - // Fix the pluginfile.php link. - $post->userpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php", - $post->userpictureurl); + $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) = @@ -491,12 +488,13 @@ class mod_forum_external extends external_api { 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' => file_encode_url($CFG->wwwroot.'/webservice/pluginfile.php', - '/'.$modcontext->id.'/mod_forum/attachment/'.$post->id.'/'.$filename) + 'fileurl' => $fileurl->out(false) ); } } diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index c9c63868a27..cb2dca5c8c5 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -465,10 +465,8 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'warnings' => array(), ); - $userpictureurl = moodle_url::make_pluginfile_url( - context_user::instance($discussion1reply2->userid)->id, 'user', 'icon', null, '/', 'f1'); - // Fix the pluginfile.php link. - $userpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php", $userpictureurl); + $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, @@ -493,10 +491,8 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'userpictureurl' => $userpictureurl ); - $userpictureurl = moodle_url::make_pluginfile_url( - context_user::instance($discussion1reply1->userid)->id, 'user', 'icon', null, '/', 'f1'); - // Fix the pluginfile.php link. - $userpictureurl = str_replace("pluginfile.php", "webservice/pluginfile.php", $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,