Note: You are currently viewing documentation for Moodle 4.0. Up-to-date documentation for the latest stable version of Moodle may be available here: Implementing Reset course functionality in a module.

Development:Implementing Reset course functionality in a module

From MoodleDocs
Revision as of 18:28, 12 September 2008 by Takayuki ISHIKAWA (talk | contribs) (link to Japanese)

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

mymodule_reset_course_form($course)

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

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($course) {
    echo get_string('resetforums', 'forum'); echo ':<br />';
    print_checkbox('reset_forum_news', 1, true, get_string('namenews','forum'), '', '');  echo '<br />';
    print_checkbox('reset_forum_single', 1, true, get_string('singleforum','forum'), '', '');  echo '<br />';
    print_checkbox('reset_forum_eachuser', 1, true, get_string('eachuserforum','forum'), '', '');  echo '<br />';
    print_checkbox('reset_forum_general', 1, true, get_string('generalforum','forum'), '', '');  echo '<br />';
    echo '<p>';
    print_checkbox('reset_forum_subscriptions', 1, true, get_string('resetsubscriptions','forum'), '', '');
    echo '</p>';
}

mymodule_delete_userdata($data, $showfeedback)

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 coures we are cleaning up.

The $showfeedback parameter determines whether we should output progress information as we go along.

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

function example_delete_userdata($data, $showfeedback=true) {
    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');
        }
    }
}