MDL-59495 moodlenet: move all moodlenet code into new namespace
[moodle.git] / lib / classes / hub / course_publication_form.php
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/>.
17 /**
18  * Class course_publication_form
19  *
20  * @package    core
21  * @copyright  2017 Marina Glancy
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 namespace core\hub;
26 defined('MOODLE_INTERNAL') || die();
28 use stdClass;
29 use license_manager;
30 use moodle_url;
31 use core_collator;
33 global $CFG;
34 require_once($CFG->libdir . '/formslib.php');
35 require_once($CFG->libdir . '/licenselib.php');
37 /**
38  * The forms used for course publication
39  *
40  * @package    core
41  * @copyright  2017 Marina Glancy
42  * @author     Jerome Mouneyrac <jerome@mouneyrac.com>
43  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class course_publication_form extends \moodleform {
47     /**
48      * Form definition
49      */
50     public function definition() {
51         global $CFG, $USER, $PAGE;
53         $strrequired = get_string('required');
54         $mform = & $this->_form;
55         $course = $this->_customdata['course'];
56         if (!empty($this->_customdata['publication'])) {
57             // We are editing existing publication.
58             $publication = $this->_customdata['publication'];
59             $advertise = $publication->enrollable;
60             $publishedcourse = publication::get_published_course($publication);
61         } else {
62             $publication = null;
63             $advertise = $this->_customdata['advertise'];
64         }
65         $share = !$advertise;
67         if (!empty($publishedcourse)) {
68             $hubcourseid = $publishedcourse['id'];
69             $defaultfullname = $publishedcourse['fullname'];
70             $defaultshortname = $publishedcourse['shortname'];
71             $defaultsummary = $publishedcourse['description'];
72             $defaultlanguage = $publishedcourse['language'];
73             $defaultpublishername = $publishedcourse['publishername'];
74             $defaultpublisheremail = $publishedcourse['publisheremail'];
75             $defaultcontributornames = $publishedcourse['contributornames'];
76             $defaultcoverage = $publishedcourse['coverage'];
77             $defaultcreatorname = $publishedcourse['creatorname'];
78             $defaultlicenceshortname = $publishedcourse['licenceshortname'];
79             $defaultsubject = $publishedcourse['subject'];
80             $defaultaudience = $publishedcourse['audience'];
81             $defaulteducationallevel = $publishedcourse['educationallevel'];
82             $defaultcreatornotes = $publishedcourse['creatornotes'];
83             $defaultcreatornotesformat = $publishedcourse['creatornotesformat'];
84             $screenshotsnumber = $publishedcourse['screenshots'];
85             $screenshotbaseurl = $publishedcourse['screenshotbaseurl'];
86             $privacy = $publishedcourse['privacy'];
87             if (($screenshotsnumber > 0) and !empty($privacy)) {
88                 $PAGE->requires->yui_module('moodle-block_community-imagegallery',
89                     'M.blocks_community.init_imagegallery',
90                     array(array('imageids' => array($hubcourseid),
91                         'imagenumbers' => array($screenshotsnumber),
92                         'huburl' => HUB_MOODLEORGHUBURL)));
93             }
94         } else {
95             $defaultfullname = $course->fullname;
96             $defaultshortname = $course->shortname;
97             $defaultsummary = clean_param($course->summary, PARAM_TEXT);
98             if (empty($course->lang)) {
99                 $language = get_site()->lang;
100                 if (empty($language)) {
101                     $defaultlanguage = current_language();
102                 } else {
103                     $defaultlanguage = $language;
104                 }
105             } else {
106                 $defaultlanguage = $course->lang;
107             }
108             $defaultpublishername = $USER->firstname . ' ' . $USER->lastname;
109             $defaultpublisheremail = $USER->email;
110             $defaultcontributornames = '';
111             $defaultcoverage = '';
112             $defaultcreatorname = $USER->firstname . ' ' . $USER->lastname;
113             $defaultlicenceshortname = 'cc';
114             $defaultsubject = 'none';
115             $defaultaudience = publication::HUB_AUDIENCE_STUDENTS;
116             $defaulteducationallevel = publication::HUB_EDULEVEL_TERTIARY;
117             $defaultcreatornotes = '';
118             $defaultcreatornotesformat = FORMAT_HTML;
119             $screenshotsnumber = 0;
120             $screenshotbaseurl = null;
121         }
123         // The input parameters.
124         $mform->addElement('header', 'moodle', get_string('publicationinfo', 'hub'));
126         $mform->addElement('text', 'name', get_string('coursename', 'hub'),
127             array('class' => 'metadatatext'));
128         $mform->addRule('name', $strrequired, 'required', null, 'client');
129         $mform->setType('name', PARAM_TEXT);
130         $mform->setDefault('name', $defaultfullname);
131         $mform->addHelpButton('name', 'name', 'hub');
133         $mform->addElement('hidden', 'id', $course->id);
134         $mform->setType('id', PARAM_INT);
136         $mform->addElement('hidden', 'publicationid', $publication ? $publication->id : null);
137         $mform->setType('publicationid', PARAM_INT);
139         if ($share) {
140             $buttonlabel = get_string('shareon', 'hub', 'Moodle.net');
142             $mform->addElement('hidden', 'share', $share);
143             $mform->setType('share', PARAM_BOOL);
144             $mform->addElement('text', 'demourl', get_string('demourl', 'hub'),
145                 array('class' => 'metadatatext'));
146             $mform->setType('demourl', PARAM_URL);
147             $mform->setDefault('demourl', new moodle_url("/course/view.php?id=" . $course->id));
148             $mform->addHelpButton('demourl', 'demourl', 'hub');
149         }
151         if ($advertise) {
152             if (!$publication) {
153                 $buttonlabel = get_string('advertiseon', 'hub', 'Moodle.net');
154             } else {
155                 $buttonlabel = get_string('readvertiseon', 'hub', 'Moodle.net');
156             }
157             $mform->addElement('hidden', 'advertise', $advertise);
158             $mform->setType('advertise', PARAM_BOOL);
159             $mform->addElement('hidden', 'courseurl', $CFG->wwwroot . "/course/view.php?id=" . $course->id);
160             $mform->setType('courseurl', PARAM_URL);
161             $mform->addElement('static', 'courseurlstring', get_string('courseurl', 'hub'));
162             $mform->setDefault('courseurlstring', new moodle_url("/course/view.php?id=" . $course->id));
163             $mform->addHelpButton('courseurlstring', 'courseurl', 'hub');
164         }
166         $mform->addElement('text', 'courseshortname', get_string('courseshortname', 'hub'),
167             array('class' => 'metadatatext'));
168         $mform->setDefault('courseshortname', $defaultshortname);
169         $mform->addHelpButton('courseshortname', 'courseshortname', 'hub');
170         $mform->setType('courseshortname', PARAM_TEXT);
171         $mform->addElement('textarea', 'description', get_string('description', 'hub'), array('rows' => 10,
172             'cols' => 57));
173         $mform->addRule('description', $strrequired, 'required', null, 'client');
174         $mform->setDefault('description', $defaultsummary);
175         $mform->setType('description', PARAM_TEXT);
176         $mform->addHelpButton('description', 'description', 'hub');
178         $languages = get_string_manager()->get_list_of_languages();
179         core_collator::asort($languages);
180         $mform->addElement('select', 'language', get_string('language'), $languages);
181         $mform->setDefault('language', $defaultlanguage);
182         $mform->addHelpButton('language', 'language', 'hub');
184         $mform->addElement('text', 'publishername', get_string('publishername', 'hub'),
185             array('class' => 'metadatatext'));
186         $mform->setDefault('publishername', $defaultpublishername);
187         $mform->addRule('publishername', $strrequired, 'required', null, 'client');
188         $mform->addHelpButton('publishername', 'publishername', 'hub');
189         $mform->setType('publishername', PARAM_NOTAGS);
191         $mform->addElement('text', 'publisheremail', get_string('publisheremail', 'hub'),
192             array('class' => 'metadatatext'));
193         $mform->setDefault('publisheremail', $defaultpublisheremail);
194         $mform->addRule('publisheremail', $strrequired, 'required', null, 'client');
195         $mform->addHelpButton('publisheremail', 'publisheremail', 'hub');
196         $mform->setType('publisheremail', PARAM_EMAIL);
198         $mform->addElement('text', 'creatorname', get_string('creatorname', 'hub'),
199             array('class' => 'metadatatext'));
200         $mform->addRule('creatorname', $strrequired, 'required', null, 'client');
201         $mform->setType('creatorname', PARAM_NOTAGS);
202         $mform->setDefault('creatorname', $defaultcreatorname);
203         $mform->addHelpButton('creatorname', 'creatorname', 'hub');
205         $mform->addElement('text', 'contributornames', get_string('contributornames', 'hub'),
206             array('class' => 'metadatatext'));
207         $mform->setDefault('contributornames', $defaultcontributornames);
208         $mform->addHelpButton('contributornames', 'contributornames', 'hub');
209         $mform->setType('contributornames', PARAM_NOTAGS);
211         $mform->addElement('text', 'coverage', get_string('tags', 'hub'),
212             array('class' => 'metadatatext'));
213         $mform->setType('coverage', PARAM_TEXT);
214         $mform->setDefault('coverage', $defaultcoverage);
215         $mform->addHelpButton('coverage', 'tags', 'hub');
217         $licensemanager = new license_manager();
218         $licences = $licensemanager->get_licenses();
219         $options = array();
220         foreach ($licences as $license) {
221             $options[$license->shortname] = get_string($license->shortname, 'license');
222         }
223         $mform->addElement('select', 'licence', get_string('license'), $options);
224         $mform->setDefault('licence', $defaultlicenceshortname);
225         unset($options);
226         $mform->addHelpButton('licence', 'licence', 'hub');
228         $options = publication::get_sorted_subjects();
230         $mform->addElement('searchableselector', 'subject',
231             get_string('subject', 'hub'), $options);
232         unset($options);
233         $mform->addHelpButton('subject', 'subject', 'hub');
234         $mform->setDefault('subject', $defaultsubject);
235         $mform->addRule('subject', $strrequired, 'required', null, 'client');
237         $options = publication::audience_options();
238         $mform->addElement('select', 'audience', get_string('audience', 'hub'), $options);
239         $mform->setDefault('audience', $defaultaudience);
240         unset($options);
241         $mform->addHelpButton('audience', 'audience', 'hub');
243         $options = publication::educational_level_options();
244         $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'hub'), $options);
245         $mform->setDefault('educationallevel', $defaulteducationallevel);
246         unset($options);
247         $mform->addHelpButton('educationallevel', 'educationallevel', 'hub');
249         $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false);
250         $mform->addElement('editor', 'creatornotes', get_string('creatornotes', 'hub'), '', $editoroptions);
251         $mform->addRule('creatornotes', $strrequired, 'required', null, 'client');
252         $mform->setType('creatornotes', PARAM_CLEANHTML);
253         $mform->addHelpButton('creatornotes', 'creatornotes', 'hub');
255         if ($advertise) {
256             if (!empty($screenshotsnumber)) {
257                 if (!empty($privacy)) {
258                     $screenshothtml = \html_writer::empty_tag('img',
259                         array('src' => $screenshotbaseurl, 'alt' => $defaultfullname));
260                     $screenshothtml = \html_writer::tag('div', $screenshothtml,
261                         array('class' => 'coursescreenshot',
262                             'id' => 'image-' . $hubcourseid));
263                 } else {
264                     $screenshothtml = get_string('existingscreenshotnumber', 'hub', $screenshotsnumber);
265                 }
266                 $mform->addElement('static', 'existingscreenshots', get_string('existingscreenshots', 'hub'), $screenshothtml);
267                 $mform->addHelpButton('existingscreenshots', 'deletescreenshots', 'hub');
268                 $mform->addElement('checkbox', 'deletescreenshots', '', ' ' . get_string('deletescreenshots', 'hub'));
269             }
271             $mform->addElement('hidden', 'existingscreenshotnumber', $screenshotsnumber);
272             $mform->setType('existingscreenshotnumber', PARAM_INT);
273         }
275         $mform->addElement('filemanager', 'screenshots', get_string('addscreenshots', 'hub'), null,
276             array('subdirs' => 0,
277                 'maxbytes' => 1000000,
278                 'maxfiles' => 3
279             ));
280         $mform->addHelpButton('screenshots', 'screenshots', 'hub');
282         $this->add_action_buttons(false, $buttonlabel);
284         // Set default value for creatornotes editor.
285         $data = new stdClass();
286         $data->creatornotes = array();
287         $data->creatornotes['text'] = $defaultcreatornotes;
288         $data->creatornotes['format'] = $defaultcreatornotesformat;
289         $this->set_data($data);
290     }
292     /**
293      * Custom form validation
294      *
295      * @param array $data
296      * @param array $files
297      * @return array
298      */
299     public function validation($data, $files) {
300         $errors = parent::validation($data, $files);
302         if ($this->_form->_submitValues['subject'] == 'none') {
303             $errors['subject'] = get_string('mustselectsubject', 'hub');
304         }
306         return $errors;
307     }