8d16563501495d2625374573dd1ffd63104a11ea
[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 = $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                     $additionaldesc = get_string('sites', 'block_community') . ': ' . $hub['sites'] . ' - ' .
159                             get_string('courses', 'block_community') . ': ' . $hub['courses'];
160                     $hubdescriptiontext .= html_writer::tag('span', $additionaldesc,
161                                     array('class' => 'hubadditionaldesc'));
162                     if ($hub['trusted']) {
163                     $hubtrusted =  get_string('hubtrusted', 'block_community');
164                     $hubdescriptiontext .= html_writer::tag('span',
165                                     $hubtrusted . ' ' . $OUTPUT->doc_link('trusted_hubs'),
166                                     array('class' => 'trusted'));
168                     }
169                     $hubdescriptiontext = html_writer::tag('span', $hubdescriptiontext,
170                             array('class' => 'hubdescriptiontext'));
172                     $hubdescription = html_writer::tag('span',
173                                     $hubdescription . $hubdescriptiontext,
174                                     array('class' => $hub['trusted'] ? 'hubtrusted' : 'hubnottrusted'));
175                 } else {
176                     $hubdescription = html_writer::tag('a', $hub['name'],
177                                     array('class' => 'hublink', 'href' => $hub['url']));
178                 }
180                 if (empty($firsthub)) {
181                     $mform->addElement('radio', 'huburl', get_string('selecthub', 'block_community'),
182                             $hubdescription, $hub['url']);
183                     $mform->setDefault('huburl', $huburl);
184                     $firsthub = true;
185                 } else {
186                     $mform->addElement('radio', 'huburl', '', $hubdescription, $hub['url']);
187                 }
188             }
190             //display enrol/download select box if the USER has the download capability on the course
191             if (has_capability('moodle/community:download',
192                             get_context_instance(CONTEXT_COURSE, $this->_customdata['courseid']))) {
193                 $options = array(0 => get_string('enrollable', 'block_community'),
194                     1 => get_string('downloadable', 'block_community'));
195                 $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
196                         $options);
197                 $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
198             } else {
199                 $mform->addElement('hidden', 'downloadable', 0);
200             }
202             $options = array();
203             $options['all'] = get_string('any');
204             $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
205             $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
206             $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
207             $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
208             $mform->setDefault('audience', $audience);
209             unset($options);
210             $mform->addHelpButton('audience', 'audience', 'block_community');
212             $options = array();
213             $options['all'] = get_string('any');
214             $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
215             $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
216             $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
217             $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
218             $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
219             $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
220             $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
221             $mform->addElement('select', 'educationallevel',
222                     get_string('educationallevel', 'block_community'), $options);
223             $mform->setDefault('educationallevel', $educationallevel);
224             unset($options);
225             $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
227             $publicationmanager = new course_publish_manager();
228             $options = $publicationmanager->get_sorted_subjects();
229             foreach ($options as $key => &$option) {
230                 $keylength = strlen($key);
231                 if ($keylength == 10) {
232                     $option = "&nbsp;&nbsp;" . $option;
233                 } else if ($keylength == 12) {
234                     $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
235                 }
236             }
237             $options = array_merge(array('all' => get_string('any')), $options);
238             $mform->addElement('select', 'subject', get_string('subject', 'block_community'),
239                     $options, array('id' => 'communitysubject'));
240             $mform->setDefault('subject', $subject);
241             unset($options);
242             $mform->addHelpButton('subject', 'subject', 'block_community');
243             $this->init_javascript_enhancement('subject', 'smartselect',
244                     array('selectablecategories' => true, 'mode' => 'compact'));
246             require_once($CFG->libdir . "/licenselib.php");
247             $licensemanager = new license_manager();
248             $licences = $licensemanager->get_licenses();
249             $options = array();
250             $options['all'] = get_string('any');
251             foreach ($licences as $license) {
252                 $options[$license->shortname] = get_string($license->shortname, 'license');
253             }
254             $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
255             unset($options);
256             $mform->addHelpButton('licence', 'licence', 'block_community');
257             $mform->setDefault('licence', $licence);
259             $languages = get_string_manager()->get_list_of_languages();
260             textlib_get_instance()->asort($languages);
261             $languages = array_merge(array('all' => get_string('any')), $languages);
262             $mform->addElement('select', 'language', get_string('language'), $languages);
263             $mform->setDefault('language', $language);
264             $mform->addHelpButton('language', 'language', 'block_community');
266             $mform->addElement('radio', 'orderby', get_string('orderby', 'block_community'),
267                     get_string('orderbynewest', 'block_community'), 'newest');
268             $mform->addElement('radio', 'orderby', null,
269                     get_string('orderbyeldest', 'block_community'), 'eldest');
270             $mform->addElement('radio', 'orderby', null,
271                     get_string('orderbyname', 'block_community'), 'fullname');
272             $mform->addElement('radio', 'orderby', null,
273                     get_string('orderbypublisher', 'block_community'), 'publisher');
274             $mform->addElement('radio', 'orderby', null,
275                     get_string('orderbyratingaverage', 'block_community'), 'ratingaverage');
276             $mform->setDefault('orderby', $orderby);
277             $mform->setType('orderby', PARAM_ALPHA);
279             $mform->addElement('text', 'search', get_string('keywords', 'block_community'));
280             $mform->addHelpButton('search', 'keywords', 'block_community');
283             $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
284         }
285     }
287     function validation($data, $files) {
288         global $CFG;
290         $errors = array();
292         if (empty($this->_form->_submitValues['huburl'])) {
293             $errors['huburl'] = get_string('nohubselected', 'hub');
294         }
296         return $errors;
297     }