MDL-67264 core_course: Begin set up for Activity chooser
[moodle.git] / course / reset_form.php
CommitLineData
aa6c1ced 1<?php
e04a0f6b
DM
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.
13//
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/>.
16
17/**
18 * Provides the course_reset_form class.
19 *
20 * @package core
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
bfebaf64 26
0b5a80a1 27require_once $CFG->libdir.'/formslib.php';
8643c576 28require_once($CFG->dirroot . '/course/lib.php');
0b5a80a1 29
e04a0f6b
DM
30/**
31 * Defines the course reset settings form.
32 *
33 * @copyright 2007 Petr Skoda
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
0b5a80a1 36class course_reset_form extends moodleform {
37 function definition (){
6bb08163 38 global $CFG, $COURSE, $DB;
0b5a80a1 39
40 $mform =& $this->_form;
41
42 $mform->addElement('header', 'generalheader', get_string('general'));
43
57422d71 44 $mform->addElement('date_time_selector', 'reset_start_date', get_string('startdate'), array('optional' => true));
5fb88219 45 $mform->addHelpButton('reset_start_date', 'startdate');
57422d71 46 $mform->addElement('date_time_selector', 'reset_end_date', get_string('enddate'), array('optional' => true));
8643c576 47 $mform->addHelpButton('reset_end_date', 'enddate');
0b5a80a1 48 $mform->addElement('checkbox', 'reset_events', get_string('deleteevents', 'calendar'));
0b5a80a1 49 $mform->addElement('checkbox', 'reset_notes', get_string('deletenotes', 'notes'));
c343ddf8 50 $mform->addElement('checkbox', 'reset_comments', get_string('deleteallcomments', 'moodle'));
5ae17b09 51 $mform->addElement('checkbox', 'reset_completion', get_string('deletecompletiondata', 'completion'));
5a5cd650 52 $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
2650f44d 53 $mform->addHelpButton('delete_blog_associations', 'deleteblogassociations', 'blog');
56537316 54 $mform->addElement('checkbox', 'reset_competency_ratings', get_string('deletecompetencyratings', 'core_competency'));
0b5a80a1 55
56 $mform->addElement('header', 'rolesheader', get_string('roles'));
57
9a5e297b 58 $roles = get_assignable_roles(context_course::instance($COURSE->id));
2e15b12b
PS
59 $roles[0] = get_string('noroles', 'role');
60 $roles = array_reverse($roles, true);
61
df997f84 62 $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, array('multiple' => 'multiple'));
0b5a80a1 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'));
66
67
68 $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
69
70 $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
bb6114d8 71 $mform->addHelpButton('reset_gradebook_items', 'removeallcourseitems', 'grades');
0b5a80a1 72 $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
bb6114d8 73 $mform->addHelpButton('reset_gradebook_grades', 'removeallcoursegrades', 'grades');
0b5a80a1 74 $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
75
76
77 $mform->addElement('header', 'groupheader', get_string('groups'));
78
79 $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
0b5a80a1 80 $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
0b5a80a1 81 $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
82
98da6021 83 $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
98da6021 84 $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
98da6021 85 $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
0b5a80a1 86
87 $unsupported_mods = array();
6bb08163 88 if ($allmods = $DB->get_records('modules') ) {
0b5a80a1 89 foreach ($allmods as $mod) {
90 $modname = $mod->name;
0b5a80a1 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)) {
30338539
TL
95 if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
96 continue; // Skip mods with no instances
97 }
0b5a80a1 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;
103 }
104 } else {
105 debugging('Missing lib.php in '.$modname.' module');
106 }
107 }
108 }
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);
115 }
116 }
117
118 $mform->addElement('hidden', 'id', $COURSE->id);
d18e0fe6 119 $mform->setType('id', PARAM_INT);
0b5a80a1 120
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');
128 }
129
130 function load_defaults() {
6bb08163 131 global $CFG, $COURSE, $DB;
0b5a80a1 132
133 $mform =& $this->_form;
134
bb8d6e1a 135 $defaults = array ('reset_events'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
0b5a80a1 136
b2da6472
RT
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);
140 }
141
6bb08163 142 if ($allmods = $DB->get_records('modules') ) {
0b5a80a1 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)) {
97481934 150 if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
151 $defaults = $defaults + $moddefs;
152 }
0b5a80a1 153 }
154 }
155 }
156 }
157
158 foreach ($defaults as $element=>$default) {
159 $mform->setDefault($element, $default);
160 }
161 }
8643c576
DM
162
163 /**
164 * Validation.
165 *
166 * @param array $data
167 * @param array $files
168 * @return array the errors that were found
169 */
170 public function validation($data, $files) {
171 global $DB;
172
173 $course = get_course($data['id']);
174
175 $errors = parent::validation($data, $files);
176
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;
182 }
183
184 if ($data['reset_end_date'] != 0) {
185 // End date set by the user has preference.
186 $coursedata['enddate'] = $data['reset_end_date'];
40eea06c
DM
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.
8643c576
DM
189 $timeshift = $data['reset_start_date'] - usergetmidnight($course->startdate);
190 $coursedata['enddate'] = $course->enddate + $timeshift;
191 } else {
192 $coursedata['enddate'] = $course->enddate;
193 }
194
195 if ($errorcode = course_validate_dates($coursedata)) {
196 $errors['reset_end_date'] = get_string($errorcode, 'error');
197 }
198
199 return $errors;
200 }
201
0b5a80a1 202}