7998e5d4bf2b186ad9d62fdc27d6fc0485e572bd
[moodle.git] / course / publish / forms.php
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 ///////////////////////////////////////////////////////////////////////////
23 /*
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
29  *
30  * The forms used for course publication
31  */
34 require_once($CFG->libdir . '/formslib.php');
35 require_once($CFG->dirroot . "/" . $CFG->admin . "/registration/lib.php");
36 require_once($CFG->dirroot . "/course/publish/lib.php");
38 /*
39  * Hub selector to choose on which hub we want to publish.
40  */
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;
64             }
65             $mform->addElement('radio', 'huburl', null, ' ' . $hubname, $hub->huburl);
66             if ($hub->huburl == HUB_MOODLEORGHUBURL) {
67                 $mform->setDefault('huburl', $hub->huburl);
68             }
69         }
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);
79         }
81         $this->add_action_buttons(false, $buttonlabel);
82     }
84 }
86 /*
87  * Course publication form
88  */
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('search' => '', 'downloadable' => $share,
123                 'enrollable' => !$share, 'options' => $options);
124             $serverurl = $huburl . "/local/hub/webservice/webservices.php";
125             require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
126             $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
127             try {
128                 $result = $xmlrpcclient->call($function, $params);
129                 $publishedcourses = $result['courses'];
130             } catch (Exception $e) {
131                 $error = $OUTPUT->notification(get_string('errorcourseinfo', 'hub', $e->getMessage()));
132                 $mform->addElement('static', 'errorhub', '', $error);
133             }
134         }
137         if (!empty($publishedcourses)) {
138             $publishedcourse = $publishedcourses[0];
139             $hubcourseid = $publishedcourse['id'];
140             $defaultfullname = $publishedcourse['fullname'];
141             $defaultshortname = $publishedcourse['shortname'];
142             $defaultsummary = $publishedcourse['description'];
143             $defaultlanguage = $publishedcourse['language'];
144             $defaultpublishername = $publishedcourse['publishername'];
145             $defaultpublisheremail = $publishedcourse['publisheremail'];
146             $defaultcontributornames = $publishedcourse['contributornames'];
147             $defaultcoverage = $publishedcourse['coverage'];
148             $defaultcreatorname = $publishedcourse['creatorname'];
149             $defaultlicenceshortname = $publishedcourse['licenceshortname'];
150             $defaultsubject = $publishedcourse['subject'];
151             $defaultaudience = $publishedcourse['audience'];
152             $defaulteducationallevel = $publishedcourse['educationallevel'];
153             $defaultcreatornotes = $publishedcourse['creatornotes'];
154             $screenshotsnumber = $publishedcourse['screenshots'];
155             $privacy = $publishedcourse['privacy'];
156         } else {
157             $defaultfullname = $course->fullname;
158             $defaultshortname = $course->shortname;
159             $defaultsummary = clean_param($course->summary, PARAM_TEXT);
160             if (empty($course->lang)) {
161                 $language = get_site()->lang;
162                 if (empty($language)) {
163                     $defaultlanguage = current_language();
164                 } else {
165                     $defaultlanguage = $language;
166                 }
167             } else {
168                 $defaultlanguage = $course->lang;
169             }
170             $defaultpublishername = $USER->firstname . ' ' . $USER->lastname;
171             $defaultpublisheremail = $USER->email;
172             $defaultcontributornames = '';
173             $defaultcoverage = '';
174             $defaultcreatorname = $USER->firstname . ' ' . $USER->lastname;
175             $defaultlicenceshortname = 'cc';
176             $defaultsubject = 'none';
177             $defaultaudience = HUB_AUDIENCE_STUDENTS;
178             $defaulteducationallevel = HUB_EDULEVEL_TERTIARY;
179             $defaultcreatornotes = '';
180             $screenshotsnumber = 0;
181         }
184         //the input parameters
185         $mform->addElement('header', 'moodle', get_string('publicationinfo', 'hub'));
187         $mform->addElement('text', 'name', get_string('coursename', 'hub'));
188         $mform->addRule('name', $strrequired, 'required', null, 'client');
189         $mform->setType('name', PARAM_TEXT);
190         $mform->setDefault('name', $defaultfullname);
191         $mform->addHelpButton('name', 'name', 'hub');
193         $mform->addElement('hidden', 'id', $this->_customdata['id']);
195         if ($share) {
196             $buttonlabel = get_string('shareon', 'hub', !empty($hubname) ? $hubname : $huburl);
198             $mform->addElement('hidden', 'share', $share);
200             $mform->addElement('text', 'demourl', get_string('demourl', 'hub'));
201             $mform->setType('demourl', PARAM_URL);
202             $mform->setDefault('demourl', new moodle_url("/course/view.php?id=" . $course->id));
203             $mform->addHelpButton('demourl', 'demourl', 'hub');
204         }
206         if ($advertise) {
207             if (empty($publishedcourses)) {
208                 $buttonlabel = get_string('advertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
209             } else {
210                 $buttonlabel = get_string('readvertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
211             }
212             $mform->addElement('hidden', 'advertise', $advertise);
213             $mform->addElement('hidden', 'courseurl', $CFG->wwwroot . "/course/view.php?id=" . $course->id);
214             $mform->addElement('static', 'courseurlstring', get_string('courseurl', 'hub'));
215             $mform->setDefault('courseurlstring', new moodle_url("/course/view.php?id=" . $course->id));
216             $mform->addHelpButton('courseurlstring', 'courseurl', 'hub');
217         }
219         $mform->addElement('text', 'courseshortname', get_string('courseshortname', 'hub'));
220         $mform->setDefault('courseshortname', $defaultshortname);
221         $mform->addHelpButton('courseshortname', 'courseshortname', 'hub');
223         $mform->addElement('textarea', 'description', get_string('description'), array('rows' => 10,
224             'cols' => 20));
225         $mform->addRule('description', $strrequired, 'required', null, 'client');
226         $mform->setDefault('description', $defaultsummary);
227         $mform->setType('description', PARAM_TEXT);
228         $mform->addHelpButton('description', 'description', 'hub');
230         $languages = get_string_manager()->get_list_of_languages();
231         textlib_get_instance()->asort($languages);
232         $mform->addElement('select', 'language', get_string('language'), $languages);
233         $mform->setDefault('language', $defaultlanguage);
234         $mform->addHelpButton('language', 'language', 'hub');
237         $mform->addElement('text', 'publishername', get_string('publishername', 'hub'));
238         $mform->setDefault('publishername', $defaultpublishername);
239         $mform->addRule('publishername', $strrequired, 'required', null, 'client');
240         $mform->addHelpButton('publishername', 'publishername', 'hub');
242         $mform->addElement('text', 'publisheremail', get_string('publisheremail', 'hub'));
243         $mform->setDefault('publisheremail', $defaultpublisheremail);
244         $mform->addRule('publisheremail', $strrequired, 'required', null, 'client');
245         $mform->addHelpButton('publisheremail', 'publisheremail', 'hub');
247         $mform->addElement('text', 'creatorname', get_string('creatorname', 'hub'));
248         $mform->addRule('creatorname', $strrequired, 'required', null, 'client');
249         $mform->setType('creatorname', PARAM_TEXT);
250         $mform->setDefault('creatorname', $defaultcreatorname);
251         $mform->addHelpButton('creatorname', 'creatorname', 'hub');
253         $mform->addElement('text', 'contributornames', get_string('contributornames', 'hub'));
254         $mform->setDefault('contributornames', $defaultcontributornames);
255         $mform->addHelpButton('contributornames', 'contributornames', 'hub');
257         $mform->addElement('text', 'coverage', get_string('tags', 'hub'));
258         $mform->setType('coverage', PARAM_TEXT);
259         $mform->setDefault('coverage', $defaultcoverage);
260         $mform->addHelpButton('coverage', 'tags', 'hub');
264         require_once($CFG->libdir . "/licenselib.php");
265         $licensemanager = new license_manager();
266         $licences = $licensemanager->get_licenses();
267         $options = array();
268         foreach ($licences as $license) {
269             $options[$license->shortname] = get_string($license->shortname, 'license');
270         }
271         $mform->addElement('select', 'licence', get_string('license'), $options);
272         $mform->setDefault('licence', $defaultlicenceshortname);
273         unset($options);
274         $mform->addHelpButton('licence', 'licence', 'hub');
276         $options = $publicationmanager->get_sorted_subjects();
278         //prepare data for the smartselect
279         foreach ($options as $key => &$option) {
280             $keylength = strlen($key);
281             if ($keylength == 10) {
282                 $option = "&nbsp;&nbsp;" . $option;
283             } else if ($keylength == 12) {
284                 $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
285             }
286         }
288         $options = array('none' => get_string('none', 'hub')) + $options;
289         $mform->addElement('select', 'subject', get_string('subject', 'hub'), $options);
290         unset($options);
291         $mform->addHelpButton('subject', 'subject', 'hub');
292         $mform->setDefault('subject', $defaultsubject);
293         $mform->addRule('subject', $strrequired, 'required', null, 'client');
294         $this->init_javascript_enhancement('subject', 'smartselect', array('selectablecategories' => false, 'mode' => 'compact'));
296         $options = array();
297         $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
298         $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
299         $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
300         $mform->addElement('select', 'audience', get_string('audience', 'hub'), $options);
301         $mform->setDefault('audience', $defaultaudience);
302         unset($options);
303         $mform->addHelpButton('audience', 'audience', 'hub');
305         $options = array();
306         $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
307         $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
308         $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
309         $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
310         $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
311         $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
312         $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
313         $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'hub'), $options);
314         $mform->setDefault('educationallevel', $defaulteducationallevel);
315         unset($options);
316         $mform->addHelpButton('educationallevel', 'educationallevel', 'hub');
318         $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false);
319         $mform->addElement('editor', 'creatornotes', get_string('creatornotes', 'hub'), '', $editoroptions);
320         $mform->addRule('creatornotes', $strrequired, 'required', null, 'client');
321         $mform->setDefault('creatornotes', $defaultcreatornotes);
322         $mform->setType('creatornotes', PARAM_CLEANHTML);
323         $mform->addHelpButton('creatornotes', 'creatornotes', 'hub');
325         if (!empty($screenshotsnumber)) {
327             if (!empty($privacy)) {
328                 $images = array();
329                 $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php', array('courseid' => $hubcourseid, 'filetype' => HUB_SCREENSHOT_FILE_TYPE));
330                 for ($i = 1; $i <= $screenshotsnumber; $i = $i + 1) {
331                     $params['screenshotnumber'] = $i;
332                     $images[] = array(
333                         'thumburl' => new moodle_url($baseurl, array('screenshotnumber' => $i)),
334                         'imageurl' => new moodle_url($baseurl, array('screenshotnumber' => $i, 'imagewidth' => 'original')),
335                         'title' => $defaultfullname,
336                         'alt' => $defaultfullname
337                     );
338                 }
339                 $imagegallery = new image_gallery($images, $defaultshortname);
340                 $imagegallery->displayfirstimageonly = true;
341                 $screenshothtml = $OUTPUT->render($imagegallery);
342             } else {
343                 $screenshothtml = get_string('existingscreenshotnumber', 'hub', $screenshotsnumber);
344             }
345             $mform->addElement('static', 'existingscreenshots', get_string('existingscreenshots', 'hub'), $screenshothtml);
346             $mform->addHelpButton('existingscreenshots', 'deletescreenshots', 'hub');
347             $mform->addElement('checkbox', 'deletescreenshots', '', ' ' . get_string('deletescreenshots', 'hub'));
348         }
350         $mform->addElement('hidden', 'existingscreenshotnumber', $screenshotsnumber);
352         $mform->addElement('filemanager', 'screenshots', get_string('addscreenshots', 'hub'), null,
353                 array('subdirs' => 0,
354                     'maxbytes' => 1000000,
355                     'maxfiles' => 3
356         ));
357         $mform->addHelpButton('screenshots', 'screenshots', 'hub');
359         $this->add_action_buttons(false, $buttonlabel);
360     }
362     function validation($data, $files) {
363         global $CFG;
365         $errors = array();
367         if ($this->_form->_submitValues['subject'] == 'none') {
368             $errors['subject'] = get_string('mustselectsubject', 'hub');
369         }
371         return $errors;
372     }
375 ?>