MDL-48894 core_registration: make it more obvious if site is registered
[moodle.git] / admin / registration / lib.php
CommitLineData
07ab0c80 1<?php
2ed993a6 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
07ab0c80 18
07ab0c80 19
20
07ab0c80 21//// SITE PRIVACY /////
22
23/**
24 * Site privacy: private
25 */
06d4578c 26define('HUB_SITENOTPUBLISHED', 'notdisplayed');
07ab0c80 27
28/**
29 * Site privacy: public
30 */
06d4578c 31define('HUB_SITENAMEPUBLISHED', 'named');
07ab0c80 32
33/**
34 * Site privacy: public and global
35 */
06d4578c 36define('HUB_SITELINKPUBLISHED', 'linked');
07ab0c80 37
07ab0c80 38/**
94788de2 39 *
40 * Site registration library
41 *
42 * @package course
43 * @copyright 2010 Moodle Pty Ltd (http://moodle.com)
44 * @author Jerome Mouneyrac
45 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
07ab0c80 47 */
94788de2 48class registration_manager {
2ed993a6 49
50 /**
51 * Automatically update the registration on all hubs
52 */
53 public function cron() {
54 global $CFG;
55 if (extension_loaded('xmlrpc')) {
56 //check if the last registration cron update was less than a week ago
57 $lastcron = get_config('registration', 'crontime');
58 if ($lastcron === false or $lastcron < strtotime("-7 day")) { //set to a week, see MDL-23704
59 $function = 'hub_update_site_info';
60 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
61
62 //update all hub where the site is registered on
63 $hubs = $this->get_registered_on_hubs();
64 foreach ($hubs as $hub) {
65 //update the registration
66 $siteinfo = $this->get_site_info($hub->huburl);
2a4eec82 67 $params = array('siteinfo' => $siteinfo);
2ed993a6 68 $serverurl = $hub->huburl . "/local/hub/webservice/webservices.php";
69 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
70 try {
71 $result = $xmlrpcclient->call($function, $params);
806c06fc 72 $this->update_registeredhub($hub); // To update timemodified.
2ed993a6 73 mtrace(get_string('siteupdatedcron', 'hub', $hub->hubname));
74 } catch (Exception $e) {
75 $errorparam = new stdClass();
76 $errorparam->errormessage = $e->getMessage();
77 $errorparam->hubname = $hub->hubname;
78 mtrace(get_string('errorcron', 'hub', $errorparam));
79 }
80 }
81 set_config('crontime', time(), 'registration');
82 }
83 } else {
84 mtrace(get_string('errorcronnoxmlrpc', 'hub'));
85 }
86 }
87
c73f1963 88 /**
89 * Return the site secret for a given hub
90 * site identifier is assigned to Mooch
91 * each hub has a unique and personal site secret.
92 * @param string $huburl
93 * @return string site secret
94 */
95 public function get_site_secret_for_hub($huburl) {
96 global $DB;
97
98 $existingregistration = $DB->get_record('registration_hubs',
99 array('huburl' => $huburl));
100
101 if (!empty($existingregistration)) {
102 return $existingregistration->secret;
103 }
104
105 if ($huburl == HUB_MOODLEORGHUBURL) {
106 $siteidentifier = get_site_identifier();
107 } else {
108 $siteidentifier = random_string(32) . $_SERVER['HTTP_HOST'];
109 }
110
111 return $siteidentifier;
112
113 }
114
94788de2 115 /**
116 * When the site register on a hub, he must call this function
117 * @param object $hub where the site is registered on
118 * @return integer id of the record
119 */
07ab0c80 120 public function add_registeredhub($hub) {
121 global $DB;
806c06fc 122 $hub->timemodified = time();
07ab0c80 123 $id = $DB->insert_record('registration_hubs', $hub);
124 return $id;
125 }
126
f57c4047 127 /**
128 * When a site unregister from a hub, he must call this function
129 * @param string $huburl the huburl to delete
130 */
2ed993a6 131 public function delete_registeredhub($huburl) {
f57c4047 132 global $DB;
133 $DB->delete_records('registration_hubs', array('huburl' => $huburl));
134 }
135
94788de2 136 /**
137 * Get a hub on which the site is registered for a given url or token
138 * Mostly use to check if the site is registered on a specific hub
139 * @param string $huburl
140 * @param string $token
141 * @return object the hub
142 */
07ab0c80 143 public function get_registeredhub($huburl = null, $token = null) {
144 global $DB;
145
146 $params = array();
147 if (!empty($huburl)) {
148 $params['huburl'] = $huburl;
149 }
150 if (!empty($token)) {
151 $params['token'] = $token;
152 }
4b1acb3a 153 $params['confirmed'] = 1;
2ed993a6 154 $token = $DB->get_record('registration_hubs', $params);
4b1acb3a 155 return $token;
156 }
157
94788de2 158 /**
2ed993a6 159 * Get the hub which has not confirmed that the site is registered on,
160 * but for which a request has been sent
94788de2 161 * @param string $huburl
162 * @return object the hub
163 */
4b1acb3a 164 public function get_unconfirmedhub($huburl) {
165 global $DB;
166
167 $params = array();
168 $params['huburl'] = $huburl;
169 $params['confirmed'] = 0;
2ed993a6 170 $token = $DB->get_record('registration_hubs', $params);
07ab0c80 171 return $token;
172 }
173
94788de2 174 /**
175 * Update a registered hub (mostly use to update the confirmation status)
806c06fc 176 * @param object $hub the hub
94788de2 177 */
806c06fc 178 public function update_registeredhub($hub) {
07ab0c80 179 global $DB;
806c06fc
AD
180 $hub->timemodified = time();
181 $DB->update_record('registration_hubs', $hub);
07ab0c80 182 }
183
94788de2 184 /**
806c06fc 185 * Return all hubs where the site is registered
94788de2 186 */
187 public function get_registered_on_hubs() {
4676eaf7 188 global $DB;
f57c4047 189 $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
94788de2 190 return $hubs;
4676eaf7 191 }
178097a0 192
07ab0c80 193 /**
194 * Return site information for a specific hub
195 * @param string $huburl
196 * @return array site info
197 */
198 public function get_site_info($huburl) {
199 global $CFG, $DB;
200
201 $siteinfo = array();
202 $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
2ed993a6 203 $siteinfo['name'] = get_config('hub', 'site_name_' . $cleanhuburl);
204 $siteinfo['description'] = get_config('hub', 'site_description_' . $cleanhuburl);
205 $siteinfo['contactname'] = get_config('hub', 'site_contactname_' . $cleanhuburl);
206 $siteinfo['contactemail'] = get_config('hub', 'site_contactemail_' . $cleanhuburl);
207 $siteinfo['contactphone'] = get_config('hub', 'site_contactphone_' . $cleanhuburl);
208 $siteinfo['imageurl'] = get_config('hub', 'site_imageurl_' . $cleanhuburl);
209 $siteinfo['privacy'] = get_config('hub', 'site_privacy_' . $cleanhuburl);
210 $siteinfo['street'] = get_config('hub', 'site_address_' . $cleanhuburl);
211 $siteinfo['regioncode'] = get_config('hub', 'site_region_' . $cleanhuburl);
212 $siteinfo['countrycode'] = get_config('hub', 'site_country_' . $cleanhuburl);
213 $siteinfo['geolocation'] = get_config('hub', 'site_geolocation_' . $cleanhuburl);
214 $siteinfo['contactable'] = get_config('hub', 'site_contactable_' . $cleanhuburl);
215 $siteinfo['emailalert'] = get_config('hub', 'site_emailalert_' . $cleanhuburl);
216 if (get_config('hub', 'site_coursesnumber_' . $cleanhuburl) == -1) {
07ab0c80 217 $coursecount = -1;
218 } else {
2ed993a6 219 $coursecount = $DB->count_records('course') - 1;
07ab0c80 220 }
221 $siteinfo['courses'] = $coursecount;
2ed993a6 222 if (get_config('hub', 'site_usersnumber_' . $cleanhuburl) == -1) {
07ab0c80 223 $usercount = -1;
224 } else {
2ed993a6 225 $usercount = $DB->count_records('user', array('deleted' => 0));
07ab0c80 226 }
227 $siteinfo['users'] = $usercount;
228
2ed993a6 229 if (get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl) == -1) {
07ab0c80 230 $roleassigncount = -1;
231 } else {
232 $roleassigncount = $DB->count_records('role_assignments');
233 }
234 $siteinfo['enrolments'] = $roleassigncount;
2ed993a6 235 if (get_config('hub', 'site_postsnumber_' . $cleanhuburl) == -1) {
07ab0c80 236 $postcount = -1;
237 } else {
238 $postcount = $DB->count_records('forum_posts');
239 }
240 $siteinfo['posts'] = $postcount;
2ed993a6 241 if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) {
07ab0c80 242 $questioncount = -1;
243 } else {
244 $questioncount = $DB->count_records('question');
245 }
246 $siteinfo['questions'] = $questioncount;
2ed993a6 247 if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) {
07ab0c80 248 $resourcecount = -1;
249 } else {
250 $resourcecount = $DB->count_records('resource');
251 }
252 $siteinfo['resources'] = $resourcecount;
27806552
YB
253 // Badge statistics.
254 require_once($CFG->libdir . '/badgeslib.php');
255 if (get_config('hub', 'site_badges_' . $cleanhuburl) == -1) {
256 $badges = -1;
257 } else {
258 $badges = $DB->count_records_select('badge', 'status <> ' . BADGE_STATUS_ARCHIVED);
259 }
260 $siteinfo['badges'] = $badges;
261 if (get_config('hub', 'site_issuedbadges_' . $cleanhuburl) == -1) {
262 $issuedbadges = -1;
263 } else {
264 $issuedbadges = $DB->count_records('badge_issued');
265 }
266 $siteinfo['issuedbadges'] = $issuedbadges;
07ab0c80 267 //TODO
2ed993a6 268 require_once($CFG->dirroot . "/course/lib.php");
269 if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) {
07ab0c80 270 $participantnumberaverage = -1;
271 } else {
272 $participantnumberaverage = average_number_of_participants();
273 }
274 $siteinfo['participantnumberaverage'] = $participantnumberaverage;
2ed993a6 275 if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) {
07ab0c80 276 $modulenumberaverage = -1;
277 } else {
278 $modulenumberaverage = average_number_of_courses_modules();
279 }
280 $siteinfo['modulenumberaverage'] = $modulenumberaverage;
daff57fb 281 $siteinfo['language'] = get_config('hub', 'site_language_' . $cleanhuburl);
07ab0c80 282 $siteinfo['moodleversion'] = $CFG->version;
283 $siteinfo['moodlerelease'] = $CFG->release;
284 $siteinfo['url'] = $CFG->wwwroot;
285
286 return $siteinfo;
287 }
288
07ab0c80 289 /**
290 * Retrieve the site privacy string matching the define value
291 * @param string $privacy must match the define into moodlelib.php
292 * @return string
293 */
294 public function get_site_privacy_string($privacy) {
295 switch ($privacy) {
06d4578c 296 case HUB_SITENOTPUBLISHED:
297 $privacystring = get_string('siteprivacynotpublished', 'hub');
07ab0c80 298 break;
06d4578c 299 case HUB_SITENAMEPUBLISHED:
300 $privacystring = get_string('siteprivacypublished', 'hub');
07ab0c80 301 break;
06d4578c 302 case HUB_SITELINKPUBLISHED:
303 $privacystring = get_string('siteprivacylinked', 'hub');
07ab0c80 304 break;
305 }
306 if (empty($privacystring)) {
307 throw new moodle_exception('unknownprivacy');
308 }
309 return $privacystring;
310 }
94788de2 311
2ed993a6 312}
94788de2 313?>