Merge branch 'wip-MDL-62796-master' of git://github.com/marinaglancy/moodle
[moodle.git] / course / reset_form.php
blob1accadd6cfdfe8cfc3adb1cdc92320251f065e29
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';
28 require_once($CFG->dirroot . '/course/lib.php');
30 /**
31 * Defines the course reset settings form.
33 * @copyright 2007 Petr Skoda
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class course_reset_form extends moodleform {
37 function definition (){
38 global $CFG, $COURSE, $DB;
40 $mform =& $this->_form;
42 $mform->addElement('header', 'generalheader', get_string('general'));
44 $mform->addElement('date_time_selector', 'reset_start_date', get_string('startdate'), array('optional' => true));
45 $mform->addHelpButton('reset_start_date', 'startdate');
46 $mform->addElement('date_time_selector', 'reset_end_date', get_string('enddate'), array('optional' => true));
47 $mform->addHelpButton('reset_end_date', 'enddate');
48 $mform->addElement('checkbox', 'reset_events', get_string('deleteevents', 'calendar'));
49 $mform->addElement('checkbox', 'reset_notes', get_string('deletenotes', 'notes'));
50 $mform->addElement('checkbox', 'reset_comments', get_string('deleteallcomments', 'moodle'));
51 $mform->addElement('checkbox', 'reset_completion', get_string('deletecompletiondata', 'completion'));
52 $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
53 $mform->addHelpButton('delete_blog_associations', 'deleteblogassociations', 'blog');
54 $mform->addElement('checkbox', 'reset_competency_ratings', get_string('deletecompetencyratings', 'core_competency'));
56 $mform->addElement('header', 'rolesheader', get_string('roles'));
58 $roles = get_assignable_roles(context_course::instance($COURSE->id));
59 $roles[0] = get_string('noroles', 'role');
60 $roles = array_reverse($roles, true);
62 $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, array('multiple' => 'multiple'));
63 $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
64 $mform->setAdvanced('reset_roles_overrides');
65 $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
68 $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
70 $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
71 $mform->addHelpButton('reset_gradebook_items', 'removeallcourseitems', 'grades');
72 $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
73 $mform->addHelpButton('reset_gradebook_grades', 'removeallcoursegrades', 'grades');
74 $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
77 $mform->addElement('header', 'groupheader', get_string('groups'));
79 $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
80 $mform->setAdvanced('reset_groups_remove');
81 $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
82 $mform->setAdvanced('reset_groups_members');
83 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
85 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
86 $mform->setAdvanced('reset_groupings_remove');
87 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
88 $mform->setAdvanced('reset_groupings_members');
89 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
91 $unsupported_mods = array();
92 if ($allmods = $DB->get_records('modules') ) {
93 foreach ($allmods as $mod) {
94 $modname = $mod->name;
95 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
96 $mod_reset_course_form_definition = $modname.'_reset_course_form_definition';
97 $mod_reset__userdata = $modname.'_reset_userdata';
98 if (file_exists($modfile)) {
99 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
100 continue; // Skip mods with no instances
102 include_once($modfile);
103 if (function_exists($mod_reset_course_form_definition)) {
104 $mod_reset_course_form_definition($mform);
105 } else if (!function_exists($mod_reset__userdata)) {
106 $unsupported_mods[] = $mod;
108 } else {
109 debugging('Missing lib.php in '.$modname.' module');
113 // mention unsupported mods
114 if (!empty($unsupported_mods)) {
115 $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
116 foreach($unsupported_mods as $mod) {
117 $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
118 $mform->setAdvanced('unsup'.$mod->name);
122 $mform->addElement('hidden', 'id', $COURSE->id);
123 $mform->setType('id', PARAM_INT);
125 $buttonarray = array();
126 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
127 $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
128 $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
129 $buttonarray[] = &$mform->createElement('cancel');
130 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
131 $mform->closeHeaderBefore('buttonar');
134 function load_defaults() {
135 global $CFG, $COURSE, $DB;
137 $mform =& $this->_form;
139 $defaults = array ('reset_events'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
141 // Set student as default in unenrol user list, if role with student archetype exist.
142 if ($studentrole = get_archetype_roles('student')) {
143 $defaults['unenrol_users'] = array_keys($studentrole);
146 if ($allmods = $DB->get_records('modules') ) {
147 foreach ($allmods as $mod) {
148 $modname = $mod->name;
149 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
150 $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
151 if (file_exists($modfile)) {
152 @include_once($modfile);
153 if (function_exists($mod_reset_course_form_defaults)) {
154 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
155 $defaults = $defaults + $moddefs;
162 foreach ($defaults as $element=>$default) {
163 $mform->setDefault($element, $default);
168 * Validation.
170 * @param array $data
171 * @param array $files
172 * @return array the errors that were found
174 public function validation($data, $files) {
175 global $DB;
177 $course = get_course($data['id']);
179 $errors = parent::validation($data, $files);
181 // We check the values that would be used as start and end.
182 if ($data['reset_start_date'] != 0) {
183 $coursedata['startdate'] = $data['reset_start_date'];
184 } else {
185 $coursedata['startdate'] = $course->startdate;
188 if ($data['reset_end_date'] != 0) {
189 // End date set by the user has preference.
190 $coursedata['enddate'] = $data['reset_end_date'];
191 } else if ($data['reset_start_date'] > 0 && $course->enddate != 0) {
192 // Otherwise, if the current course enddate is set, reset_course_userdata will add the start date time shift to it.
193 $timeshift = $data['reset_start_date'] - usergetmidnight($course->startdate);
194 $coursedata['enddate'] = $course->enddate + $timeshift;
195 } else {
196 $coursedata['enddate'] = $course->enddate;
199 if ($errorcode = course_validate_dates($coursedata)) {
200 $errors['reset_end_date'] = get_string($errorcode, 'error');
203 return $errors;