Moodle release 2.1.5
[moodle.git] / course / reset_form.php
blobce8b0f708b69361de9b69ef85ab25459be69f6c5
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
6 require_once $CFG->libdir.'/formslib.php';
8 class course_reset_form extends moodleform {
9 function definition (){
10 global $CFG, $COURSE, $DB;
12 $mform =& $this->_form;
14 $mform->addElement('header', 'generalheader', get_string('general'));
16 $mform->addElement('date_selector', 'reset_start_date', get_string('startdate'), array('optional'=>true));
17 $mform->addHelpButton('reset_start_date', 'startdate');
18 $mform->addElement('checkbox', 'reset_events', get_string('deleteevents', 'calendar'));
19 $mform->addElement('checkbox', 'reset_logs', get_string('deletelogs'));
20 $mform->addElement('checkbox', 'reset_notes', get_string('deletenotes', 'notes'));
21 $mform->addElement('checkbox', 'reset_comments', get_string('deleteallcomments', 'moodle'));
22 $mform->addElement('checkbox', 'reset_course_completion', get_string('deletecoursecompletiondata', 'completion'));
23 $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
24 $mform->addHelpButton('delete_blog_associations', 'deleteblogassociations', 'blog');
27 $mform->addElement('header', 'rolesheader', get_string('roles'));
29 $roles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, $COURSE->id));
30 $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, array('multiple' => 'multiple'));
31 $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
32 $mform->setAdvanced('reset_roles_overrides');
33 $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
36 $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
38 $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
39 $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
40 $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
43 $mform->addElement('header', 'groupheader', get_string('groups'));
45 $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
46 $mform->setAdvanced('reset_groups_remove');
47 $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
48 $mform->setAdvanced('reset_groups_members');
49 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
51 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
52 $mform->setAdvanced('reset_groupings_remove');
53 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
54 $mform->setAdvanced('reset_groupings_members');
55 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
57 $unsupported_mods = array();
58 if ($allmods = $DB->get_records('modules') ) {
59 foreach ($allmods as $mod) {
60 $modname = $mod->name;
61 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
62 continue; // skip mods with no instances
64 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
65 $mod_reset_course_form_definition = $modname.'_reset_course_form_definition';
66 $mod_reset__userdata = $modname.'_reset_userdata';
67 if (file_exists($modfile)) {
68 include_once($modfile);
69 if (function_exists($mod_reset_course_form_definition)) {
70 $mod_reset_course_form_definition($mform);
71 } else if (!function_exists($mod_reset__userdata)) {
72 $unsupported_mods[] = $mod;
74 } else {
75 debugging('Missing lib.php in '.$modname.' module');
79 // mention unsupported mods
80 if (!empty($unsupported_mods)) {
81 $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
82 foreach($unsupported_mods as $mod) {
83 $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
84 $mform->setAdvanced('unsup'.$mod->name);
88 $mform->addElement('hidden', 'id', $COURSE->id);
89 $mform->setType('id', PARAM_INT);
91 $buttonarray = array();
92 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
93 $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
94 $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
95 $buttonarray[] = &$mform->createElement('cancel');
96 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
97 $mform->closeHeaderBefore('buttonar');
100 function load_defaults() {
101 global $CFG, $COURSE, $DB;
103 $mform =& $this->_form;
105 $defaults = array ('reset_events'=>1, 'reset_logs'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
107 if ($allmods = $DB->get_records('modules') ) {
108 foreach ($allmods as $mod) {
109 $modname = $mod->name;
110 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
111 $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
112 if (file_exists($modfile)) {
113 @include_once($modfile);
114 if (function_exists($mod_reset_course_form_defaults)) {
115 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
116 $defaults = $defaults + $moddefs;
123 foreach ($defaults as $element=>$default) {
124 $mform->setDefault($element, $default);