Merge branch 'MDL-74350-master' of https://github.com/HuongNV13/moodle
[moodle.git] / course / reset_form.php
blob23fe7f83164c8996d9c23bdc6632c8fb1d98393a
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->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
81 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
83 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
84 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
85 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
87 $unsupported_mods = array();
88 if ($allmods = $DB->get_records('modules') ) {
89 foreach ($allmods as $mod) {
90 $modname = $mod->name;
91 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
92 $mod_reset_course_form_definition = $modname.'_reset_course_form_definition';
93 $mod_reset__userdata = $modname.'_reset_userdata';
94 if (file_exists($modfile)) {
95 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
96 continue; // Skip mods with no instances
98 include_once($modfile);
99 if (function_exists($mod_reset_course_form_definition)) {
100 $mod_reset_course_form_definition($mform);
101 } else if (!function_exists($mod_reset__userdata)) {
102 $unsupported_mods[] = $mod;
104 } else {
105 debugging('Missing lib.php in '.$modname.' module');
109 // mention unsupported mods
110 if (!empty($unsupported_mods)) {
111 $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
112 foreach($unsupported_mods as $mod) {
113 $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
114 $mform->setAdvanced('unsup'.$mod->name);
118 $mform->addElement('hidden', 'id', $COURSE->id);
119 $mform->setType('id', PARAM_INT);
121 $buttonarray = array();
122 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
123 $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
124 $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
125 $buttonarray[] = &$mform->createElement('cancel');
126 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
127 $mform->closeHeaderBefore('buttonar');
130 function load_defaults() {
131 global $CFG, $COURSE, $DB;
133 $mform =& $this->_form;
135 $defaults = array ('reset_events'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
137 // Set student as default in unenrol user list, if role with student archetype exist.
138 if ($studentrole = get_archetype_roles('student')) {
139 $defaults['unenrol_users'] = array_keys($studentrole);
142 if ($allmods = $DB->get_records('modules') ) {
143 foreach ($allmods as $mod) {
144 $modname = $mod->name;
145 $modfile = $CFG->dirroot."/mod/$modname/lib.php";
146 $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
147 if (file_exists($modfile)) {
148 @include_once($modfile);
149 if (function_exists($mod_reset_course_form_defaults)) {
150 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
151 $defaults = $defaults + $moddefs;
158 foreach ($defaults as $element=>$default) {
159 $mform->setDefault($element, $default);
164 * Validation.
166 * @param array $data
167 * @param array $files
168 * @return array the errors that were found
170 public function validation($data, $files) {
171 global $DB;
173 $course = get_course($data['id']);
175 $errors = parent::validation($data, $files);
177 // We check the values that would be used as start and end.
178 if ($data['reset_start_date'] != 0) {
179 $coursedata['startdate'] = $data['reset_start_date'];
180 } else {
181 $coursedata['startdate'] = $course->startdate;
184 if ($data['reset_end_date'] != 0) {
185 // End date set by the user has preference.
186 $coursedata['enddate'] = $data['reset_end_date'];
187 } else if ($data['reset_start_date'] > 0 && $course->enddate != 0) {
188 // Otherwise, if the current course enddate is set, reset_course_userdata will add the start date time shift to it.
189 $timeshift = $data['reset_start_date'] - usergetmidnight($course->startdate);
190 $coursedata['enddate'] = $course->enddate + $timeshift;
191 } else {
192 $coursedata['enddate'] = $course->enddate;
195 if ($errorcode = course_validate_dates($coursedata)) {
196 $errors['reset_end_date'] = get_string($errorcode, 'error');
199 return $errors;