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
/**
18
 * Course copy form class.
19
 *
20
 * @package     core_backup
21
 * @copyright   2020 onward The Moodle Users Association <https://moodleassociation.org/>
22
 * @author      Matt Porritt <mattp@catalyst-au.net>
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core_backup\output;
27
 
1441 ariadna 28
use core\di;
29
use core\hook\manager;
30
use core_backup\hook\after_copy_form_definition;
31
 
1 efrain 32
defined('MOODLE_INTERNAL') || die();
33
 
34
require_once("$CFG->libdir/formslib.php");
35
 
36
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
37
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
38
 
39
/**
40
 * Course copy form class.
41
 *
42
 * @package     core_backup
43
 * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
44
 * @author     Matt Porritt <mattp@catalyst-au.net>
45
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46
 */
47
class copy_form extends \moodleform {
48
 
49
    /**
50
     * Build form for the course copy settings.
51
     *
52
     * {@inheritDoc}
53
     * @see \moodleform::definition()
54
     */
55
    public function definition() {
56
        global $CFG, $OUTPUT, $USER;
57
 
58
        $mform = $this->_form;
59
        $course = $this->_customdata['course'];
60
        $coursecontext = \context_course::instance($course->id);
61
        $courseconfig = get_config('moodlecourse');
62
        $returnto = $this->_customdata['returnto'];
63
        $returnurl = $this->_customdata['returnurl'];
64
 
65
        if (empty($course->category)) {
66
            $course->category = $course->categoryid;
67
        }
68
 
69
        // Course ID.
70
        $mform->addElement('hidden', 'courseid', $course->id);
71
        $mform->setType('courseid', PARAM_INT);
72
 
73
        // Return to type.
74
        $mform->addElement('hidden', 'returnto', null);
75
        $mform->setType('returnto', PARAM_ALPHANUM);
76
        $mform->setConstant('returnto', $returnto);
77
 
78
        // Notifications of current copies.
79
        $copies = \copy_helper::get_copies($USER->id, $course->id);
80
        if (!empty($copies)) {
81
            $progresslink = new \moodle_url('/backup/copyprogress.php?', array('id' => $course->id));
82
            $notificationmsg = get_string('copiesinprogress', 'backup', $progresslink->out());
83
            $notification = $OUTPUT->notification($notificationmsg, 'notifymessage');
84
            $mform->addElement('html', $notification);
85
        }
86
 
87
        // Return to URL.
88
        $mform->addElement('hidden', 'returnurl', null);
89
        $mform->setType('returnurl', PARAM_LOCALURL);
90
        $mform->setConstant('returnurl', $returnurl);
91
 
92
        // Form heading.
1441 ariadna 93
        $mform->addElement('html', \html_writer::div(get_string('copycoursedesc', 'backup'), 'form-text mb-6'));
1 efrain 94
 
95
        // Course fullname.
1441 ariadna 96
        $mform->addElement('text', 'fullname', get_string('fullnamecourse'),
97
            ['maxlength' => \core_course\constants::FULLNAME_MAXIMUM_LENGTH, 'size' => 50]);
1 efrain 98
        $mform->addHelpButton('fullname', 'fullnamecourse');
99
        $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
100
        $mform->setType('fullname', PARAM_TEXT);
101
 
102
        // Course shortname.
1441 ariadna 103
        $mform->addElement('text', 'shortname', get_string('shortnamecourse'),
104
            ['maxlength' => \core_course\constants::SHORTNAME_MAXIMUM_LENGTH, 'size' => 20]);
1 efrain 105
        $mform->addHelpButton('shortname', 'shortnamecourse');
106
        $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
107
        $mform->setType('shortname', PARAM_TEXT);
108
 
109
        // Course category.
110
        $displaylist = \core_course_category::make_categories_list(\core_course\management\helper::get_course_copy_capabilities());
111
        if (!isset($displaylist[$course->category])) {
112
            // Always keep current category.
113
            $displaylist[$course->category] = \core_course_category::get($course->category, MUST_EXIST, true)->get_formatted_name();
114
        }
115
        $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
116
        $mform->addRule('category', null, 'required', null, 'client');
117
        $mform->addHelpButton('category', 'coursecategory');
118
 
119
        // Course visibility.
120
        $choices = array();
121
        $choices['0'] = get_string('hide');
122
        $choices['1'] = get_string('show');
123
        $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
124
        $mform->addHelpButton('visible', 'coursevisibility');
125
        $mform->setDefault('visible', $courseconfig->visible);
126
        if (!has_capability('moodle/course:visibility', $coursecontext)) {
127
            $mform->hardFreeze('visible');
128
            $mform->setConstant('visible', $course->visible);
129
        }
130
 
131
        // Course start date.
132
        $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
133
        $mform->addHelpButton('startdate', 'startdate');
134
        $date = (new \DateTime())->setTimestamp(usergetmidnight(time()));
135
        $date->modify('+1 day');
136
        $mform->setDefault('startdate', $date->getTimestamp());
137
 
138
        // Course enddate.
139
        $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
140
        $mform->addHelpButton('enddate', 'enddate');
141
 
142
        if (!empty($CFG->enablecourserelativedates)) {
143
            $attributes = [
144
                'aria-describedby' => 'relativedatesmode_warning'
145
            ];
146
            if (!empty($course->id)) {
147
                $attributes['disabled'] = true;
148
            }
149
            $relativeoptions = [
150
 
151
                1 => get_string('yes'),
152
            ];
153
            $relativedatesmodegroup = [];
154
            $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
155
                $relativeoptions, $attributes);
156
            $relativedatesmodegroup[] = $mform->createElement('html', \html_writer::span(get_string('relativedatesmode_warning'),
157
                '', ['id' => 'relativedatesmode_warning']));
158
            $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
159
            $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
160
        }
161
 
162
        // Course ID number (default to the current course ID number; blank for users who can't change ID numbers).
163
        $mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100"  size="10"');
164
        $mform->setDefault('idnumber', $course->idnumber);
165
        $mform->addHelpButton('idnumber', 'idnumbercourse');
166
        $mform->setType('idnumber', PARAM_RAW);
167
        if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
168
            $mform->hardFreeze('idnumber');
169
            $mform->setConstant('idnumber', '');
170
        }
