Merge branch 'MDL-57812-master' of git://github.com/jleyva/moodle
[moodle.git] / admin / registration / forms.php
blob5f89d0c0d9b4032d8e3f8986e476bb648f976bc0
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
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
30 * The forms needed by registration pages.
34 require_once($CFG->libdir . '/formslib.php');
35 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
37 /**
38 * This form display a unregistration form.
40 class site_unregistration_form extends moodleform {
42 public function definition() {
43 $mform = & $this->_form;
44 $mform->addElement('header', 'site', get_string('unregister', 'hub'));
46 $huburl = $this->_customdata['huburl'];
47 $hubname = $this->_customdata['hubname'];
49 $unregisterlabel = get_string('unregister', 'hub');
50 $mform->addElement('checkbox', 'unpublishalladvertisedcourses', '',
51 ' ' . get_string('unpublishalladvertisedcourses', 'hub'));
52 $mform->setType('unpublishalladvertisedcourses', PARAM_INT);
53 $mform->addElement('checkbox', 'unpublishalluploadedcourses', '',
54 ' ' . get_string('unpublishalluploadedcourses', 'hub'));
55 $mform->setType('unpublishalluploadedcourses', PARAM_INT);
57 $mform->addElement('hidden', 'confirm', 1);
58 $mform->setType('confirm', PARAM_INT);
59 $mform->addElement('hidden', 'unregistration', 1);
60 $mform->setType('unregistration', PARAM_INT);
61 $mform->addElement('hidden', 'huburl', $huburl);
62 $mform->setType('huburl', PARAM_URL);
63 $mform->addElement('hidden', 'hubname', $hubname);
64 $mform->setType('hubname', PARAM_TEXT);
66 $this->add_action_buttons(true, $unregisterlabel);
71 /**
72 * This form display a clean registration data form.
74 class site_clean_registration_data_form extends moodleform {
76 public function definition() {
77 $mform = & $this->_form;
78 $mform->addElement('header', 'site', get_string('unregister', 'hub'));
80 $huburl = $this->_customdata['huburl'];
81 $hubname = $this->_customdata['hubname'];
84 $unregisterlabel = get_string('forceunregister', 'hub');
85 $mform->addElement('static', '', get_string('warning', 'hub'), get_string('forceunregisterconfirmation', 'hub', $hubname));
87 $mform->addElement('hidden', 'confirm', 1);
88 $mform->setType('confirm', PARAM_INT);
89 $mform->addElement('hidden', 'unregistration', 1);
90 $mform->setType('unregistration', PARAM_INT);
91 $mform->addElement('hidden', 'cleanregdata', 1);
92 $mform->setType('cleanregdata', PARAM_INT);
93 $mform->addElement('hidden', 'huburl', $huburl);
94 $mform->setType('huburl', PARAM_URL);
95 $mform->addElement('hidden', 'hubname', $hubname);
96 $mform->setType('hubname', PARAM_TEXT);
98 $this->add_action_buttons(true, $unregisterlabel);
104 * This form display a hub selector.
105 * The hub list is retrieved from Moodle.org hub directory.
106 * Also displayed, a text field to enter private hub url + its password
108 class hub_selector_form extends moodleform {
110 public function definition() {
111 global $CFG, $OUTPUT;
112 $mform = & $this->_form;
113 $mform->addElement('header', 'site', get_string('selecthub', 'hub'));
115 //retrieve the hub list on the hub directory by web service
116 $function = 'hubdirectory_get_hubs';
117 $params = array();
118 $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
119 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
120 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, 'publichubdirectory');
121 try {
122 $hubs = $xmlrpcclient->call($function, $params);
123 } catch (Exception $e) {
124 $error = $OUTPUT->notification(get_string('errorhublisting', 'hub', $e->getMessage()));
125 $mform->addElement('static', 'errorhub', '', $error);
126 $hubs = array();
129 //remove moodle.org from the hub list
130 foreach ($hubs as $key => $hub) {
131 if ($hub['url'] == HUB_MOODLEORGHUBURL) {
132 unset($hubs[$key]);
136 //Public hub list
137 $options = array();
138 foreach ($hubs as $hub) {
139 //to not display a name longer than 100 character (too big)
140 if (core_text::strlen($hub['name']) > 100) {
141 $hubname = core_text::substr($hub['name'], 0, 100);
142 $hubname = $hubname . "...";
143 } else {
144 $hubname = $hub['name'];
146 $options[$hub['url']] = $hubname;
147 $mform->addElement('hidden', clean_param($hub['url'], PARAM_ALPHANUMEXT), $hubname);
148 $mform->setType(clean_param($hub['url'], PARAM_ALPHANUMEXT), PARAM_ALPHANUMEXT);
150 if (!empty($hubs)) {
151 $mform->addElement('select', 'publichub', get_string('publichub', 'hub'),
152 $options, array("size" => 15));
153 $mform->setType('publichub', PARAM_URL);
156 $mform->addElement('static', 'or', '', get_string('orenterprivatehub', 'hub'));
158 //Private hub
159 $mform->addElement('text', 'unlistedurl', get_string('privatehuburl', 'hub'),
160 array('class' => 'registration_textfield'));
161 $mform->setType('unlistedurl', PARAM_URL);
162 $mform->addElement('text', 'password', get_string('password'),
163 array('class' => 'registration_textfield'));
164 $mform->setType('password', PARAM_RAW);
166 $this->add_action_buttons(false, get_string('selecthub', 'hub'));
170 * Check the unlisted URL is a URL
172 function validation($data, $files) {
173 global $CFG;
174 $errors = parent::validation($data, $files);
176 $unlistedurl = $this->_form->_submitValues['unlistedurl'];
178 if (empty($unlistedurl)) {
179 $errors['unlistedurl'] = get_string('badurlformat', 'hub');
182 return $errors;
188 * The site registration form. Information will be sent to a given hub.
190 class site_registration_form extends moodleform {
192 public function definition() {
193 global $CFG, $DB;
195 $strrequired = get_string('required');
196 $mform = & $this->_form;
197 $huburl = $this->_customdata['huburl'];
198 $hubname = $this->_customdata['hubname'];
199 $password = $this->_customdata['password'];
200 $admin = get_admin();
201 $site = get_site();
203 //retrieve config for this hub and set default if they don't exist
204 $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
205 $sitename = get_config('hub', 'site_name_' . $cleanhuburl);
206 if ($sitename === false) {
207 $sitename = format_string($site->fullname, true, array('context' => context_course::instance(SITEID)));
209 $sitedescription = get_config('hub', 'site_description_' . $cleanhuburl);
210 if ($sitedescription === false) {
211 $sitedescription = $site->summary;
213 $contactname = get_config('hub', 'site_contactname_' . $cleanhuburl);
214 if ($contactname === false) {
215 $contactname = fullname($admin, true);
217 $contactemail = get_config('hub', 'site_contactemail_' . $cleanhuburl);
218 if ($contactemail === false) {
219 $contactemail = $admin->email;
221 $contactphone = get_config('hub', 'site_contactphone_' . $cleanhuburl);
222 if ($contactphone === false) {
223 $contactphone = $admin->phone1;
225 $imageurl = get_config('hub', 'site_imageurl_' . $cleanhuburl);
226 $privacy = get_config('hub', 'site_privacy_' . $cleanhuburl);
227 $address = get_config('hub', 'site_address_' . $cleanhuburl);
228 $region = get_config('hub', 'site_region_' . $cleanhuburl);
229 $country = get_config('hub', 'site_country_' . $cleanhuburl);
230 if ($country === false) {
231 $country = $admin->country;
233 $language = get_config('hub', 'site_language_' . $cleanhuburl);
234 if ($language === false) {
235 $language = current_language();
237 $geolocation = get_config('hub', 'site_geolocation_' . $cleanhuburl);
238 $contactable = get_config('hub', 'site_contactable_' . $cleanhuburl);
239 $emailalert = get_config('hub', 'site_emailalert_' . $cleanhuburl);
240 $emailalert = ($emailalert === 0) ? 0 : 1;
241 $coursesnumber = get_config('hub', 'site_coursesnumber_' . $cleanhuburl);
242 $usersnumber = get_config('hub', 'site_usersnumber_' . $cleanhuburl);
243 $roleassignmentsnumber = get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl);
244 $postsnumber = get_config('hub', 'site_postsnumber_' . $cleanhuburl);
245 $questionsnumber = get_config('hub', 'site_questionsnumber_' . $cleanhuburl);
246 $resourcesnumber = get_config('hub', 'site_resourcesnumber_' . $cleanhuburl);
247 $badgesnumber = get_config('hub', 'site_badges_' . $cleanhuburl);
248 $issuedbadgesnumber = get_config('hub', 'site_issuedbadges_' . $cleanhuburl);
249 $mediancoursesize = get_config('hub', 'site_mediancoursesize_' . $cleanhuburl);
250 $participantnumberaveragecfg = get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl);
251 $modulenumberaveragecfg = get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl);
253 //hidden parameters
254 $mform->addElement('hidden', 'huburl', $huburl);
255 $mform->setType('huburl', PARAM_URL);
256 $mform->addElement('hidden', 'hubname', $hubname);
257 $mform->setType('hubname', PARAM_TEXT);
258 $mform->addElement('hidden', 'password', $password);
259 $mform->setType('password', PARAM_RAW);
261 //the input parameters
262 $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
264 $mform->addElement('text', 'name', get_string('sitename', 'hub'),
265 array('class' => 'registration_textfield'));
266 $mform->addRule('name', $strrequired, 'required', null, 'client');
267 $mform->setType('name', PARAM_TEXT);
268 $mform->setDefault('name', $sitename);
269 $mform->addHelpButton('name', 'sitename', 'hub');
271 $options = array();
272 $registrationmanager = new registration_manager();
273 $options[HUB_SITENOTPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENOTPUBLISHED);
274 $options[HUB_SITENAMEPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENAMEPUBLISHED);
275 $options[HUB_SITELINKPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITELINKPUBLISHED);
276 $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), $options);
277 $mform->setDefault('privacy', $privacy);
278 $mform->setType('privacy', PARAM_ALPHA);
279 $mform->addHelpButton('privacy', 'privacy', 'hub');
280 unset($options);
282 $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'),
283 array('rows' => 8, 'cols' => 41));
284 $mform->addRule('description', $strrequired, 'required', null, 'client');
285 $mform->setDefault('description', $sitedescription);
286 $mform->setType('description', PARAM_TEXT);
287 $mform->addHelpButton('description', 'sitedesc', 'hub');
289 $languages = get_string_manager()->get_list_of_languages();
290 core_collator::asort($languages);
291 $mform->addElement('select', 'language', get_string('sitelang', 'hub'),
292 $languages);
293 $mform->setType('language', PARAM_ALPHANUMEXT);
294 $mform->addHelpButton('language', 'sitelang', 'hub');
295 $mform->setDefault('language', $language);
297 $mform->addElement('textarea', 'address', get_string('postaladdress', 'hub'),
298 array('rows' => 4, 'cols' => 41));
299 $mform->setType('address', PARAM_TEXT);
300 $mform->setDefault('address', $address);
301 $mform->addHelpButton('address', 'postaladdress', 'hub');
303 //TODO: use the region array I generated
304 // $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-'));
305 // $mform->setDefault('region', $region);
306 $mform->addElement('hidden', 'regioncode', '-');
307 $mform->setType('regioncode', PARAM_ALPHANUMEXT);
309 $countries = get_string_manager()->get_list_of_countries();
310 $mform->addElement('select', 'countrycode', get_string('sitecountry', 'hub'), $countries);
311 $mform->setDefault('countrycode', $country);
312 $mform->setType('countrycode', PARAM_ALPHANUMEXT);
313 $mform->addHelpButton('countrycode', 'sitecountry', 'hub');
315 $mform->addElement('text', 'geolocation', get_string('sitegeolocation', 'hub'),
316 array('class' => 'registration_textfield'));
317 $mform->setDefault('geolocation', $geolocation);
318 $mform->setType('geolocation', PARAM_RAW);
319 $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub');
321 $mform->addElement('text', 'contactname', get_string('siteadmin', 'hub'),
322 array('class' => 'registration_textfield'));
323 $mform->addRule('contactname', $strrequired, 'required', null, 'client');
324 $mform->setType('contactname', PARAM_TEXT);
325 $mform->setDefault('contactname', $contactname);
326 $mform->addHelpButton('contactname', 'siteadmin', 'hub');
328 $mform->addElement('text', 'contactphone', get_string('sitephone', 'hub'),
329 array('class' => 'registration_textfield'));
330 $mform->setType('contactphone', PARAM_TEXT);
331 $mform->addHelpButton('contactphone', 'sitephone', 'hub');
332 $mform->setForceLtr('contactphone');
334 $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'),
335 array('class' => 'registration_textfield'));
336 $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
337 $mform->setType('contactemail', PARAM_EMAIL);
338 $mform->setDefault('contactemail', $contactemail);
339 $mform->addHelpButton('contactemail', 'siteemail', 'hub');
341 $options = array();
342 $options[0] = get_string("registrationcontactno");
343 $options[1] = get_string("registrationcontactyes");
344 $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options);
345 $mform->setDefault('contactable', $contactable);
346 $mform->setType('contactable', PARAM_INT);
347 $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub');
348 unset($options);
350 $options = array();
351 $options[0] = get_string("registrationno");
352 $options[1] = get_string("registrationyes");
353 $mform->addElement('select', 'emailalert', get_string('siteregistrationemail', 'hub'), $options);
354 $mform->setDefault('emailalert', $emailalert);
355 $mform->setType('emailalert', PARAM_INT);
356 $mform->addHelpButton('emailalert', 'siteregistrationemail', 'hub');
357 unset($options);
359 //TODO site logo
360 $mform->addElement('hidden', 'imageurl', ''); //TODO: temporary
361 $mform->setType('imageurl', PARAM_URL);
363 $mform->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $CFG->wwwroot);
364 $mform->addHelpButton('urlstring', 'siteurl', 'hub');
366 $mform->addElement('static', 'versionstring', get_string('siteversion', 'hub'), $CFG->version);
367 $mform->addElement('hidden', 'moodleversion', $CFG->version);
368 $mform->setType('moodleversion', PARAM_INT);
369 $mform->addHelpButton('versionstring', 'siteversion', 'hub');
371 $mform->addElement('static', 'releasestring', get_string('siterelease', 'hub'), $CFG->release);
372 $mform->addElement('hidden', 'moodlerelease', $CFG->release);
373 $mform->setType('moodlerelease', PARAM_TEXT);
374 $mform->addHelpButton('releasestring', 'siterelease', 'hub');
376 /// Display statistic that are going to be retrieve by the hub
377 $coursecount = $DB->count_records('course') - 1;
378 $usercount = $DB->count_records('user', array('deleted' => 0));
379 $roleassigncount = $DB->count_records('role_assignments');
380 $postcount = $DB->count_records('forum_posts');
381 $questioncount = $DB->count_records('question');
382 $resourcecount = $DB->count_records('resource');
383 require_once($CFG->dirroot . "/course/lib.php");
384 $participantnumberaverage = number_format(average_number_of_participants(), 2);
385 $modulenumberaverage = number_format(average_number_of_courses_modules(), 2);
386 require_once($CFG->libdir . '/badgeslib.php');
387 $badges = $DB->count_records_select('badge', 'status <> ' . BADGE_STATUS_ARCHIVED);
388 $issuedbadges = $DB->count_records('badge_issued');
390 if (HUB_MOODLEORGHUBURL != $huburl) {
391 $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'),
392 " " . get_string('coursesnumber', 'hub', $coursecount));
393 $mform->setDefault('courses', $coursesnumber != -1);
394 $mform->setType('courses', PARAM_INT);
395 $mform->addHelpButton('courses', 'sendfollowinginfo', 'hub');
397 $mform->addElement('checkbox', 'users', '',
398 " " . get_string('usersnumber', 'hub', $usercount));
399 $mform->setDefault('users', $usersnumber != -1);
400 $mform->setType('users', PARAM_INT);
402 $mform->addElement('checkbox', 'roleassignments', '',
403 " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
404 $mform->setDefault('roleassignments', $roleassignmentsnumber != -1);
405 $mform->setType('roleassignments', PARAM_INT);
407 $mform->addElement('checkbox', 'posts', '',
408 " " . get_string('postsnumber', 'hub', $postcount));
409 $mform->setDefault('posts', $postsnumber != -1);
410 $mform->setType('posts', PARAM_INT);
412 $mform->addElement('checkbox', 'questions', '',
413 " " . get_string('questionsnumber', 'hub', $questioncount));
414 $mform->setDefault('questions', $questionsnumber != -1);
415 $mform->setType('questions', PARAM_INT);
417 $mform->addElement('checkbox', 'resources', '',
418 " " . get_string('resourcesnumber', 'hub', $resourcecount));
419 $mform->setDefault('resources', $resourcesnumber != -1);
420 $mform->setType('resources', PARAM_INT);
422 $mform->addElement('checkbox', 'badges', '',
423 " " . get_string('badgesnumber', 'hub', $badges));
424 $mform->setDefault('badges', $badgesnumber != -1);
425 $mform->setType('resources', PARAM_INT);
427 $mform->addElement('checkbox', 'issuedbadges', '',
428 " " . get_string('issuedbadgesnumber', 'hub', $issuedbadges));
429 $mform->setDefault('issuedbadges', $issuedbadgesnumber != -1);
430 $mform->setType('resources', PARAM_INT);
432 $mform->addElement('checkbox', 'participantnumberaverage', '',
433 " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
434 $mform->setDefault('participantnumberaverage', $participantnumberaveragecfg != -1);
435 $mform->setType('participantnumberaverage', PARAM_FLOAT);
437 $mform->addElement('checkbox', 'modulenumberaverage', '',
438 " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
439 $mform->setDefault('modulenumberaverage', $modulenumberaveragecfg != -1);
440 $mform->setType('modulenumberaverage', PARAM_FLOAT);
441 } else {
442 $mform->addElement('static', 'courseslabel', get_string('sendfollowinginfo', 'hub'),
443 " " . get_string('coursesnumber', 'hub', $coursecount));
444 $mform->addElement('hidden', 'courses', 1);
445 $mform->setType('courses', PARAM_INT);
446 $mform->addHelpButton('courseslabel', 'sendfollowinginfo', 'hub');
448 $mform->addElement('static', 'userslabel', '',
449 " " . get_string('usersnumber', 'hub', $usercount));
450 $mform->addElement('hidden', 'users', 1);
451 $mform->setType('users', PARAM_INT);
453 $mform->addElement('static', 'roleassignmentslabel', '',
454 " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
455 $mform->addElement('hidden', 'roleassignments', 1);
456 $mform->setType('roleassignments', PARAM_INT);
458 $mform->addElement('static', 'postslabel', '',
459 " " . get_string('postsnumber', 'hub', $postcount));
460 $mform->addElement('hidden', 'posts', 1);
461 $mform->setType('posts', PARAM_INT);
463 $mform->addElement('static', 'questionslabel', '',
464 " " . get_string('questionsnumber', 'hub', $questioncount));
465 $mform->addElement('hidden', 'questions', 1);
466 $mform->setType('questions', PARAM_INT);
468 $mform->addElement('static', 'resourceslabel', '',
469 " " . get_string('resourcesnumber', 'hub', $resourcecount));
470 $mform->addElement('hidden', 'resources', 1);
471 $mform->setType('resources', PARAM_INT);
473 $mform->addElement('static', 'badgeslabel', '',
474 " " . get_string('badgesnumber', 'hub', $badges));
475 $mform->addElement('hidden', 'badges', 1);
476 $mform->setType('badges', PARAM_INT);
478 $mform->addElement('static', 'issuedbadgeslabel', '',
479 " " . get_string('issuedbadgesnumber', 'hub', $issuedbadges));
480 $mform->addElement('hidden', 'issuedbadges', true);
481 $mform->setType('issuedbadges', PARAM_INT);
483 $mform->addElement('static', 'participantnumberaveragelabel', '',
484 " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
485 $mform->addElement('hidden', 'participantnumberaverage', 1);
486 $mform->setType('participantnumberaverage', PARAM_FLOAT);
488 $mform->addElement('static', 'modulenumberaveragelabel', '',
489 " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
490 $mform->addElement('hidden', 'modulenumberaverage', 1);
491 $mform->setType('modulenumberaverage', PARAM_FLOAT);
494 //check if it's a first registration or update
495 $hubregistered = $registrationmanager->get_registeredhub($huburl);
497 if (!empty($hubregistered)) {
498 $buttonlabel = get_string('updatesite', 'hub',
499 !empty($hubname) ? $hubname : $huburl);
500 $mform->addElement('hidden', 'update', true);
501 $mform->setType('update', PARAM_BOOL);
502 } else {
503 $buttonlabel = get_string('registersite', 'hub',
504 !empty($hubname) ? $hubname : $huburl);
507 $this->add_action_buttons(false, $buttonlabel);