From 8bb9dfcae6963524b75b63408befa54e86715ac3 Mon Sep 17 00:00:00 2001
From: David Mudrak <david.mudrak@gmail.com>
Date: Wed, 29 Jul 2009 15:28:27 +0200
Subject: [PATCH] MDL-19956 $PAGE->set_url() accepts instance of moodle_url

---
 lib/pagelib.php |   26 +++++++++++++++++++-------
 1 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/lib/pagelib.php b/lib/pagelib.php
index 4d265b7..1024e9e 100644
--- a/lib/pagelib.php
+++ b/lib/pagelib.php
@@ -720,17 +720,29 @@ class moodle_page {
      * For example, course/view.php does:
      *      $id = optional_param('id', 0, PARAM_INT);
      *      $PAGE->set_url('course/view.php', array('id' => $id));
-     * @param string $url a URL, relative to $CFG->wwwroot.
+     * @param mixed $url moodle_url|string URL relative to $CFG->wwwroot or {@link moodle_url} instance
      * @param array $params paramters to add ot the URL.
      */
     public function set_url($url, $params = array()) {
         global $CFG;
-        $this->_url = new moodle_url($CFG->wwwroot . '/' . $url, $params);
-        if (is_null($this->_pagetype)) {
-            $this->initialise_default_pagetype($url);
-        }
-        if (!is_null($this->_legacypageobject)) {
-            $this->_legacypageobject->set_url($url, $params);
+        if ($url instanceof moodle_url) {
+            $this->_url = clone($url);
+            if (is_null($this->_pagetype)) {
+                $relurl = str_replace($CFG->wwwroot, '', $url->out(true)); // extracts the 'mod/foobar/view.php' part
+                $this->initialise_default_pagetype($relurl);
+            }
+            if (!is_null($this->_legacypageobject)) {
+                debugging('Deprecated legacy page object should not be passed moodle_url instance', DEBUG_DEVELOPER);
+                $this->_legacypageobject->set_url($url->out(true), $url->params());
+            }
+        } else {
+            $this->_url = new moodle_url($CFG->wwwroot . '/' . $url, $params);
+            if (is_null($this->_pagetype)) {
+                $this->initialise_default_pagetype($url);
+            }
+            if (!is_null($this->_legacypageobject)) {
+                $this->_legacypageobject->set_url($url, $params);
+            }
         }
     }
 
-- 
1.6.0.6

