2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Bulk course upload step 2.
20 * @package tool_uploadcourse
21 * @copyright 2011 Piers Harding
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/course/lib.php');
30 * Specify course upload details.
32 * @package tool_uploadcourse
33 * @copyright 2011 Piers Harding
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form {
39 * The standard form definiton.
42 public function definition () {
45 $mform = $this->_form;
46 $data = $this->_customdata['data'];
47 $courseconfig = get_config('moodlecourse');
50 $this->add_import_options();
53 $mform->addElement('header', 'courseoptionshdr', get_string('courseprocess', 'tool_uploadcourse'));
54 $mform->setExpanded('courseoptionshdr', true);
56 $mform->addElement('text', 'options[shortnametemplate]', get_string('shortnametemplate', 'tool_uploadcourse'),
57 'maxlength="100" size="20"');
58 $mform->setType('options[shortnametemplate]', PARAM_RAW);
59 $mform->addHelpButton('options[shortnametemplate]', 'shortnametemplate', 'tool_uploadcourse');
60 $mform->disabledIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE);
61 $mform->disabledIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_UPDATE_ONLY);
63 // Restore file is not in the array options on purpose, because formslib can't handle it!
64 $contextid = $this->_customdata['contextid'];
65 $mform->addElement('hidden', 'contextid', $contextid);
66 $mform->setType('contextid', PARAM_INT);
67 $mform->addElement('filepicker', 'restorefile', get_string('templatefile', 'tool_uploadcourse'));
68 $mform->addHelpButton('restorefile', 'templatefile', 'tool_uploadcourse');
70 $mform->addElement('text', 'options[templatecourse]', get_string('coursetemplatename', 'tool_uploadcourse'));
71 $mform->setType('options[templatecourse]', PARAM_TEXT);
72 $mform->addHelpButton('options[templatecourse]', 'coursetemplatename', 'tool_uploadcourse');
74 $mform->addElement('selectyesno', 'options[reset]', get_string('reset', 'tool_uploadcourse'));
75 $mform->setDefault('options[reset]', 0);
76 $mform->disabledIf('options[reset]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_NEW);
77 $mform->disabledIf('options[reset]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_ALL);
78 $mform->disabledIf('options[reset]', 'options[allowresets]', 'eq', 0);
79 $mform->addHelpButton('options[reset]', 'reset', 'tool_uploadcourse');
82 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploadcourse'));
83 $mform->setExpanded('defaultheader', true);
85 $displaylist = coursecat::make_categories_list('moodle/course:create');
86 $mform->addElement('select', 'defaults[category]', get_string('coursecategory'), $displaylist);
87 $mform->addHelpButton('defaults[category]', 'coursecategory');
90 $choices['0'] = get_string('hide');
91 $choices['1'] = get_string('show');
92 $mform->addElement('select', 'defaults[visible]', get_string('coursevisibility'), $choices);
93 $mform->addHelpButton('defaults[visible]', 'coursevisibility');
94 $mform->setDefault('defaults[visible]', $courseconfig->visible);
96 $mform->addElement('date_selector', 'defaults[startdate]', get_string('startdate'));
97 $mform->addHelpButton('defaults[startdate]', 'startdate');
98 $mform->setDefault('defaults[startdate]', time() + 3600 * 24);
100 $mform->addElement('date_selector', 'defaults[enddate]', get_string('enddate'), array('optional' => true));
101 $mform->addHelpButton('defaults[enddate]', 'enddate');
103 $courseformats = get_sorted_course_formats(true);
104 $formcourseformats = array();
105 foreach ($courseformats as $courseformat) {
106 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
108 $mform->addElement('select', 'defaults[format]', get_string('format'), $formcourseformats);
109 $mform->addHelpButton('defaults[format]', 'format');
110 $mform->setDefault('defaults[format]', $courseconfig->format);
112 if (!empty($CFG->allowcoursethemes)) {
113 $themeobjects = get_list_of_themes();
115 $themes[''] = get_string('forceno');
116 foreach ($themeobjects as $key => $theme) {
117 if (empty($theme->hidefromselector)) {
118 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
121 $mform->addElement('select', 'defaults[theme]', get_string('forcetheme'), $themes);
124 $languages = array();
125 $languages[''] = get_string('forceno');
126 $languages += get_string_manager()->get_list_of_translations();
127 $mform->addElement('select', 'defaults[lang]', get_string('forcelanguage'), $languages);
128 $mform->setDefault('defaults[lang]', $courseconfig->lang);
130 $options = range(0, 10);
131 $mform->addElement('select', 'defaults[newsitems]', get_string('newsitemsnumber'), $options);
132 $mform->addHelpButton('defaults[newsitems]', 'newsitemsnumber');
133 $mform->setDefault('defaults[newsitems]', $courseconfig->newsitems);
135 $mform->addElement('selectyesno', 'defaults[showgrades]', get_string('showgrades'));
136 $mform->addHelpButton('defaults[showgrades]', 'showgrades');
137 $mform->setDefault('defaults[showgrades]', $courseconfig->showgrades);
139 $mform->addElement('selectyesno', 'defaults[showreports]', get_string('showreports'));
140 $mform->addHelpButton('defaults[showreports]', 'showreports');
141 $mform->setDefault('defaults[showreports]', $courseconfig->showreports);
143 if (!empty($CFG->legacyfilesinnewcourses)) {
144 $mform->addElement('select', 'defaults[legacyfiles]', get_string('courselegacyfiles'), $choices);
145 $mform->addHelpButton('defaults[legacyfiles]', 'courselegacyfiles');
146 if (!isset($courseconfig->legacyfiles)) {
147 $courseconfig->legacyfiles = 0;
149 $mform->setDefault('defaults[legacyfiles]', $courseconfig->legacyfiles);
152 $choices = get_max_upload_sizes($CFG->maxbytes);
153 $mform->addElement('select', 'defaults[maxbytes]', get_string('maximumupload'), $choices);
154 $mform->addHelpButton('defaults[maxbytes]', 'maximumupload');
155 $mform->setDefault('defaults[maxbytes]', $courseconfig->maxbytes);
158 $choices[NOGROUPS] = get_string('groupsnone', 'group');
159 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
160 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
161 $mform->addElement('select', 'defaults[groupmode]', get_string('groupmode', 'group'), $choices);
162 $mform->addHelpButton('defaults[groupmode]', 'groupmode', 'group');
163 $mform->setDefault('defaults[groupmode]', $courseconfig->groupmode);
165 $mform->addElement('selectyesno', 'defaults[groupmodeforce]', get_string('groupmodeforce', 'group'));
166 $mform->addHelpButton('defaults[groupmodeforce]', 'groupmodeforce', 'group');
167 $mform->setDefault('defaults[groupmodeforce]', $courseconfig->groupmodeforce);
169 // Completion tracking.
170 if (!empty($CFG->enablecompletion)) {
171 $mform->addElement('selectyesno', 'defaults[enablecompletion]', get_string('enablecompletion', 'completion'));
172 $mform->setDefault('defaults[enablecompletion]', $courseconfig->enablecompletion);
173 $mform->addHelpButton('defaults[enablecompletion]', 'enablecompletion', 'completion');
177 $mform->addElement('hidden', 'importid');
178 $mform->setType('importid', PARAM_INT);
180 $mform->addElement('hidden', 'previewrows');
181 $mform->setType('previewrows', PARAM_INT);
183 $this->add_action_buttons(true, get_string('uploadcourses', 'tool_uploadcourse'));
185 $this->set_data($data);
189 * Add actopm buttons.
191 * @param bool $cancel whether to show cancel button, default true
192 * @param string $submitlabel label for submit button, defaults to get_string('savechanges')
195 public function add_action_buttons($cancel = true, $submitlabel = null) {
196 $mform =& $this->_form;
197 $buttonarray = array();
198 $buttonarray[] = &$mform->createElement('submit', 'showpreview', get_string('preview', 'tool_uploadcourse'));
199 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
200 $buttonarray[] = &$mform->createElement('cancel');
201 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
202 $mform->closeHeaderBefore('buttonar');
206 * Sets the enddate default after set_data is called.
208 public function definition_after_data() {
210 $mform = $this->_form;
212 // The default end date depends on the course format.
213 $format = course_get_format((object)array('format' => get_config('moodlecourse', 'format')));
215 $enddate = $format->get_default_course_enddate($mform, array('startdate' => 'defaults[startdate]'));
216 // We add 1 day like we do above in startdate.
217 $mform->setDefault('defaults[enddate]', $enddate + 3600 * 24);
224 * @param array $files
225 * @return array the errors that were found
227 public function validation($data, $files) {
230 $errors = parent::validation($data, $files);
232 if ($errorcode = course_validate_dates($data['defaults'])) {
233 $errors['defaults[enddate]'] = get_string($errorcode, 'error');