MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / course / reset_form.php
CommitLineData
aa6c1ced 1<?php
bfebaf64
MD
2if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
4}
5
0b5a80a1 6require_once $CFG->libdir.'/formslib.php';
7
8class course_reset_form extends moodleform {
9 function definition (){
6bb08163 10 global $CFG, $COURSE, $DB;
0b5a80a1 11
12 $mform =& $this->_form;
13
14 $mform->addElement('header', 'generalheader', get_string('general'));
15
16 $mform->addElement('date_selector', 'reset_start_date', get_string('startdate'), array('optional'=>true));
5fb88219 17 $mform->addHelpButton('reset_start_date', 'startdate');
0b5a80a1 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'));
5a5cd650
NC
21 $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
22 $mform->setHelpButton('delete_blog_associations', array('deleteblogassociations', get_string('deleteblogassociations', 'blog'), 'blog'));
0b5a80a1 23
24
25 $mform->addElement('header', 'rolesheader', get_string('roles'));
26
27 $roles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, $COURSE->id));
28 $mform->addElement('select', 'reset_roles', get_string('unenrolroleusers'), $roles, array('multiple' => 'multiple'));
29 $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
30 $mform->setAdvanced('reset_roles_overrides');
31 $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
32
33
34 $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
35
36 $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
37 $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
38 $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
39
40
41 $mform->addElement('header', 'groupheader', get_string('groups'));
42
43 $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
44 $mform->setAdvanced('reset_groups_remove');
45 $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
46 $mform->setAdvanced('reset_groups_members');
47 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
48
98da6021
PS
49 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
50 $mform->setAdvanced('reset_groupings_remove');
51 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
52 $mform->setAdvanced('reset_groupings_members');
53 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
0b5a80a1 54
55 $unsupported_mods = array();
6bb08163 56 if ($allmods = $DB->get_records('modules') ) {
0b5a80a1 57 foreach ($allmods as $mod) {
58 $modname = $mod->name;
c7da4357 59 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
0b5a80a1 60 continue; // skip mods with no instances
61 }
62 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
63 $mod_reset_course_form_definition = $modname.'_reset_course_form_definition';
64 $mod_reset__userdata = $modname.'_reset_userdata';
65 if (file_exists($modfile)) {
66 include_once($modfile);
67 if (function_exists($mod_reset_course_form_definition)) {
68 $mod_reset_course_form_definition($mform);
69 } else if (!function_exists($mod_reset__userdata)) {
70 $unsupported_mods[] = $mod;
71 }
72 } else {
73 debugging('Missing lib.php in '.$modname.' module');
74 }
75 }
76 }
77 // mention unsupported mods
78 if (!empty($unsupported_mods)) {
79 $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
80 foreach($unsupported_mods as $mod) {
81 $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
82 $mform->setAdvanced('unsup'.$mod->name);
83 }
84 }
85
86 $mform->addElement('hidden', 'id', $COURSE->id);
d18e0fe6 87 $mform->setType('id', PARAM_INT);
0b5a80a1 88
89 $buttonarray = array();
90 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
91 $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
92 $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
93 $buttonarray[] = &$mform->createElement('cancel');
94 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
95 $mform->closeHeaderBefore('buttonar');
96 }
97
98 function load_defaults() {
6bb08163 99 global $CFG, $COURSE, $DB;
0b5a80a1 100
101 $mform =& $this->_form;
102
103 $defaults = array ('reset_events'=>1, 'reset_logs'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
104
105 if (!empty($COURSE->defaultrole)) {
106 $defaults['reset_roles'] = array($COURSE->defaultrole);
107 } else {
108 $defaults['reset_roles'] = array($CFG->defaultcourseroleid);
109 }
110
6bb08163 111 if ($allmods = $DB->get_records('modules') ) {
0b5a80a1 112 foreach ($allmods as $mod) {
113 $modname = $mod->name;
114 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
115 $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
116 if (file_exists($modfile)) {
117 @include_once($modfile);
118 if (function_exists($mod_reset_course_form_defaults)) {
97481934 119 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
120 $defaults = $defaults + $moddefs;
121 }
0b5a80a1 122 }
123 }
124 }
125 }
126
127 foreach ($defaults as $element=>$default) {
128 $mform->setDefault($element, $default);
129 }
130 }
131}