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