MDL-64843 Backup: Course copy user interface
[moodle.git] / backup / util / ui / classes / output / copy_form.php
CommitLineData
01436f75
MP
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
26namespace core_backup\output;
27
28defined('MOODLE_INTERNAL') || die();
29
30require_once("$CFG->libdir/formslib.php");
31
32require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
33require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
34
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 */
43class copy_form extends \moodleform {
44
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;
53
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'];
60
61 if (empty($course->category)) {
62 $course->category = $course->categoryid;
63 }
64
65 // Course ID.
66 $mform->addElement('hidden', 'courseid', $course->id);
67 $mform->setType('courseid', PARAM_INT);
68
69 // Return to type.
70 $mform->addElement('hidden', 'returnto', null);
71 $mform->setType('returnto', PARAM_ALPHANUM);
72 $mform->setConstant('returnto', $returnto);
73
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 }
82
83 // Return to URL.
84 $mform->addElement('hidden', 'returnurl', null);
85 $mform->setType('returnurl', PARAM_LOCALURL);
86 $mform->setConstant('returnurl', $returnurl);
87
88 // Form heading.
89 $mform->addElement('html', \html_writer::div(get_string('copycoursedesc', 'backup'), 'form-description mb-3'));
90
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);
96
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);
102
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('select', 'category', get_string('coursecategory'), $displaylist);
110 $mform->addHelpButton('category', 'coursecategory');
111
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 }
123
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());
130
131 // Course enddate.
132 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
133 $mform->addHelpButton('enddate', 'enddate');
134
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 }
154
155 // Course id number.
156 $mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100" size="10"');
157 $mform->addHelpButton('idnumber', 'idnumbercourse');
158 $mform->setType('idnumber', PARAM_RAW);
159 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
160 $mform->hardFreeze('idnumber');
161 $mform->setConstants('idnumber', $course->idnumber);
162 }
163
164 // Keep source course user data.
165 $requiredcapabilities = array(
166 'moodle/restore:createuser', 'moodle/backup:userinfo', 'moodle/restore:userinfo'
167 );
168 if (has_all_capabilities($requiredcapabilities, $coursecontext)) {
169 $dataarray = array();
170 $dataarray[] = $mform->createElement('advcheckbox', 'userdata',
171 get_string('enable'), '', array('group' => 1), array(0, 1));
172 $mform->addGroup($dataarray, 'dataarray', get_string('userdata', 'backup'), ' ', false);
173 $mform->addHelpButton('dataarray', 'userdata', 'backup');
174 }
175
176 // Keep manual enrolments.
177 // Only get roles actually used in this course.
178 $roles = role_fix_names(get_roles_used_in_context($coursecontext, false), $coursecontext);
179
180 // Only add the option if there are roles in this course.
181 if (!empty($roles) && has_capability('moodle/restore:createuser', $coursecontext)) {
182 $rolearray = array();
183 foreach ($roles as $role) {
184 $roleid = 'role_' . $role->id;
185 $rolearray[] = $mform->createElement('advcheckbox', $roleid,
186 $role->localname, '', array('group' => 2), array(0, $role->id));
187 }
188
189 $mform->addGroup($rolearray, 'rolearray', get_string('keptroles', 'backup'), ' ', false);
190 $mform->addHelpButton('rolearray', 'keptroles', 'backup');
191 $this->add_checkbox_controller(2);
192 }
193
194 $buttonarray = array();
195 $buttonarray[] = $mform->createElement('submit', 'submitreturn', get_string('copyreturn', 'backup'));
196 $buttonarray[] = $mform->createElement('submit', 'submitdisplay', get_string('copyview', 'backup'));
197 $buttonarray[] = $mform->createElement('cancel');
198 $mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
199
200 }
201
202 /**
203 * Validation of the form.
204 *
205 * @param array $data
206 * @param array $files
207 * @return array the errors that were found
208 */
209 public function validation($data, $files) {
210 global $DB;
211 $errors = parent::validation($data, $files);
212
213 // Add field validation check for duplicate shortname.
214 $courseshortname = $DB->get_record('course', array('shortname' => $data['shortname']), 'fullname', IGNORE_MULTIPLE);
215 if ($courseshortname) {
216 $errors['shortname'] = get_string('shortnametaken', '', $courseshortname->fullname);
217 }
218
219 // Add field validation check for duplicate idnumber.
220 if (!empty($data['idnumber'])) {
221 $courseidnumber = $DB->get_record('course', array('idnumber' => $data['idnumber']), 'fullname', IGNORE_MULTIPLE);
222 if ($courseidnumber) {
223 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $courseidnumber->fullname);
224 }
225 }
226
227 // Validate the dates (make sure end isn't greater than start).
228 if ($errorcode = course_validate_dates($data)) {
229 $errors['enddate'] = get_string($errorcode, 'error');
230 }
231
232 return $errors;
233 }
234
235}