community hub finder MDL-25768 private hubs weren't displayed in the hub list
[moodle.git] / blocks / community / forms.php
CommitLineData
07ab0c80 1<?php
178f282b 2
07ab0c80 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 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
178f282b 31 */
07ab0c80 32
a8e174cc 33require_once($CFG->libdir . '/formslib.php');
178f282b 34require_once($CFG->dirroot . '/course/publish/lib.php');
a8e174cc 35require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
07ab0c80 36
37class community_hub_search_form extends moodleform {
38
39 public function definition() {
178f282b 40 global $CFG, $USER, $OUTPUT;
07ab0c80 41 $strrequired = get_string('required');
178f282b 42 $mform = & $this->_form;
c48ab36a 43
44 //set default value
07ab0c80 45 $search = $this->_customdata['search'];
c48ab36a 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 {
fb5302b5 69 $language = current_language();
c48ab36a 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 }
91
07ab0c80 92 $mform->addElement('header', 'site', get_string('search', 'block_community'));
93
b48c4478 94 //add the course id (of the context)
95 $mform->addElement('hidden', 'courseid', $this->_customdata['courseid']);
c48ab36a 96 $mform->addElement('hidden', 'executesearch', 1);
b48c4478 97
07ab0c80 98 //retrieve the hub list on the hub directory by web service
99 $function = 'hubdirectory_get_hubs';
100 $params = array();
178f282b 101 $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
102 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
001251f2 103 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, 'publichubdirectory');
178f282b 104 try {
001251f2 105 $hubs = $xmlrpcclient->call($function, $params);
178f282b 106 } catch (Exception $e) {
107 $hubs = array();
108 $error = $OUTPUT->notification(get_string('errorhublisting', 'block_community', $e->getMessage()));
109 $mform->addElement('static', 'errorhub', '', $error);
07ab0c80 110 }
ac90abb4 111
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)) {
6a658c91 134 $hubs = array_merge($hubs, $additionalhubs);
ac90abb4 135 }
136
137 if (!empty($hubs)) {
138 //TODO: sort hubs by trusted/prioritize
178f282b 139 //Public hub list
140 $options = array();
d2a8163d 141 $firsthub = false;
178f282b 142 foreach ($hubs as $hub) {
ac90abb4 143 if (key_exists('id', $hub)) {
144 $params = array('hubid' => $hub['id'],
145 'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
fb6b4542 146 $imgurl = new moodle_url(HUB_HUBDIRECTORYURL .
2031735b 147 "/local/hubdirectory/webservice/download.php", $params);
fb6b4542 148 $ascreenshothtml = html_writer::empty_tag('img',
2031735b 149 array('src' => $imgurl, 'alt' => $hub['name']));
c6947ba7 150
2031735b 151 $hubdescription = html_writer::tag('a', $hub['name'],
152 array('class' => 'hublink clearfix', 'href' => $hub['url'],
fb6b4542 153 'onclick' => 'this.target="_blank"'));
fb6b4542 154 $hubdescription .= html_writer::tag('span', $ascreenshothtml,
2031735b 155 array('class' => 'hubscreenshot'));
d4f866ea 156 $hubdescriptiontext = html_writer::tag('span', format_text($hub['description'], FORMAT_PLAIN),
2031735b 157 array('class' => 'hubdescription'));
ac90abb4 158 $additionaldesc = get_string('sites', 'block_community') . ': ' . $hub['sites'] . ' - ' .
159 get_string('courses', 'block_community') . ': ' . $hub['courses'];
d4f866ea 160 $hubdescriptiontext .= html_writer::tag('span', $additionaldesc,
2031735b 161 array('class' => 'hubadditionaldesc'));
d4f866ea 162 if ($hub['trusted']) {
163 $hubtrusted = get_string('hubtrusted', 'block_community');
164 $hubdescriptiontext .= html_writer::tag('span',
2031735b 165 $hubtrusted . ' ' . $OUTPUT->doc_link('trusted_hubs'),
d4f866ea 166 array('class' => 'trusted'));
167
168 }
169 $hubdescriptiontext = html_writer::tag('span', $hubdescriptiontext,
170 array('class' => 'hubdescriptiontext'));
2031735b 171
ac90abb4 172 $hubdescription = html_writer::tag('span',
d4f866ea 173 $hubdescription . $hubdescriptiontext,
ac90abb4 174 array('class' => $hub['trusted'] ? 'hubtrusted' : 'hubnottrusted'));
175 } else {
cbe80ba5 176 $hubdescription = html_writer::tag('a', $hub['name'],
6a658c91 177 array('class' => 'hublink hubtrusted', 'href' => $hub['url']));
ac90abb4 178 }
178f282b 179
180 if (empty($firsthub)) {
181 $mform->addElement('radio', 'huburl', get_string('selecthub', 'block_community'),
182 $hubdescription, $hub['url']);
c48ab36a 183 $mform->setDefault('huburl', $huburl);
178f282b 184 $firsthub = true;
185 } else {
186 $mform->addElement('radio', 'huburl', '', $hubdescription, $hub['url']);
187 }
188 }
07ab0c80 189
8a3b20ce 190 //display enrol/download select box if the USER has the download capability on the course
fb6b4542 191 if (has_capability('moodle/community:download',
192 get_context_instance(CONTEXT_COURSE, $this->_customdata['courseid']))) {
178f282b 193 $options = array(0 => get_string('enrollable', 'block_community'),
755f96f8 194 1 => get_string('downloadable', 'block_community'));
178f282b 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 }
07ab0c80 201
178f282b 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);
c48ab36a 208 $mform->setDefault('audience', $audience);
178f282b 209 unset($options);
210 $mform->addHelpButton('audience', 'audience', 'block_community');
211
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);
c48ab36a 223 $mform->setDefault('educationallevel', $educationallevel);
178f282b 224 unset($options);
225 $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
226
efba7034 227 $publicationmanager = new course_publish_manager();
228 $options = $publicationmanager->get_sorted_subjects();
178f282b 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 }
abf63f98 236 }
178f282b 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'));
c48ab36a 240 $mform->setDefault('subject', $subject);
178f282b 241 unset($options);
242 $mform->addHelpButton('subject', 'subject', 'block_community');
243 $this->init_javascript_enhancement('subject', 'smartselect',
244 array('selectablecategories' => true, 'mode' => 'compact'));
245
a8e174cc 246 require_once($CFG->libdir . "/licenselib.php");
178f282b 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);
178f282b 255 unset($options);
256 $mform->addHelpButton('licence', 'licence', 'block_community');
c48ab36a 257 $mform->setDefault('licence', $licence);
276f590e 258
178f282b 259 $languages = get_string_manager()->get_list_of_languages();
c6947ba7 260 textlib_get_instance()->asort($languages);
178f282b 261 $languages = array_merge(array('all' => get_string('any')), $languages);
262 $mform->addElement('select', 'language', get_string('language'), $languages);
c48ab36a 263 $mform->setDefault('language', $language);
178f282b 264 $mform->addHelpButton('language', 'language', 'block_community');
276f590e 265
43a58be7 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');
7a0d0804 274 $mform->addElement('radio', 'orderby', null,
275 get_string('orderbyratingaverage', 'block_community'), 'ratingaverage');
c48ab36a 276 $mform->setDefault('orderby', $orderby);
43a58be7 277 $mform->setType('orderby', PARAM_ALPHA);
278
178f282b 279 $mform->addElement('text', 'search', get_string('keywords', 'block_community'));
280 $mform->addHelpButton('search', 'keywords', 'block_community');
276f590e 281
07ab0c80 282
178f282b 283 $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
284 }
07ab0c80 285 }
286
276f590e 287 function validation($data, $files) {
288 global $CFG;
289
290 $errors = array();
291
292 if (empty($this->_form->_submitValues['huburl'])) {
293 $errors['huburl'] = get_string('nohubselected', 'hub');
294 }
295
296 return $errors;
297 }
298
07ab0c80 299}