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