From 48ddc9bfd67960f3b431373b9ee17eba27d0c98e Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Wed, 24 Aug 2011 15:20:46 +0100 Subject: [PATCH 1/2] MDL-29091 weblib - Introduce out_as_local_url() method to moodle_url This can be used for passing around PARAM_LOCALURL's where necessary stripping the wwwroot --- lib/simpletest/testweblib.php | 9 +++++++++ lib/weblib.php | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php index 35caacab4cd..bdcdd5509de 100644 --- a/lib/simpletest/testweblib.php +++ b/lib/simpletest/testweblib.php @@ -127,6 +127,15 @@ class web_test extends UnitTestCase { $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); } + function test_out_as_local_url() { + $url1 = new moodle_url('/lib/simpletest/testweblib.php'); + $this->assertEqual('/lib/simpletest/testweblib.php', $url1->out_as_local_url()); + + $url2 = new moodle_url('http://www.google.com/lib/simpletest/testweblib.php'); + $this->expectException('coding_exception'); + $url2->out_as_local_url(); + } + public function test_html_to_text_simple() { $this->assertEqual("\n\n_Hello_ WORLD!", html_to_text('

Hello world!

')); } diff --git a/lib/weblib.php b/lib/weblib.php index 89bdce5d639..c97e06b9d9c 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -717,6 +717,28 @@ class moodle_url { $urlbase = "$CFG->wwwroot/file.php"; return self::make_file_url($urlbase, '/'.$courseid.'/'.$filepath, $forcedownload); } + + /** + * Returns URL a relative path from $CFG->wwwroot + * + * Can be used for passing around urls with the wwwroot stripped + * + * @param boolean $escaped Use & as params separator instead of plain & + * @param array $overrideparams params to add to the output url, these override existing ones with the same name. + * @return string Resulting URL + * @throws coding_exception if called on a non-local url + */ + public function out_as_local_url($escaped = true, array $overrideparams = null) { + global $CFG; + + $url = $this->out($escaped, $overrideparams); + + if (strpos($url, $CFG->wwwroot) !== 0) { + throw new coding_exception('out_as_local_url called on a non-local URL'); + } + + return str_replace($CFG->wwwroot, '', $url); + } } /** From a530d4a93b2f33219a247f02138a5b3fff89f49a Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Wed, 24 Aug 2011 15:31:56 +0100 Subject: [PATCH 2/2] MDL-29091 quiz - switch to use moodle_url::out_as_local_url --- mod/quiz/edit.php | 2 +- mod/quiz/editlib.php | 4 ++-- mod/quiz/locallib.php | 2 +- question/editlib.php | 2 +- question/engine/renderer.php | 3 +-- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 257208e89a9..eb1e658ddcb 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -561,7 +561,7 @@ if (!$quiz_reordertool) { $randomform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts); $randomform->set_data(array( 'category' => $pagevars['cat'], - 'returnurl' => str_replace($CFG->wwwroot, '', $thispageurl->out(false)), + 'returnurl' => $thispageurl->out_as_local_url(false), 'cmid' => $cm->id, )); ?> diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 608e38e1027..5ee8b4b660f 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -453,7 +453,7 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete, $reordertool, $pageopen = false; - $returnurl = str_replace($CFG->wwwroot, '', $pageurl->out(false)); + $returnurl = $pageurl->out_as_local_url(false); $questiontotalcount = count($order); foreach ($order as $count => $qnum) { @@ -731,7 +731,7 @@ function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts, $defaultc $returnurladdtoquiz = new moodle_url($pageurl, array('addonpage' => $page)); // Print a button linking to the choose question type page. - $returnurladdtoquiz = str_replace($CFG->wwwroot, '', $returnurladdtoquiz->out(false)); + $returnurladdtoquiz = $returnurladdtoquiz->out_as_local_url(false); $newquestionparams = array('returnurl' => $returnurladdtoquiz, 'cmid' => $quiz->cmid, 'appendqnumstring' => 'addquestion'); create_new_question_button($defaultcategoryid, $newquestionparams, diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 7e10d16999d..2d5d8e06b5d 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -829,7 +829,7 @@ function quiz_question_edit_button($cmid, $question, $returnurl, $contentafteric // Build the icon. if ($action) { if ($returnurl instanceof moodle_url) { - $returnurl = str_replace($CFG->wwwroot, '', $returnurl->out(false)); + $returnurl = $returnurl->out_as_local_url(false); } $questionparams = array('returnurl' => $returnurl, 'cmid' => $cmid, 'id' => $question->id); $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams); diff --git a/question/editlib.php b/question/editlib.php index 7f781a14b99..8d76077c71f 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -871,7 +871,7 @@ class question_bank_view { } // Create the url of the new question page to forward to. - $returnurl = str_replace($CFG->wwwroot, '', $pageurl->out(false)); + $returnurl = $pageurl->out_as_local_url(false); $this->editquestionurl = new moodle_url('/question/question.php', array('returnurl' => $returnurl)); if ($cm !== null){ diff --git a/question/engine/renderer.php b/question/engine/renderer.php index 96b8353d120..006729aacc1 100644 --- a/question/engine/renderer.php +++ b/question/engine/renderer.php @@ -278,8 +278,7 @@ class core_question_renderer extends plugin_renderer_base { $params = $options->editquestionparams; if ($params['returnurl'] instanceof moodle_url) { - $params['returnurl'] = str_replace($CFG->wwwroot, '', - $params['returnurl']->out(false)); + $params['returnurl'] = $params['returnurl']->out_as_local_url(false); } $params['id'] = $qa->get_question()->id; $editurl = new moodle_url('/question/question.php', $params);