Community block MDL-19314 better error messages when an communication error occurs...
[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->dirroot . '/lib/formslib.php');
34 require_once($CFG->dirroot . '/course/publish/lib.php');
36 class community_hub_search_form extends moodleform {
38     public function definition() {
39         global $CFG, $USER, $OUTPUT;
40         $strrequired = get_string('required');
41         $mform = & $this->_form;
42         $search = $this->_customdata['search'];
43         $mform->addElement('header', 'site', get_string('search', 'block_community'));
45         //retrieve the hub list on the hub directory by web service
46         $function = 'hubdirectory_get_hubs';
47         $params = array();
48         $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
49         require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
50         $xmlrpcclient = new webservice_xmlrpc_client();
51         try {
52             $hubs = $xmlrpcclient->call($serverurl, 'publichubdirectory', $function, $params);
53         } catch (Exception $e) {
54             $hubs = array();
55             $error = $OUTPUT->notification(get_string('errorhublisting', 'block_community', $e->getMessage()));
56             $mform->addElement('static', 'errorhub', '', $error);
57         }
58         if (empty($error)) {
59             //sort hubs by trusted/prioritize
60             //Public hub list
61             $options = array();
62             // $mform->addElement('static','huburlstring',get_string('selecthub', 'block_community'));
64             foreach ($hubs as $hub) {
65                 $params = array('hubid' => $hub['id'],
66                     'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
67                 $imgurl = new moodle_url(HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/download.php", $params);
68                 $ascreenshothtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hub['name']));
69                 $brtag = html_writer::empty_tag('br');
70                 $hubdescription = '&nbsp;&nbsp;' . $hub['name'];
71                 $hubdescription .= $brtag;
72                 $hubdescription .= html_writer::tag('span', $ascreenshothtml, array('class' => 'hubscreenshot'));
73                 $hubdescription .= html_writer::tag('span', $hub['description'], array('class' => 'hubdescription'));
74                 $hubdescription .= $brtag;
75                 $additionaldesc = get_string('sites', 'block_community') . ': ' . $hub['sites'] . ' - ' .
76                         get_string('courses', 'block_community') . ': ' . $hub['courses'];
77                 $hubdescription .= html_writer::tag('span', $additionaldesc, array('class' => 'hubadditionaldesc'));
78                 $hubdescription .= $brtag;
79                 $hubdescription .= html_writer::tag('span',
80                                 $hub['trusted'] ? get_string('hubtrusted', 'block_community') :
81                                         get_string('hubnottrusted', 'block_community'),
82                                 array('class' => $hub['trusted'] ? 'trusted' : 'nottrusted'));
83                 $hubdescription = html_writer::tag('span',
84                                 $hubdescription,
85                                 array('class' => $hub['trusted'] ? 'hubtrusted' : 'hubnottrusted'));
87                 if (empty($firsthub)) {
88                     $mform->addElement('radio', 'huburl', get_string('selecthub', 'block_community'),
89                             $hubdescription, $hub['url']);
90                     $mform->setDefault('huburl', HUB_MOODLEORGHUBURL);
91                     $firsthub = true;
92                 } else {
93                     $mform->addElement('radio', 'huburl', '', $hubdescription, $hub['url']);
94                 }
95             }
97             //display enrol/download select box if the USER has the download capability
98             if (has_capability('moodle/community:download', get_context_instance(CONTEXT_USER, $USER->id))) {
99                 $options = array(0 => get_string('enrollable', 'block_community'),
100                     1 => get_string('downloadable', 'block_community'));
101                 $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
102                         $options);
103                 $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
104             } else {
105                 $mform->addElement('hidden', 'downloadable', 0);
106             }
108             $options = array();
109             $options['all'] = get_string('any');
110             $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
111             $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
112             $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
113             $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
114             $mform->setDefault('audience', 'all');
115             unset($options);
116             $mform->addHelpButton('audience', 'audience', 'block_community');
118             $options = array();
119             $options['all'] = get_string('any');
120             $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
121             $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
122             $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
123             $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
124             $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
125             $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
126             $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
127             $mform->addElement('select', 'educationallevel',
128                     get_string('educationallevel', 'block_community'), $options);
129             $mform->setDefault('educationallevel', 'all');
130             unset($options);
131             $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
133             $options = get_string_manager()->load_component_strings('edufields', current_language());
134             foreach ($options as $key => &$option) {
135                 $keylength = strlen($key);
136                 if ($keylength == 10) {
137                     $option = "&nbsp;&nbsp;" . $option;
138                 } else if ($keylength == 12) {
139                     $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
140                 }
141             }
142             $options = array_merge(array('all' => get_string('any')), $options);
143             $mform->addElement('select', 'subject', get_string('subject', 'block_community'),
144                     $options, array('id' => 'communitysubject'));
145             $mform->setDefault('subject', 'all');
146             unset($options);
147             $mform->addHelpButton('subject', 'subject', 'block_community');
148             $this->init_javascript_enhancement('subject', 'smartselect',
149                     array('selectablecategories' => true, 'mode' => 'compact'));
151             require_once($CFG->dirroot . "/lib/licenselib.php");
152             $licensemanager = new license_manager();
153             $licences = $licensemanager->get_licenses();
154             $options = array();
155             $options['all'] = get_string('any');
156             foreach ($licences as $license) {
157                 $options[$license->shortname] = get_string($license->shortname, 'license');
158             }
159             $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
160             $mform->setDefault('licence', 'cc');
161             unset($options);
162             $mform->addHelpButton('licence', 'licence', 'block_community');
163             $mform->setDefault('licence', 'all');
165             $languages = get_string_manager()->get_list_of_languages();
166             asort($languages, SORT_LOCALE_STRING);
167             $languages = array_merge(array('all' => get_string('any')), $languages);
168             $mform->addElement('select', 'language', get_string('language'), $languages);
169             $mform->setDefault('language', 'all');
170             $mform->addHelpButton('language', 'language', 'block_community');
172             $mform->addElement('text', 'search', get_string('keywords', 'block_community'));
173             $mform->addHelpButton('search', 'keywords', 'block_community');
176             $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
177         }
178     }
180     function validation($data, $files) {
181         global $CFG;
183         $errors = array();
185         if (empty($this->_form->_submitValues['huburl'])) {
186             $errors['huburl'] = get_string('nohubselected', 'hub');
187         }
189         return $errors;
190     }