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

Implementing Reset course functionality in a module

From MoodleDocs
Revision as of 10:19, 8 November 2018 by Séverin Terrier (talk | contribs) (Typos)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The course reset code is triggered from /course/reset.php. To have your module included, you need to implement the three functions described below. To work out how to do this, a good example is to look at the implementations in /mod/forum/lib.php


This is called directly by /course/reset.php. It needs to output some form controls to control different options for resetting your module. You should use Form API for create form.

The convention is to call settings relating your your module reset_mymodule_something.

The forum implementation is a good model:

 * Called by course/reset.php
function forum_reset_course_form_definition(&$mform) {
    $mform->addElement('header', 'forumheader', get_string('modulenameplural', 'forum'));

    $mform->addElement('checkbox', 'reset_forum_all', get_string('resetforumsall','forum'));

    $mform->addElement('select', 'reset_forum_types', get_string('resetforums', 'forum'), forum_get_forum_types_all(), array('multiple' => 'multiple'));
    $mform->disabledIf('reset_forum_types', 'reset_forum_all', 'checked');

    $mform->addElement('checkbox', 'reset_forum_subscriptions', get_string('resetsubscriptions','forum'));

    $mform->addElement('checkbox', 'reset_forum_track_prefs', get_string('resettrackprefs','forum'));
    $mform->disabledIf('reset_forum_track_prefs', 'reset_forum_all', 'checked');

    $mform->addElement('checkbox', 'reset_forum_ratings', get_string('deleteallratings'));
    $mform->disabledIf('reset_forum_ratings', 'reset_forum_all', 'checked');


This actually does the resetting. It is called indirectly, /course/reset.php calls reset_course_userdata() in /lib/moodlelib.php, which then calls the functions for each module.

The $data parameter is what came back from the form printed by forum_reset_course. In particular, $data->courseid is the id of the course we are cleaning up.

The forum implementation is again a good one to study, here is a very simple example:

function example_reset_userdata($data) {
    if (!empty($data->reset_example_frogs)) {
        if (delete_records('example_frogs', 'couresid', $data->courseid) and $showfeedback) {
            notify(get_string('frogsdeleted', 'example'), 'notifysuccess');
    if (!empty($data->reset_example_newts)) {
        if (delete_records('example_newts', 'couresid', $data->courseid) and $showfeedback) {
            notify(get_string('newtsdeleted', 'example'), 'notifysuccess');


Used for set default values to form's elements displayed by mymodule_reset_course_form_definition. The forum implementation:

function forum_reset_course_form_defaults($course) {
    return array('reset_forum_all'=>1, 'reset_forum_subscriptions'=>0, 'reset_forum_track_prefs'=>0, 'reset_forum_ratings'=>1);