a61b2732cde1eaaec551bbc8a5dae6066d725635
[moodle.git] / blocks / community / forms.php
1 <?php
2 ///////////////////////////////////////////////////////////////////////////
3 //                                                                       //
4 // This file is part of Moodle - http://moodle.org/                      //
5 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
6 //                                                                       //
7 // Moodle is free software: you can redistribute it and/or modify        //
8 // it under the terms of the GNU General Public License as published by  //
9 // the Free Software Foundation, either version 3 of the License, or     //
10 // (at your option) any later version.                                   //
11 //                                                                       //
12 // Moodle is distributed in the hope that it will be useful,             //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
15 // GNU General Public License for more details.                          //
16 //                                                                       //
17 // You should have received a copy of the GNU General Public License     //
18 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.       //
19 //                                                                       //
20 ///////////////////////////////////////////////////////////////////////////
22 /**
23  * Form for community search
24  *
25  * @package    block_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  */
31 require_once($CFG->libdir . '/formslib.php');
32 require_once($CFG->dirroot . '/course/publish/lib.php');
33 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
35 class community_hub_search_form extends moodleform {
37     public function definition() {
38         global $CFG, $USER, $OUTPUT;
39         $strrequired = get_string('required');
40         $mform = & $this->_form;
42         //set default value
43         $search = $this->_customdata['search'];
44         if (isset($this->_customdata['coverage'])) {
45             $coverage = $this->_customdata['coverage'];
46         } else {
47             $coverage = 'all';
48         }
49         if (isset($this->_customdata['licence'])) {
50             $licence = $this->_customdata['licence'];
51         } else {
52             $licence = 'all';
53         }
54         if (isset($this->_customdata['subject'])) {
55             $subject = $this->_customdata['subject'];
56         } else {
57             $subject = 'all';
58         }
59         if (isset($this->_customdata['audience'])) {
60             $audience = $this->_customdata['audience'];
61         } else {
62             $audience = 'all';
63         }
64         if (isset($this->_customdata['language'])) {
65             $language = $this->_customdata['language'];
66         } else {
67             $language = current_language();
68         }
69         if (isset($this->_customdata['educationallevel'])) {
70             $educationallevel = $this->_customdata['educationallevel'];
71         } else {
72             $educationallevel = 'all';
73         }
74         if (isset($this->_customdata['downloadable'])) {
75             $downloadable = $this->_customdata['downloadable'];
76         } else {
77             $downloadable = 1;
78         }
79         if (isset($this->_customdata['orderby'])) {
80             $orderby = $this->_customdata['orderby'];
81         } else {
82             $orderby = 'newest';
83         }
84         if (isset($this->_customdata['huburl'])) {
85             $huburl = $this->_customdata['huburl'];
86         } else {
87             $huburl = HUB_MOODLEORGHUBURL;
88         }
90         $mform->addElement('header', 'site', get_string('search', 'block_community'));
92         //add the course id (of the context)
93         $mform->addElement('hidden', 'courseid', $this->_customdata['courseid']);
94         $mform->setType('courseid', PARAM_INT);
95         $mform->addElement('hidden', 'executesearch', 1);
96         $mform->setType('executesearch', PARAM_INT);
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         // Hubdirectory returns old URL for the moodle.net hub, substitute it.
113         foreach ($hubs as $key => $hub) {
114             if ($hub['url'] === HUB_OLDMOODLEORGHUBURL) {
115                 $hubs[$key]['url'] = HUB_MOODLEORGHUBURL;
116             }
117         }
119         //display list of registered on hub
120         $registrationmanager = new registration_manager();
121         $registeredhubs = $registrationmanager->get_registered_on_hubs();
122         //retrieve some additional hubs that we will add to
123         //the hub list got from the hub directory
124         $additionalhubs = array();
125         foreach ($registeredhubs as $registeredhub) {
126             $inthepubliclist = false;
127             foreach ($hubs as $hub) {
128                 if ($hub['url'] == $registeredhub->huburl) {
129                     $inthepubliclist = true;
130                     $hub['registeredon'] = true;
131                 }
132             }
133             if (!$inthepubliclist) {
134                 $additionalhub = array();
135                 $additionalhub['name'] = $registeredhub->hubname;
136                 $additionalhub['url'] = $registeredhub->huburl;
137                 $additionalhubs[] = $additionalhub;
138             }
139         }
140         if (!empty($additionalhubs)) {
141             $hubs = array_merge($hubs, $additionalhubs);
142         }
144         if (!empty($hubs)) {
145             $htmlhubs = array();
146             foreach ($hubs as $hub) {
147                 // Name can come from hub directory - need some cleaning.
148                 $hubname = clean_text($hub['name'], PARAM_TEXT);
149                 $smalllogohtml = '';
150                 if (array_key_exists('id', $hub)) {
152                     // Retrieve hub logo + generate small logo.
153                     $params = array('hubid' => $hub['id'], 'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
154                     $imgurl = new moodle_url(HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/download.php", $params);
155                     $imgsize = getimagesize($imgurl->out(false));
156                     if ($imgsize[0] > 1) {
157                         $ascreenshothtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hubname));
158                         $smalllogohtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hubname
159                                         , 'height' => 30, 'width' => 40));
160                     } else {
161                         $ascreenshothtml = '';
162                     }
163                     $hubimage = html_writer::tag('div', $ascreenshothtml, array('class' => 'hubimage'));
165                     // Statistics + trusted info.
166                     $hubstats = '';
167                     if (isset($hub['enrollablecourses'])) { //check needed to avoid warnings for Moodle version < 2011081700
168                         $additionaldesc = get_string('enrollablecourses', 'block_community') . ': ' . $hub['enrollablecourses'] . ' - ' .
169                                 get_string('downloadablecourses', 'block_community') . ': ' . $hub['downloadablecourses'];
170                         $hubstats .= html_writer::tag('div', $additionaldesc);
171                     }
172                     if ($hub['trusted']) {
173                         $hubtrusted =  get_string('hubtrusted', 'block_community');
174                         $hubstats .= $OUTPUT->doc_link('trusted_hubs') . html_writer::tag('div', $hubtrusted);
175                     }
176                     $hubstats = html_writer::tag('div', $hubstats, array('class' => 'hubstats'));
178                     // hub name link + hub description.
179                     $hubnamelink = html_writer::link($hub['url'], html_writer::tag('h2',$hubname),
180                                     array('class' => 'hubtitlelink'));
181                     // The description can come from the hub directory - need to clean.
182                     $hubdescription = clean_param($hub['description'], PARAM_TEXT);
183                     $hubdescriptiontext = html_writer::tag('div', format_text($hubdescription, FORMAT_PLAIN),
184                                     array('class' => 'hubdescription'));
186                     $hubtext = html_writer::tag('div', $hubdescriptiontext . $hubstats, array('class' => 'hubtext'));
188                     $hubimgandtext = html_writer::tag('div', $hubimage . $hubtext, array('class' => 'hubimgandtext'));
190                     $hubfulldesc = html_writer::tag('div', $hubnamelink . $hubimgandtext, array('class' => 'hubmainhmtl'));
191                 } else {
192                     $hubfulldesc = html_writer::link($hub['url'], $hubname);
193                 }
195                 // Add hub to the hub items.
196                 $hubinfo = new stdClass();
197                 $hubinfo->mainhtml = $hubfulldesc;
198                 $hubinfo->rowhtml = html_writer::tag('div', $smalllogohtml , array('class' => 'hubsmalllogo')) . $hubname;
199                 $hubitems[$hub['url']] = $hubinfo;
200             }
202             // Hub listing form element.
203             $mform->addElement('listing','huburl', '', '', array('items' => $hubitems,
204                 'showall' => get_string('showall', 'block_community'),
205                 'hideall' => get_string('hideall', 'block_community')));
206             $mform->setDefault('huburl', $huburl);
208             //display enrol/download select box if the USER has the download capability on the course
209             if (has_capability('moodle/community:download',
210                             context_course::instance($this->_customdata['courseid']))) {
211                 $options = array(0 => get_string('enrollable', 'block_community'),
212                     1 => get_string('downloadable', 'block_community'));
213                 $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
214                         $options);
215                 $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
217                 $mform->setDefault('downloadable', $downloadable);
218             } else {
219                 $mform->addElement('hidden', 'downloadable', 0);
220             }
221             $mform->setType('downloadable', PARAM_INT);
223             $options = array();
224             $options['all'] = get_string('any');
225             $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
226             $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
227             $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
228             $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
229             $mform->setDefault('audience', $audience);
230             unset($options);
231             $mform->addHelpButton('audience', 'audience', 'block_community');
233             $options = array();
234             $options['all'] = get_string('any');
235             $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
236             $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
237             $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
238             $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
239             $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
240             $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
241             $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
242             $mform->addElement('select', 'educationallevel',
243                     get_string('educationallevel', 'block_community'), $options);
244             $mform->setDefault('educationallevel', $educationallevel);
245             unset($options);
246             $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
248             $publicationmanager = new course_publish_manager();
249             $options = $publicationmanager->get_sorted_subjects();
250             $mform->addElement('searchableselector', 'subject', get_string('subject', 'block_community'),
251                     $options, array('id' => 'communitysubject'));
252             $mform->setDefault('subject', $subject);
253             unset($options);
254             $mform->addHelpButton('subject', 'subject', 'block_community');
256             require_once($CFG->libdir . "/licenselib.php");
257             $licensemanager = new license_manager();
258             $licences = $licensemanager->get_licenses();
259             $options = array();
260             $options['all'] = get_string('any');
261             foreach ($licences as $license) {
262                 $options[$license->shortname] = get_string($license->shortname, 'license');
263             }
264             $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
265             unset($options);
266             $mform->addHelpButton('licence', 'licence', 'block_community');
267             $mform->setDefault('licence', $licence);
269             $languages = get_string_manager()->get_list_of_languages();
270             core_collator::asort($languages);
271             $languages = array_merge(array('all' => get_string('any')), $languages);
272             $mform->addElement('select', 'language', get_string('language'), $languages);
274             $mform->setDefault('language', $language);
275             $mform->addHelpButton('language', 'language', 'block_community');
277             $mform->addElement('select', 'orderby', get_string('orderby', 'block_community'),
278                 array('newest' => get_string('orderbynewest', 'block_community'),
279                     'eldest' => get_string('orderbyeldest', 'block_community'),
280                     'fullname' => get_string('orderbyname', 'block_community'),
281                     'publisher' => get_string('orderbypublisher', 'block_community'),
282                     'ratingaverage' => get_string('orderbyratingaverage', 'block_community')));
284             $mform->setDefault('orderby', $orderby);
285             $mform->addHelpButton('orderby', 'orderby', 'block_community');
286             $mform->setType('orderby', PARAM_ALPHA);
288             $mform->setAdvanced('audience');
289             $mform->setAdvanced('educationallevel');
290             $mform->setAdvanced('subject');
291             $mform->setAdvanced('licence');
292             $mform->setAdvanced('language');
293             $mform->setAdvanced('orderby');
295             $mform->addElement('text', 'search', get_string('keywords', 'block_community'),
296                 array('size' => 30));
297             $mform->addHelpButton('search', 'keywords', 'block_community');
298             $mform->setType('search', PARAM_NOTAGS);
300             $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
301         }
302     }
304     function validation($data, $files) {
305         global $CFG;
307         $errors = array();
309         if (empty($this->_form->_submitValues['huburl'])) {
310             $errors['huburl'] = get_string('nohubselected', 'hub');
311         }
313         return $errors;
314     }