Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
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
defined('MOODLE_INTERNAL') || die();
18
 
1441 ariadna 19
require_once($CFG->libdir.'/formslib.php');
1 efrain 20
require_once($CFG->dirroot . '/course/lib.php');
21
 
22
/**
23
 * Defines the course reset settings form.
24
 *
1441 ariadna 25
 * @package     core_course
1 efrain 26
 * @copyright   2007 Petr Skoda
27
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class course_reset_form extends moodleform {
1441 ariadna 30
 
31
    /**
32
     * Form definition.
33
     */
34
    public function definition() {
1 efrain 35
        global $CFG, $COURSE, $DB;
36
 
1441 ariadna 37
        $mform = $this->_form;
1 efrain 38
 
39
        $mform->addElement('header', 'generalheader', get_string('general'));
1441 ariadna 40
        $mform->addElement('date_time_selector', 'reset_start_date', get_string('startdate'), ['optional' => true]);
1 efrain 41
        $mform->addHelpButton('reset_start_date', 'startdate');
1441 ariadna 42
        $mform->addElement('date_time_selector', 'reset_end_date', get_string('enddate'), ['optional' => true]);
1 efrain 43
        $mform->addHelpButton('reset_end_date', 'enddate');
1441 ariadna 44
        $mform->addElement('static', 'generaldelete', get_string('delete'));
1 efrain 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');
51
        $mform->addElement('checkbox', 'reset_competency_ratings', get_string('deletecompetencyratings', 'core_competency'));
52
 
53
        $mform->addElement('header', 'rolesheader', get_string('roles'));
54
        $roles = get_assignable_roles(context_course::instance($COURSE->id));
55
        $roles[0] = get_string('noroles', 'role');
56
        $roles = array_reverse($roles, true);
1441 ariadna 57
        $attributes = [
58
            'multiple' => 1,
59
            'size' => min(count($roles), 10),
60
        ];
61
        $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, $attributes);
62
        $mform->addElement('static', 'rolesdelete', get_string('delete'));
63
        $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
1 efrain 64
        $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
65
 
66
        $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
1441 ariadna 67
        $mform->addElement('static', 'gradebookdelete', get_string('delete'));
1 efrain 68
        $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
69
        $mform->addHelpButton('reset_gradebook_items', 'removeallcourseitems', 'grades');
70
        $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
71
        $mform->addHelpButton('reset_gradebook_grades', 'removeallcoursegrades', 'grades');
72
        $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
73
 
74
        $mform->addElement('header', 'groupheader', get_string('groups'));
1441 ariadna 75
        $mform->addElement('static', 'groupdelete', get_string('delete'));
1 efrain 76
        $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
77
        $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
78
        $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
79
        $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
80
        $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
81
        $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
82
 
1441 ariadna 83
        $unsupportedmods = [];
1 efrain 84
        if ($allmods = $DB->get_records('modules') ) {
85
            foreach ($allmods as $mod) {
86
                $modname = $mod->name;
87
                $modfile = $CFG->dirroot."/mod/$modname/lib.php";
1441 ariadna 88
                $modresetcourseformdefinition = $modname.'_reset_course_form_definition';
89
                $modresetuserdata = $modname.'_reset_userdata';
1 efrain 90
                if (file_exists($modfile)) {
1441 ariadna 91
                    if (!$DB->count_records($modname, ['course' => $COURSE->id])) {
92
                        continue; // Skip mods with no instances.
1 efrain 93
                    }
94
                    include_once($modfile);
1441 ariadna 95
                    if (function_exists($modresetcourseformdefinition)) {
96
                        $modresetcourseformdefinition($mform);
97
                    } else if (!function_exists($modresetuserdata)) {
98
                        $unsupportedmods[] = $mod;
1 efrain 99
                    }
100
                } else {
101
                    debugging('Missing lib.php in '.$modname.' module');
102
                }
103
            }
104
        }
1441 ariadna 105
        // Mention unsupported mods.
106
        if (!empty($unsupportedmods)) {
1 efrain 107
            $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
1441 ariadna 108
            $mform->addElement('static', 'unsupportedinfo', get_string('resetnotimplementedinfo'));
109
            foreach ($unsupportedmods as $mod) {
1 efrain 110
                $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
111
            }
112
        }
113
 
1441 ariadna 114
        $elements = $mform->_elements;
115
        foreach ($elements as $element) {
116
            if (is_a($element, 'HTML_QuickForm_header')) {
117
                $mform->setExpanded($element->getName());
118
            }
119
        }
