Merge branch 'MD-31803' of git://github.com/mouneyrac/moodle
[moodle.git] / admin / registration / index.php
CommitLineData
07ab0c80 1<?php
f57c4047 2
07ab0c80 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/*
19 * @package moodle
20 * @subpackage registration
21 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
23 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
24 *
b3245b75
DP
25 * On this page the administrator selects which hub he wants to register,
26 * except for MOOCH. Admins can register with MOOCH with the top admin menu "Registration" link.
27 * On this page the administrator can also unregister from any hubs, including MOOCH.
f57c4047 28 */
07ab0c80 29
30require('../../config.php');
31
f57c4047 32require_once($CFG->libdir . '/adminlib.php');
a8e174cc 33require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
34require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/forms.php');
f57c4047 35require_once($CFG->dirroot . '/course/publish/lib.php');
36require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
07ab0c80 37
b3245b75 38admin_externalpage_setup('registrationhubs');
07ab0c80 39
40$renderer = $PAGE->get_renderer('core', 'register');
41
f57c4047 42$unregistration = optional_param('unregistration', 0, PARAM_INT);
43$cleanregdata = optional_param('cleanregdata', 0, PARAM_BOOL);
44$confirm = optional_param('confirm', 0, PARAM_INT);
45$huburl = optional_param('huburl', '', PARAM_URL);
46$cancel = optional_param('cancel', null, PARAM_ALPHA);
47
48$registrationmanager = new registration_manager();
49$publicationmanager = new course_publish_manager();
50$errormessage = '';
51if (empty($cancel) and $unregistration and $confirm and confirm_sesskey()) {
52
53 $hub = $registrationmanager->get_registeredhub($huburl);
54
55 //unpublish course and unregister the site by web service
56 if (!$cleanregdata) {
57
58 //check if we need to unpublish courses
59 //enrollable courses
60 $unpublishalladvertisedcourses = optional_param('unpublishalladvertisedcourses', 0, PARAM_INT);
61 $hubcourseids = array();
62 if ($unpublishalladvertisedcourses) {
63 $enrollablecourses = $publicationmanager->get_publications($huburl, null, 1);
64 if (!empty($enrollablecourses)) {
65 foreach ($enrollablecourses as $enrollablecourse) {
66 $hubcourseids[] = $enrollablecourse->hubcourseid;
67 }
68 }
69 }
70 //downloadable courses
71 $unpublishalluploadedcourses = optional_param('unpublishalluploadedcourses', 0, PARAM_INT);
72 if ($unpublishalluploadedcourses) {
73 $downloadablecourses = $publicationmanager->get_publications($huburl, null, 0);
74 if (!empty($downloadablecourses)) {
75 foreach ($downloadablecourses as $downloadablecourse) {
76 $hubcourseids[] = $downloadablecourse->hubcourseid;
77 }
78 }
79 }
80
81 //unpublish the courses by web service
82 if (!empty($hubcourseids)) {
83 $function = 'hub_unregister_courses';
2a4eec82 84 $params = array('courseids' => $hubcourseids);
f57c4047 85 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
001251f2 86 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
f57c4047 87 try {
001251f2 88 $result = $xmlrpcclient->call($function, $params);
f57c4047 89 //delete the published courses
90 if (!empty($enrollablecourses)) {
91 $publicationmanager->delete_hub_publications($huburl, 1);
92 }
93 if (!empty($downloadablecourses)) {
94 $publicationmanager->delete_hub_publications($huburl, 0);
95 }
96 } catch (Exception $e) {
97 $errormessage = $e->getMessage();
98 $errormessage .= html_writer::empty_tag('br') .
99 get_string('errorunpublishcourses', 'hub');
100 $confirm = false;
101 $cleanregdata = 1;
102 }
103 }
5bfbe04e 104 }
f57c4047 105
5bfbe04e 106 //course unpublish went ok, unregister the site now
107 if ($confirm) {
108 $function = 'hub_unregister_site';
109 $params = array();
110 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
001251f2 111 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
5bfbe04e 112 try {
001251f2 113 $result = $xmlrpcclient->call($function, $params);
5bfbe04e 114 } catch (Exception $e) {
115 if (!$cleanregdata) {
f57c4047 116 $errormessage = $e->getMessage();
117 $confirm = false;
118 $cleanregdata = 1;
119 }
120 }
121 }
122
123 //check that we are still processing the unregistration,
124 //it could have been unset if an exception were previsouly catched
125 if ($confirm) {
126 $registrationmanager->delete_registeredhub($huburl);
127 }
128}
129
f57c4047 130if (empty($cancel) and $unregistration and !$confirm) {
b3245b75
DP
131
132 echo $OUTPUT->header();
133
134 //do not check sesskey if confirm = false because this script is linked into email message
135 if (!empty($errormessage)) {
136 echo $OUTPUT->notification(get_string('unregistrationerror', 'hub', $errormessage));
137 }
138
f57c4047 139 $hub = $registrationmanager->get_registeredhub($huburl);
140 echo $OUTPUT->heading(get_string('unregisterfrom', 'hub', $hub->hubname), 3, 'main');
141 if ($cleanregdata) {
142 $siteunregistrationform = new site_clean_registration_data_form('',
143 array('huburl' => $huburl, 'hubname' => $hub->hubname));
144 } else {
145 $siteunregistrationform = new site_unregistration_form('',
146 array('huburl' => $huburl, 'hubname' => $hub->hubname));
147 }
b3245b75 148
f57c4047 149 $siteunregistrationform->display();
150} else {
29ca816c 151 $registeredonmoodleorg = false;
152 $moodleorghub = $registrationmanager->get_registeredhub(HUB_MOODLEORGHUBURL);
153 if (!empty($moodleorghub)) {
154 $registeredonmoodleorg = true;
6e9cd669 155 }
29ca816c 156
b3245b75
DP
157 // load the hub selector form
158 $hubselectorform = new hub_selector_form();
159 $fromform = $hubselectorform->get_data();
160 $selectedhuburl = optional_param('publichub', false, PARAM_URL);
161 $unlistedhuburl = optional_param('unlistedurl', false, PARAM_TEXT);
162 $password = optional_param('password', '', PARAM_RAW);
163
164 if (!empty($unlistedhuburl)) {
165 if (clean_param($unlistedhuburl, PARAM_URL) !== '') {
166 $huburl = $unlistedhuburl;
167 }
168 } else if (!empty($selectedhuburl)) {
169 $huburl = $selectedhuburl;
170 }
171
172 // a hub has been selected, redirect to the hub registration page
173 if (empty($cancel) and !empty($huburl) and confirm_sesskey()) {
174 $hubname = optional_param(clean_param($huburl, PARAM_ALPHANUMEXT), '', PARAM_TEXT);
175 $params = array('sesskey' => sesskey(), 'huburl' => $huburl,
176 'password' => $password, 'hubname' => $hubname);
177 redirect(new moodle_url($CFG->wwwroot . "/" . $CFG->admin . "/registration/register.php",
178 $params));
179 }
180
181 echo $OUTPUT->header();
182
183 //check if the site is registered on Moodle.org and display a message about registering on MOOCH
184 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
185 if (empty($registered)) {
186 $warningmsg = get_string('registermoochtips', 'hub');
187 $warningmsg .= $renderer->single_button(new moodle_url('register.php', array('huburl' => HUB_MOODLEORGHUBURL
188 , 'hubname' => 'Moodle.org')), get_string('register', 'admin'));
189 echo $renderer->box($warningmsg, 'buttons mdl-align generalbox adminwarning');
190 }
191
192 //do not check sesskey if confirm = false because this script is linked into email message
193 if (!empty($errormessage)) {
194 echo $OUTPUT->notification(get_string('unregistrationerror', 'hub', $errormessage));
195 }
196
197 echo $OUTPUT->heading(get_string('registerwith', 'hub'));
198
199 $hubselectorform->display();
5cbbcc1a 200
6e9cd669 201 if (extension_loaded('xmlrpc')) {
5cbbcc1a 202 $hubs = $registrationmanager->get_registered_on_hubs();
203 if (!empty($hubs)) {
204 echo $OUTPUT->heading(get_string('registeredon', 'hub'), 3, 'main');
205 echo $renderer->registeredonhublisting($hubs);
206 }
207 } else { //display notice about xmlrpc
0d5fae12 208 $xmlrpcnotification = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
5cbbcc1a 209 $xmlrpcnotification .= get_string('xmlrpcdisabledregistration', 'hub');
210 echo $OUTPUT->notification($xmlrpcnotification);
f57c4047 211 }
212}
03d9401e 213echo $OUTPUT->footer();