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
m (link to Japanese)
m (Typos)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
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
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


==''mymodule''_reset_course_form($course)==
==''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.
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 convention is to call settings relating your your module reset_''mymodule''_''something''.
Line 13: Line 13:
  * Called by course/reset.php
  * Called by course/reset.php
  */
  */
function forum_reset_course_form($course) {
function forum_reset_course_form_definition(&$mform) {
     echo get_string('resetforums', 'forum'); echo ':<br />';
     $mform->addElement('header', 'forumheader', get_string('modulenameplural', 'forum'));
     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 />';
     $mform->addElement('checkbox', 'reset_forum_all', get_string('resetforumsall','forum'));
     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 />';
     $mform->addElement('select', 'reset_forum_types', get_string('resetforums', 'forum'), forum_get_forum_types_all(), array('multiple' => 'multiple'));
     echo '<p>';
    $mform->setAdvanced('reset_forum_types');
     print_checkbox('reset_forum_subscriptions', 1, true, get_string('resetsubscriptions','forum'), '', '');
     $mform->disabledIf('reset_forum_types', 'reset_forum_all', 'checked');
     echo '</p>';
 
    $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');
}
}
</nowiki></pre>
</nowiki></pre>


==''mymodule''_delete_userdata($data, $showfeedback)==
==''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.
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 $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 $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:
The forum implementation is again a good one to study, here is a very simple example:


<pre><nowiki>
<pre><nowiki>
function example_delete_userdata($data, $showfeedback=true) {
function example_reset_userdata($data) {
     if (!empty($data->reset_example_frogs)) {
     if (!empty($data->reset_example_frogs)) {
         if (delete_records('example_frogs', 'couresid', $data->courseid) and $showfeedback) {
         if (delete_records('example_frogs', 'couresid', $data->courseid) and $showfeedback) {
Line 47: Line 54:
         }
         }
     }
     }
}
</nowiki></pre>
==''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:
<pre><nowiki>
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);
}
}
</nowiki></pre>
</nowiki></pre>


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

Latest revision as of 10:19, 8 November 2018

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

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

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