Distinguish a page looking at css classes/ids

Re: Distinguish a page looking at css classes/ids

by Mark Sharp -
Number of replies: 0
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Not without some coding. In your theme you can add a page_init function to your theme's lib.php file (if your theme is called theme_yourtheme, then the function will be theme_yourtheme_page_init), and this passes in the moodle_page object just before it's rendered.

Inside that function you can use the $page->add_body_class() function to add your own classes. Here's an example:

function theme_yourtheme_page_init(moodle_page $page) {
    global $COURSE;
    $systemcontext = context_system::instance();
    $coursecontext = context_course::instance($COURSE->id);

    // Set up body classes to be used by scripts.
    if (isloggedin()) {
        $page->add_body_class('loggedin');
    }

    if (is_siteadmin()) {
        $page->add_body_class('systemrole-admin');
    }

    if ($roles = get_user_roles($coursecontext)) {
        foreach ($roles as $role) {
            $page->add_body_class('courserole-' . $role->shortname);
        }
    }
}

Remember, Moodle doesn't naturally have a concept for a student outside of a course, but if you have some other identifier (e.g. email address), then I suppose that could be used here.