開発:コースのリセット機能をモジュールに実装する

提供:MoodleDocs
移動先:案内検索

コースリセットのコードは /course/reset.php から呼び出されます。 あなたが作ったモジュールを含めるには,以下の2つの関数を実装する必要があります。 その方法を理解するのに良い例は,/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');
        }
    }
}