120
 
1 efrain 121
        $mform->addElement('hidden', 'id', $COURSE->id);
122
        $mform->setType('id', PARAM_INT);
123
 
1441 ariadna 124
        $buttonarray = [];
125
        $buttonarray[] = $mform->createElement(
126
            'submit',
127
            'selectdefault',
128
            get_string('selectdefault'),
129
            null,
130
            null,
131
            ['customclassoverride' => 'btn-secondary'],
132
        );
133
        $buttonarray[] = $mform->createElement(
134
            'submit',
135
            'deselectall',
136
            get_string('deselectall'),
137
            null,
138
            null,
139
            ['customclassoverride' => 'btn-secondary'],
140
        );
141
        $mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
142
 
143
        $mform->addElement(
144
            'submit',
145
            'submitbutton',
146
            get_string('resetcourse'),
147
            [
148
                'data-action' => 'resetcourse',
149
                'data-courseid' => $COURSE->id,
150
                'data-coursename' => $COURSE->fullname,
151
            ],
152
            null,
153
            ['customclassoverride' => 'btn-danger'],
154
        );
155
        $mform->set_sticky_footer('submitbutton');
156
 
1 efrain 157
        $mform->closeHeaderBefore('buttonar');
158
    }
159
 
1441 ariadna 160
    /**
161
     * Method to load default values for the reset course form.
162
     */
163
    public function load_defaults() {
1 efrain 164
        global $CFG, $COURSE, $DB;
165
 
1441 ariadna 166
        $mform = $this->_form;
1 efrain 167
 
1441 ariadna 168
        $defaults = [
169
            'reset_events' => 1,
170
            'reset_roles_local' => 1,
171
            'reset_gradebook_grades' => 1,
172
            'reset_notes' => 1,
173
        ];
1 efrain 174
 
175
        // Set student as default in unenrol user list, if role with student archetype exist.
176
        if ($studentrole = get_archetype_roles('student')) {
177
            $defaults['unenrol_users'] = array_keys($studentrole);
178
        }
179
 
180
        if ($allmods = $DB->get_records('modules') ) {
181
            foreach ($allmods as $mod) {
182
                $modname = $mod->name;
183
                $modfile = $CFG->dirroot."/mod/$modname/lib.php";
1441 ariadna 184
                $modresetcourseformdefaults = $modname.'_reset_course_form_defaults';
1 efrain 185
                if (file_exists($modfile)) {
186
                    @include_once($modfile);
1441 ariadna 187
                    if (function_exists($modresetcourseformdefaults)) {
188
                        if ($moddefs = $modresetcourseformdefaults($COURSE)) {
1 efrain 189
                            $defaults = $defaults + $moddefs;
190
                        }
191
                    }
192
                }
193
            }
194
        }
195
 
1441 ariadna 196
        foreach ($defaults as $element => $default) {
1 efrain 197
            $mform->setDefault($element, $default);
198
        }
199
    }
200
 
201
    /**
202
     * Validation.
203
     *
204
     * @param array $data
205
     * @param array $files
206
     * @return array the errors that were found
207
     */
208
    public function validation($data, $files) {
209
        $course = get_course($data['id']);
210
 
211
        $errors = parent::validation($data, $files);
212
 
213
        // We check the values that would be used as start and end.
214
        if ($data['reset_start_date'] != 0) {
215
            $coursedata['startdate'] = $data['reset_start_date'];
216
        } else {
217
            $coursedata['startdate'] = $course->startdate;
218
        }
219
 
220
        if ($data['reset_end_date'] != 0) {
221
            // End date set by the user has preference.
222
            $coursedata['enddate'] = $data['reset_end_date'];
223
        } else if ($data['reset_start_date'] > 0 && $course->enddate != 0) {
224
            // Otherwise, if the current course enddate is set, reset_course_userdata will add the start date time shift to it.
1441 ariadna 225
            $timeshift = $data['reset_start_date'] - $course->startdate;
1 efrain 226
            $coursedata['enddate'] = $course->enddate + $timeshift;
227
        } else {
228
            $coursedata['enddate'] = $course->enddate;
229
        }
230
 
231
        if ($errorcode = course_validate_dates($coursedata)) {
232
            $errors['reset_end_date'] = get_string($errorcode, 'error');
233
        }
234
 
235
        return $errors;
236
    }
237
 
238
}