Note:

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: Difference between revisions

From MoodleDocs
(Metods renamed, using Form API)
mNo edit summary
Line 57: Line 57:
</nowiki></pre>
</nowiki></pre>


==''mymodule''reset_course_form_defaults($course)==
==''mymodule''_reset_course_form_defaults($course)==


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

Revision as of 20:26, 9 November 2008

The course reset code is triggered from /course/reset.php. To have your module included, you need to implement the three 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_definition(&$mform)

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->setAdvanced('reset_forum_types');
    $mform->disabledIf('reset_forum_types', 'reset_forum_all', 'checked');

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

    $mform->addElement('checkbox', 'reset_forum_track_prefs', get_string('resettrackprefs','forum'));
    $mform->setAdvanced('reset_forum_track_prefs');
    $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');
}

mymodule_reset_userdata($data)

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 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');
        }
    }
}

mymodule_reset_course_form_defaults($course)

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);
}