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 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
|
|
|
30 |
require_once("$CFG->libdir/formslib.php");
|
|
|
31 |
|
|
|
32 |
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
|
|
|
33 |
require_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 |
*/
|
|
|
43 |
class 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 = \copy_helper::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-6'));
|
|
|
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('autocomplete', 'category', get_string('coursecategory'), $displaylist);
|
|
|
110 |
$mform->addRule('category', null, 'required', null, 'client');
|
|
|
111 |
$mform->addHelpButton('category', 'coursecategory');
|
|
|
112 |
|
|
|
113 |
// Course visibility.
|
|
|
114 |
$choices = array();
|
|
|
115 |
$choices['0'] = get_string('hide');
|
|
|
116 |
$choices['1'] = get_string('show');
|
|
|
117 |
$mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
|
|
|
118 |
$mform->addHelpButton('visible', 'coursevisibility');
|
|
|
119 |
$mform->setDefault('visible', $courseconfig->visible);
|
|
|
120 |
if (!has_capability('moodle/course:visibility', $coursecontext)) {
|
|
|
121 |
$mform->hardFreeze('visible');
|
|
|
122 |
$mform->setConstant('visible', $course->visible);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
// Course start date.
|
|
|
126 |
$mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
|
|
|
127 |
$mform->addHelpButton('startdate', 'startdate');
|
|
|
128 |
$date = (new \DateTime())->setTimestamp(usergetmidnight(time()));
|
|
|
129 |
$date->modify('+1 day');
|
|
|
130 |
$mform->setDefault('startdate', $date->getTimestamp());
|
|
|
131 |
|
|
|
132 |
// Course enddate.
|
|
|
133 |
$mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
|
|
|
134 |
$mform->addHelpButton('enddate', 'enddate');
|
|
|
135 |
|
|
|
136 |
if (!empty($CFG->enablecourserelativedates)) {
|
|
|
137 |
$attributes = [
|
|
|
138 |
'aria-describedby' => 'relativedatesmode_warning'
|
|
|
139 |
];
|
|
|
140 |
if (!empty($course->id)) {
|
|
|
141 |
$attributes['disabled'] = true;
|
|
|
142 |
}
|
|
|
143 |
$relativeoptions = [
|
|
|
144 |
|
|
|
145 |
1 => get_string('yes'),
|
|
|
146 |
];
|
|
|
147 |
$relativedatesmodegroup = [];
|
|
|
148 |
$relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
|
|
|
149 |
$relativeoptions, $attributes);
|
|
|
150 |
$relativedatesmodegroup[] = $mform->createElement('html', \html_writer::span(get_string('relativedatesmode_warning'),
|
|
|
151 |
'', ['id' => 'relativedatesmode_warning']));
|
|
|
152 |
$mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
|
|
|
153 |
$mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// Course ID number (default to the current course ID number; blank for users who can't change ID numbers).
|
|
|
157 |
$mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100" size="10"');
|
|
|
158 |
$mform->setDefault('idnumber', $course->idnumber);
|
|
|
159 |
$mform->addHelpButton('idnumber', 'idnumbercourse');
|
|
|
160 |
$mform->setType('idnumber', PARAM_RAW);
|
|
|
161 |
if (!has_capability('moodle/course:changeidnumber', $coursecontext)) {
|
|
|
162 |
$mform->hardFreeze('idnumber');
|
|
|
163 |
$mform->setConstant('idnumber', '');
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
// Keep source course user data.
|
|
|
167 |
$mform->addElement('select', 'userdata', get_string('userdata', 'backup'),
|
|
|
168 |
[0 => get_string('no'), 1 => get_string('yes')]);
|
|
|
169 |
$mform->setDefault('userdata', 0);
|
|
|
170 |
$mform->addHelpButton('userdata', 'userdata', 'backup');
|
|
|
171 |
|
|
|
172 |
$requiredcapabilities = array(
|
|
|
173 |
'moodle/restore:createuser', 'moodle/backup:userinfo', 'moodle/restore:userinfo'
|
|
|
174 |
);
|
|
|
175 |
if (!has_all_capabilities($requiredcapabilities, $coursecontext)) {
|
|
|
176 |
$mform->hardFreeze('userdata');
|
|
|
177 |
$mform->setConstant('userdata', 0);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
// Keep manual enrolments.
|
|
|
181 |
// Only get roles actually used in this course.
|
|
|
182 |
$roles = role_fix_names(get_roles_used_in_context($coursecontext, false), $coursecontext);
|
|
|
183 |
|
|
|
184 |
// Only add the option if there are roles in this course.
|
|
|
185 |
if (!empty($roles) && has_capability('moodle/restore:createuser', $coursecontext)) {
|
|
|
186 |
$rolearray = array();
|
|
|
187 |
foreach ($roles as $role) {
|
|
|
188 |
$roleid = 'role_' . $role->id;
|
|
|
189 |
$rolearray[] = $mform->createElement('advcheckbox', $roleid,
|
|
|
190 |
$role->localname, '', array('group' => 2), array(0, $role->id));
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$mform->addGroup($rolearray, 'rolearray', get_string('keptroles', 'backup'), ' ', false);
|
|
|
194 |
$mform->addHelpButton('rolearray', 'keptroles', 'backup');
|
|
|
195 |
$this->add_checkbox_controller(2);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
$buttonarray = array();
|
|
|
199 |
$buttonarray[] = $mform->createElement('submit', 'submitreturn', get_string('copyreturn', 'backup'));
|
|
|
200 |
$buttonarray[] = $mform->createElement('submit', 'submitdisplay', get_string('copyview', 'backup'));
|
|
|
201 |
$buttonarray[] = $mform->createElement('cancel');
|
|
|
202 |
$mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
|
|
|
203 |
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Validation of the form.
|
|
|
208 |
*
|
|
|
209 |
* @param array $data
|
|
|
210 |
* @param array $files
|
|
|
211 |
* @return array the errors that were found
|
|
|
212 |
*/
|
|
|
213 |
public function validation($data, $files) {
|
|
|
214 |
global $DB;
|
|
|
215 |
$errors = parent::validation($data, $files);
|
|
|
216 |
|
|
|
217 |
// Add field validation check for duplicate shortname.
|
|
|
218 |
$courseshortname = $DB->get_record('course', array('shortname' => $data['shortname']), 'fullname', IGNORE_MULTIPLE);
|
|
|
219 |
if ($courseshortname) {
|
|
|
220 |
$errors['shortname'] = get_string('shortnametaken', '', $courseshortname->fullname);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// Add field validation check for duplicate idnumber.
|
|
|
224 |
if (!empty($data['idnumber'])) {
|
|
|
225 |
$courseidnumber = $DB->get_record('course', array('idnumber' => $data['idnumber']), 'fullname', IGNORE_MULTIPLE);
|
|
|
226 |
if ($courseidnumber) {
|
|
|
227 |
$errors['idnumber'] = get_string('courseidnumbertaken', 'error', $courseidnumber->fullname);
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
// Validate the dates (make sure end isn't greater than start).
|
|
|
232 |
if ($errorcode = course_validate_dates($data)) {
|
|
|
233 |
$errors['enddate'] = get_string($errorcode, 'error');
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
return $errors;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
}
|