MDL-23997 new tests for bound query compatibility
[moodle.git] / admin / registration / forms.php
CommitLineData
07ab0c80 1<?php
2c9bb9c3 2
07ab0c80 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///////////////////////////////////////////////////////////////////////////
22
23/*
24 * @package moodle
25 * @subpackage registration
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 * The forms needed by registration pages.
2c9bb9c3 31 */
07ab0c80 32
33
2c9bb9c3 34require_once($CFG->dirroot . '/lib/formslib.php');
35require_once($CFG->dirroot . '/admin/registration/lib.php');
07ab0c80 36
f57c4047 37/**
38 * This form display a unregistration form.
39 */
40class site_unregistration_form extends moodleform {
41
42 public function definition() {
43 $mform = & $this->_form;
44 $mform->addElement('header', 'site', get_string('unregister', 'hub'));
45
46 $huburl = $this->_customdata['huburl'];
47 $hubname = $this->_customdata['hubname'];
48
49 $unregisterlabel = get_string('unregister', 'hub');
50 $mform->addElement('checkbox', 'unpublishalladvertisedcourses', '',
51 ' ' . get_string('unpublishalladvertisedcourses', 'hub'));
52 $mform->addElement('checkbox', 'unpublishalluploadedcourses', '',
53 ' ' . get_string('unpublishalluploadedcourses', 'hub'));
54
55 $mform->addElement('hidden', 'confirm', 1);
56 $mform->addElement('hidden', 'unregistration', 1);
57 $mform->addElement('hidden', 'huburl', $huburl);
58 $mform->addElement('hidden', 'hubname', $hubname);
59
60 $this->add_action_buttons(true, $unregisterlabel);
61 }
62
63}
64
65/**
66 * This form display a clean registration data form.
67 */
68class site_clean_registration_data_form extends moodleform {
69
70 public function definition() {
71 $mform = & $this->_form;
72 $mform->addElement('header', 'site', get_string('unregister', 'hub'));
73
74 $huburl = $this->_customdata['huburl'];
75 $hubname = $this->_customdata['hubname'];
76
77
78 $unregisterlabel = get_string('forceunregister', 'hub');
79 $mform->addElement('static', '', get_string('warning', 'hub'), get_string('forceunregisterconfirmation', 'hub', $hubname));
80
81
82 $mform->addElement('hidden', 'confirm', 1);
83 $mform->addElement('hidden', 'unregistration', 1);
84 $mform->addElement('hidden', 'cleanregdata', 1);
85 $mform->addElement('hidden', 'huburl', $huburl);
86 $mform->addElement('hidden', 'hubname', $hubname);
87
88 $this->add_action_buttons(true, $unregisterlabel);
89 }
90
91}
92
07ab0c80 93/**
94 * This form display a hub selector.
95 * The hub list is retrieved from Moodle.org hub directory.
96 * Also displayed, a text field to enter private hub url + its password
97 */
98class hub_selector_form extends moodleform {
99
100 public function definition() {
2c9bb9c3 101 global $CFG, $OUTPUT;
102 $mform = & $this->_form;
07ab0c80 103 $mform->addElement('header', 'site', get_string('selecthub', 'hub'));
104
105 //retrieve the hub list on the hub directory by web service
106 $function = 'hubdirectory_get_hubs';
107 $params = array();
2c9bb9c3 108 $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
109 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
001251f2 110 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, 'publichubdirectory');
2c9bb9c3 111 try {
001251f2 112 $hubs = $xmlrpcclient->call($function, $params);
2c9bb9c3 113 } catch (Exception $e) {
114 $error = $OUTPUT->notification(get_string('errorhublisting', 'hub', $e->getMessage()));
115 $mform->addElement('static', 'errorhub', '', $error);
116 $hubs = array();
117 }
07ab0c80 118
a33cc71f 119 //remove moodle.org from the hub list
120 foreach ($hubs as $key => $hub) {
06d4578c 121 if ($hub['url'] == HUB_MOODLEORGHUBURL) {
a33cc71f 122 unset($hubs[$key]);
123 }
124 }
125
07ab0c80 126 //Public hub list
127 $options = array();
128 foreach ($hubs as $hub) {
129 //to not display a name longer than 100 character (too big)
2c9bb9c3 130 if (strlen($hub['name']) > 100) {
131 $hubname = substr($hub['name'], 0, 100);
132 $hubname = $hubname . "...";
07ab0c80 133 } else {
134 $hubname = $hub['name'];
135 }
136 $options[$hub['url']] = $hubname;
137 $mform->addElement('hidden', clean_param($hub['url'], PARAM_ALPHANUMEXT), $hubname);
138 }
2c9bb9c3 139 if (!empty($hubs)) {
140 $mform->addElement('select', 'publichub', get_string('publichub', 'hub'),
141 $options, array("size" => 15));
142 }
07ab0c80 143
2c9bb9c3 144 $mform->addElement('static', 'or', '', get_string('orenterprivatehub', 'hub'));
07ab0c80 145
146 //Private hub
2c9bb9c3 147 $mform->addElement('text', 'unlistedurl', get_string('privatehuburl', 'hub'));
148 $mform->addElement('text', 'password', get_string('password'));
07ab0c80 149
150 $this->add_action_buttons(false, get_string('selecthub', 'hub'));
151 }
152
153 /**
154 * Check the unlisted URL is a URL
155 */
156 function validation($data, $files) {
157 global $CFG;
158 $errors = parent::validation($data, $files);
159
160 $unlistedurl = $this->_form->_submitValues['unlistedurl'];
161
162 if (!empty($unlistedurl)) {
163 $unlistedurltotest = clean_param($unlistedurl, PARAM_URL);
164 if (empty($unlistedurltotest)) {
165 $errors['unlistedurl'] = get_string('badurlformat', 'hub');
166 }
167 }
168
169 return $errors;
170 }
171
172}
173
07ab0c80 174/**
175 * The site registration form. Information will be sent to a given hub.
176 */
177class site_registration_form extends moodleform {
178
179 public function definition() {
180 global $CFG, $DB;
181
182 $strrequired = get_string('required');
2c9bb9c3 183 $mform = & $this->_form;
07ab0c80 184 $huburl = $this->_customdata['huburl'];
185 $hubname = $this->_customdata['hubname'];
4b1acb3a 186 $password = $this->_customdata['password'];
07ab0c80 187 $admin = get_admin();
188 $site = get_site();
189
190 //retrieve config for this hub and set default if they don't exist
191 $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
2c9bb9c3 192 $sitename = get_config('hub', 'site_name_' . $cleanhuburl);
07ab0c80 193 if ($sitename === false) {
194 $sitename = $site->fullname;
195 }
2c9bb9c3 196 $sitedescription = get_config('hub', 'site_description_' . $cleanhuburl);
07ab0c80 197 if ($sitedescription === false) {
198 $sitedescription = $site->summary;
199 }
2c9bb9c3 200 $contactname = get_config('hub', 'site_contactname_' . $cleanhuburl);
07ab0c80 201 if ($contactname === false) {
202 $contactname = fullname($admin, true);
203 }
2c9bb9c3 204 $contactemail = get_config('hub', 'site_contactemail_' . $cleanhuburl);
07ab0c80 205 if ($contactemail === false) {
206 $contactemail = $admin->email;
207 }
2c9bb9c3 208 $contactphone = get_config('hub', 'site_contactphone_' . $cleanhuburl);
07ab0c80 209 if ($contactphone === false) {
210 $contactphone = $admin->phone1;
211 }
2c9bb9c3 212 $imageurl = get_config('hub', 'site_imageurl_' . $cleanhuburl);
213 $privacy = get_config('hub', 'site_privacy_' . $cleanhuburl);
214 $address = get_config('hub', 'site_address_' . $cleanhuburl);
215 $region = get_config('hub', 'site_region_' . $cleanhuburl);
216 $country = get_config('hub', 'site_country_' . $cleanhuburl);
07ab0c80 217 if ($country === false) {
2c9bb9c3 218 $country = $admin->country;
07ab0c80 219 }
2c9bb9c3 220 $geolocation = get_config('hub', 'site_geolocation_' . $cleanhuburl);
221 $contactable = get_config('hub', 'site_contactable_' . $cleanhuburl);
222 $emailalert = get_config('hub', 'site_emailalert_' . $cleanhuburl);
223 $coursesnumber = get_config('hub', 'site_coursesnumber_' . $cleanhuburl);
224 $usersnumber = get_config('hub', 'site_usersnumber_' . $cleanhuburl);
225 $roleassignmentsnumber = get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl);
226 $postsnumber = get_config('hub', 'site_postsnumber_' . $cleanhuburl);
227 $questionsnumber = get_config('hub', 'site_questionsnumber_' . $cleanhuburl);
228 $resourcesnumber = get_config('hub', 'site_resourcesnumber_' . $cleanhuburl);
229 $mediancoursesize = get_config('hub', 'site_mediancoursesize_' . $cleanhuburl);
07ab0c80 230
231 //hidden parameters
232 $mform->addElement('hidden', 'huburl', $huburl);
233 $mform->addElement('hidden', 'hubname', $hubname);
4b1acb3a 234 $mform->addElement('hidden', 'password', $password);
07ab0c80 235
236 //the input parameters
237 $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
238
2c9bb9c3 239 $mform->addElement('text', 'name', get_string('sitename', 'hub'),
139978e5 240 array('class' => 'registration_textfield'));
07ab0c80 241 $mform->addRule('name', $strrequired, 'required', null, 'client');
242 $mform->setType('name', PARAM_TEXT);
243 $mform->setDefault('name', $sitename);
139978e5 244 $mform->addHelpButton('name', 'sitename', 'hub');
07ab0c80 245
246 $options = array();
94788de2 247 $registrationmanager = new registration_manager();
248 $options[HUB_SITENOTPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENOTPUBLISHED);
249 $options[HUB_SITENAMEPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENAMEPUBLISHED);
250 $options[HUB_SITELINKPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITELINKPUBLISHED);
07ab0c80 251 $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), $options);
252 $mform->setDefault('privacy', $privacy);
4032415e 253 $mform->addHelpButton('privacy', 'privacy', 'hub');
07ab0c80 254 unset($options);
255
139978e5 256 $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'),
2c9bb9c3 257 array('rows' => 8, 'cols' => 41));
07ab0c80 258 $mform->addRule('description', $strrequired, 'required', null, 'client');
259 $mform->setDefault('description', $sitedescription);
260 $mform->setType('description', PARAM_TEXT);
139978e5 261 $mform->addHelpButton('description', 'sitedesc', 'hub');
07ab0c80 262
2c9bb9c3 263 $mform->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $CFG->wwwroot);
139978e5 264 $mform->addHelpButton('urlstring', 'siteurl', 'hub');
07ab0c80 265
266 $languages = get_string_manager()->get_list_of_languages();
2c9bb9c3 267 $mform->addElement('static', 'langstring', get_string('sitelang', 'hub'), $languages[current_language()]);
07ab0c80 268 $mform->addElement('hidden', 'language', current_language());
139978e5 269 $mform->addHelpButton('langstring', 'sitelang', 'hub');
07ab0c80 270
2c9bb9c3 271 $mform->addElement('static', 'versionstring', get_string('siteversion', 'hub'), $CFG->version);
07ab0c80 272 $mform->addElement('hidden', 'moodleversion', $CFG->version);
139978e5 273 $mform->addHelpButton('versionstring', 'siteversion', 'hub');
07ab0c80 274
2c9bb9c3 275 $mform->addElement('static', 'releasestring', get_string('siterelease', 'hub'), $CFG->release);
07ab0c80 276 $mform->addElement('hidden', 'moodlerelease', $CFG->release);
139978e5 277 $mform->addHelpButton('releasestring', 'siterelease', 'hub');
07ab0c80 278
2c9bb9c3 279 $mform->addElement('textarea', 'address', get_string('postaladdress', 'hub'),
280 array('rows' => 4, 'cols' => 41));
07ab0c80 281 $mform->setType('address', PARAM_TEXT);
282 $mform->setDefault('address', $address);
139978e5 283 $mform->addHelpButton('address', 'postaladdress', 'hub');
07ab0c80 284
285 //TODO: use the region array I generated
4032415e 286// $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-'));
287// $mform->setDefault('region', $region);
288 $mform->addElement('hidden', 'regioncode', '-');
07ab0c80 289
290 $countries = get_string_manager()->get_list_of_countries();
139978e5 291 $mform->addElement('select', 'countrycode', get_string('sitecountry', 'hub'), $countries);
4032415e 292 $mform->setDefault('countrycode', $country);
139978e5 293 $mform->addHelpButton('countrycode', 'sitecountry', 'hub');
07ab0c80 294
2c9bb9c3 295 $mform->addElement('text', 'geolocation', get_string('sitegeolocation', 'hub'),
139978e5 296 array('class' => 'registration_textfield'));
07ab0c80 297 $mform->setDefault('geolocation', $geolocation);
139978e5 298 $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub');
07ab0c80 299
139978e5 300 $mform->addElement('text', 'contactname', get_string('siteadmin', 'hub'),
301 array('class' => 'registration_textfield'));
07ab0c80 302 $mform->addRule('contactname', $strrequired, 'required', null, 'client');
303 $mform->setType('contactname', PARAM_TEXT);
304 $mform->setDefault('contactname', $contactname);
139978e5 305 $mform->addHelpButton('contactname', 'siteadmin', 'hub');
07ab0c80 306
2c9bb9c3 307 $mform->addElement('text', 'contactphone', get_string('sitephone', 'hub'),
139978e5 308 array('class' => 'registration_textfield'));
07ab0c80 309 $mform->setType('contactphone', PARAM_TEXT);
139978e5 310 $mform->addHelpButton('contactphone', 'sitephone', 'hub');
07ab0c80 311
139978e5 312 $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'),
313 array('class' => 'registration_textfield'));
07ab0c80 314 $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
315 $mform->setType('contactemail', PARAM_TEXT);
316 $mform->setDefault('contactemail', $contactemail);
139978e5 317 $mform->addHelpButton('contactemail', 'siteemail', 'hub');
07ab0c80 318
319 $options = array();
320 $options[0] = get_string("registrationcontactno");
321 $options[1] = get_string("registrationcontactyes");
139978e5 322 $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options);
07ab0c80 323 $mform->setDefault('contactable', $contactable);
139978e5 324 $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub');
07ab0c80 325 unset($options);
326
327 $options = array();
328 $options[0] = get_string("registrationno");
329 $options[1] = get_string("registrationyes");
139978e5 330 $mform->addElement('select', 'emailalert', get_string('siteregistrationemail', 'hub'), $options);
07ab0c80 331 $mform->setDefault('emailalert', $emailalert);
139978e5 332 $mform->addHelpButton('emailalert', 'siteregistrationemail', 'hub');
07ab0c80 333 unset($options);
334
139978e5 335 //TODO site logo
e275f25e 336 $mform->addElement('hidden', 'imageurl', ''); //TODO: temporary
07ab0c80 337 /// Display statistic that are going to be retrieve by the hub
2c9bb9c3 338 $coursecount = $DB->count_records('course') - 1;
339 $usercount = $DB->count_records('user', array('deleted' => 0));
07ab0c80 340 $roleassigncount = $DB->count_records('role_assignments');
341 $postcount = $DB->count_records('forum_posts');
342 $questioncount = $DB->count_records('question');
343 $resourcecount = $DB->count_records('resource');
2c9bb9c3 344 require_once($CFG->dirroot . "/course/lib.php");
345 $participantnumberaverage = average_number_of_participants();
346 $modulenumberaverage = average_number_of_courses_modules();
07ab0c80 347
06d4578c 348 if (HUB_MOODLEORGHUBURL != $huburl) {
2c9bb9c3 349 $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'),
350 " " . get_string('coursesnumber', 'hub', $coursecount));
07ab0c80 351 $mform->setDefault('courses', true);
139978e5 352 $mform->addHelpButton('courses', 'sendfollowinginfo', 'hub');
07ab0c80 353
354 $mform->addElement('checkbox', 'users', '',
2c9bb9c3 355 " " . get_string('usersnumber', 'hub', $usercount));
07ab0c80 356 $mform->setDefault('users', true);
357
358 $mform->addElement('checkbox', 'roleassignments', '',
2c9bb9c3 359 " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
07ab0c80 360 $mform->setDefault('roleassignments', true);
361
362 $mform->addElement('checkbox', 'posts', '',
2c9bb9c3 363 " " . get_string('postsnumber', 'hub', $postcount));
07ab0c80 364 $mform->setDefault('posts', true);
365
366 $mform->addElement('checkbox', 'questions', '',
2c9bb9c3 367 " " . get_string('questionsnumber', 'hub', $questioncount));
07ab0c80 368 $mform->setDefault('questions', true);
369
370 $mform->addElement('checkbox', 'resources', '',
2c9bb9c3 371 " " . get_string('resourcesnumber', 'hub', $resourcecount));
07ab0c80 372 $mform->setDefault('resources', true);
373
374 $mform->addElement('checkbox', 'participantnumberaverage', '',
2c9bb9c3 375 " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
07ab0c80 376 $mform->setDefault('participantnumberaverage', true);
377
378 $mform->addElement('checkbox', 'modulenumberaverage', '',
2c9bb9c3 379 " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
07ab0c80 380 $mform->setDefault('modulenumberaverage', true);
381 } else {
2c9bb9c3 382 $mform->addElement('static', 'courseslabel', get_string('sendfollowinginfo', 'hub'),
383 " " . get_string('coursesnumber', 'hub', $coursecount));
07ab0c80 384 $mform->addElement('hidden', 'courses', true);
2c9bb9c3 385 $mform->addHelpButton('courseslabel', 'sendfollowinginfo', 'hub');
07ab0c80 386
387 $mform->addElement('static', 'userslabel', '',
2c9bb9c3 388 " " . get_string('usersnumber', 'hub', $usercount));
07ab0c80 389 $mform->addElement('hidden', 'users', true);
390
391 $mform->addElement('static', 'roleassignmentslabel', '',
2c9bb9c3 392 " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
07ab0c80 393 $mform->addElement('hidden', 'roleassignments', true);
394
395 $mform->addElement('static', 'postslabel', '',
2c9bb9c3 396 " " . get_string('postsnumber', 'hub', $postcount));
07ab0c80 397 $mform->addElement('hidden', 'posts', true);
398
399 $mform->addElement('static', 'questionslabel', '',
2c9bb9c3 400 " " . get_string('questionsnumber', 'hub', $questioncount));
07ab0c80 401 $mform->addElement('hidden', 'questions', true);
402
403 $mform->addElement('static', 'resourceslabel', '',
2c9bb9c3 404 " " . get_string('resourcesnumber', 'hub', $resourcecount));
07ab0c80 405 $mform->addElement('hidden', 'resources', true);
406
407 $mform->addElement('static', 'participantnumberaveragelabel', '',
2c9bb9c3 408 " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
07ab0c80 409 $mform->addElement('hidden', 'participantnumberaverage', true);
410
411 $mform->addElement('static', 'modulenumberaveragelabel', '',
2c9bb9c3 412 " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
07ab0c80 413 $mform->addElement('hidden', 'modulenumberaverage', true);
414 }
415
416 //check if it's a first registration or update
94788de2 417 $hubregistered = $registrationmanager->get_registeredhub($huburl);
07ab0c80 418
419 if (!empty($hubregistered)) {
420 $buttonlabel = get_string('updatesite', 'hub',
2c9bb9c3 421 !empty($hubname) ? $hubname : $huburl);
07ab0c80 422 $mform->addElement('hidden', 'update', true);
423 } else {
424 $buttonlabel = get_string('registersite', 'hub',
2c9bb9c3 425 !empty($hubname) ? $hubname : $huburl);
07ab0c80 426 }
427
428 $this->add_action_buttons(false, $buttonlabel);
429 }
430
07ab0c80 431}
432