Merge branch 'wip-mdl-34190' of git://github.com/rajeshtaneja/moodle
[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'));
c343ddf8 21 $mform->addElement('checkbox', 'reset_comments', get_string('deleteallcomments', 'moodle'));
5ae17b09 22 $mform->addElement('checkbox', 'reset_completion', get_string('deletecompletiondata', 'completion'));
5a5cd650 23 $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
2650f44d 24 $mform->addHelpButton('delete_blog_associations', 'deleteblogassociations', 'blog');
0b5a80a1 25
26
27 $mform->addElement('header', 'rolesheader', get_string('roles'));
28
9a5e297b 29 $roles = get_assignable_roles(context_course::instance($COURSE->id));
2e15b12b
PS
30 $roles[0] = get_string('noroles', 'role');
31 $roles = array_reverse($roles, true);
32
df997f84 33 $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, array('multiple' => 'multiple'));
0b5a80a1 34 $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
35 $mform->setAdvanced('reset_roles_overrides');
36 $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
37
38
39 $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
40
41 $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
42 $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
43 $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
44
45
46 $mform->addElement('header', 'groupheader', get_string('groups'));
47
48 $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
49 $mform->setAdvanced('reset_groups_remove');
50 $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
51 $mform->setAdvanced('reset_groups_members');
52 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
53
98da6021
PS
54 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
55 $mform->setAdvanced('reset_groupings_remove');
56 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
57 $mform->setAdvanced('reset_groupings_members');
58 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
0b5a80a1 59
60 $unsupported_mods = array();
6bb08163 61 if ($allmods = $DB->get_records('modules') ) {
0b5a80a1 62 foreach ($allmods as $mod) {
63 $modname = $mod->name;
0b5a80a1 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)) {
30338539
TL
68 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
69 continue; // Skip mods with no instances
70 }
0b5a80a1 71 include_once($modfile);
72 if (function_exists($mod_reset_course_form_definition)) {
73 $mod_reset_course_form_definition($mform);
74 } else if (!function_exists($mod_reset__userdata)) {
75 $unsupported_mods[] = $mod;
76 }
77 } else {
78 debugging('Missing lib.php in '.$modname.' module');
79 }
80 }
81 }
82 // mention unsupported mods
83 if (!empty($unsupported_mods)) {
84 $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
85 foreach($unsupported_mods as $mod) {
86 $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
87 $mform->setAdvanced('unsup'.$mod->name);
88 }
89 }
90
91 $mform->addElement('hidden', 'id', $COURSE->id);
d18e0fe6 92 $mform->setType('id', PARAM_INT);
0b5a80a1 93
94 $buttonarray = array();
95 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
96 $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
97 $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
98 $buttonarray[] = &$mform->createElement('cancel');
99 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
100 $mform->closeHeaderBefore('buttonar');
101 }
102
103 function load_defaults() {
6bb08163 104 global $CFG, $COURSE, $DB;
0b5a80a1 105
106 $mform =& $this->_form;
107
108 $defaults = array ('reset_events'=>1, 'reset_logs'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
109
b2da6472
RT
110 // Set student as default in unenrol user list, if role with student archetype exist.
111 if ($studentrole = get_archetype_roles('student')) {
112 $defaults['unenrol_users'] = array_keys($studentrole);
113 }
114
6bb08163 115 if ($allmods = $DB->get_records('modules') ) {
0b5a80a1 116 foreach ($allmods as $mod) {
117 $modname = $mod->name;
118 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
119 $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
120 if (file_exists($modfile)) {
121 @include_once($modfile);
122 if (function_exists($mod_reset_course_form_defaults)) {
97481934 123 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
124 $defaults = $defaults + $moddefs;
125 }
0b5a80a1 126 }
127 }
128 }
129 }
130
131 foreach ($defaults as $element=>$default) {
132 $mform->setDefault($element, $default);
133 }
134 }
135}