MDL-21655 "big scary enrolment and roles improvements" fixed a typo in previous check...
[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();
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 */
106class 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);
4032415e 178 $mform->addHelpButton('privacy', 'privacy', 'hub');
07ab0c80 179 unset($options);
180
181 $mform->addElement('textarea', 'description', get_string('description'), array('rows'=>10));
182 $mform->addRule('description', $strrequired, 'required', null, 'client');
183 $mform->setDefault('description', $sitedescription);
184 $mform->setType('description', PARAM_TEXT);
185
186 $mform->addElement('static', 'urlstring',get_string('siteurl', 'hub'), $CFG->wwwroot);
187
188 $languages = get_string_manager()->get_list_of_languages();
189 $mform->addElement('static', 'langstring',get_string('language'), $languages[current_language()]);
190 $mform->addElement('hidden', 'language', current_language());
191
192 $mform->addElement('static', 'versionstring',get_string('moodleversion'), $CFG->version);
193 $mform->addElement('hidden', 'moodleversion', $CFG->version);
194
195 $mform->addElement('static', 'releasestring',get_string('moodlerelease'), $CFG->release);
196 $mform->addElement('hidden', 'moodlerelease', $CFG->release);
197
198 $mform->addElement('textarea','address' , get_string('postaladdress', 'hub'));
199 $mform->setType('address', PARAM_TEXT);
200 $mform->setDefault('address', $address);
201
202 //TODO: use the region array I generated
4032415e 203// $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-'));
204// $mform->setDefault('region', $region);
205 $mform->addElement('hidden', 'regioncode', '-');
07ab0c80 206
207 $countries = get_string_manager()->get_list_of_countries();
4032415e 208 $mform->addElement('select', 'countrycode', get_string('selectacountry'), $countries);
209 $mform->setDefault('countrycode', $country);
07ab0c80 210
211 $mform->addElement('text','geolocation' , get_string('geolocation'));
212 $mform->setDefault('geolocation', $geolocation);
4032415e 213 $mform->addHelpButton('geolocation', 'geolocation', 'hub');
07ab0c80 214
215 $mform->addElement('text', 'contactname', get_string('administrator'));
216 $mform->addRule('contactname', $strrequired, 'required', null, 'client');
217 $mform->setType('contactname', PARAM_TEXT);
218 $mform->setDefault('contactname', $contactname);
219
220 $mform->addElement('text','contactphone' , get_string('phone'));
221 $mform->setType('contactphone', PARAM_TEXT);
4032415e 222 $mform->addHelpButton('contactphone', 'contactphone', 'hub');
07ab0c80 223
224 $mform->addElement('text', 'contactemail', get_string('email'));
225 $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
226 $mform->setType('contactemail', PARAM_TEXT);
227 $mform->setDefault('contactemail', $contactemail);
228
229 $options = array();
230 $options[0] = get_string("registrationcontactno");
231 $options[1] = get_string("registrationcontactyes");
232 $mform->addElement('select', 'contactable', get_string('registrationcontact'), $options);
233 $mform->setDefault('contactable', $contactable);
4032415e 234 $mform->addHelpButton('contactable', 'contactable', 'hub');
07ab0c80 235 unset($options);
236
237 $options = array();
238 $options[0] = get_string("registrationno");
239 $options[1] = get_string("registrationyes");
240 $mform->addElement('select', 'emailalert', get_string('registrationemail'), $options);
241 $mform->setDefault('emailalert', $emailalert);
4032415e 242 $mform->addHelpButton('emailalert', 'emailalert', 'hub');
07ab0c80 243 unset($options);
244
4032415e 245// $mform->addElement('text','imageurl' , get_string('logourl', 'hub'));
246// $mform->setType('imageurl', PARAM_URL);
247// $mform->setDefault('imageurl', $imageurl);
248// $mform->addHelpButton('imageurl', 'imageurl', 'hub');
e275f25e 249 $mform->addElement('hidden', 'imageurl', ''); //TODO: temporary
07ab0c80 250
251 /// Display statistic that are going to be retrieve by the hub
252 $coursecount = $DB->count_records('course')-1;
253 $usercount = $DB->count_records('user', array('deleted'=>0));
254 $roleassigncount = $DB->count_records('role_assignments');
255 $postcount = $DB->count_records('forum_posts');
256 $questioncount = $DB->count_records('question');
257 $resourcecount = $DB->count_records('resource');
258 require_once($CFG->dirroot."/course/lib.php");
259 $participantnumberaverage= average_number_of_participants();
260 $modulenumberaverage= average_number_of_courses_modules();
261
262 if (MOODLEORGHUBURL != $huburl) {
263 $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'),
264 " ".get_string('coursesnumber', 'hub', $coursecount));
265 $mform->setDefault('courses', true);
266
267 $mform->addElement('checkbox', 'users', '',
268 " ".get_string('usersnumber', 'hub', $usercount));
269 $mform->setDefault('users', true);
270
271 $mform->addElement('checkbox', 'roleassignments', '',
272 " ".get_string('roleassignmentsnumber', 'hub', $roleassigncount));
273 $mform->setDefault('roleassignments', true);
274
275 $mform->addElement('checkbox', 'posts', '',
276 " ".get_string('postsnumber', 'hub', $postcount));
277 $mform->setDefault('posts', true);
278
279 $mform->addElement('checkbox', 'questions', '',
280 " ".get_string('questionsnumber', 'hub', $questioncount));
281 $mform->setDefault('questions', true);
282
283 $mform->addElement('checkbox', 'resources', '',
284 " ".get_string('resourcesnumber', 'hub', $resourcecount));
285 $mform->setDefault('resources', true);
286
287 $mform->addElement('checkbox', 'participantnumberaverage', '',
288 " ".get_string('participantnumberaverage', 'hub', $participantnumberaverage));
289 $mform->setDefault('participantnumberaverage', true);
290
291 $mform->addElement('checkbox', 'modulenumberaverage', '',
292 " ".get_string('modulenumberaverage', 'hub', $modulenumberaverage));
293 $mform->setDefault('modulenumberaverage', true);
294 } else {
295 $mform->addElement('static', 'courseslabel',get_string('sendfollowinginfo', 'hub'),
296 " ".get_string('coursesnumber', 'hub', $coursecount));
297 $mform->addElement('hidden', 'courses', true);
298
299 $mform->addElement('static', 'userslabel', '',
300 " ".get_string('usersnumber', 'hub', $usercount));
301 $mform->addElement('hidden', 'users', true);
302
303 $mform->addElement('static', 'roleassignmentslabel', '',
304 " ".get_string('roleassignmentsnumber', 'hub', $roleassigncount));
305 $mform->addElement('hidden', 'roleassignments', true);
306
307 $mform->addElement('static', 'postslabel', '',
308 " ".get_string('postsnumber', 'hub', $postcount));
309 $mform->addElement('hidden', 'posts', true);
310
311 $mform->addElement('static', 'questionslabel', '',
312 " ".get_string('questionsnumber', 'hub', $questioncount));
313 $mform->addElement('hidden', 'questions', true);
314
315 $mform->addElement('static', 'resourceslabel', '',
316 " ".get_string('resourcesnumber', 'hub', $resourcecount));
317 $mform->addElement('hidden', 'resources', true);
318
319 $mform->addElement('static', 'participantnumberaveragelabel', '',
320 " ".get_string('participantnumberaverage', 'hub', $participantnumberaverage));
321 $mform->addElement('hidden', 'participantnumberaverage', true);
322
323 $mform->addElement('static', 'modulenumberaveragelabel', '',
324 " ".get_string('modulenumberaverage', 'hub', $modulenumberaverage));
325 $mform->addElement('hidden', 'modulenumberaverage', true);
326 }
327
328 //check if it's a first registration or update
329 $hubregistered = $hub->get_registeredhub($huburl);
330
331 if (!empty($hubregistered)) {
332 $buttonlabel = get_string('updatesite', 'hub',
333 !empty($hubname)?$hubname:$huburl);
334 $mform->addElement('hidden', 'update', true);
335 } else {
336 $buttonlabel = get_string('registersite', 'hub',
337 !empty($hubname)?$hubname:$huburl);
338 }
339
340 $this->add_action_buttons(false, $buttonlabel);
341 }
342
343 /**
344 * Check that the image size is correct
345 */
346 function validation($data, $files) {
347 global $CFG;
348
349 $errors = array();
350
351 //check if the image (imageurl) has a correct size
352 $imageurl = $this->_form->_submitValues['imageurl'];
353 if (!empty($imageurl)) {
354 list($imagewidth, $imageheight, $imagetype, $imageattr) = getimagesize($imageurl); //getimagesize is a GD function
355 if ($imagewidth > SITEIMAGEWIDTH or $imageheight > SITEIMAGEHEIGHT) {
356 $sizestrings = new stdClass();
357 $sizestrings->width = SITEIMAGEWIDTH;
358 $sizestrings->height = SITEIMAGEHEIGHT;
359 $errors['imageurl'] = get_string('errorbadimageheightwidth', 'hub', $sizestrings);
360 }
361 }
362
363 return $errors;
364 }
365
366}
367
368?>