"MDL-23308, bring course files back"
[moodle.git] / course / publish / forms.php
blob327443ad97581f0cfc73e249d11e36893cd5a440
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // This file is part of Moodle - http://moodle.org/ //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // //
8 // Moodle is free software: you can redistribute it and/or modify //
9 // it under the terms of the GNU General Public License as published by //
10 // the Free Software Foundation, either version 3 of the License, or //
11 // (at your option) any later version. //
12 // //
13 // Moodle is distributed in the hope that it will be useful, //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
16 // GNU General Public License for more details. //
17 // //
18 // You should have received a copy of the GNU General Public License //
19 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. //
20 // //
21 ///////////////////////////////////////////////////////////////////////////
24 * @package course
25 * @subpackage publish
26 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
28 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
30 * The forms used for course publication
34 require_once($CFG->dirroot . '/lib/formslib.php');
35 require_once($CFG->dirroot . "/admin/registration/lib.php");
36 require_once($CFG->dirroot . "/course/publish/lib.php");
39 * Hub selector to choose on which hub we want to publish.
42 class hub_publish_selector_form extends moodleform {
44 public function definition() {
45 global $CFG;
46 $mform = & $this->_form;
47 $share = $this->_customdata['share'];
49 $mform->addElement('header', 'site', get_string('selecthub', 'hub'));
51 $mform->addElement('static', 'info', '', get_string('selecthubinfo', 'hub') . html_writer::empty_tag('br'));
53 $registrationmanager = new registration_manager();
54 $registeredhubs = $registrationmanager->get_registered_on_hubs();
56 //Public hub list
57 $options = array();
58 foreach ($registeredhubs as $hub) {
60 $hubname = $hub->hubname;
61 $mform->addElement('hidden', clean_param($hub->huburl, PARAM_ALPHANUMEXT), $hubname);
62 if (empty($hubname)) {
63 $hubname = $hub->huburl;
65 $mform->addElement('radio', 'huburl', null, ' ' . $hubname, $hub->huburl);
66 if ($hub->huburl == HUB_MOODLEORGHUBURL) {
67 $mform->setDefault('huburl', $hub->huburl);
71 $mform->addElement('hidden', 'id', $this->_customdata['id']);
73 if ($share) {
74 $buttonlabel = get_string('shareonhub', 'hub');
75 $mform->addElement('hidden', 'share', true);
76 } else {
77 $buttonlabel = get_string('advertiseonhub', 'hub');
78 $mform->addElement('hidden', 'advertise', true);
81 $this->add_action_buttons(false, $buttonlabel);
87 * Course publication form
90 class course_publication_form extends moodleform {
92 public function definition() {
93 global $CFG, $DB, $USER, $OUTPUT;
95 $strrequired = get_string('required');
96 $mform = & $this->_form;
97 $huburl = $this->_customdata['huburl'];
98 $hubname = $this->_customdata['hubname'];
99 $course = $this->_customdata['course'];
100 $advertise = $this->_customdata['advertise'];
101 $share = $this->_customdata['share'];
102 $site = get_site();
104 //hidden parameters
105 $mform->addElement('hidden', 'huburl', $huburl);
106 $mform->addElement('hidden', 'hubname', $hubname);
108 //check on the hub if the course has already been published
109 $registrationmanager = new registration_manager();
110 $registeredhub = $registrationmanager->get_registeredhub($huburl);
111 $publicationmanager = new course_publish_manager();
112 $publications = $publicationmanager->get_publications($registeredhub->huburl, $course->id, $advertise);
114 if (!empty($publications)) {
115 //get the last publication of this course
116 $publication = array_pop($publications);
118 $function = 'hub_get_courses';
119 $options = new stdClass();
120 $options->ids = array($publication->hubcourseid);
121 $options->allsitecourses = 1;
122 $params = array('', $share, !$share, $options);
123 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
124 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
125 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
126 try {
127 $publishedcourses = $xmlrpcclient->call($function, $params);
128 } catch (Exception $e) {
129 $error = $OUTPUT->notification(get_string('errorcourseinfo', 'hub', $e->getMessage()));
130 $mform->addElement('static', 'errorhub', '', $error);
135 if (!empty($publishedcourses)) {
136 $publishedcourse = $publishedcourses[0];
137 $hubcourseid = $publishedcourse['id'];
138 $defaultfullname = $publishedcourse['fullname'];
139 $defaultshortname = $publishedcourse['shortname'];
140 $defaultsummary = $publishedcourse['description'];
141 $defaultlanguage = $publishedcourse['language'];
142 $defaultpublishername = $publishedcourse['publishername'];
143 $defaultpublisheremail = $publishedcourse['publisheremail'];
144 $defaultcontributornames = $publishedcourse['contributornames'];
145 $defaultcoverage = $publishedcourse['coverage'];
146 $defaultcreatorname = $publishedcourse['creatorname'];
147 $defaultlicenceshortname = $publishedcourse['licenceshortname'];
148 $defaultsubject = $publishedcourse['subject'];
149 $defaultaudience = $publishedcourse['audience'];
150 $defaulteducationallevel = $publishedcourse['educationallevel'];
151 $defaultcreatornotes = $publishedcourse['creatornotes'];
152 $screenshotsnumber = $publishedcourse['screenshots'];
153 $privacy = $publishedcourse['privacy'];
154 } else {
155 $defaultfullname = $course->fullname;
156 $defaultshortname = $course->shortname;
157 $defaultsummary = $course->summary;
158 if (empty($course->lang)) {
159 $language = get_site()->lang;
160 if (empty($language)) {
161 $defaultlanguage = current_language();
162 } else {
163 $defaultlanguage = $language;
165 } else {
166 $defaultlanguage = $course->lang;
168 $defaultpublishername = $USER->firstname . ' ' . $USER->lastname;
169 $defaultpublisheremail = $USER->email;
170 $defaultcontributornames = '';
171 $defaultcoverage = '';
172 $defaultcreatorname = $USER->firstname . ' ' . $USER->lastname;
173 $defaultlicenceshortname = 'cc';
174 $defaultsubject = 'none';
175 $defaultaudience = HUB_AUDIENCE_STUDENTS;
176 $defaulteducationallevel = HUB_EDULEVEL_TERTIARY;
177 $defaultcreatornotes = '';
178 $screenshotsnumber = 0;
182 //the input parameters
183 $mform->addElement('header', 'moodle', get_string('publicationinfo', 'hub'));
185 $mform->addElement('text', 'name', get_string('coursename', 'hub'));
186 $mform->addRule('name', $strrequired, 'required', null, 'client');
187 $mform->setType('name', PARAM_TEXT);
188 $mform->setDefault('name', $defaultfullname);
189 $mform->addHelpButton('name', 'name', 'hub');
191 $mform->addElement('hidden', 'id', $this->_customdata['id']);
193 if ($share) {
194 $buttonlabel = get_string('shareon', 'hub', !empty($hubname) ? $hubname : $huburl);
196 $mform->addElement('hidden', 'share', $share);
198 $mform->addElement('text', 'demourl', get_string('demourl', 'hub'));
199 $mform->setType('demourl', PARAM_URL);
200 $mform->setDefault('demourl', new moodle_url("/course/view.php?id=" . $course->id));
201 $mform->addHelpButton('demourl', 'demourl', 'hub');
204 if ($advertise) {
205 if (empty($publishedcourses)) {
206 $buttonlabel = get_string('advertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
207 } else {
208 $buttonlabel = get_string('readvertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
210 $mform->addElement('hidden', 'advertise', $advertise);
211 $mform->addElement('hidden', 'courseurl', $CFG->wwwroot . "/course/view.php?id=" . $course->id);
212 $mform->addElement('static', 'courseurlstring', get_string('courseurl', 'hub'));
213 $mform->setDefault('courseurlstring', new moodle_url("/course/view.php?id=" . $course->id));
214 $mform->addHelpButton('courseurlstring', 'courseurl', 'hub');
217 $mform->addElement('text', 'courseshortname', get_string('courseshortname', 'hub'));
218 $mform->setDefault('courseshortname', $defaultshortname);
219 $mform->addHelpButton('courseshortname', 'courseshortname', 'hub');
221 $mform->addElement('textarea', 'description', get_string('description'), array('rows' => 10,
222 'cols' => 20));
223 $mform->addRule('description', $strrequired, 'required', null, 'client');
224 $mform->setDefault('description', $defaultsummary);
225 $mform->setType('description', PARAM_TEXT);
226 $mform->addHelpButton('description', 'description', 'hub');
228 $languages = get_string_manager()->get_list_of_languages();
229 asort($languages, SORT_LOCALE_STRING);
230 $mform->addElement('select', 'language', get_string('language'), $languages);
231 $mform->setDefault('language', $defaultlanguage);
232 $mform->addHelpButton('language', 'language', 'hub');
235 $mform->addElement('text', 'publishername', get_string('publishername', 'hub'));
236 $mform->setDefault('publishername', $defaultpublishername);
237 $mform->addRule('publishername', $strrequired, 'required', null, 'client');
238 $mform->addHelpButton('publishername', 'publishername', 'hub');
240 $mform->addElement('text', 'publisheremail', get_string('publisheremail', 'hub'));
241 $mform->setDefault('publisheremail', $defaultpublisheremail);
242 $mform->addRule('publisheremail', $strrequired, 'required', null, 'client');
243 $mform->addHelpButton('publisheremail', 'publisheremail', 'hub');
245 $mform->addElement('text', 'creatorname', get_string('creatorname', 'hub'));
246 $mform->addRule('creatorname', $strrequired, 'required', null, 'client');
247 $mform->setType('creatorname', PARAM_TEXT);
248 $mform->setDefault('creatorname', $defaultcreatorname);
249 $mform->addHelpButton('creatorname', 'creatorname', 'hub');
251 $mform->addElement('text', 'contributornames', get_string('contributornames', 'hub'));
252 $mform->setDefault('contributornames', $defaultcontributornames);
253 $mform->addHelpButton('contributornames', 'contributornames', 'hub');
255 $mform->addElement('text', 'coverage', get_string('tags', 'hub'));
256 $mform->setType('coverage', PARAM_TEXT);
257 $mform->setDefault('coverage', $defaultcoverage);
258 $mform->addHelpButton('coverage', 'tags', 'hub');
262 require_once($CFG->dirroot . "/lib/licenselib.php");
263 $licensemanager = new license_manager();
264 $licences = $licensemanager->get_licenses();
265 $options = array();
266 foreach ($licences as $license) {
267 $options[$license->shortname] = get_string($license->shortname, 'license');
269 $mform->addElement('select', 'licence', get_string('license'), $options);
270 $mform->setDefault('licence', $defaultlicenceshortname);
271 unset($options);
272 $mform->addHelpButton('licence', 'licence', 'hub');
274 $options = $publicationmanager->get_sorted_subjects();
276 //prepare data for the smartselect
277 foreach ($options as $key => &$option) {
278 $keylength = strlen($key);
279 if ($keylength == 10) {
280 $option = "&nbsp;&nbsp;" . $option;
281 } else if ($keylength == 12) {
282 $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
286 $options = array('none' => get_string('none', 'hub')) + $options;
287 $mform->addElement('select', 'subject', get_string('subject', 'hub'), $options);
288 unset($options);
289 $mform->addHelpButton('subject', 'subject', 'hub');
290 $mform->setDefault('subject', $defaultsubject);
291 $mform->addRule('subject', $strrequired, 'required', null, 'client');
292 $this->init_javascript_enhancement('subject', 'smartselect', array('selectablecategories' => false, 'mode' => 'compact'));
294 $options = array();
295 $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
296 $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
297 $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
298 $mform->addElement('select', 'audience', get_string('audience', 'hub'), $options);
299 $mform->setDefault('audience', $defaultaudience);
300 unset($options);
301 $mform->addHelpButton('audience', 'audience', 'hub');
303 $options = array();
304 $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
305 $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
306 $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
307 $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
308 $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
309 $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
310 $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
311 $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'hub'), $options);
312 $mform->setDefault('educationallevel', $defaulteducationallevel);
313 unset($options);
314 $mform->addHelpButton('educationallevel', 'educationallevel', 'hub');
316 $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false);
317 $mform->addElement('editor', 'creatornotes', get_string('creatornotes', 'hub'), '', $editoroptions);
318 $mform->addRule('creatornotes', $strrequired, 'required', null, 'client');
319 $mform->setDefault('creatornotes', $defaultcreatornotes);
320 $mform->setType('creatornotes', PARAM_CLEANHTML);
321 $mform->addHelpButton('creatornotes', 'creatornotes', 'hub');
323 if (!empty($screenshotsnumber)) {
325 if (!empty($privacy)) {
326 $images = array();
327 $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php', array('courseid' => $hubcourseid, 'filetype' => HUB_SCREENSHOT_FILE_TYPE));
328 for ($i = 1; $i <= $screenshotsnumber; $i = $i + 1) {
329 $params['screenshotnumber'] = $i;
330 $images[] = array(
331 'thumburl' => new moodle_url($baseurl, array('screenshotnumber' => $i)),
332 'imageurl' => new moodle_url($baseurl, array('screenshotnumber' => $i, 'imagewidth' => 'original')),
333 'title' => $defaultfullname,
334 'alt' => $defaultfullname
337 $imagegallery = new image_gallery($images, $defaultshortname);
338 $imagegallery->displayfirstimageonly = true;
339 $screenshothtml = $OUTPUT->render($imagegallery);
340 } else {
341 $screenshothtml = get_string('existingscreenshotnumber', 'hub', $screenshotsnumber);
343 $mform->addElement('static', 'existingscreenshots', get_string('existingscreenshots', 'hub'), $screenshothtml);
344 $mform->addHelpButton('existingscreenshots', 'deletescreenshots', 'hub');
345 $mform->addElement('checkbox', 'deletescreenshots', '', ' ' . get_string('deletescreenshots', 'hub'));
348 $mform->addElement('hidden', 'existingscreenshotnumber', $screenshotsnumber);
350 $mform->addElement('filemanager', 'screenshots', get_string('addscreenshots', 'hub'), null,
351 array('subdirs' => 0,
352 'maxbytes' => 1000000,
353 'maxfiles' => 3
355 $mform->addHelpButton('screenshots', 'screenshots', 'hub');
357 $this->add_action_buttons(false, $buttonlabel);
360 function validation($data, $files) {
361 global $CFG;
363 $errors = array();
365 if ($this->_form->_submitValues['subject'] == 'none') {
366 $errors['subject'] = get_string('mustselectsubject', 'hub');
369 return $errors;