Development:Module visibility and display

From MoodleDocs

Template:Moodle 2.0

The features described here are available since Moodle 2.0.2.

Summary

A new API allows you to customise how your module displays on the main course page:

  • You can display custom HTML below the link to your module.
  • If your module does not have a link (like Label, where it is only for display on the main page) then you can remove the link from the main page and from all navigation etc.
  • You can display HTML next to the link to your module that indicates dynamic information (like Forum, where it displays information about unread messages).
  • You can display additional icons next to the other module editing icons when the user is editing the page.

In addition, existing things you could already do (like change the icon on the main page) are still available when using the new API.

Backward compatibility

All modules and code written for Moodle 2.0 should continue to behave in exactly the same manner. There is no need to change existing modules for this API.

Customising module display, in course cache: _get_coursemodule_info

The first place you can customise your module display is in the existing _get_coursemodule_info API function. This function obtains information about the module which will be stored in the course cache (the modinfo field of the course table).

The course cache is only updated when somebody edits a module, so it can't be used for dynamic information - but it's okay if it takes a few database queries to calculate the data because it will be cached for future use.

The function should return a value of class cached_cm_info. For example:

function mod_frog_get_coursemodule_info($cm) {

   $info = new cached_cm_info;

$info->content = '

This will display below the module.

';

   return $info;

}

You can change several properties which are documented in that class definition. If you don't change a property, its value remains default.

  • name - name of activity (text of the link on course page).
  • icon, iconcomponent - name and component name of icon to display by the link.
  • content - extra HTML content to display below the module link on course page (not shown in navigation etc).
  • customdata - arbitrary extra PHP data to store in modinfo cache; useful if, for performance reasons, your module needs to store data that should be accessible very quickly from other parts of the course.
  • extraclasses - extra CSS class or classes that will be added to the activity on the main page, so that you can alter the styling.
  • onclick - already-escaped HTML that will be inserted as the value of the onclick attribute.