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