1566a68d766aac95b6e01584bbf9d1109e3c3cf7
[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  * @package    blocks
24  * @subpackage community
25  * @author     Jerome Mouneyrac <jerome@mouneyrac.com>
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
27  * @copyright  (C) 1999 onwards Martin Dougiamas  http://dougiamas.com
28  *
29  * Form for community search
30 */
32 require_once($CFG->dirroot.'/lib/formslib.php');
33 require_once($CFG->dirroot.'/course/publish/lib.php');
35 class community_hub_search_form extends moodleform {
37     public function definition() {
38         global $CFG, $USER;
39         $strrequired = get_string('required');
40         $mform =& $this->_form;
41         $search = $this->_customdata['search'];
42         $mform->addElement('header', 'site', get_string('search', 'block_community'));
44         //retrieve the hub list on the hub directory by web service
45         $function = 'hubdirectory_get_hubs';
46         $params = array();
47         $serverurl = HUB_HUBDIRECTORYURL."/local/hubdirectory/webservice/webservices.php";
48         require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
49         $xmlrpcclient = new webservice_xmlrpc_client();
50         $hubs = $xmlrpcclient->call($serverurl, 'publichubdirectory', $function, $params);
52         //sort hubs by trusted/prioritize
54         //Public hub list
55         $options = array();
56        // $mform->addElement('static','huburlstring',get_string('selecthub', 'block_community'));
57         
58         foreach ($hubs as $hub) {
59             $params = array('hubid' => $hub['id'],
60                 'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
61             $imgurl = new moodle_url(HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/download.php", $params);
62             $ascreenshothtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hub['name']));
63             $brtag = html_writer::empty_tag('br');
64             $hubdescription =   '&nbsp;&nbsp;'.$hub['name'];
65             $hubdescription .= $brtag;
66             $hubdescription .=  html_writer::tag('span', $ascreenshothtml, array('class' => 'hubscreenshot'));
67             $hubdescription .= html_writer::tag('span', $hub['description'], array('class' => 'hubdescription'));
68             $hubdescription .= $brtag;
69             $additionaldesc = get_string('sites', 'block_community'). ': ' . $hub['sites'] . ' - ' .
70                                 get_string('courses', 'block_community'). ': ' . $hub['courses'];
71             $hubdescription .= html_writer::tag('span', $additionaldesc, array('class' => 'hubadditionaldesc'));
72             $hubdescription .= $brtag;
73             $hubdescription .= html_writer::tag('span',
74                     $hub['trusted']?get_string('hubtrusted', 'block_community'):get_string('hubnottrusted', 'block_community'),
75                     array('class' => $hub['trusted']?'trusted':'nottrusted'));
76              $hubdescription = html_writer::tag('span',
77                     $hubdescription,
78                     array('class' => $hub['trusted']?'hubtrusted':'hubnottrusted'));
80             if (empty($firsthub)) {
81                 $mform->addElement('radio','huburl',get_string('selecthub', 'block_community'),
82                         $hubdescription, $hub['url']);
83                 $mform->setDefault('huburl', HUB_MOODLEORGHUBURL);
84                 $firsthub = true;
85             } else {
86                 $mform->addElement('radio','huburl','', $hubdescription, $hub['url']);
87             }
88         }
90         //display enrol/download select box if the USER has the download capability
91         if (has_capability('moodle/community:download', get_context_instance(CONTEXT_USER, $USER->id))) {
92             $options = array(0 => get_string('enrollable', 'block_community'),
93                     1 => get_string('downloadable', 'block_community'));
94             $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
95                     $options);
96             $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
97         } else {
98             $mform->addElement('hidden', 'downloadable', 0);
99         }
101         $options = array();
102         $options['all'] = get_string('any');
103         $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
104         $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
105         $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
106         $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
107         $mform->setDefault('audience', 'all');
108         unset($options);
109         $mform->addHelpButton('audience', 'audience', 'block_community');
111         $options = array();
112         $options['all'] = get_string('any');
113         $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
114         $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
115         $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
116         $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
117         $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
118         $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
119         $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
120         $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'block_community'), $options);
121         $mform->setDefault('educationallevel', 'all');
122         unset($options);
123         $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
125         $options = get_string_manager()->load_component_strings('edufields', current_language());
126         foreach ($options as $key => &$option) {
127             $keylength = strlen ( $key );
128             if ( $keylength == 10) {
129                 $option = "&nbsp;&nbsp;" . $option;
130             } else  if ( $keylength == 12) {
131                 $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
132             }
133         }
134         $options = array_merge (array('all' => get_string('any')),$options);
135         $mform->addElement('select', 'subject', get_string('subject', 'block_community'), $options, array('id'=>'communitysubject'));
136         $mform->setDefault('subject', 'all');
137         unset($options);
138         $mform->addHelpButton('subject', 'subject', 'block_community');
139         $this->init_javascript_enhancement('subject', 'smartselect', array('selectablecategories' => true, 'mode'=>'compact'));
141         require_once($CFG->dirroot."/lib/licenselib.php");
142         $licensemanager = new license_manager();
143         $licences = $licensemanager->get_licenses();
144         $options = array();
145         $options['all'] = get_string('any');
146         foreach ($licences as $license) {
147             $options[$license->shortname] = get_string($license->shortname, 'license');
148         }
149         $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
150         $mform->setDefault('licence', 'cc');
151         unset($options);
152         $mform->addHelpButton('licence', 'licence', 'block_community');
153         $mform->setDefault('licence', 'all');
155         $languages = get_string_manager()->get_list_of_languages();
156         asort($languages, SORT_LOCALE_STRING);
157         $languages = array_merge (array('all' => get_string('any')),$languages);
158         $mform->addElement('select', 'language',get_string('language'), $languages);
159         $mform->setDefault('language', 'all');
160         $mform->addHelpButton('language', 'language', 'block_community');
163         $mform->addElement('text','search' , get_string('keywords', 'block_community'));
164         $mform->addHelpButton('search', 'keywords', 'block_community');
166         $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
167     }
169     function validation($data, $files) {
170         global $CFG;
172         $errors = array();
174         if (empty($this->_form->_submitValues['huburl'])) {
175             $errors['huburl'] = get_string('nohubselected', 'hub');
176         }
178         return $errors;
179     }