MDL-29005 blocks : clarified comment
[moodle.git] / blocks / community / 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    blocks
25  * @subpackage community
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  * Form for community search
31  */
33 require_once($CFG->libdir . '/formslib.php');
34 require_once($CFG->dirroot . '/course/publish/lib.php');
35 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
37 class community_hub_search_form extends moodleform {
39     public function definition() {
40         global $CFG, $USER, $OUTPUT;
41         $strrequired = get_string('required');
42         $mform = & $this->_form;
44         //set default value
45         $search = $this->_customdata['search'];
46         if (isset($this->_customdata['coverage'])) {
47             $coverage = $this->_customdata['coverage'];
48         } else {
49             $coverage = 'all';
50         }
51         if (isset($this->_customdata['licence'])) {
52             $licence = $this->_customdata['licence'];
53         } else {
54             $licence = 'all';
55         }
56         if (isset($this->_customdata['subject'])) {
57             $subject = $this->_customdata['subject'];
58         } else {
59             $subject = 'all';
60         }
61         if (isset($this->_customdata['audience'])) {
62             $audience = $this->_customdata['audience'];
63         } else {
64             $audience = 'all';
65         }
66         if (isset($this->_customdata['language'])) {
67             $language = $this->_customdata['language'];
68         } else {
69             $language = current_language();
70         }
71         if (isset($this->_customdata['educationallevel'])) {
72             $educationallevel = $this->_customdata['educationallevel'];
73         } else {
74             $educationallevel = 'all';
75         }
76         if (isset($this->_customdata['downloadable'])) {
77             $downloadable = $this->_customdata['downloadable'];
78         } else {
79             $downloadable = 0;
80         }
81         if (isset($this->_customdata['orderby'])) {
82             $orderby = $this->_customdata['orderby'];
83         } else {
84             $orderby = 'newest';
85         }
86         if (isset($this->_customdata['huburl'])) {
87             $huburl = $this->_customdata['huburl'];
88         } else {
89             $huburl = HUB_MOODLEORGHUBURL;
90         }
92         $mform->addElement('header', 'site', get_string('search', 'block_community'));
94         //add the course id (of the context)
95         $mform->addElement('hidden', 'courseid', $this->_customdata['courseid']);
96         $mform->addElement('hidden', 'executesearch', 1);
98         //retrieve the hub list on the hub directory by web service
99         $function = 'hubdirectory_get_hubs';
100         $params = array();
101         $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
102         require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
103         $xmlrpcclient = new webservice_xmlrpc_client($serverurl, 'publichubdirectory');
104         try {
105             $hubs = $xmlrpcclient->call($function, $params);
106         } catch (Exception $e) {
107             $hubs = array();
108             $error = $OUTPUT->notification(get_string('errorhublisting', 'block_community', $e->getMessage()));
109             $mform->addElement('static', 'errorhub', '', $error);
110         }
112         //display list of registered on hub
113         $registrationmanager = new registration_manager();
114         $registeredhubs = $registrationmanager->get_registered_on_hubs();
115         //retrieve some additional hubs that we will add to
116         //the hub list got from the hub directory
117         $additionalhubs = array();
118         foreach ($registeredhubs as $registeredhub) {
119             $inthepubliclist = false;
120             foreach ($hubs as $hub) {
121                 if ($hub['url'] == $registeredhub->huburl) {
122                     $inthepubliclist = true;
123                     $hub['registeredon'] = true;
124                 }
125             }
126             if (!$inthepubliclist) {
127                 $additionalhub = array();
128                 $additionalhub['name'] = $registeredhub->hubname;
129                 $additionalhub['url'] = $registeredhub->huburl;
130                 $additionalhubs[] = $additionalhub;
131             }
132         }
133         if (!empty($additionalhubs)) {
134             $hubs = array_merge($hubs, $additionalhubs);
135         }
137         if (!empty($hubs)) {
138             //TODO: sort hubs by trusted/prioritize
139             //Public hub list
140             $options = array();
141             $firsthub = false;
142             foreach ($hubs as $hub) {
143                 if (key_exists('id', $hub)) {
144                     $params = array('hubid' => $hub['id'],
145                         'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
146                     $imgurl = new moodle_url(HUB_HUBDIRECTORYURL .
147                                     "/local/hubdirectory/webservice/download.php", $params);
148                     $ascreenshothtml = html_writer::empty_tag('img',
149                                     array('src' => $imgurl, 'alt' => $hub['name']));
151                     $hubdescription = html_writer::tag('a', $hub['name'],
152                                     array('class' => 'hublink clearfix', 'href' => $hub['url'],
153                                         'onclick' => 'this.target="_blank"'));
154                     $hubdescription .= html_writer::tag('span', $ascreenshothtml,
155                                     array('class' => 'hubscreenshot'));
156                     $hubdescriptiontext = html_writer::tag('span', format_text($hub['description'], FORMAT_PLAIN),
157                                     array('class' => 'hubdescription'));
158                     if (isset($hub['enrollablecourses'])) { //check needed to avoid warnings for Moodle version < 2011081700
159                         $additionaldesc = get_string('enrollablecourses', 'block_community') . ': ' . $hub['enrollablecourses'] . ' - ' .
160                                 get_string('downloadablecourses', 'block_community') . ': ' . $hub['downloadablecourses'];
161                         $hubdescriptiontext .= html_writer::tag('span', $additionaldesc,
162                                         array('class' => 'hubadditionaldesc'));
163                     }
164                     if ($hub['trusted']) {
165                     $hubtrusted =  get_string('hubtrusted', 'block_community');
166                     $hubdescriptiontext .= html_writer::tag('span',
167                                     $hubtrusted . ' ' . $OUTPUT->doc_link('trusted_hubs'),
168                                     array('class' => 'trusted'));
170                     }
171                     $hubdescriptiontext = html_writer::tag('span', $hubdescriptiontext,
172                             array('class' => 'hubdescriptiontext'));
174                     $hubdescription = html_writer::tag('span',
175                                     $hubdescription . $hubdescriptiontext,
176                                     array('class' => $hub['trusted'] ? 'hubtrusted' : 'hubnottrusted'));
177                 } else {
178                     $hubdescription = html_writer::tag('a', $hub['name'],
179                                     array('class' => 'hublink hubtrusted', 'href' => $hub['url']));
180                 }
182                 if (empty($firsthub)) {
183                     $mform->addElement('radio', 'huburl', get_string('selecthub', 'block_community'),
184                             $hubdescription, $hub['url']);
185                     $mform->setDefault('huburl', $huburl);
186                     $firsthub = true;
187                 } else {
188                     $mform->addElement('radio', 'huburl', '', $hubdescription, $hub['url']);
189                 }
190             }
192             //display enrol/download select box if the USER has the download capability on the course
193             if (has_capability('moodle/community:download',
194                             get_context_instance(CONTEXT_COURSE, $this->_customdata['courseid']))) {
195                 $options = array(0 => get_string('enrollable', 'block_community'),
196                     1 => get_string('downloadable', 'block_community'));
197                 $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
198                         $options);
199                 $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
200             } else {
201                 $mform->addElement('hidden', 'downloadable', 0);
202             }
204             $options = array();
205             $options['all'] = get_string('any');
206             $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
207             $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
208             $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
209             $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
210             $mform->setDefault('audience', $audience);
211             unset($options);
212             $mform->addHelpButton('audience', 'audience', 'block_community');
214             $options = array();
215             $options['all'] = get_string('any');
216             $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
217             $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
218             $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
219             $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
220             $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
221             $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
222             $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
223             $mform->addElement('select', 'educationallevel',
224                     get_string('educationallevel', 'block_community'), $options);
225             $mform->setDefault('educationallevel', $educationallevel);
226             unset($options);
227             $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
229             $publicationmanager = new course_publish_manager();
230             $options = $publicationmanager->get_sorted_subjects();
231             foreach ($options as $key => &$option) {
232                 $keylength = strlen($key);
233                 if ($keylength == 10) {
234                     $option = "&nbsp;&nbsp;" . $option;
235                 } else if ($keylength == 12) {
236                     $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
237                 }
238             }
239             $options = array_merge(array('all' => get_string('any')), $options);
240             $mform->addElement('select', 'subject', get_string('subject', 'block_community'),
241                     $options, array('id' => 'communitysubject'));
242             $mform->setDefault('subject', $subject);
243             unset($options);
244             $mform->addHelpButton('subject', 'subject', 'block_community');
245             $this->init_javascript_enhancement('subject', 'smartselect',
246                     array('selectablecategories' => true, 'mode' => 'compact'));
248             require_once($CFG->libdir . "/licenselib.php");
249             $licensemanager = new license_manager();
250             $licences = $licensemanager->get_licenses();
251             $options = array();
252             $options['all'] = get_string('any');
253             foreach ($licences as $license) {
254                 $options[$license->shortname] = get_string($license->shortname, 'license');
255             }
256             $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
257             unset($options);
258             $mform->addHelpButton('licence', 'licence', 'block_community');
259             $mform->setDefault('licence', $licence);
261             $languages = get_string_manager()->get_list_of_languages();
262             textlib_get_instance()->asort($languages);
263             $languages = array_merge(array('all' => get_string('any')), $languages);
264             $mform->addElement('select', 'language', get_string('language'), $languages);
265             $mform->setDefault('language', $language);
266             $mform->addHelpButton('language', 'language', 'block_community');
268             $mform->addElement('radio', 'orderby', get_string('orderby', 'block_community'),
269                     get_string('orderbynewest', 'block_community'), 'newest');
270             $mform->addElement('radio', 'orderby', null,
271                     get_string('orderbyeldest', 'block_community'), 'eldest');
272             $mform->addElement('radio', 'orderby', null,
273                     get_string('orderbyname', 'block_community'), 'fullname');
274             $mform->addElement('radio', 'orderby', null,
275                     get_string('orderbypublisher', 'block_community'), 'publisher');
276             $mform->addElement('radio', 'orderby', null,
277                     get_string('orderbyratingaverage', 'block_community'), 'ratingaverage');
278             $mform->setDefault('orderby', $orderby);
279             $mform->setType('orderby', PARAM_ALPHA);
281             $mform->addElement('text', 'search', get_string('keywords', 'block_community'));
282             $mform->addHelpButton('search', 'keywords', 'block_community');
285             $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
286         }
287     }
289     function validation($data, $files) {
290         global $CFG;
292         $errors = array();
294         if (empty($this->_form->_submitValues['huburl'])) {
295             $errors['huburl'] = get_string('nohubselected', 'hub');
296         }
298         return $errors;
299     }