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)); }
/** * Fetch all enabled tours matching the specified target. * * @param moodle_url $targetmatch The URL to match. */ public static function get_matching_tourdata(\moodle_url $targetmatch) { $tours = self::get_enabled_tourdata(); // Attempt to determine whether this is the front page. // This is a special case because the frontpage uses a shortened page path making it difficult to detect exactly. $isfrontpage = $targetmatch->compare(new \moodle_url('/'), URL_MATCH_BASE); $target = $targetmatch->out_as_local_url(); return array_filter($tours, function ($tour) use($isfrontpage, $target) { if ($isfrontpage && $tour->pathmatch === 'FRONTPAGE') { return true; } $pattern = preg_quote($tour->pathmatch, '@'); $pattern = str_replace('%', '.*', $pattern); return !!preg_match("@{$pattern}@", $target); }); }
/** * This function encapsulates all the logic behind the complex * navigation, titles and headings of the blog listing page, depending * on URL params. It looks at URL params and at the current context level. * It builds and returns an array containing: * * 1. heading: The heading displayed above the blog entries * 2. stradd: The text to be used as the "Add entry" link * 3. strview: The text to be used as the "View entries" link * 4. url: The moodle_url object used as the base for add and view links * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block * * All other variables are set directly in $PAGE * * It uses the current URL to build these variables. * A number of mutually exclusive use cases are used to structure this function. * * @return array */ function blog_get_headers($courseid = null, $groupid = null, $userid = null, $tagid = null) { global $CFG, $PAGE, $DB, $USER; $id = optional_param('id', null, PARAM_INT); $tag = optional_param('tag', null, PARAM_NOTAGS); $tagid = optional_param('tagid', $tagid, PARAM_INT); $userid = optional_param('userid', $userid, PARAM_INT); $modid = optional_param('modid', null, PARAM_INT); $entryid = optional_param('entryid', null, PARAM_INT); $groupid = optional_param('groupid', $groupid, PARAM_INT); $courseid = optional_param('courseid', $courseid, PARAM_INT); $search = optional_param('search', null, PARAM_RAW); $action = optional_param('action', null, PARAM_ALPHA); $confirm = optional_param('confirm', false, PARAM_BOOL); // Ignore userid when action == add if ($action == 'add' && $userid) { unset($userid); $PAGE->url->remove_params(array('userid')); } else { if ($action == 'add' && $entryid) { unset($entryid); $PAGE->url->remove_params(array('entryid')); } } $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array()); $blogurl = new moodle_url('/blog/index.php'); // If the title is not yet set, it's likely that the context isn't set either, so skip this part $pagetitle = $PAGE->title; if (!empty($pagetitle)) { $contexturl = blog_get_context_url(); // Look at the context URL, it may have additional params that are not in the current URL if (!$blogurl->compare($contexturl)) { $blogurl = $contexturl; if (empty($courseid)) { $courseid = $blogurl->param('courseid'); } if (empty($modid)) { $modid = $blogurl->param('modid'); } } } $headers['stradd'] = get_string('addnewentry', 'blog'); $headers['strview'] = null; $site = $DB->get_record('course', array('id' => SITEID)); $sitecontext = get_context_instance(CONTEXT_SYSTEM); // Common Lang strings $strparticipants = get_string("participants"); $strblogentries = get_string("blogentries", 'blog'); // Prepare record objects as needed if (!empty($courseid)) { $headers['filters']['course'] = $courseid; $course = $DB->get_record('course', array('id' => $courseid)); } if (!empty($userid)) { $headers['filters']['user'] = $userid; $user = $DB->get_record('user', array('id' => $userid)); } if (!empty($groupid)) { // groupid always overrides courseid $headers['filters']['group'] = $groupid; $group = $DB->get_record('groups', array('id' => $groupid)); $course = $DB->get_record('course', array('id' => $group->courseid)); } $PAGE->set_pagelayout('standard'); // modid always overrides courseid, so the $course object may be reset here if (!empty($modid) && $CFG->useblogassociations) { $headers['filters']['module'] = $modid; // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid)); $course = $DB->get_record('course', array('id' => $courseid)); $cm = $DB->get_record('course_modules', array('id' => $modid)); $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module)); $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance)); $a = new stdClass(); $a->type = get_string('modulename', $cm->modname); $PAGE->set_cm($cm, $course); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); } // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid) // Note: if action is set to 'add' or 'edit', we do this at the end if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) { $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $PAGE->navbar->add($strblogentries, $blogurl); $PAGE->set_title("{$shortname}: " . get_string('blog', 'blog')); $PAGE->set_heading("{$shortname}: " . get_string('blog', 'blog')); $headers['heading'] = get_string('siteblog', 'blog', $shortname); // $headers['strview'] = get_string('viewsiteentries', 'blog'); } // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information if (!empty($entryid)) { $headers['filters']['entry'] = $entryid; $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id'; $user = $DB->get_record_sql($sql, array($entryid)); $entry = $DB->get_record('post', array('id' => $entryid)); $blogurl->param('userid', $user->id); if (!empty($course)) { $mycourseid = $course->id; $blogurl->param('courseid', $mycourseid); } else { $mycourseid = $site->id; } $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $PAGE->navbar->add($strblogentries, $blogurl); $blogurl->remove_params('userid'); $PAGE->navbar->add($entry->subject, $blogurl); $PAGE->set_title("{$shortname}: " . fullname($user) . ": {$entry->subject}"); $PAGE->set_heading("{$shortname}: " . fullname($user) . ": {$entry->subject}"); $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user)); // We ignore tag and search params if (empty($action) || !$CFG->useblogassociations) { $headers['url'] = $blogurl; return $headers; } } // Case 3: A user's blog entries if (!empty($userid) && empty($entryid) && (empty($courseid) && empty($modid) || !$CFG->useblogassociations)) { $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $blogurl->param('userid', $userid); $PAGE->set_title("{$shortname}: " . fullname($user) . ": " . get_string('blog', 'blog')); $PAGE->set_heading("{$shortname}: " . fullname($user) . ": " . get_string('blog', 'blog')); $headers['heading'] = get_string('userblog', 'blog', fullname($user)); $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user)); } else { // Case 4: No blog associations, no userid if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) { $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $PAGE->set_title("{$shortname}: " . get_string('blog', 'blog')); $PAGE->set_heading("{$shortname}: " . get_string('blog', 'blog')); $headers['heading'] = get_string('siteblog', 'blog', $shortname); } else { // Case 5: Blog entries associated with an activity by a specific user (courseid ignored) if (!empty($userid) && !empty($modid) && empty($entryid)) { $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $blogurl->param('userid', $userid); $blogurl->param('modid', $modid); // Course module navigation is handled by build_navigation as the second param $headers['cm'] = $cm; $PAGE->navbar->add(fullname($user), "{$CFG->wwwroot}/user/view.php?id={$user->id}"); $PAGE->navbar->add($strblogentries, $blogurl); $PAGE->set_title("{$shortname}: {$cm->name}: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); $PAGE->set_heading("{$shortname}: {$cm->name}: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); $a = new stdClass(); $a->user = fullname($user); $a->mod = $cm->name; $a->type = get_string('modulename', $cm->modname); $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); } else { // Case 6: Blog entries associated with a course by a specific user if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) { $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $blogurl->param('userid', $userid); $blogurl->param('courseid', $courseid); $PAGE->navbar->add($strblogentries, $blogurl); $PAGE->set_title("{$siteshortname}: {$courseshortname}: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); $PAGE->set_heading("{$siteshortname}: {$courseshortname}: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); $a = new stdClass(); $a->user = fullname($user); $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $a->type = get_string('course'); $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewblogentries', 'blog', $a); // Remove the userid from the URL to inform the blog_menu block correctly $blogurl->remove_params(array('userid')); } else { // Case 7: Blog entries by members of a group, associated with that group's course if (!empty($groupid) && empty($modid) && empty($entryid)) { $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $blogurl->param('courseid', $course->id); $PAGE->navbar->add($strblogentries, $blogurl); $blogurl->remove_params(array('courseid')); $blogurl->param('groupid', $groupid); $PAGE->navbar->add($group->name, $blogurl); $PAGE->set_title("{$siteshortname}: {$courseshortname}: " . get_string('blogentries', 'blog') . ": {$group->name}"); $PAGE->set_heading("{$siteshortname}: {$courseshortname}: " . get_string('blogentries', 'blog') . ": {$group->name}"); $a = new stdClass(); $a->group = $group->name; $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $a->type = get_string('course'); $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewblogentries', 'blog', $a); } else { // Case 8: Blog entries by members of a group, associated with an activity in that course if (!empty($groupid) && !empty($modid) && empty($entryid)) { $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $headers['cm'] = $cm; $blogurl->param('modid', $modid); $PAGE->navbar->add($strblogentries, $blogurl); $blogurl->param('groupid', $groupid); $PAGE->navbar->add($group->name, $blogurl); $PAGE->set_title("{$siteshortname}: {$courseshortname}: {$cm->name}: " . get_string('blogentries', 'blog') . ": {$group->name}"); $PAGE->set_heading("{$siteshortname}: {$courseshortname}: {$cm->name}: " . get_string('blogentries', 'blog') . ": {$group->name}"); $a = new stdClass(); $a->group = $group->name; $a->mod = $cm->name; $a->type = get_string('modulename', $cm->modname); $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); } else { // Case 9: All blog entries associated with an activity if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) { $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $PAGE->set_cm($cm, $course); $blogurl->param('modid', $modid); $PAGE->navbar->add($strblogentries, $blogurl); $PAGE->set_title("{$siteshortname}: {$courseshortname}: {$cm->name}: " . get_string('blogentries', 'blog')); $PAGE->set_heading("{$siteshortname}: {$courseshortname}: {$cm->name}: " . get_string('blogentries', 'blog')); $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name); $a = new stdClass(); $a->type = get_string('modulename', $cm->modname); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); } else { // Case 10: All blog entries associated with a course if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) { $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); $blogurl->param('courseid', $courseid); $PAGE->navbar->add($strblogentries, $blogurl); $PAGE->set_title("{$siteshortname}: {$courseshortname}: " . get_string('blogentries', 'blog')); $PAGE->set_heading("{$siteshortname}: {$courseshortname}: " . get_string('blogentries', 'blog')); $a = new stdClass(); $a->type = get_string('course'); $headers['heading'] = get_string('blogentriesabout', 'blog', format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)))); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewblogentries', 'blog', $a); $blogurl->remove_params(array('userid')); } } } } } } } } if (!in_array($action, array('edit', 'add'))) { // Append Tag info if (!empty($tagid)) { $headers['filters']['tag'] = $tagid; $blogurl->param('tagid', $tagid); $tagrec = $DB->get_record('tag', array('id' => $tagid)); $PAGE->navbar->add($tagrec->name, $blogurl); } elseif (!empty($tag)) { $blogurl->param('tag', $tag); $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl); } // Append Search info if (!empty($search)) { $headers['filters']['search'] = $search; $blogurl->param('search', $search); $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out()); } } // Append edit mode info if (!empty($action) && $action == 'add') { } else { if (!empty($action) && $action == 'edit') { $PAGE->navbar->add(get_string('editentry', 'blog')); } } if (empty($headers['url'])) { $headers['url'] = $blogurl; } return $headers; }
/** * Helper procedure/macro for installing remote plugins at admin/index.php * * Does not return, always redirects or exits. * * @param array $installable list of \core\update\remote_info * @param bool $confirmed false: display the validation screen, true: proceed installation * @param string $heading validation screen heading * @param moodle_url|string|null $continue URL to proceed with installation at the validation screen * @param moodle_url|string|null $return URL to go back on cancelling at the validation screen */ function upgrade_install_plugins(array $installable, $confirmed, $heading = '', $continue = null, $return = null) { global $CFG, $PAGE; if (empty($return)) { $return = $PAGE->url; } if (!empty($CFG->disableupdateautodeploy)) { redirect($return); } if (empty($installable)) { redirect($return); } $pluginman = core_plugin_manager::instance(); if ($confirmed) { // Installation confirmed at the validation results page. if (!$pluginman->install_plugins($installable, true, true)) { throw new moodle_exception('install_plugins_failed', 'core_plugin', $return); } // Always redirect to admin/index.php to perform the database upgrade. // Do not throw away the existing $PAGE->url parameters such as // confirmupgrade or confirmrelease if $PAGE->url is a superset of the // URL we must go to. $mustgoto = new moodle_url('/admin/index.php', array('cache' => 0, 'confirmplugincheck' => 0)); if ($mustgoto->compare($PAGE->url, URL_MATCH_PARAMS)) { redirect($PAGE->url); } else { redirect($mustgoto); } } else { $output = $PAGE->get_renderer('core', 'admin'); echo $output->header(); if ($heading) { echo $output->heading($heading, 3); } echo html_writer::start_tag('pre', array('class' => 'plugin-install-console')); $validated = $pluginman->install_plugins($installable, false, false); echo html_writer::end_tag('pre'); if ($validated) { echo $output->plugins_management_confirm_buttons($continue, $return); } else { echo $output->plugins_management_confirm_buttons(null, $return); } echo $output->footer(); die; } }
/** * Test the method mod_workshop_portfolio_caller::get_return_url() */ public function test_get_return_url() { $this->resetAfterTest(true); $student1 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id); $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop'); $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id); $portfoliocaller = new mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]); $portfoliocaller->set_formats_from_button([]); $portfoliocaller->load_data(); $expected = new moodle_url('/mod/workshop/submission.php', ['cmid' => $this->workshop->cm->id, 'id' => $subid1]); $actual = new moodle_url($portfoliocaller->get_return_url()); $this->assertTrue($expected->compare($actual)); }
/** * Override the URLs of the default popup_form, which only supports one base URL * @param array $options value=>label pairs representing select options * @return void */ public function override_option_values($options) { global $PAGE; $originalcount = count($options); $this->initialise_options(); $newcount = count($this->options); $first = true; reset($options); foreach ($this->options as $optkey => $optgroup) { if ($optgroup instanceof html_select_optgroup) { foreach ($optgroup->options as $key => $option) { next($options); $this->options[$optkey]->options[$key]->value = key($options); $optionurl = new moodle_url(key($options)); if ($optionurl->compare($PAGE->url, URL_MATCH_PARAMS)) { $this->options[$optkey]->options[$key]->selected = 'selected'; } } next($options); } else { if ($optgroup instanceof html_select_option && !($first && $originalcount < $newcount)) { $this->options[$optkey]->value = key($options); $optionurl = new moodle_url(key($options)); if ($optionurl->compare($PAGE->url, URL_MATCH_PARAMS)) { $this->options[$optkey]->selected = 'selected'; } next($options); } } $first = false; } }