Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Activity completion API

From MoodleDocs

Moodle 2.0


Modules do not need to be changed to support conditional availability, but they do need changing to support the completion system.

If you make no changes to a module whatsoever, it can only support 'manual' completion (where the user ticks a box).

Feature support

To support the completion system, your module must include a modulename_supports function in its lib.php. Here is an example:

/**
 * Indicates API features that the forum supports.
 *
 * @param string $feature
 * @return mixed True if yes (some features may use other values)
 */
function forum_supports($feature) {
    switch($feature) {
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
        case FEATURE_COMPLETION_HAS_RULES: return true;
        default: return null;
    }
}

The relevant features for completion are:

  • FEATURE_COMPLETION_TRACKS_VIEWS - the module can support completion 'on view', meaning that an activity becomes marked complete as soon as a user clicks on it.
  • FEATURE_GRADE_HAS_GRADE - the module provides (or may provide, depending on settings) a grade for students. When a module supports grades, it can support completion 'on grade', meaning that an activity becomes marked complete as soon as a user is assigned a grade.
  • FEATURE_COMPLETION_HAS_RULES - the module has custom completion rules.

Completion on view

Completion on view means that, if selected, an activity is marked complete as soon as the user views it. 'View' is usually defined as seeing the module's main page; if you click on the activity, and there isn't an error, you have probably viewed it. However it is up to each module precisely how they define 'view'.

How to implement

In your module's _supports function, return true for FEATURE_COMPLETION_TRACKS_VIEWS.

Then add this code to run whenever a user successfully views the activity (for example, near the print_footer() call in view.php):

$completion=new completion_info($course);
$completion->set_module_viewed($cm);

Performance issues

Calling this method has no significant performance cost if 'on view' completion is not enabled for the activity. If it is enabled, then the performance cost is kept low because the 'viewed' state is cached; it doesn't add a database query to every request.

Completion on grade

Completion on grade means that, if selected, an activity is marked complete as soon as the user receives a grade from that activity.

How to implement

In your module's _supports function, return true for FEATURE_GRADE_HAS_GRADE. No other action is necessary.

Performance issues

When 'on grade' completion is enabled, there will be some additional database queries after a grade is assigned or changed. Unless your activity changes grades very frequently, this is unlikely to be an issue.

Custom completion rules

Custom completion rules allow for module-specific conditions. For example, the forum has custom rules so that you can make it mark a user completed when they make a certain number of posts to the forum.

It is a lot harder to implement custom completion rules than it is to use the system-provided 'view' or 'grade' conditions, but the instructions below should help make it clear.

How to implement

To implement custom completion rules, you need to:

  1. Return true for FEATURE_COMPLETION_HAS_RULES in your module's _supports function.
  2. Add database fields to your module's main table to store the custom completion settings.
  3. Add controls to your module's settings form so that users can select the custom rules, altering these database settings.
  4. Add a function that checks the value of these rules (if set).
  5. Add code so that whenever the value affecting a rule might change, you inform the completion system.

Database fields for completion settings

When you provide a custom completion rule for a module, that rule requires data to be stored with each module instance: whether the rule is enabled for that instance, and any options that apply to the rule.

Usually the best place to store this information is your module's main table because:

  • The information in the relevant row of this table is likely to be available in most parts of your code, so code changes are minimised.
  • You already read this row with most requests, so there is no need for additional database queries which would reduce performance.
  • The main table is used for most other module options so it is a logical place for this information.

If you are adding a basic completion condition you probably only need to add one field. To add a field to an existing module, you need to change the install.xml and then the update.php in the same way as adding any other field.

Example

Throughout this section I am using the forum as an example. The forum provides three completion options but because they all behave the same way, I am only showing one of them.

The forum adds this field to store a completion option:

  • completionposts - this may be 0 or an integer. If it's an integer, say 3, then the user needs to add 3 forum posts (either new discussions or replies) in order for the forum to count as 'completed'.

Form changes for completion settings

Completion value function

Notifying the completion system