index.php 8.8 KB

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