eac6400cb5390933cc19916ef702d979f7d69f8b
[moodle.git] / admin / registration / register.php
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 ///////////////////////////////////////////////////////////////////////////
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  * This page displays the site registration form.
31  * It handles redirection to the hub to continue the registration workflow process.
32  * It also handles update operation by web service.
33  */
36 require_once('../../config.php');
37 require_once($CFG->libdir . '/adminlib.php');
38 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/forms.php');
39 require_once($CFG->dirroot . '/webservice/lib.php');
40 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
42 admin_externalpage_setup('registrationindex');
44 $huburl = required_param('huburl', PARAM_URL);
45 $huburl = rtrim($huburl, "/");
46 $password = optional_param('password', '', PARAM_TEXT);
47 $hubname = optional_param('hubname', '', PARAM_TEXT);
48 if (!confirm_sesskey()) {
49     throw new moodle_exception('missingparameter');
50 }
52 $registrationmanager = new registration_manager();
54 $registeredhub = $registrationmanager->get_registeredhub($huburl);
56 $siteregistrationform = new site_registration_form('',
57                 array('alreadyregistered' => !empty($registeredhub->token),
58                     'huburl' => $huburl, 'hubname' => $hubname,
59                     'password' => $password));
60 $fromform = $siteregistrationform->get_data();
62 if (!empty($fromform) and confirm_sesskey()) {
63     //save the settings
64     $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
65     set_config('site_name_' . $cleanhuburl, $fromform->name, 'hub');
66     set_config('site_description_' . $cleanhuburl, $fromform->description, 'hub');
67     set_config('site_contactname_' . $cleanhuburl, $fromform->contactname, 'hub');
68     set_config('site_contactemail_' . $cleanhuburl, $fromform->contactemail, 'hub');
69     set_config('site_contactphone_' . $cleanhuburl, $fromform->contactphone, 'hub');
70     set_config('site_imageurl_' . $cleanhuburl, $fromform->imageurl, 'hub');
71     set_config('site_privacy_' . $cleanhuburl, $fromform->privacy, 'hub');
72     set_config('site_address_' . $cleanhuburl, $fromform->address, 'hub');
73     set_config('site_region_' . $cleanhuburl, $fromform->regioncode, 'hub');
74     set_config('site_country_' . $cleanhuburl, $fromform->countrycode, 'hub');
75     set_config('site_language_' . $cleanhuburl, $fromform->language, 'hub');
76     set_config('site_geolocation_' . $cleanhuburl, $fromform->geolocation, 'hub');
77     set_config('site_contactable_' . $cleanhuburl, $fromform->contactable, 'hub');
78     set_config('site_emailalert_' . $cleanhuburl, $fromform->emailalert, 'hub');
79     set_config('site_coursesnumber_' . $cleanhuburl, $fromform->courses, 'hub');
80     set_config('site_usersnumber_' . $cleanhuburl, $fromform->users, 'hub');
81     set_config('site_roleassignmentsnumber_' . $cleanhuburl, $fromform->roleassignments, 'hub');
82     set_config('site_postsnumber_' . $cleanhuburl, $fromform->posts, 'hub');
83     set_config('site_questionsnumber_' . $cleanhuburl, $fromform->questions, 'hub');
84     set_config('site_resourcesnumber_' . $cleanhuburl, $fromform->resources, 'hub');
85     set_config('site_modulenumberaverage_' . $cleanhuburl, $fromform->modulenumberaverage, 'hub');
86     set_config('site_participantnumberaverage_' . $cleanhuburl, $fromform->participantnumberaverage, 'hub');
87 }
89 /////// UPDATE ACTION ////////
91 // update the hub registration
92 $update = optional_param('update', 0, PARAM_INT);
93 if ($update and confirm_sesskey()) {
95     //update the registration
96     $function = 'hub_update_site_info';
97     $siteinfo = $registrationmanager->get_site_info($huburl);
98     $params = array('siteinfo' => $siteinfo);
99     $serverurl = $huburl . "/local/hub/webservice/webservices.php";
100     require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
101     $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
102     try {
103         $result = $xmlrpcclient->call($function, $params);
104     } catch (Exception $e) {
105         $error = $OUTPUT->notification(get_string('errorregistration', 'hub', $e->getMessage()));
106     }
109 /////// FORM REGISTRATION ACTION //////
111 if (!empty($fromform) and empty($update) and confirm_sesskey()) {
113     if (!empty($fromform) and confirm_sesskey()) { // if the register button has been clicked
114         $params = (array) $fromform; //we are using the form input as the redirection parameters (token, url and name)
116         $unconfirmedhub = $registrationmanager->get_unconfirmedhub($huburl);
117         if (empty($unconfirmedhub)) {
118             //we save the token into the communication table in order to have a reference
119             $unconfirmedhub = new stdClass();
120             $unconfirmedhub->token = $registrationmanager->get_site_secret_for_hub($huburl);
121             $unconfirmedhub->secret = $unconfirmedhub->token;
122             $unconfirmedhub->huburl = $huburl;
123             $unconfirmedhub->hubname = $hubname;
124             $unconfirmedhub->confirmed = 0;
125             $unconfirmedhub->id = $registrationmanager->add_registeredhub($unconfirmedhub);
126         }
128         $params['token'] = $unconfirmedhub->token;
129         $params['url'] = $CFG->wwwroot;
130         redirect(new moodle_url($huburl . '/local/hub/siteregistration.php', $params));
131     }
134 /////// OUTPUT SECTION /////////////
136 echo $OUTPUT->header();
137 //Display update notification result
138 if (!empty($registeredhub->confirmed)) {
139     if (!empty($result)) {
140         echo $OUTPUT->notification(get_string('siteregistrationupdated', 'hub'), 'notifysuccess');
141     }
144 if (!empty($error)) {
145     echo $error;
148 $siteregistrationform->display();
149 echo $OUTPUT->footer();