3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 //// SITE PRIVACY /////
24 * Site privacy: private
26 define('HUB_SITENOTPUBLISHED', 'notdisplayed');
29 * Site privacy: public
31 define('HUB_SITENAMEPUBLISHED', 'named');
34 * Site privacy: public and global
36 define('HUB_SITELINKPUBLISHED', 'linked');
40 * Site registration library
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
48 class registration_manager
{
51 * Automatically update the registration on all hubs
53 public function cron() {
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");
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
);
67 $params = array('siteinfo' => $siteinfo);
68 $serverurl = $hub->huburl
. "/local/hub/webservice/webservices.php";
69 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token
);
71 $result = $xmlrpcclient->call($function, $params);
72 mtrace(get_string('siteupdatedcron', 'hub', $hub->hubname
));
73 } catch (Exception
$e) {
74 $errorparam = new stdClass();
75 $errorparam->errormessage
= $e->getMessage();
76 $errorparam->hubname
= $hub->hubname
;
77 mtrace(get_string('errorcron', 'hub', $errorparam));
80 set_config('crontime', time(), 'registration');
83 mtrace(get_string('errorcronnoxmlrpc', 'hub'));
88 * Return the site secret for a given hub
89 * site identifier is assigned to Mooch
90 * each hub has a unique and personal site secret.
91 * @param string $huburl
92 * @return string site secret
94 public function get_site_secret_for_hub($huburl) {
97 $existingregistration = $DB->get_record('registration_hubs',
98 array('huburl' => $huburl));
100 if (!empty($existingregistration)) {
101 return $existingregistration->secret
;
104 if ($huburl == HUB_MOODLEORGHUBURL
) {
105 $siteidentifier = get_site_identifier();
107 $siteidentifier = random_string(32) . $_SERVER['HTTP_HOST'];
110 return $siteidentifier;
115 * When the site register on a hub, he must call this function
116 * @param object $hub where the site is registered on
117 * @return integer id of the record
119 public function add_registeredhub($hub) {
121 $id = $DB->insert_record('registration_hubs', $hub);
126 * When a site unregister from a hub, he must call this function
127 * @param string $huburl the huburl to delete
129 public function delete_registeredhub($huburl) {
131 $DB->delete_records('registration_hubs', array('huburl' => $huburl));
135 * Get a hub on which the site is registered for a given url or token
136 * Mostly use to check if the site is registered on a specific hub
137 * @param string $huburl
138 * @param string $token
139 * @return object the hub
141 public function get_registeredhub($huburl = null, $token = null) {
145 if (!empty($huburl)) {
146 $params['huburl'] = $huburl;
148 if (!empty($token)) {
149 $params['token'] = $token;
151 $params['confirmed'] = 1;
152 $token = $DB->get_record('registration_hubs', $params);
157 * Get the hub which has not confirmed that the site is registered on,
158 * but for which a request has been sent
159 * @param string $huburl
160 * @return object the hub
162 public function get_unconfirmedhub($huburl) {
166 $params['huburl'] = $huburl;
167 $params['confirmed'] = 0;
168 $token = $DB->get_record('registration_hubs', $params);
173 * Update a registered hub (mostly use to update the confirmation status)
174 * @param object $communication the hub
176 public function update_registeredhub($communication) {
178 $DB->update_record('registration_hubs', $communication);
182 * Return all hubs where the site is registered on
184 public function get_registered_on_hubs() {
186 $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
191 * Return site information for a specific hub
192 * @param string $huburl
193 * @return array site info
195 public function get_site_info($huburl) {
199 $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT
);
200 $siteinfo['name'] = get_config('hub', 'site_name_' . $cleanhuburl);
201 $siteinfo['description'] = get_config('hub', 'site_description_' . $cleanhuburl);
202 $siteinfo['contactname'] = get_config('hub', 'site_contactname_' . $cleanhuburl);
203 $siteinfo['contactemail'] = get_config('hub', 'site_contactemail_' . $cleanhuburl);
204 $siteinfo['contactphone'] = get_config('hub', 'site_contactphone_' . $cleanhuburl);
205 $siteinfo['imageurl'] = get_config('hub', 'site_imageurl_' . $cleanhuburl);
206 $siteinfo['privacy'] = get_config('hub', 'site_privacy_' . $cleanhuburl);
207 $siteinfo['street'] = get_config('hub', 'site_address_' . $cleanhuburl);
208 $siteinfo['regioncode'] = get_config('hub', 'site_region_' . $cleanhuburl);
209 $siteinfo['countrycode'] = get_config('hub', 'site_country_' . $cleanhuburl);
210 $siteinfo['geolocation'] = get_config('hub', 'site_geolocation_' . $cleanhuburl);
211 $siteinfo['contactable'] = get_config('hub', 'site_contactable_' . $cleanhuburl);
212 $siteinfo['emailalert'] = get_config('hub', 'site_emailalert_' . $cleanhuburl);
213 if (get_config('hub', 'site_coursesnumber_' . $cleanhuburl) == -1) {
216 $coursecount = $DB->count_records('course') - 1;
218 $siteinfo['courses'] = $coursecount;
219 if (get_config('hub', 'site_usersnumber_' . $cleanhuburl) == -1) {
222 $usercount = $DB->count_records('user', array('deleted' => 0));
224 $siteinfo['users'] = $usercount;
226 if (get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl) == -1) {
227 $roleassigncount = -1;
229 $roleassigncount = $DB->count_records('role_assignments');
231 $siteinfo['enrolments'] = $roleassigncount;
232 if (get_config('hub', 'site_postsnumber_' . $cleanhuburl) == -1) {
235 $postcount = $DB->count_records('forum_posts');
237 $siteinfo['posts'] = $postcount;
238 if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) {
241 $questioncount = $DB->count_records('question');
243 $siteinfo['questions'] = $questioncount;
244 if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) {
247 $resourcecount = $DB->count_records('resource');
249 $siteinfo['resources'] = $resourcecount;
251 require_once($CFG->libdir
. '/badgeslib.php');
252 if (get_config('hub', 'site_badges_' . $cleanhuburl) == -1) {
255 $badges = $DB->count_records_select('badge', 'status <> ' . BADGE_STATUS_ARCHIVED
);
257 $siteinfo['badges'] = $badges;
258 if (get_config('hub', 'site_issuedbadges_' . $cleanhuburl) == -1) {
261 $issuedbadges = $DB->count_records('badge_issued');
263 $siteinfo['issuedbadges'] = $issuedbadges;
265 require_once($CFG->dirroot
. "/course/lib.php");
266 if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) {
267 $participantnumberaverage = -1;
269 $participantnumberaverage = average_number_of_participants();
271 $siteinfo['participantnumberaverage'] = $participantnumberaverage;
272 if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) {
273 $modulenumberaverage = -1;
275 $modulenumberaverage = average_number_of_courses_modules();
277 $siteinfo['modulenumberaverage'] = $modulenumberaverage;
278 $siteinfo['language'] = get_config('hub', 'site_language_' . $cleanhuburl);
279 $siteinfo['moodleversion'] = $CFG->version
;
280 $siteinfo['moodlerelease'] = $CFG->release
;
281 $siteinfo['url'] = $CFG->wwwroot
;
287 * Retrieve the site privacy string matching the define value
288 * @param string $privacy must match the define into moodlelib.php
291 public function get_site_privacy_string($privacy) {
293 case HUB_SITENOTPUBLISHED
:
294 $privacystring = get_string('siteprivacynotpublished', 'hub');
296 case HUB_SITENAMEPUBLISHED
:
297 $privacystring = get_string('siteprivacypublished', 'hub');
299 case HUB_SITELINKPUBLISHED
:
300 $privacystring = get_string('siteprivacylinked', 'hub');
303 if (empty($privacystring)) {
304 throw new moodle_exception('unknownprivacy');
306 return $privacystring;