Merge branch 'MDL-39463_master' of https://github.com/markn86/moodle
[moodle.git] / admin / registration / register.php
CommitLineData
07ab0c80 1<?php
7399bcef 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 *
b3245b75 30 * This page displays the site registration form for Moodle.org/MOOCH or for a different hub.
07ab0c80 31 * It handles redirection to the hub to continue the registration workflow process.
32 * It also handles update operation by web service.
7399bcef 33 */
07ab0c80 34
35
36require_once('../../config.php');
7399bcef 37require_once($CFG->libdir . '/adminlib.php');
a8e174cc 38require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/forms.php');
7399bcef 39require_once($CFG->dirroot . '/webservice/lib.php');
a8e174cc 40require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
07ab0c80 41
87142168 42$huburl = required_param('huburl', PARAM_URL);
43$huburl = rtrim($huburl, "/");
b3245b75
DP
44
45if ($huburl == HUB_MOODLEORGHUBURL) { // register to Moodle.org
46 admin_externalpage_setup('registrationmoodleorg');
47} else { //register to a hub
48 admin_externalpage_setup('registrationhub');
49}
50
4b1acb3a 51$password = optional_param('password', '', PARAM_TEXT);
07ab0c80 52$hubname = optional_param('hubname', '', PARAM_TEXT);
07ab0c80 53
94788de2 54$registrationmanager = new registration_manager();
07ab0c80 55
94788de2 56$registeredhub = $registrationmanager->get_registeredhub($huburl);
07ab0c80 57
58$siteregistrationform = new site_registration_form('',
7399bcef 59 array('alreadyregistered' => !empty($registeredhub->token),
60 'huburl' => $huburl, 'hubname' => $hubname,
61 'password' => $password));
07ab0c80 62$fromform = $siteregistrationform->get_data();
63
64if (!empty($fromform) and confirm_sesskey()) {
65 //save the settings
66 $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
7399bcef 67 set_config('site_name_' . $cleanhuburl, $fromform->name, 'hub');
68 set_config('site_description_' . $cleanhuburl, $fromform->description, 'hub');
69 set_config('site_contactname_' . $cleanhuburl, $fromform->contactname, 'hub');
70 set_config('site_contactemail_' . $cleanhuburl, $fromform->contactemail, 'hub');
71 set_config('site_contactphone_' . $cleanhuburl, $fromform->contactphone, 'hub');
72 set_config('site_imageurl_' . $cleanhuburl, $fromform->imageurl, 'hub');
73 set_config('site_privacy_' . $cleanhuburl, $fromform->privacy, 'hub');
74 set_config('site_address_' . $cleanhuburl, $fromform->address, 'hub');
75 set_config('site_region_' . $cleanhuburl, $fromform->regioncode, 'hub');
76 set_config('site_country_' . $cleanhuburl, $fromform->countrycode, 'hub');
daff57fb 77 set_config('site_language_' . $cleanhuburl, $fromform->language, 'hub');
7399bcef 78 set_config('site_geolocation_' . $cleanhuburl, $fromform->geolocation, 'hub');
79 set_config('site_contactable_' . $cleanhuburl, $fromform->contactable, 'hub');
80 set_config('site_emailalert_' . $cleanhuburl, $fromform->emailalert, 'hub');
81 set_config('site_coursesnumber_' . $cleanhuburl, $fromform->courses, 'hub');
82 set_config('site_usersnumber_' . $cleanhuburl, $fromform->users, 'hub');
83 set_config('site_roleassignmentsnumber_' . $cleanhuburl, $fromform->roleassignments, 'hub');
84 set_config('site_postsnumber_' . $cleanhuburl, $fromform->posts, 'hub');
85 set_config('site_questionsnumber_' . $cleanhuburl, $fromform->questions, 'hub');
86 set_config('site_resourcesnumber_' . $cleanhuburl, $fromform->resources, 'hub');
27806552
YB
87 set_config('site_badges_' . $cleanhuburl, $fromform->badges, 'hub');
88 set_config('site_issuedbadges_' . $cleanhuburl, $fromform->issuedbadges, 'hub');
7399bcef 89 set_config('site_modulenumberaverage_' . $cleanhuburl, $fromform->modulenumberaverage, 'hub');
90 set_config('site_participantnumberaverage_' . $cleanhuburl, $fromform->participantnumberaverage, 'hub');
07ab0c80 91}
92
07ab0c80 93/////// UPDATE ACTION ////////
b11837c9 94
07ab0c80 95// update the hub registration
7399bcef 96$update = optional_param('update', 0, PARAM_INT);
07ab0c80 97if ($update and confirm_sesskey()) {
98
99 //update the registration
100 $function = 'hub_update_site_info';
94788de2 101 $siteinfo = $registrationmanager->get_site_info($huburl);
2a4eec82 102 $params = array('siteinfo' => $siteinfo);
7399bcef 103 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
104 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
001251f2 105 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
7399bcef 106 try {
001251f2 107 $result = $xmlrpcclient->call($function, $params);
7399bcef 108 } catch (Exception $e) {
109 $error = $OUTPUT->notification(get_string('errorregistration', 'hub', $e->getMessage()));
110 }
07ab0c80 111}
112
113/////// FORM REGISTRATION ACTION //////
114
115if (!empty($fromform) and empty($update) and confirm_sesskey()) {
116
117 if (!empty($fromform) and confirm_sesskey()) { // if the register button has been clicked
118 $params = (array) $fromform; //we are using the form input as the redirection parameters (token, url and name)
119
94788de2 120 $unconfirmedhub = $registrationmanager->get_unconfirmedhub($huburl);
4b1acb3a 121 if (empty($unconfirmedhub)) {
07ab0c80 122 //we save the token into the communication table in order to have a reference
4b1acb3a 123 $unconfirmedhub = new stdClass();
c73f1963 124 $unconfirmedhub->token = $registrationmanager->get_site_secret_for_hub($huburl);
125 $unconfirmedhub->secret = $unconfirmedhub->token;
87142168 126 $unconfirmedhub->huburl = $huburl;
4b1acb3a 127 $unconfirmedhub->hubname = $hubname;
128 $unconfirmedhub->confirmed = 0;
94788de2 129 $unconfirmedhub->id = $registrationmanager->add_registeredhub($unconfirmedhub);
07ab0c80 130 }
131
4b1acb3a 132 $params['token'] = $unconfirmedhub->token;
07ab0c80 133 $params['url'] = $CFG->wwwroot;
034b502f 134 redirect(new moodle_url($huburl . '/local/hub/siteregistration.php', $params));
07ab0c80 135 }
136}
137
138/////// OUTPUT SECTION /////////////
139
140echo $OUTPUT->header();
141//Display update notification result
142if (!empty($registeredhub->confirmed)) {
143 if (!empty($result)) {
144 echo $OUTPUT->notification(get_string('siteregistrationupdated', 'hub'), 'notifysuccess');
145 }
146}
07ab0c80 147
7399bcef 148if (!empty($error)) {
149 echo $error;
150}
07ab0c80 151
b3245b75
DP
152//some Moodle.org resitration explanation
153if ($huburl == HUB_MOODLEORGHUBURL) {
154 echo $OUTPUT->heading(get_string('registerwithmoodleorg', 'admin'));
155 $renderer = $PAGE->get_renderer('core', 'register');
156 echo $renderer->moodleorg_registration_message();
157}
158
7399bcef 159$siteregistrationform->display();
15ac57cb 160echo $OUTPUT->footer();