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