171
 
172
        // Keep source course user data.
173
        $mform->addElement('select', 'userdata', get_string('userdata', 'backup'),
174
            [0 => get_string('no'), 1 => get_string('yes')]);
175
        $mform->setDefault('userdata', 0);
176
        $mform->addHelpButton('userdata', 'userdata', 'backup');
177
 
178
        $requiredcapabilities = array(
179
            'moodle/restore:createuser', 'moodle/backup:userinfo', 'moodle/restore:userinfo'
180
        );
181
        if (!has_all_capabilities($requiredcapabilities, $coursecontext)) {
182
            $mform->hardFreeze('userdata');
183
            $mform->setConstant('userdata', 0);
184
        }
185
 
186
        // Keep manual enrolments.
187
        // Only get roles actually used in this course.
188
        $roles = role_fix_names(get_roles_used_in_context($coursecontext, false), $coursecontext);
189
 
190
        // Only add the option if there are roles in this course.
191
        if (!empty($roles) && has_capability('moodle/restore:createuser', $coursecontext)) {
192
            $rolearray = array();
193
            foreach ($roles as $role) {
194
                $roleid = 'role_' . $role->id;
195
                $rolearray[] = $mform->createElement('advcheckbox', $roleid,
196
                    $role->localname, '', array('group' => 2), array(0, $role->id));
197
            }
198
 
199
            $mform->addGroup($rolearray, 'rolearray', get_string('keptroles', 'backup'), ' ', false);
200
            $mform->addHelpButton('rolearray', 'keptroles', 'backup');
201
            $this->add_checkbox_controller(2);
202
        }
203
 
1441 ariadna 204
        // Dispatch hook to allow more elements to be added to the form.
205
        $hook = new after_copy_form_definition($mform);
206
        di::get(manager::class)->dispatch($hook);
207
 
1 efrain 208
        $buttonarray = array();
209
        $buttonarray[] = $mform->createElement('submit', 'submitreturn', get_string('copyreturn', 'backup'));
210
        $buttonarray[] = $mform->createElement('submit', 'submitdisplay', get_string('copyview', 'backup'));
211
        $buttonarray[] = $mform->createElement('cancel');
212
        $mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
213
 
214
    }
215
 
216
    /**
217
     * Validation of the form.
218
     *
219
     * @param array $data
220
     * @param array $files
221
     * @return array the errors that were found
222
     */
223
    public function validation($data, $files) {
224
        global $DB;
225
        $errors = parent::validation($data, $files);
226
 
227
        // Add field validation check for duplicate shortname.
228
        $courseshortname = $DB->get_record('course', array('shortname' => $data['shortname']), 'fullname', IGNORE_MULTIPLE);
229
        if ($courseshortname) {
230
            $errors['shortname'] = get_string('shortnametaken', '', $courseshortname->fullname);
231
        }
232
 
233
        // Add field validation check for duplicate idnumber.
234
        if (!empty($data['idnumber'])) {
235
            $courseidnumber = $DB->get_record('course', array('idnumber' => $data['idnumber']), 'fullname', IGNORE_MULTIPLE);
236
            if ($courseidnumber) {
237
                $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $courseidnumber->fullname);
238
            }
239
        }
240
 
241
        // Validate the dates (make sure end isn't greater than start).
242
        if ($errorcode = course_validate_dates($data)) {
243
            $errors['enddate'] = get_string($errorcode, 'error');
244
        }
245
 
246
        return $errors;
247
    }
248
 
249
}