45ccbfb212f87f3be0c21f4394a6e794a4ca2479
[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             $mform->setType(clean_param($hub->huburl, PARAM_ALPHANUMEXT), PARAM_ALPHANUMEXT);
63             if (empty($hubname)) {
64                 $hubname = $hub->huburl;
65             }
66             $mform->addElement('radio', 'huburl', null, ' ' . $hubname, $hub->huburl);
67             if ($hub->huburl == HUB_MOODLEORGHUBURL) {
68                 $mform->setDefault('huburl', $hub->huburl);
69             }
70         }
72         $mform->addElement('hidden', 'id', $this->_customdata['id']);
73         $mform->setType('id', PARAM_INT);
75         if ($share) {
76             $buttonlabel = get_string('shareonhub', 'hub');
77             $mform->addElement('hidden', 'share', true);
78             $mform->setType('share', PARAM_BOOL);
79         } else {
80             $buttonlabel = get_string('advertiseonhub', 'hub');
81             $mform->addElement('hidden', 'advertise', true);
82             $mform->setType('advertise', PARAM_BOOL);
83         }
85         $this->add_action_buttons(false, $buttonlabel);
86     }
88 }
90 /*
91  * Course publication form
92  */
94 class course_publication_form extends moodleform {
96     public function definition() {
97         global $CFG, $DB, $USER, $OUTPUT;
99         $strrequired = get_string('required');
100         $mform = & $this->_form;
101         $huburl = $this->_customdata['huburl'];
102         $hubname = $this->_customdata['hubname'];
103         $course = $this->_customdata['course'];
104         $advertise = $this->_customdata['advertise'];
105         $share = $this->_customdata['share'];
106         $page = $this->_customdata['page'];
107         $site = get_site();
109         //hidden parameters
110         $mform->addElement('hidden', 'huburl', $huburl);
111         $mform->setType('huburl', PARAM_URL);
112         $mform->addElement('hidden', 'hubname', $hubname);
113         $mform->setType('hubname', PARAM_TEXT);
115         //check on the hub if the course has already been published
116         $registrationmanager = new registration_manager();
117         $registeredhub = $registrationmanager->get_registeredhub($huburl);
118         $publicationmanager = new course_publish_manager();
119         $publications = $publicationmanager->get_publications($registeredhub->huburl, $course->id, $advertise);
121         if (!empty($publications)) {
122             //get the last publication of this course
123             $publication = array_pop($publications);
125             $function = 'hub_get_courses';
126             $options = new stdClass();
127             $options->ids = array($publication->hubcourseid);
128             $options->allsitecourses = 1;
129             $params = array('search' => '', 'downloadable' => $share,
130                 'enrollable' => !$share, 'options' => $options);
131             $serverurl = $huburl . "/local/hub/webservice/webservices.php";
132             require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
133             $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
134             try {
135                 $result = $xmlrpcclient->call($function, $params);
136                 $publishedcourses = $result['courses'];
137             } catch (Exception $e) {
138                 $error = $OUTPUT->notification(get_string('errorcourseinfo', 'hub', $e->getMessage()));
139                 $mform->addElement('static', 'errorhub', '', $error);
140             }
141         }
143         if (!empty($publishedcourses)) {
144             $publishedcourse = $publishedcourses[0];
145             $hubcourseid = $publishedcourse['id'];
146             $defaultfullname = $publishedcourse['fullname'];
147             $defaultshortname = $publishedcourse['shortname'];
148             $defaultsummary = $publishedcourse['description'];
149             $defaultlanguage = $publishedcourse['language'];
150             $defaultpublishername = $publishedcourse['publishername'];
151             $defaultpublisheremail = $publishedcourse['publisheremail'];
152             $defaultcontributornames = $publishedcourse['contributornames'];
153             $defaultcoverage = $publishedcourse['coverage'];
154             $defaultcreatorname = $publishedcourse['creatorname'];
155             $defaultlicenceshortname = $publishedcourse['licenceshortname'];
156             $defaultsubject = $publishedcourse['subject'];
157             $defaultaudience = $publishedcourse['audience'];
158             $defaulteducationallevel = $publishedcourse['educationallevel'];
159             $defaultcreatornotes = $publishedcourse['creatornotes'];
160             $defaultcreatornotesformat = $publishedcourse['creatornotesformat'];
161             $screenshotsnumber = $publishedcourse['screenshots'];
162             $privacy = $publishedcourse['privacy'];
163             if (($screenshotsnumber > 0) and !empty($privacy)) {
164                 $page->requires->yui_module('moodle-block_community-imagegallery',
165                         'M.blocks_community.init_imagegallery',
166                         array(array('imageids' => array($hubcourseid),
167                                 'imagenumbers' => array($screenshotsnumber),
168                                 'huburl' => $huburl)));
169             }
170         } else {
171             $defaultfullname = $course->fullname;
172             $defaultshortname = $course->shortname;
173             $defaultsummary = clean_param($course->summary, PARAM_TEXT);
174             if (empty($course->lang)) {
175                 $language = get_site()->lang;
176                 if (empty($language)) {
177                     $defaultlanguage = current_language();
178                 } else {
179                     $defaultlanguage = $language;
180                 }
181             } else {
182                 $defaultlanguage = $course->lang;
183             }
184             $defaultpublishername = $USER->firstname . ' ' . $USER->lastname;
185             $defaultpublisheremail = $USER->email;
186             $defaultcontributornames = '';
187             $defaultcoverage = '';
188             $defaultcreatorname = $USER->firstname . ' ' . $USER->lastname;
189             $defaultlicenceshortname = 'cc';
190             $defaultsubject = 'none';
191             $defaultaudience = HUB_AUDIENCE_STUDENTS;
192             $defaulteducationallevel = HUB_EDULEVEL_TERTIARY;
193             $defaultcreatornotes = '';
194             $defaultcreatornotesformat = FORMAT_HTML;
195             $screenshotsnumber = 0;
196         }
198         //the input parameters
199         $mform->addElement('header', 'moodle', get_string('publicationinfo', 'hub'));
201         $mform->addElement('text', 'name', get_string('coursename', 'hub'),
202                 array('class' => 'metadatatext'));
203         $mform->addRule('name', $strrequired, 'required', null, 'client');
204         $mform->setType('name', PARAM_TEXT);
205         $mform->setDefault('name', $defaultfullname);
206         $mform->addHelpButton('name', 'name', 'hub');
208         $mform->addElement('hidden', 'id', $this->_customdata['id']);
209         $mform->setType('id', PARAM_INT);
211         if ($share) {
212             $buttonlabel = get_string('shareon', 'hub', !empty($hubname) ? $hubname : $huburl);
214             $mform->addElement('hidden', 'share', $share);
215             $mform->setType('share', PARAM_BOOL);
216             $mform->addElement('text', 'demourl', get_string('demourl', 'hub'),
217                     array('class' => 'metadatatext'));
218             $mform->setType('demourl', PARAM_URL);
219             $mform->setDefault('demourl', new moodle_url("/course/view.php?id=" . $course->id));
220             $mform->addHelpButton('demourl', 'demourl', 'hub');
221         }
223         if ($advertise) {
224             if (empty($publishedcourses)) {
225                 $buttonlabel = get_string('advertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
226             } else {
227                 $buttonlabel = get_string('readvertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
228             }
229             $mform->addElement('hidden', 'advertise', $advertise);
230             $mform->setType('advertise', PARAM_BOOL);
231             $mform->addElement('hidden', 'courseurl', $CFG->wwwroot . "/course/view.php?id=" . $course->id);
232             $mform->setType('courseurl', PARAM_URL);
233             $mform->addElement('static', 'courseurlstring', get_string('courseurl', 'hub'));
234             $mform->setDefault('courseurlstring', new moodle_url("/course/view.php?id=" . $course->id));
235             $mform->addHelpButton('courseurlstring', 'courseurl', 'hub');
236         }
238         $mform->addElement('text', 'courseshortname', get_string('courseshortname', 'hub'),
239                 array('class' => 'metadatatext'));
240         $mform->setDefault('courseshortname', $defaultshortname);
241         $mform->addHelpButton('courseshortname', 'courseshortname', 'hub');
242         $mform->setType('courseshortname', PARAM_TEXT);
243         $mform->addElement('textarea', 'description', get_string('description'), array('rows' => 10,
244             'cols' => 57));
245         $mform->addRule('description', $strrequired, 'required', null, 'client');
246         $mform->setDefault('description', $defaultsummary);
247         $mform->setType('description', PARAM_TEXT);
248         $mform->addHelpButton('description', 'description', 'hub');
250         $languages = get_string_manager()->get_list_of_languages();
251         core_collator::asort($languages);
252         $mform->addElement('select', 'language', get_string('language'), $languages);
253         $mform->setDefault('language', $defaultlanguage);
254         $mform->addHelpButton('language', 'language', 'hub');
257         $mform->addElement('text', 'publishername', get_string('publishername', 'hub'),
258                 array('class' => 'metadatatext'));
259         $mform->setDefault('publishername', $defaultpublishername);
260         $mform->addRule('publishername', $strrequired, 'required', null, 'client');
261         $mform->addHelpButton('publishername', 'publishername', 'hub');
262         $mform->setType('publishername', PARAM_NOTAGS);
264         $mform->addElement('text', 'publisheremail', get_string('publisheremail', 'hub'),
265                 array('class' => 'metadatatext'));
266         $mform->setDefault('publisheremail', $defaultpublisheremail);
267         $mform->addRule('publisheremail', $strrequired, 'required', null, 'client');
268         $mform->addHelpButton('publisheremail', 'publisheremail', 'hub');
269         $mform->setType('publisheremail', PARAM_EMAIL);
271         $mform->addElement('text', 'creatorname', get_string('creatorname', 'hub'),
272                 array('class' => 'metadatatext'));
273         $mform->addRule('creatorname', $strrequired, 'required', null, 'client');
274         $mform->setType('creatorname', PARAM_NOTAGS);
275         $mform->setDefault('creatorname', $defaultcreatorname);
276         $mform->addHelpButton('creatorname', 'creatorname', 'hub');
278         $mform->addElement('text', 'contributornames', get_string('contributornames', 'hub'),
279                 array('class' => 'metadatatext'));
280         $mform->setDefault('contributornames', $defaultcontributornames);
281         $mform->addHelpButton('contributornames', 'contributornames', 'hub');
282         $mform->setType('contributornames', PARAM_NOTAGS);
284         $mform->addElement('text', 'coverage', get_string('tags', 'hub'),
285                 array('class' => 'metadatatext'));
286         $mform->setType('coverage', PARAM_TEXT);
287         $mform->setDefault('coverage', $defaultcoverage);
288         $mform->addHelpButton('coverage', 'tags', 'hub');
292         require_once($CFG->libdir . "/licenselib.php");
293         $licensemanager = new license_manager();
294         $licences = $licensemanager->get_licenses();
295         $options = array();
296         foreach ($licences as $license) {
297             $options[$license->shortname] = get_string($license->shortname, 'license');
298         }
299         $mform->addElement('select', 'licence', get_string('license'), $options);
300         $mform->setDefault('licence', $defaultlicenceshortname);
301         unset($options);
302         $mform->addHelpButton('licence', 'licence', 'hub');
304         $options = $publicationmanager->get_sorted_subjects();
306         $mform->addElement('searchableselector', 'subject',
307             get_string('subject', 'hub'), $options);
308         unset($options);
309         $mform->addHelpButton('subject', 'subject', 'hub');
310         $mform->setDefault('subject', $defaultsubject);
311         $mform->addRule('subject', $strrequired, 'required', null, 'client');
313         $options = array();
314         $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
315         $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
316         $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
317         $mform->addElement('select', 'audience', get_string('audience', 'hub'), $options);
318         $mform->setDefault('audience', $defaultaudience);
319         unset($options);
320         $mform->addHelpButton('audience', 'audience', 'hub');
322         $options = array();
323         $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
324         $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
325         $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
326         $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
327         $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
328         $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
329         $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
330         $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'hub'), $options);
331         $mform->setDefault('educationallevel', $defaulteducationallevel);
332         unset($options);
333         $mform->addHelpButton('educationallevel', 'educationallevel', 'hub');
335         $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false);
336         $mform->addElement('editor', 'creatornotes', get_string('creatornotes', 'hub'), '', $editoroptions);
337         $mform->addRule('creatornotes', $strrequired, 'required', null, 'client');
338         $mform->setType('creatornotes', PARAM_CLEANHTML);
339         $mform->addHelpButton('creatornotes', 'creatornotes', 'hub');
341         if ($advertise) {
342             if (!empty($screenshotsnumber)) {
344                 if (!empty($privacy)) {
345                     $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php',
346                                     array('courseid' => $hubcourseid, 'filetype' => HUB_SCREENSHOT_FILE_TYPE));
347                     $screenshothtml = html_writer::empty_tag('img',
348                                     array('src' => $baseurl, 'alt' => $defaultfullname));
349                     $screenshothtml = html_writer::tag('div', $screenshothtml,
350                                     array('class' => 'coursescreenshot',
351                                         'id' => 'image-' . $hubcourseid));
352                 } else {
353                     $screenshothtml = get_string('existingscreenshotnumber', 'hub', $screenshotsnumber);
354                 }
355                 $mform->addElement('static', 'existingscreenshots', get_string('existingscreenshots', 'hub'), $screenshothtml);
356                 $mform->addHelpButton('existingscreenshots', 'deletescreenshots', 'hub');
357                 $mform->addElement('checkbox', 'deletescreenshots', '', ' ' . get_string('deletescreenshots', 'hub'));
358             }
360             $mform->addElement('hidden', 'existingscreenshotnumber', $screenshotsnumber);
361             $mform->setType('existingscreenshotnumber', PARAM_INT);
362         }
364         $mform->addElement('filemanager', 'screenshots', get_string('addscreenshots', 'hub'), null,
365                 array('subdirs' => 0,
366                     'maxbytes' => 1000000,
367                     'maxfiles' => 3
368         ));
369         $mform->addHelpButton('screenshots', 'screenshots', 'hub');
371         $this->add_action_buttons(false, $buttonlabel);
373         //set default value for creatornotes editor
374         $data = new stdClass();
375         $data->creatornotes = array();
376         $data->creatornotes['text'] = $defaultcreatornotes;
377         $data->creatornotes['format'] = $defaultcreatornotesformat;
378         $this->set_data($data);
379     }
381     function validation($data, $files) {
382         global $CFG;
384         $errors = array();
386         if ($this->_form->_submitValues['subject'] == 'none') {
387             $errors['subject'] = get_string('mustselectsubject', 'hub');
388         }
390         return $errors;
391     }