course publication MDL-19315 display better error message if web service communicatio...
[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');
94788de2 34require_once($CFG->dirroot.'/admin/registration/lib.php');
07ab0c80 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
139978e5 175 $mform->addElement('text','name' , get_string('sitename', 'hub'),
176 array('class' => 'registration_textfield'));
07ab0c80 177 $mform->addRule('name', $strrequired, 'required', null, 'client');
178 $mform->setType('name', PARAM_TEXT);
179 $mform->setDefault('name', $sitename);
139978e5 180 $mform->addHelpButton('name', 'sitename', 'hub');
07ab0c80 181
182 $options = array();
94788de2 183 $registrationmanager = new registration_manager();
184 $options[HUB_SITENOTPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENOTPUBLISHED);
185 $options[HUB_SITENAMEPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENAMEPUBLISHED);
186 $options[HUB_SITELINKPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITELINKPUBLISHED);
07ab0c80 187 $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), $options);
188 $mform->setDefault('privacy', $privacy);
4032415e 189 $mform->addHelpButton('privacy', 'privacy', 'hub');
07ab0c80 190 unset($options);
191
139978e5 192 $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'),
193 array('rows'=>8, 'cols'=>41));
07ab0c80 194 $mform->addRule('description', $strrequired, 'required', null, 'client');
195 $mform->setDefault('description', $sitedescription);
196 $mform->setType('description', PARAM_TEXT);
139978e5 197 $mform->addHelpButton('description', 'sitedesc', 'hub');
07ab0c80 198
199 $mform->addElement('static', 'urlstring',get_string('siteurl', 'hub'), $CFG->wwwroot);
139978e5 200 $mform->addHelpButton('urlstring', 'siteurl', 'hub');
07ab0c80 201
202 $languages = get_string_manager()->get_list_of_languages();
139978e5 203 $mform->addElement('static', 'langstring',get_string('sitelang', 'hub'), $languages[current_language()]);
07ab0c80 204 $mform->addElement('hidden', 'language', current_language());
139978e5 205 $mform->addHelpButton('langstring', 'sitelang', 'hub');
07ab0c80 206
139978e5 207 $mform->addElement('static', 'versionstring',get_string('siteversion', 'hub'), $CFG->version);
07ab0c80 208 $mform->addElement('hidden', 'moodleversion', $CFG->version);
139978e5 209 $mform->addHelpButton('versionstring', 'siteversion', 'hub');
07ab0c80 210
139978e5 211 $mform->addElement('static', 'releasestring',get_string('siterelease', 'hub'), $CFG->release);
07ab0c80 212 $mform->addElement('hidden', 'moodlerelease', $CFG->release);
139978e5 213 $mform->addHelpButton('releasestring', 'siterelease', 'hub');
07ab0c80 214
139978e5 215 $mform->addElement('textarea','address' , get_string('postaladdress', 'hub'),
216 array('rows'=>4, 'cols'=>41));
07ab0c80 217 $mform->setType('address', PARAM_TEXT);
218 $mform->setDefault('address', $address);
139978e5 219 $mform->addHelpButton('address', 'postaladdress', 'hub');
07ab0c80 220
221 //TODO: use the region array I generated
4032415e 222// $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-'));
223// $mform->setDefault('region', $region);
224 $mform->addElement('hidden', 'regioncode', '-');
07ab0c80 225
226 $countries = get_string_manager()->get_list_of_countries();
139978e5 227 $mform->addElement('select', 'countrycode', get_string('sitecountry', 'hub'), $countries);
4032415e 228 $mform->setDefault('countrycode', $country);
139978e5 229 $mform->addHelpButton('countrycode', 'sitecountry', 'hub');
07ab0c80 230
139978e5 231 $mform->addElement('text','geolocation' , get_string('sitegeolocation', 'hub'),
232 array('class' => 'registration_textfield'));
07ab0c80 233 $mform->setDefault('geolocation', $geolocation);
139978e5 234 $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub');
07ab0c80 235
139978e5 236 $mform->addElement('text', 'contactname', get_string('siteadmin', 'hub'),
237 array('class' => 'registration_textfield'));
07ab0c80 238 $mform->addRule('contactname', $strrequired, 'required', null, 'client');
239 $mform->setType('contactname', PARAM_TEXT);
240 $mform->setDefault('contactname', $contactname);
139978e5 241 $mform->addHelpButton('contactname', 'siteadmin', 'hub');
07ab0c80 242
139978e5 243 $mform->addElement('text','contactphone' , get_string('sitephone', 'hub'),
244 array('class' => 'registration_textfield'));
07ab0c80 245 $mform->setType('contactphone', PARAM_TEXT);
139978e5 246 $mform->addHelpButton('contactphone', 'sitephone', 'hub');
07ab0c80 247
139978e5 248 $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'),
249 array('class' => 'registration_textfield'));
07ab0c80 250 $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
251 $mform->setType('contactemail', PARAM_TEXT);
252 $mform->setDefault('contactemail', $contactemail);
139978e5 253 $mform->addHelpButton('contactemail', 'siteemail', 'hub');
07ab0c80 254
255 $options = array();
256 $options[0] = get_string("registrationcontactno");
257 $options[1] = get_string("registrationcontactyes");
139978e5 258 $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options);
07ab0c80 259 $mform->setDefault('contactable', $contactable);
139978e5 260 $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub');
07ab0c80 261 unset($options);
262
263 $options = array();
264 $options[0] = get_string("registrationno");
265 $options[1] = get_string("registrationyes");
139978e5 266 $mform->addElement('select', 'emailalert', get_string('siteregistrationemail', 'hub'), $options);
07ab0c80 267 $mform->setDefault('emailalert', $emailalert);
139978e5 268 $mform->addHelpButton('emailalert', 'siteregistrationemail', 'hub');
07ab0c80 269 unset($options);
270
139978e5 271 //TODO site logo
e275f25e 272 $mform->addElement('hidden', 'imageurl', ''); //TODO: temporary
07ab0c80 273
274 /// Display statistic that are going to be retrieve by the hub
275 $coursecount = $DB->count_records('course')-1;
276 $usercount = $DB->count_records('user', array('deleted'=>0));
277 $roleassigncount = $DB->count_records('role_assignments');
278 $postcount = $DB->count_records('forum_posts');
279 $questioncount = $DB->count_records('question');
280 $resourcecount = $DB->count_records('resource');
281 require_once($CFG->dirroot."/course/lib.php");
282 $participantnumberaverage= average_number_of_participants();
283 $modulenumberaverage= average_number_of_courses_modules();
284
06d4578c 285 if (HUB_MOODLEORGHUBURL != $huburl) {
07ab0c80 286 $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'),
287 " ".get_string('coursesnumber', 'hub', $coursecount));
288 $mform->setDefault('courses', true);
139978e5 289 $mform->addHelpButton('courses', 'sendfollowinginfo', 'hub');
07ab0c80 290
291 $mform->addElement('checkbox', 'users', '',
292 " ".get_string('usersnumber', 'hub', $usercount));
293 $mform->setDefault('users', true);
294
295 $mform->addElement('checkbox', 'roleassignments', '',
296 " ".get_string('roleassignmentsnumber', 'hub', $roleassigncount));
297 $mform->setDefault('roleassignments', true);
298
299 $mform->addElement('checkbox', 'posts', '',
300 " ".get_string('postsnumber', 'hub', $postcount));
301 $mform->setDefault('posts', true);
302
303 $mform->addElement('checkbox', 'questions', '',
304 " ".get_string('questionsnumber', 'hub', $questioncount));
305 $mform->setDefault('questions', true);
306
307 $mform->addElement('checkbox', 'resources', '',
308 " ".get_string('resourcesnumber', 'hub', $resourcecount));
309 $mform->setDefault('resources', true);
310
311 $mform->addElement('checkbox', 'participantnumberaverage', '',
312 " ".get_string('participantnumberaverage', 'hub', $participantnumberaverage));
313 $mform->setDefault('participantnumberaverage', true);
314
315 $mform->addElement('checkbox', 'modulenumberaverage', '',
316 " ".get_string('modulenumberaverage', 'hub', $modulenumberaverage));
317 $mform->setDefault('modulenumberaverage', true);
318 } else {
319 $mform->addElement('static', 'courseslabel',get_string('sendfollowinginfo', 'hub'),
320 " ".get_string('coursesnumber', 'hub', $coursecount));
321 $mform->addElement('hidden', 'courses', true);
139978e5 322 $mform->addHelpButton('courseslabel', 'sendfollowinginfo', 'hub');
07ab0c80 323
324 $mform->addElement('static', 'userslabel', '',
325 " ".get_string('usersnumber', 'hub', $usercount));
326 $mform->addElement('hidden', 'users', true);
327
328 $mform->addElement('static', 'roleassignmentslabel', '',
329 " ".get_string('roleassignmentsnumber', 'hub', $roleassigncount));
330 $mform->addElement('hidden', 'roleassignments', true);
331
332 $mform->addElement('static', 'postslabel', '',
333 " ".get_string('postsnumber', 'hub', $postcount));
334 $mform->addElement('hidden', 'posts', true);
335
336 $mform->addElement('static', 'questionslabel', '',
337 " ".get_string('questionsnumber', 'hub', $questioncount));
338 $mform->addElement('hidden', 'questions', true);
339
340 $mform->addElement('static', 'resourceslabel', '',
341 " ".get_string('resourcesnumber', 'hub', $resourcecount));
342 $mform->addElement('hidden', 'resources', true);
343
344 $mform->addElement('static', 'participantnumberaveragelabel', '',
345 " ".get_string('participantnumberaverage', 'hub', $participantnumberaverage));
346 $mform->addElement('hidden', 'participantnumberaverage', true);
347
348 $mform->addElement('static', 'modulenumberaveragelabel', '',
349 " ".get_string('modulenumberaverage', 'hub', $modulenumberaverage));
350 $mform->addElement('hidden', 'modulenumberaverage', true);
351 }
352
353 //check if it's a first registration or update
94788de2 354 $hubregistered = $registrationmanager->get_registeredhub($huburl);
07ab0c80 355
356 if (!empty($hubregistered)) {
357 $buttonlabel = get_string('updatesite', 'hub',
358 !empty($hubname)?$hubname:$huburl);
359 $mform->addElement('hidden', 'update', true);
360 } else {
361 $buttonlabel = get_string('registersite', 'hub',
362 !empty($hubname)?$hubname:$huburl);
363 }
364
365 $this->add_action_buttons(false, $buttonlabel);
366 }
367
07ab0c80 368}
369
370?>