07ab0c80 |
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 | /////////////////////////////////////////////////////////////////////////// |
21 | |
22 | /* |
23 | * @package moodle |
24 | * @subpackage registration |
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 | * The forms needed by registration pages. |
30 | */ |
31 | |
32 | |
33 | require_once($CFG->dirroot.'/lib/formslib.php'); |
34 | require_once($CFG->dirroot.'/lib/hublib.php'); |
35 | |
36 | /** |
37 | * This form display a hub selector. |
38 | * The hub list is retrieved from Moodle.org hub directory. |
39 | * Also displayed, a text field to enter private hub url + its password |
40 | */ |
41 | class hub_selector_form extends moodleform { |
42 | |
43 | public function definition() { |
44 | global $CFG; |
45 | $mform =& $this->_form; |
46 | $mform->addElement('header', 'site', get_string('selecthub', 'hub')); |
47 | |
48 | //retrieve the hub list on the hub directory by web service |
49 | $function = 'hubdirectory_get_hubs'; |
50 | $params = array(); |
51 | $serverurl = HUBDIRECTORYURL."/local/hubdirectory/webservice/webservices.php"; |
52 | require_once($CFG->dirroot."/webservice/xmlrpc/lib.php"); |
53 | $xmlrpcclient = new webservice_xmlrpc_client(); |
54 | $hubs = $xmlrpcclient->call($serverurl, 'publichubdirectory', $function, $params); |
55 | |
56 | //Public hub list |
57 | $options = array(); |
58 | foreach ($hubs as $hub) { |
59 | //to not display a name longer than 100 character (too big) |
60 | if (strlen($hub['name'])>100) { |
61 | $hubname = substr($hub['name'],0, 100); |
62 | $hubname = $hubname."..."; |
63 | } else { |
64 | $hubname = $hub['name']; |
65 | } |
66 | $options[$hub['url']] = $hubname; |
67 | $mform->addElement('hidden', clean_param($hub['url'], PARAM_ALPHANUMEXT), $hubname); |
68 | } |
69 | $mform->addElement('select', 'publichub', get_string('publichub','hub'), |
70 | $options, array("size" => 15)); |
71 | |
72 | $mform->addElement('static','or' , '', get_string('orenterprivatehub', 'hub')); |
73 | |
74 | //Private hub |
75 | $mform->addElement('text','unlistedurl' , get_string('privatehuburl', 'hub')); |
76 | $mform->addElement('text','password' , get_string('password')); |
77 | |
78 | $this->add_action_buttons(false, get_string('selecthub', 'hub')); |
79 | } |
80 | |
81 | /** |
82 | * Check the unlisted URL is a URL |
83 | */ |
84 | function validation($data, $files) { |
85 | global $CFG; |
86 | $errors = parent::validation($data, $files); |
87 | |
88 | $unlistedurl = $this->_form->_submitValues['unlistedurl']; |
89 | |
90 | if (!empty($unlistedurl)) { |
91 | $unlistedurltotest = clean_param($unlistedurl, PARAM_URL); |
92 | if (empty($unlistedurltotest)) { |
93 | $errors['unlistedurl'] = get_string('badurlformat', 'hub'); |
94 | } |
95 | } |
96 | |
97 | return $errors; |
98 | } |
99 | |
100 | } |
101 | |
102 | |
103 | /** |
104 | * The site registration form. Information will be sent to a given hub. |
105 | */ |
106 | class site_registration_form extends moodleform { |
107 | |
108 | public function definition() { |
109 | global $CFG, $DB; |
110 | |
111 | $strrequired = get_string('required'); |
112 | $mform =& $this->_form; |
113 | $huburl = $this->_customdata['huburl']; |
114 | $hubname = $this->_customdata['hubname']; |
115 | $admin = get_admin(); |
116 | $site = get_site(); |
117 | |
118 | //retrieve config for this hub and set default if they don't exist |
119 | $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT); |
120 | $sitename = get_config('hub', 'site_name_'.$cleanhuburl); |
121 | if ($sitename === false) { |
122 | $sitename = $site->fullname; |
123 | } |
124 | $sitedescription = get_config('hub', 'site_description_'.$cleanhuburl); |
125 | if ($sitedescription === false) { |
126 | $sitedescription = $site->summary; |
127 | } |
128 | $contactname = get_config('hub', 'site_contactname_'.$cleanhuburl); |
129 | if ($contactname === false) { |
130 | $contactname = fullname($admin, true); |
131 | } |
132 | $contactemail = get_config('hub', 'site_contactemail_'.$cleanhuburl); |
133 | if ($contactemail === false) { |
134 | $contactemail = $admin->email; |
135 | } |
136 | $contactphone = get_config('hub', 'site_contactphone_'.$cleanhuburl); |
137 | if ($contactphone === false) { |
138 | $contactphone = $admin->phone1; |
139 | } |
140 | $imageurl = get_config('hub', 'site_imageurl_'.$cleanhuburl); |
141 | $privacy = get_config('hub', 'site_privacy_'.$cleanhuburl); |
142 | $address = get_config('hub', 'site_address_'.$cleanhuburl); |
143 | $region = get_config('hub', 'site_region_'.$cleanhuburl); |
144 | $country = get_config('hub', 'site_country_'.$cleanhuburl); |
145 | if ($country === false) { |
146 | $country = $admin->country; |
147 | } |
148 | $geolocation = get_config('hub', 'site_geolocation_'.$cleanhuburl); |
149 | $contactable = get_config('hub', 'site_contactable_'.$cleanhuburl); |
150 | $emailalert = get_config('hub', 'site_emailalert_'.$cleanhuburl); |
151 | $coursesnumber = get_config('hub', 'site_coursesnumber_'.$cleanhuburl); |
152 | $usersnumber = get_config('hub', 'site_usersnumber_'.$cleanhuburl); |
153 | $roleassignmentsnumber = get_config('hub', 'site_roleassignmentsnumber_'.$cleanhuburl); |
154 | $postsnumber = get_config('hub', 'site_postsnumber_'.$cleanhuburl); |
155 | $questionsnumber = get_config('hub', 'site_questionsnumber_'.$cleanhuburl); |
156 | $resourcesnumber = get_config('hub', 'site_resourcesnumber_'.$cleanhuburl); |
157 | $mediancoursesize = get_config('hub', 'site_mediancoursesize_'.$cleanhuburl); |
158 | |
159 | //hidden parameters |
160 | $mform->addElement('hidden', 'huburl', $huburl); |
161 | $mform->addElement('hidden', 'hubname', $hubname); |
162 | |
163 | //the input parameters |
164 | $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub')); |
165 | |
166 | $mform->addElement('text','name' , get_string('fullsitename')); |
167 | $mform->addRule('name', $strrequired, 'required', null, 'client'); |
168 | $mform->setType('name', PARAM_TEXT); |
169 | $mform->setDefault('name', $sitename); |
170 | |
171 | $options = array(); |
172 | $hub = new hub(); |
173 | $options[SITENOTPUBLISHED] = $hub->get_site_privacy_string(SITENOTPUBLISHED); |
174 | $options[SITENAMEPUBLISHED] = $hub->get_site_privacy_string(SITENAMEPUBLISHED); |
175 | $options[SITELINKPUBLISHED] = $hub->get_site_privacy_string(SITELINKPUBLISHED); |
176 | $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), $options); |
177 | $mform->setDefault('privacy', $privacy); |
178 | unset($options); |
179 | |
180 | $mform->addElement('textarea', 'description', get_string('description'), array('rows'=>10)); |
181 | $mform->addRule('description', $strrequired, 'required', null, 'client'); |
182 | $mform->setDefault('description', $sitedescription); |
183 | $mform->setType('description', PARAM_TEXT); |
184 | |
185 | $mform->addElement('static', 'urlstring',get_string('siteurl', 'hub'), $CFG->wwwroot); |
186 | |
187 | $languages = get_string_manager()->get_list_of_languages(); |
188 | $mform->addElement('static', 'langstring',get_string('language'), $languages[current_language()]); |
189 | $mform->addElement('hidden', 'language', current_language()); |
190 | |
191 | $mform->addElement('static', 'versionstring',get_string('moodleversion'), $CFG->version); |
192 | $mform->addElement('hidden', 'moodleversion', $CFG->version); |
193 | |
194 | $mform->addElement('static', 'releasestring',get_string('moodlerelease'), $CFG->release); |
195 | $mform->addElement('hidden', 'moodlerelease', $CFG->release); |
196 | |
197 | $mform->addElement('textarea','address' , get_string('postaladdress', 'hub')); |
198 | $mform->setType('address', PARAM_TEXT); |
199 | $mform->setDefault('address', $address); |
200 | |
201 | //TODO: use the region array I generated |
202 | $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-')); |
203 | $mform->setDefault('region', $region); |
204 | |
205 | $countries = get_string_manager()->get_list_of_countries(); |
206 | $mform->addElement('select', 'country', get_string('selectacountry'), $countries); |
207 | $mform->setDefault('country', $country); |
208 | |
209 | $mform->addElement('text','geolocation' , get_string('geolocation')); |
210 | $mform->setDefault('geolocation', $geolocation); |
211 | |
212 | $mform->addElement('text', 'contactname', get_string('administrator')); |
213 | $mform->addRule('contactname', $strrequired, 'required', null, 'client'); |
214 | $mform->setType('contactname', PARAM_TEXT); |
215 | $mform->setDefault('contactname', $contactname); |
216 | |
217 | $mform->addElement('text','contactphone' , get_string('phone')); |
218 | $mform->setType('contactphone', PARAM_TEXT); |
219 | |
220 | $mform->addElement('text', 'contactemail', get_string('email')); |
221 | $mform->addRule('contactemail', $strrequired, 'required', null, 'client'); |
222 | $mform->setType('contactemail', PARAM_TEXT); |
223 | $mform->setDefault('contactemail', $contactemail); |
224 | |
225 | $options = array(); |
226 | $options[0] = get_string("registrationcontactno"); |
227 | $options[1] = get_string("registrationcontactyes"); |
228 | $mform->addElement('select', 'contactable', get_string('registrationcontact'), $options); |
229 | $mform->setDefault('contactable', $contactable); |
230 | unset($options); |
231 | |
232 | $options = array(); |
233 | $options[0] = get_string("registrationno"); |
234 | $options[1] = get_string("registrationyes"); |
235 | $mform->addElement('select', 'emailalert', get_string('registrationemail'), $options); |
236 | $mform->setDefault('emailalert', $emailalert); |
237 | unset($options); |
238 | |
239 | $mform->addElement('text','imageurl' , get_string('logourl', 'hub')); |
240 | $mform->setType('imageurl', PARAM_URL); |
241 | $mform->setDefault('imageurl', $imageurl); |
242 | |
243 | /// Display statistic that are going to be retrieve by the hub |
244 | $coursecount = $DB->count_records('course')-1; |
245 | $usercount = $DB->count_records('user', array('deleted'=>0)); |
246 | $roleassigncount = $DB->count_records('role_assignments'); |
247 | $postcount = $DB->count_records('forum_posts'); |
248 | $questioncount = $DB->count_records('question'); |
249 | $resourcecount = $DB->count_records('resource'); |
250 | require_once($CFG->dirroot."/course/lib.php"); |
251 | $participantnumberaverage= average_number_of_participants(); |
252 | $modulenumberaverage= average_number_of_courses_modules(); |
253 | |
254 | if (MOODLEORGHUBURL != $huburl) { |
255 | $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'), |
256 | " ".get_string('coursesnumber', 'hub', $coursecount)); |
257 | $mform->setDefault('courses', true); |
258 | |
259 | $mform->addElement('checkbox', 'users', '', |
260 | " ".get_string('usersnumber', 'hub', $usercount)); |
261 | $mform->setDefault('users', true); |
262 | |
263 | $mform->addElement('checkbox', 'roleassignments', '', |
264 | " ".get_string('roleassignmentsnumber', 'hub', $roleassigncount)); |
265 | $mform->setDefault('roleassignments', true); |
266 | |
267 | $mform->addElement('checkbox', 'posts', '', |
268 | " ".get_string('postsnumber', 'hub', $postcount)); |
269 | $mform->setDefault('posts', true); |
270 | |
271 | $mform->addElement('checkbox', 'questions', '', |
272 | " ".get_string('questionsnumber', 'hub', $questioncount)); |
273 | $mform->setDefault('questions', true); |
274 | |
275 | $mform->addElement('checkbox', 'resources', '', |
276 | " ".get_string('resourcesnumber', 'hub', $resourcecount)); |
277 | $mform->setDefault('resources', true); |
278 | |
279 | $mform->addElement('checkbox', 'participantnumberaverage', '', |
280 | " ".get_string('participantnumberaverage', 'hub', $participantnumberaverage)); |
281 | $mform->setDefault('participantnumberaverage', true); |
282 | |
283 | $mform->addElement('checkbox', 'modulenumberaverage', '', |
284 | " ".get_string('modulenumberaverage', 'hub', $modulenumberaverage)); |
285 | $mform->setDefault('modulenumberaverage', true); |
286 | } else { |
287 | $mform->addElement('static', 'courseslabel',get_string('sendfollowinginfo', 'hub'), |
288 | " ".get_string('coursesnumber', 'hub', $coursecount)); |
289 | $mform->addElement('hidden', 'courses', true); |
290 | |
291 | $mform->addElement('static', 'userslabel', '', |
292 | " ".get_string('usersnumber', 'hub', $usercount)); |
293 | $mform->addElement('hidden', 'users', true); |
294 | |
295 | $mform->addElement('static', 'roleassignmentslabel', '', |
296 | " ".get_string('roleassignmentsnumber', 'hub', $roleassigncount)); |
297 | $mform->addElement('hidden', 'roleassignments', true); |
298 | |
299 | $mform->addElement('static', 'postslabel', '', |
300 | " ".get_string('postsnumber', 'hub', $postcount)); |
301 | $mform->addElement('hidden', 'posts', true); |
302 | |
303 | $mform->addElement('static', 'questionslabel', '', |
304 | " ".get_string('questionsnumber', 'hub', $questioncount)); |
305 | $mform->addElement('hidden', 'questions', true); |
306 | |
307 | $mform->addElement('static', 'resourceslabel', '', |
308 | " ".get_string('resourcesnumber', 'hub', $resourcecount)); |
309 | $mform->addElement('hidden', 'resources', true); |
310 | |
311 | $mform->addElement('static', 'participantnumberaveragelabel', '', |
312 | " ".get_string('participantnumberaverage', 'hub', $participantnumberaverage)); |
313 | $mform->addElement('hidden', 'participantnumberaverage', true); |
314 | |
315 | $mform->addElement('static', 'modulenumberaveragelabel', '', |
316 | " ".get_string('modulenumberaverage', 'hub', $modulenumberaverage)); |
317 | $mform->addElement('hidden', 'modulenumberaverage', true); |
318 | } |
319 | |
320 | //check if it's a first registration or update |
321 | $hubregistered = $hub->get_registeredhub($huburl); |
322 | |
323 | if (!empty($hubregistered)) { |
324 | $buttonlabel = get_string('updatesite', 'hub', |
325 | !empty($hubname)?$hubname:$huburl); |
326 | $mform->addElement('hidden', 'update', true); |
327 | } else { |
328 | $buttonlabel = get_string('registersite', 'hub', |
329 | !empty($hubname)?$hubname:$huburl); |
330 | } |
331 | |
332 | $this->add_action_buttons(false, $buttonlabel); |
333 | } |
334 | |
335 | /** |
336 | * Check that the image size is correct |
337 | */ |
338 | function validation($data, $files) { |
339 | global $CFG; |
340 | |
341 | $errors = array(); |
342 | |
343 | //check if the image (imageurl) has a correct size |
344 | $imageurl = $this->_form->_submitValues['imageurl']; |
345 | if (!empty($imageurl)) { |
346 | list($imagewidth, $imageheight, $imagetype, $imageattr) = getimagesize($imageurl); //getimagesize is a GD function |
347 | if ($imagewidth > SITEIMAGEWIDTH or $imageheight > SITEIMAGEHEIGHT) { |
348 | $sizestrings = new stdClass(); |
349 | $sizestrings->width = SITEIMAGEWIDTH; |
350 | $sizestrings->height = SITEIMAGEHEIGHT; |
351 | $errors['imageurl'] = get_string('errorbadimageheightwidth', 'hub', $sizestrings); |
352 | } |
353 | } |
354 | |
355 | return $errors; |
356 | } |
357 | |
358 | } |
359 | |
360 | ?> |