From b86a48c5b96cd2d41931585c65cb16dfbe99390f Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 26 Jun 2012 18:15:44 +0100 Subject: [PATCH] MDL-34035 help links: allow other types of URL for plugins. For third-parth plugins, in can be helpful if the 'More help' links in help pop-ups (the ones that come from $string['..._link'] string in the language file) can go to other places. This change support two other sorts of URL in addition to the standard 'course/editing' type of link that goes to MoodelDocs. You can use absolute URLs, starting http:// or https:/// You can use a link starting %%WWWROOT%%, and that token is replaced by $CFG->wwwroot to make the link. --- lib/setuplib.php | 45 ++++++++++++++++++---- lib/tests/setuplib_test.php | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 lib/tests/setuplib_test.php diff --git a/lib/setuplib.php b/lib/setuplib.php index 4fec92df14e..d19b13a6371 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -565,20 +565,49 @@ function get_exception_info($ex) { } /** - * Returns the Moodle Docs URL in the users language + * Returns the Moodle Docs URL in the users language for a given 'More help' link. * - * @global object - * @param string $path the end of the URL. - * @return string The MoodleDocs URL in the user's language. for example {@link http://docs.moodle.org/en/ http://docs.moodle.org/en/$path} + * There are three cases: + * + * 1. In the normal case, $path will be a short relative path 'component/thing', + * like 'mod/folder/view' 'group/import'. This gets turned into an link to + * MoodleDocs in the user's language, and for the appropriate Moodle version. + * E.g. 'group/import' may become 'http://docs.moodle.org/2x/en/group/import'. + * The 'http://docs.moodle.org' bit comes from $CFG->docroot. + * + * This is the only option that should be used in standard Moodle code. The other + * two options have been implemented because they are useful for third-party plugins. + * + * 2. $path may be an absolute URL, starting http:// or http://. In this case, + * the link is used as is. + * + * 3. $path may start %%WWWROOT%%, in which case that is replaced by + * $CFG->wwwroot to make the link. + * + * @param string $path the place to link to. See above for details. + * @return string The MoodleDocs URL in the user's language. for example @link http://docs.moodle.org/2x/en/$path} */ -function get_docs_url($path=null) { +function get_docs_url($path = null) { global $CFG; + + // Absolute URLs are used unmodified. + if (substr($path, 0, 7) === 'http://' || substr($path, 0, 8) === 'https://') { + return $path; + } + + // Paths starting %%WWWROOT%% have that replaced by $CFG->wwwroot. + if (substr($path, 0, 11) === '%%WWWROOT%%') { + return $CFG->wwwroot . substr($path, 11); + } + + // Otherwise we do the normal case, and construct a MoodleDocs URL relative to $CFG->docroot. + // Check that $CFG->branch has been set up, during installation it won't be. if (empty($CFG->branch)) { - // It's not there yet so look at version.php + // It's not there yet so look at version.php. include($CFG->dirroot.'/version.php'); } else { - // We can use $CFG->branch and avoid having to include version.php + // We can use $CFG->branch and avoid having to include version.php. $branch = $CFG->branch; } // ensure branch is valid. @@ -592,7 +621,7 @@ function get_docs_url($path=null) { if (!empty($CFG->docroot)) { return $CFG->docroot . '/' . $branch . '/' . current_language() . '/' . $path; } else { - return 'http://docs.moodle.org/'. $branch . '/en/' . $path; + return 'http://docs.moodle.org/'. $branch . '/' . current_language() . '/' . $path; } } diff --git a/lib/tests/setuplib_test.php b/lib/tests/setuplib_test.php new file mode 100644 index 00000000000..7008e583365 --- /dev/null +++ b/lib/tests/setuplib_test.php @@ -0,0 +1,74 @@ +. + +/** + * Unit tests for setuplib.php + * + * @package core_phpunit + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Unit tests for setuplib.php + * + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_setuplib_testcase extends basic_testcase { + + /** + * Test get_docs_url_standard in the normal case when we should link to Moodle docs. + */ + public function test_get_docs_url_standard() { + global $CFG; + if (empty($CFG->docroot)) { + $docroot = 'http://docs.moodle.org/'; + } else { + $docroot = $CFG->docroot; + } + $this->assertRegExp('~^' . preg_quote($docroot, '') . '/2\d/' . current_language() . '/course/editing$~', + get_docs_url('course/editing')); + } + + /** + * Test get_docs_url_standard in the special case of an absolute HTTP URL. + */ + public function test_get_docs_url_http() { + $url = 'http://moodle.org/'; + $this->assertEquals($url, get_docs_url($url)); + } + + /** + * Test get_docs_url_standard in the special case of an absolute HTTPS URL. + */ + public function test_get_docs_url_https() { + $url = 'https://moodle.org/'; + $this->assertEquals($url, get_docs_url($url)); + } + + /** + * Test get_docs_url_standard in the special case of a link relative to wwwroot. + */ + public function test_get_docs_url_wwwroot() { + global $CFG; + $this->assertEquals($CFG->wwwroot . '/lib/tests/setuplib_test.php', + get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php')); + } +}