MDL-13114 tool_uploadcourse: Optimised the import form
[moodle.git] / admin / tool / uploadcourse / classes / step2_form.php
CommitLineData
1e3e4efd
FM
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 * Bulk course upload step 2.
19 *
20 * @package tool_uploadcourse
21 * @copyright 2011 Piers Harding
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
1e3e4efd
FM
27/**
28 * Specify course upload details.
29 *
30 * @package tool_uploadcourse
31 * @copyright 2011 Piers Harding
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
7a4ec85e 34class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form {
1e3e4efd
FM
35
36 /**
37 * The standard form definiton.
38 * @return void.
39 */
40 public function definition () {
41 global $CFG, $COURSE, $DB;
42
43 $mform = $this->_form;
44 $data = $this->_customdata['data'];
45 $courseconfig = get_config('moodlecourse');
46
7a4ec85e
FM
47 // Import options.
48 $this->add_import_options();
49
50 // Course options.
51 $mform->addElement('header', 'courseoptionshdr', get_string('courseprocess', 'tool_uploadcourse'));
52 $mform->setExpanded('courseoptionshdr', true);
53
54 $mform->addElement('text', 'options[shortnametemplate]', get_string('shortnametemplate', 'tool_uploadcourse'), 'maxlength="100" size="20"');
55 $mform->setType('options[shortnametemplate]', PARAM_RAW);
56 $mform->addHelpButton('options[shortnametemplate]', 'shortnametemplate', 'tool_uploadcourse');
57 $mform->disabledIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE);
58 $mform->disabledIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_UPDATE_ONLY);
59
60 $contextid = $this->_customdata['contextid'];
61 $mform->addElement('hidden', 'contextid', $contextid);
62 $mform->setType('contextid', PARAM_INT);
63 $mform->addElement('filepicker', 'options[restorefile]', get_string('templatefile', 'tool_uploadcourse'));
64 $mform->addHelpButton('options[restorefile]', 'templatefile', 'tool_uploadcourse');
65
66 $mform->addElement('text', 'options[templatecourse]', get_string('coursetemplatename', 'tool_uploadcourse'));
67 $mform->setType('options[templatecourse]', PARAM_TEXT);
68 $mform->addHelpButton('options[templatecourse]', 'coursetemplatename', 'tool_uploadcourse');
1e3e4efd
FM
69
70 $mform->addElement('selectyesno', 'options[reset]', get_string('reset', 'tool_uploadcourse'));
71 $mform->setDefault('options[reset]', 0);
7a4ec85e
FM
72 $mform->disabledIf('options[reset]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_NEW);
73 $mform->disabledIf('options[reset]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_ALL);
74 $mform->disabledIf('options[reset]', 'options[allowresets]', 'eq', 0);
1e3e4efd
FM
75
76 // Default values.
77 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploadcourse'));
78 $mform->setExpanded('defaultheader', true);
79
1e3e4efd
FM
80 $displaylist = coursecat::make_categories_list('moodle/course:create');
81 $mform->addElement('select', 'defaults[category]', get_string('coursecategory'), $displaylist);
82 $mform->addHelpButton('defaults[category]', 'coursecategory');
83
84 $choices = array();
85 $choices['0'] = get_string('hide');
86 $choices['1'] = get_string('show');
87 $mform->addElement('select', 'defaults[visible]', get_string('visible'), $choices);
88 $mform->addHelpButton('defaults[visible]', 'visible');
7a4ec85e 89 $mform->setDefault('defaults[visible]', $courseconfig->visible);
1e3e4efd
FM
90
91 $mform->addElement('date_selector', 'defaults[startdate]', get_string('startdate'));
92 $mform->addHelpButton('defaults[startdate]', 'startdate');
93 $mform->setDefault('defaults[startdate]', time() + 3600 * 24);
94
95 $courseformats = get_sorted_course_formats(true);
96 $formcourseformats = array();
97 foreach ($courseformats as $courseformat) {
98 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
99 }
100 $mform->addElement('select', 'defaults[format]', get_string('format'), $formcourseformats);
101 $mform->addHelpButton('defaults[format]', 'format');
102 $mform->setDefault('defaults[format]', $courseconfig->format);
103
104 if (!empty($CFG->allowcoursethemes)) {
105 $themeobjects = get_list_of_themes();
106 $themes=array();
107 $themes[''] = get_string('forceno');
108 foreach ($themeobjects as $key => $theme) {
109 if (empty($theme->hidefromselector)) {
110 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
111 }
112 }
113 $mform->addElement('select', 'defaults[theme]', get_string('forcetheme'), $themes);
114 }
115
116 $languages[] = array();
117 $languages[''] = get_string('forceno');
118 $languages += get_string_manager()->get_list_of_translations();
119 $mform->addElement('select', 'defaults[lang]', get_string('forcelanguage'), $languages);
120 $mform->setDefault('defaults[lang]', $courseconfig->lang);
121
122 $options = range(0, 10);
123 $mform->addElement('select', 'defaults[newsitems]', get_string('newsitemsnumber'), $options);
124 $mform->addHelpButton('defaults[newsitems]', 'newsitemsnumber');
125 $mform->setDefault('defaults[newsitems]', $courseconfig->newsitems);
126
127 $mform->addElement('selectyesno', 'defaults[showgrades]', get_string('showgrades'));
128 $mform->addHelpButton('defaults[showgrades]', 'showgrades');
129 $mform->setDefault('defaults[showgrades]', $courseconfig->showgrades);
130
131 $mform->addElement('selectyesno', 'defaults[showreports]', get_string('showreports'));
132 $mform->addHelpButton('defaults[showreports]', 'showreports');
133 $mform->setDefault('defaults[showreports]', $courseconfig->showreports);
134
135 if (!empty($CFG->legacyfilesinnewcourses)) {
136 if (empty($course->legacyfiles)) {
137 $choices = array('0' => get_string('no'), '2' => get_string('yes'));
138 }
139 $mform->addElement('select', 'defaults[legacyfiles]', get_string('courselegacyfiles'), $choices);
140 $mform->addHelpButton('defaults[legacyfiles]', 'courselegacyfiles');
141 if (!isset($courseconfig->legacyfiles)) {
142 $courseconfig->legacyfiles = 0;
143 }
144 $mform->setDefault('defaults[legacyfiles]', $courseconfig->legacyfiles);
145 }
146
147 $choices = get_max_upload_sizes($CFG->maxbytes);
148 $mform->addElement('select', 'defaults[maxbytes]', get_string('maximumupload'), $choices);
149 $mform->addHelpButton('defaults[maxbytes]', 'maximumupload');
150 $mform->setDefault('defaults[maxbytes]', $courseconfig->maxbytes);
151
152 $choices = array();
153 $choices[NOGROUPS] = get_string('groupsnone', 'group');
154 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
155 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
156 $mform->addElement('select', 'defaults[groupmode]', get_string('groupmode', 'group'), $choices);
157 $mform->addHelpButton('defaults[groupmode]', 'groupmode', 'group');
158 $mform->setDefault('defaults[groupmode]', $courseconfig->groupmode);
159
160 $mform->addElement('selectyesno', 'defaults[groupmodeforce]', get_string('groupmodeforce', 'group'));
161 $mform->addHelpButton('defaults[groupmodeforce]', 'groupmodeforce', 'group');
162 $mform->setDefault('defaults[groupmodeforce]', $courseconfig->groupmodeforce);
163
1e3e4efd
FM
164 // Hidden fields.
165 $mform->addElement('hidden', 'iid');
166 $mform->setType('iid', PARAM_INT);
167
168 $mform->addElement('hidden', 'previewrows');
169 $mform->setType('previewrows', PARAM_INT);
170
171 $this->add_action_buttons(true, get_string('uploadcourses', 'tool_uploadcourse'));
172
173 $this->set_data($data);
174 }
175
176 /**
177 * Add actopm buttons.
178 *
179 * @param bool $cancel whether to show cancel button, default true
180 * @param string $submitlabel label for submit button, defaults to get_string('savechanges')
181 * @return void
182 */
183 function add_action_buttons($cancel = true, $submitlabel = null){
184 $mform =& $this->_form;
185 $buttonarray = array();
7a4ec85e 186 $buttonarray[] = &$mform->createElement('submit', 'showpreview', get_string('preview', 'tool_uploadcourse'));
1e3e4efd
FM
187 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
188 $buttonarray[] = &$mform->createElement('cancel');
189 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
190 $mform->closeHeaderBefore('buttonar');
191 }
192
193 /**
194 * Server side validation.
195 * @param array $data - form data
196 * @param object $files - form files
197 * @return array $errors - form errors
198 */
199 public function validation($data, $files) {
200 global $DB;
201
202 $errors = parent::validation($data, $files);
203 $columns = $this->_customdata['columns'];
204 $optype = $data['options']['mode'];
205
206 // Look for other required data.
207 if ($optype != tool_uploadcourse_processor::MODE_UPDATE_ONLY) {
208 if (!in_array('fullname', $columns)) {
209 if (isset($errors['mode'])) {
210 $errors['mode'] .= ' ';
211 }
212 $errors['mode'] .= get_string('missingfield', 'error', 'fullname');
213 }
214 if (!in_array('summary', $columns)) {
215 if (isset($errors['mode'])) {
216 $errors['mode'] .= ' ';
217 }
218 $errors['mode'] .= get_string('missingfield', 'error', 'summary');
219 }
220 }
221
222 return $errors;
223 }
224}