lib/weblib.php

Jump to: navigation, search

This library is largely concerned with generating output.

There are lots of different things in this library, and so far, only a very few parts have been documented. However, remember:

  • The code should be well written and easy to understand. Want to know what a particular function does? Well, read the code. Unlike documentation, the code does not get out of date.
  • The generated phpDocumentor output should give brief details about each function.

Documentation of particular bits

function build_navigation

You need to call this function before calling print_header(_simple) to create the $navigation parameter. There is an extensive PHPdocumentor comment in the code that explains usage.

Typical examples are:

From mod/quiz/attempt.php:
        $navigation = build_navigation($strattemptnum, $cm);
        print_header_simple(format_string($quiz->name), "", $navigation, ... )
 
From mod/forum/post.php:
        $navlinks = array();
        $navlinks[] = array('name' => format_string($post->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
        $navlinks[] = array('name' => get_string("prune", "forum"), 'link' => '', 'type' => 'title');
        $navigation = build_navigation($navlinks, $cm);
 
        print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "", $navigation, ... )

function is_https

This function must be used when determining if the site is currently served over HTTPS. This keeps the way https is treated consistent accross moodle.

Example from auth/shibboleth/logout.php.

// Find out whether host supports https
$protocol = 'http://';
if (is_https()) {
    $protocol = 'https://';
}

(Necessary because browsers give security warnings if an https page includes images or scripts from a non-https URL. Note that, it many cases, you should be using moodle_url, not string concatenation, to build URLs, but still this function has its place.)

function get_local_referer

This function should be used to get the cleaned local URL of the $_SERVER['HTTP_REFERER'] less the URL query string parameters if required.

If you need to get an external referer, you can do so by using clean_param($_SERVER['HTTP_REFERER'], PARAM_URL)

and optionally stripquerystring(), but it is not recommended to use it on moodle core development due security issues.

<?php
// Given the previous URL you came is http://previous.url.com?id=1&param=value
echo get_local_referer(); 
// outputs http://previous.url.com
 
// If you want to keep the query part of the http referer
echo get_local_referer(false);
http://previous.url.com?id=1&param=value

If the page is viewed directly the $_SERVER['HTTP_REFERER'] is not setted and get_local_referer() would return an empty string.

See also