From: nicolasconnault Date: Mon, 10 Aug 2009 03:04:01 +0000 (+0000) Subject: MDL-9644 Added a compare() method to moodle_url X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=827b2f7a66439976b2f1fa6ee581788a9b93e9d2;p=moodle.git MDL-9644 Added a compare() method to moodle_url --- diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php index 24c41a8faf..d727f618b2 100644 --- a/lib/simpletest/testweblib.php +++ b/lib/simpletest/testweblib.php @@ -116,5 +116,33 @@ class web_test extends UnitTestCase { $this->assertEqual("$CFG->wwwroot/admin/report/unittest/$relativeurl1", prepare_url(new moodle_url($relativeurl1))); $this->assertEqual("$CFG->wwwroot$relativeurl2", prepare_url(new moodle_url($relativeurl2))); } + + function test_compare_url() { + $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2)); + $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); + + $this->assertFalse($url1->compare($url2, URL_MATCH_BASE)); + $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); + $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); + + $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3)); + + $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); + $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); + $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); + + $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); + + $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); + $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); + $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); + + $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1)); + + $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); + $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); + $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); + } } + ?> diff --git a/lib/weblib.php b/lib/weblib.php index 5252458899..8281eef61d 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -66,6 +66,18 @@ define('FORMAT_MARKDOWN', '4'); // Markdown-formatted text http://daringfireba */ define('TRUSTTEXT', '#####TRUSTTEXT#####'); +/** + * A moodle_url comparison using this flag will return true if the base URLs match, params are ignored + */ +define('URL_MATCH_BASE', 0); +/** + * A moodle_url comparison using this flag will return true if the base URLs match and the params of url1 are part of url2 + */ +define('URL_MATCH_PARAMS', 1); +/** + * A moodle_url comparison using this flag will return true if the two URLs are identical, except for the order of the params + */ +define('URL_MATCH_EXACT', 2); /** * Allowed tags - string of html tags that can be tested against for safe html tags @@ -481,6 +493,48 @@ class moodle_url { $overrideparams = array('sesskey'=> sesskey()) + $overrideparams; return $this->out(false, $overrideparams); } + + /** + * Compares this moodle_url with another + * See documentation of constants for an explanation of the comparison flags. + * @param moodle_url $url The moodle_url object to compare + * @param int $matchtype The type of comparison (URL_MATCH_BASE, URL_MATCH_PARAMS, URL_MATCH_EXACT) + * @return boolean + */ + public function compare(moodle_url $url, $matchtype = URL_MATCH_EXACT) { + if ($this->out(true) != $url->out(true)) { + return false; + } + + if ($matchtype == URL_MATCH_BASE) { + return true; + } + + $urlparams = $url->params(); + foreach ($this->params() as $param => $value) { + if ($param == 'sesskey') { + continue; + } + if (!array_key_exists($param, $urlparams) || $urlparams[$param] != $value) { + return false; + } + } + + if ($matchtype == URL_MATCH_PARAMS) { + return true; + } + + foreach ($urlparams as $param => $value) { + if ($param == 'sesskey') { + continue; + } + if (!array_key_exists($param, $this->params()) || $this->param($param) != $value) { + return false; + } + } + + return true; + } } /** @@ -2588,7 +2642,7 @@ function print_recent_activity_note($time, $user, $text, $link, $return=false, $ */ function switchroles_form($courseid) { - global $CFG, $USER; + global $CFG, $USER, $OUTPUT; if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) { @@ -2611,8 +2665,11 @@ function switchroles_form($courseid) { } // unset default user role - it would not work unset($roles[$CFG->guestroleid]); - return popup_form($CFG->wwwroot.'/course/view.php?id='.$courseid.'&sesskey='.sesskey().'&switchrole=', - $roles, 'switchrole', '', get_string('switchroleto'), 'switchrole', get_string('switchroleto'), true); + $popupurl = $CFG->wwwroot.'/course/view.php?id='.$courseid.'&sesskey='.sesskey(); + $select = moodle_select::make_popup_form($popupurl, 'switchrole', $roles, 'switchrole', ''); + $select->nothinglabel = get_string('switchroleto'); + $select->set_help_icon('switchrole', get_string('switchroleto')); + return $OUTPUT->select($select); } return '';