PageRenderTime 52ms CodeModel.GetById 29ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/course/publish/forms.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 405 lines | 304 code | 60 blank | 41 comment | 28 complexity | 76588adbd19c99d4f74c78014c921c09 MD5 | raw file
  1<?php
  2
  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///////////////////////////////////////////////////////////////////////////
 22
 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 */
 32
 33
 34require_once($CFG->libdir . '/formslib.php');
 35require_once($CFG->dirroot . "/" . $CFG->admin . "/registration/lib.php");
 36require_once($CFG->dirroot . "/course/publish/lib.php");
 37
 38/*
 39 * Hub selector to choose on which hub we want to publish.
 40 */
 41
 42class hub_publish_selector_form extends moodleform {
 43
 44    public function definition() {
 45        global $CFG;
 46        $mform = & $this->_form;
 47        $share = $this->_customdata['share'];
 48
 49        $mform->addElement('header', 'site', get_string('selecthub', 'hub'));
 50
 51        $mform->addElement('static', 'info', '', get_string('selecthubinfo', 'hub') . html_writer::empty_tag('br'));
 52
 53        $registrationmanager = new registration_manager();
 54        $registeredhubs = $registrationmanager->get_registered_on_hubs();
 55
 56        //Public hub list
 57        $options = array();
 58        foreach ($registeredhubs as $hub) {
 59
 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        }
 71
 72        $mform->addElement('hidden', 'id', $this->_customdata['id']);
 73        $mform->setType('id', PARAM_INT);
 74
 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        }
 84
 85        $this->add_action_buttons(false, $buttonlabel);
 86    }
 87
 88}
 89
 90/*
 91 * Course publication form
 92 */
 93
 94class course_publication_form extends moodleform {
 95
 96    public function definition() {
 97        global $CFG, $DB, $USER, $OUTPUT;
 98
 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();
108
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);
114
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);
120
121        if (!empty($publications)) {
122            //get the last publication of this course
123            $publication = array_pop($publications);
124
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        }
142
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        }
197
198        //the input parameters
199        $mform->addElement('header', 'moodle', get_string('publicationinfo', 'hub'));
200
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');
207
208        $mform->addElement('hidden', 'id', $this->_customdata['id']);
209        $mform->setType('id', PARAM_INT);
210
211        if ($share) {
212            $buttonlabel = get_string('shareon', 'hub', !empty($hubname) ? $hubname : $huburl);
213
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        }
222
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        }
237
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');
249
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');
255
256
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);
263
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);
270
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');
277
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);
283
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');
289
290
291
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');
303
304        $options = $publicationmanager->get_sorted_subjects();
305
306        //prepare data for the smartselect
307        foreach ($options as $key => &$option) {
308            $keylength = strlen($key);
309            if ($keylength == 10) {
310                $option = "&nbsp;&nbsp;" . $option;
311            } else if ($keylength == 12) {
312                $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
313            }
314        }
315
316        $options = array('none' => get_string('none', 'hub')) + $options;
317        $mform->addElement('select', 'subject', get_string('subject', 'hub'), $options);
318        unset($options);
319        $mform->addHelpButton('subject', 'subject', 'hub');
320        $mform->setDefault('subject', $defaultsubject);
321        $mform->addRule('subject', $strrequired, 'required', null, 'client');
322        $this->init_javascript_enhancement('subject', 'smartselect', array('selectablecategories' => false, 'mode' => 'compact'));
323
324        $options = array();
325        $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
326        $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
327        $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
328        $mform->addElement('select', 'audience', get_string('audience', 'hub'), $options);
329        $mform->setDefault('audience', $defaultaudience);
330        unset($options);
331        $mform->addHelpButton('audience', 'audience', 'hub');
332
333        $options = array();
334        $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
335        $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
336        $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
337        $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
338        $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
339        $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
340        $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
341        $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'hub'), $options);
342        $mform->setDefault('educationallevel', $defaulteducationallevel);
343        unset($options);
344        $mform->addHelpButton('educationallevel', 'educationallevel', 'hub');
345
346        $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false);
347        $mform->addElement('editor', 'creatornotes', get_string('creatornotes', 'hub'), '', $editoroptions);
348        $mform->addRule('creatornotes', $strrequired, 'required', null, 'client');
349        $mform->setType('creatornotes', PARAM_CLEANHTML);
350        $mform->addHelpButton('creatornotes', 'creatornotes', 'hub');
351
352        if ($advertise) {
353            if (!empty($screenshotsnumber)) {
354
355                if (!empty($privacy)) {
356                    $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php',
357                                    array('courseid' => $hubcourseid, 'filetype' => HUB_SCREENSHOT_FILE_TYPE));
358                    $screenshothtml = html_writer::empty_tag('img',
359                                    array('src' => $baseurl, 'alt' => $defaultfullname));
360                    $screenshothtml = html_writer::tag('div', $screenshothtml,
361                                    array('class' => 'coursescreenshot',
362                                        'id' => 'image-' . $hubcourseid));
363                } else {
364                    $screenshothtml = get_string('existingscreenshotnumber', 'hub', $screenshotsnumber);
365                }
366                $mform->addElement('static', 'existingscreenshots', get_string('existingscreenshots', 'hub'), $screenshothtml);
367                $mform->addHelpButton('existingscreenshots', 'deletescreenshots', 'hub');
368                $mform->addElement('checkbox', 'deletescreenshots', '', ' ' . get_string('deletescreenshots', 'hub'));
369            }
370
371            $mform->addElement('hidden', 'existingscreenshotnumber', $screenshotsnumber);
372            $mform->setType('existingscreenshotnumber', PARAM_INT);
373        }
374
375        $mform->addElement('filemanager', 'screenshots', get_string('addscreenshots', 'hub'), null,
376                array('subdirs' => 0,
377                    'maxbytes' => 1000000,
378                    'maxfiles' => 3
379        ));
380        $mform->addHelpButton('screenshots', 'screenshots', 'hub');
381
382        $this->add_action_buttons(false, $buttonlabel);
383
384        //set default value for creatornotes editor
385        $data = new stdClass();
386        $data->creatornotes = array();
387        $data->creatornotes['text'] = $defaultcreatornotes;
388        $data->creatornotes['format'] = $defaultcreatornotesformat;
389        $this->set_data($data);
390    }
391
392    function validation($data, $files) {
393        global $CFG;
394
395        $errors = array();
396
397        if ($this->_form->_submitValues['subject'] == 'none') {
398            $errors['subject'] = get_string('mustselectsubject', 'hub');
399        }
400
401        return $errors;
402    }
403
404}
405