class url_support

internal  
 

Utility functions for Moodle URLs and redirection.

Provides a centralized interface for creating Moodle URLs with proper error handling and consistent parameter management.

Methods

static moodle_url
get(moodle_url|string $url, array|null $params = null, string|null $anchor = null, int $strictness = IGNORE_MISSING)

Safely creates a Moodle URL object.

static moodle_url
course(int $courseid, array $params = [], string|null $anchor = null)

Retrieves a course view URL.

static moodle_url
module(int $cmid, array $params = [], string|null $anchor = null)

Retrieves a course module (activity) URL.

static moodle_url
user_profile(int $userid, int|null $courseid = null, array $params = [], string|null $anchor = null)

Retrieves a user profile URL.

static moodle_url
pluginfile(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename, bool $forcedownload = false, string|null $preview = null)

Retrieves a pluginfile URL for serving files.

static moodle_url
image_url(string $imagename, string $component = middag_helper::COMPONENT_NAME)

Retrieves a component image URL from the theme.

static moodle_url
admin_settings(string $section, array $params = [], string|null $anchor = null)

Retrieves a Moodle admin settings page URL.

static moodle_url
home(array $params = [], string|null $anchor = null)

Retrieves the site home page URL.

static moodle_url
dashboard(array $params = [], string|null $anchor = null)

Retrieves the user dashboard URL.

static moodle_url
grade_report(int $courseid, string $report = 'user', array $params = [], string|null $anchor = null)

Retrieves a grade report URL.

static string
to_absolute(string $relativeurl)

Converts a relative URL to an absolute URL.

static bool
is_external(string $url)

Checks if a URL is external to the current Moodle site.

static void
redirect(moodle_url|string $url, string $message = '', int $delay = 0, string $messagetype = 'notifysuccess')

Redirects the user to a specific URL.

Details

at line 48
static moodle_url get(moodle_url|string $url, array|null $params = null, string|null $anchor = null, int $strictness = IGNORE_MISSING)

Safely creates a Moodle URL object.

Parameters

moodle_url|string $url

URL path or moodle_url object

array|null $params

optional query parameters

string|null $anchor

optional anchor fragment

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

moodle_url

generated URL object

Exceptions

moodle_exception

at line 84
static moodle_url course(int $courseid, array $params = [], string|null $anchor = null)

Retrieves a course view URL.

Parameters

int $courseid

Course ID

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

Course URL

at line 101
static moodle_url module(int $cmid, array $params = [], string|null $anchor = null)

Retrieves a course module (activity) URL.

Parameters

int $cmid

Course Module ID (cmid)

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

Module URL

at line 119
static moodle_url user_profile(int $userid, int|null $courseid = null, array $params = [], string|null $anchor = null)

Retrieves a user profile URL.

Parameters

int $userid

User ID

int|null $courseid

Optional course context ID

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

user profile URL

at line 151
static moodle_url pluginfile(int $contextid, string $component, string $filearea, int $itemid, string $filepath, string $filename, bool $forcedownload = false, string|null $preview = null)

Retrieves a pluginfile URL for serving files.

This is the proper way to generate URLs for files stored in Moodle's file storage.

Parameters

int $contextid

Context ID

string $component

Component name (e.g., 'local_middag')

string $filearea

File area name

int $itemid

Item ID

string $filepath

File path (e.g., '/')

string $filename

File name

bool $forcedownload

Force download instead of display

string|null $preview

Preview mode ('thumb', 'tinyicon', 'bigicon')

Return Value

moodle_url

pluginfile URL

at line 186
static moodle_url image_url(string $imagename, string $component = middag_helper::COMPONENT_NAME)

Retrieves a component image URL from the theme.

Parameters

string $imagename

Image file name (without extension)

string $component

Component name

Return Value

moodle_url

image URL

Exceptions

Exception

at line 208
static moodle_url admin_settings(string $section, array $params = [], string|null $anchor = null)

Retrieves a Moodle admin settings page URL.

Parameters

string $section

Settings section name

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

admin settings URL

at line 224
static moodle_url home(array $params = [], string|null $anchor = null)

Retrieves the site home page URL.

Parameters

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

home page URL

at line 237
static moodle_url dashboard(array $params = [], string|null $anchor = null)

Retrieves the user dashboard URL.

Parameters

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

dashboard URL

at line 252
static moodle_url grade_report(int $courseid, string $report = 'user', array $params = [], string|null $anchor = null)

Retrieves a grade report URL.

Parameters

int $courseid

Course ID

string $report

Report type (e.g., 'user', 'grader')

array $params

Additional query parameters

string|null $anchor

optional anchor fragment

Return Value

moodle_url

grade report URL

at line 271
static string to_absolute(string $relativeurl)

Converts a relative URL to an absolute URL.

Parameters

string $relativeurl

Relative URL path

Return Value

string

Absolute URL

at line 293
static bool is_external(string $url)

Checks if a URL is external to the current Moodle site.

Parameters

string $url

URL to check

Return Value

bool

True if external, false if internal

at line 318
static void redirect(moodle_url|string $url, string $message = '', int $delay = 0, string $messagetype = 'notifysuccess')

Redirects the user to a specific URL.

Parameters

moodle_url|string $url

Destination URL

string $message

Optional redirection message

int $delay

Delay in seconds before redirection

string $messagetype

Moodle notification type

Return Value

void