Merge branch 'MDL-50079-master' of git://github.com/danpoltawski/moodle
[moodle.git] / course / reset_form.php
blob1662307f1d2f50a7386a2d3a03afe4fee4e38bc1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Provides the course_reset_form class.
20 * @package core
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once $CFG->libdir.'/formslib.php';
29 /**
30 * Defines the course reset settings form.
32 * @copyright 2007 Petr Skoda
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class course_reset_form extends moodleform {
36 function definition (){
37 global $CFG, $COURSE, $DB;
39 $mform =& $this->_form;
41 $mform->addElement('header', 'generalheader', get_string('general'));
43 $mform->addElement('date_selector', 'reset_start_date', get_string('startdate'), array('optional'=>true));
44 $mform->addHelpButton('reset_start_date', 'startdate');
45 $mform->addElement('checkbox', 'reset_events', get_string('deleteevents', 'calendar'));
46 $mform->addElement('checkbox', 'reset_notes', get_string('deletenotes', 'notes'));
47 $mform->addElement('checkbox', 'reset_comments', get_string('deleteallcomments', 'moodle'));
48 $mform->addElement('checkbox', 'reset_completion', get_string('deletecompletiondata', 'completion'));
49 $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
50 $mform->addHelpButton('delete_blog_associations', 'deleteblogassociations', 'blog');
53 $mform->addElement('header', 'rolesheader', get_string('roles'));
55 $roles = get_assignable_roles(context_course::instance($COURSE->id));
56 $roles[0] = get_string('noroles', 'role');
57 $roles = array_reverse($roles, true);
59 $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, array('multiple' => 'multiple'));
60 $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
61 $mform->setAdvanced('reset_roles_overrides');
62 $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
65 $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
67 $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
68 $mform->addHelpButton('reset_gradebook_items', 'removeallcourseitems', 'grades');
69 $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
70 $mform->addHelpButton('reset_gradebook_grades', 'removeallcoursegrades', 'grades');
71 $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
74 $mform->addElement('header', 'groupheader', get_string('groups'));
76 $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
77 $mform->setAdvanced('reset_groups_remove');
78 $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
79 $mform->setAdvanced('reset_groups_members');
80 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
82 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
83 $mform->setAdvanced('reset_groupings_remove');
84 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
85 $mform->setAdvanced('reset_groupings_members');
86 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
88 $unsupported_mods = array();
89 if ($allmods = $DB->get_records('modules') ) {
90 foreach ($allmods as $mod) {
91 $modname = $mod->name;
92 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
93 $mod_reset_course_form_definition = $modname.'_reset_course_form_definition';
94 $mod_reset__userdata = $modname.'_reset_userdata';
95 if (file_exists($modfile)) {
96 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
97 continue; // Skip mods with no instances
99 include_once($modfile);
100 if (function_exists($mod_reset_course_form_definition)) {
101 $mod_reset_course_form_definition($mform);
102 } else if (!function_exists($mod_reset__userdata)) {
103 $unsupported_mods[] = $mod;
105 } else {
106 debugging('Missing lib.php in '.$modname.' module');
110 // mention unsupported mods
111 if (!empty($unsupported_mods)) {
112 $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
113 foreach($unsupported_mods as $mod) {
114 $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
115 $mform->setAdvanced('unsup'.$mod->name);
119 $mform->addElement('hidden', 'id', $COURSE->id);
120 $mform->setType('id', PARAM_INT);
122 $buttonarray = array();
123 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
124 $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
125 $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
126 $buttonarray[] = &$mform->createElement('cancel');
127 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
128 $mform->closeHeaderBefore('buttonar');
131 function load_defaults() {
132 global $CFG, $COURSE, $DB;
134 $mform =& $this->_form;
136 $defaults = array ('reset_events'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
138 // Set student as default in unenrol user list, if role with student archetype exist.
139 if ($studentrole = get_archetype_roles('student')) {
140 $defaults['unenrol_users'] = array_keys($studentrole);
143 if ($allmods = $DB->get_records('modules') ) {
144 foreach ($allmods as $mod) {
145 $modname = $mod->name;
146 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
147 $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
148 if (file_exists($modfile)) {
149 @include_once($modfile);
150 if (function_exists($mod_reset_course_form_defaults)) {
151 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
152 $defaults = $defaults + $moddefs;
159 foreach ($defaults as $element=>$default) {
160 $mform->setDefault($element, $default);