Merge branch 'install_22_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_22...
[moodle.git] / admin / registration / lib.php
blob4179d7908c65abc89e1a70607fa95f06bdd8df1c
1 <?php
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.
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 /////
23 /**
24 * Site privacy: private
26 define('HUB_SITENOTPUBLISHED', 'notdisplayed');
28 /**
29 * Site privacy: public
31 define('HUB_SITENAMEPUBLISHED', 'named');
33 /**
34 * Site privacy: public and global
36 define('HUB_SITELINKPUBLISHED', 'linked');
38 /**
40 * Site registration library
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
48 class registration_manager {
50 /**
51 * Automatically update the registration on all hubs
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");
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);
70 try {
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');
82 } else {
83 mtrace(get_string('errorcronnoxmlrpc', 'hub'));
87 /**
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) {
95 global $DB;
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();
106 } else {
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) {
120 global $DB;
121 $id = $DB->insert_record('registration_hubs', $hub);
122 return $id;
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) {
130 global $DB;
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) {
142 global $DB;
144 $params = array();
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);
153 return $token;
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) {
163 global $DB;
165 $params = array();
166 $params['huburl'] = $huburl;
167 $params['confirmed'] = 0;
168 $token = $DB->get_record('registration_hubs', $params);
169 return $token;
173 * Update a registered hub (mostly use to update the confirmation status)
174 * @param object $communication the hub
176 public function update_registeredhub($communication) {
177 global $DB;
178 $DB->update_record('registration_hubs', $communication);
182 * Return all hubs where the site is registered on
184 public function get_registered_on_hubs() {
185 global $DB;
186 $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
187 return $hubs;
191 * Return site information for a specific hub
192 * @param string $huburl
193 * @return array site info
195 public function get_site_info($huburl) {
196 global $CFG, $DB;
198 $siteinfo = array();
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) {
214 $coursecount = -1;
215 } else {
216 $coursecount = $DB->count_records('course') - 1;
218 $siteinfo['courses'] = $coursecount;
219 if (get_config('hub', 'site_usersnumber_' . $cleanhuburl) == -1) {
220 $usercount = -1;
221 } else {
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;
228 } else {
229 $roleassigncount = $DB->count_records('role_assignments');
231 $siteinfo['enrolments'] = $roleassigncount;
232 if (get_config('hub', 'site_postsnumber_' . $cleanhuburl) == -1) {
233 $postcount = -1;
234 } else {
235 $postcount = $DB->count_records('forum_posts');
237 $siteinfo['posts'] = $postcount;
238 if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) {
239 $questioncount = -1;
240 } else {
241 $questioncount = $DB->count_records('question');
243 $siteinfo['questions'] = $questioncount;
244 if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) {
245 $resourcecount = -1;
246 } else {
247 $resourcecount = $DB->count_records('resource');
249 $siteinfo['resources'] = $resourcecount;
250 //TODO
251 require_once($CFG->dirroot . "/course/lib.php");
252 if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) {
253 $participantnumberaverage = -1;
254 } else {
255 $participantnumberaverage = average_number_of_participants();
257 $siteinfo['participantnumberaverage'] = $participantnumberaverage;
258 if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) {
259 $modulenumberaverage = -1;
260 } else {
261 $modulenumberaverage = average_number_of_courses_modules();
263 $siteinfo['modulenumberaverage'] = $modulenumberaverage;
264 $siteinfo['language'] = get_config('hub', 'site_language_' . $cleanhuburl);
265 $siteinfo['moodleversion'] = $CFG->version;
266 $siteinfo['moodlerelease'] = $CFG->release;
267 $siteinfo['url'] = $CFG->wwwroot;
269 return $siteinfo;
273 * Retrieve the site privacy string matching the define value
274 * @param string $privacy must match the define into moodlelib.php
275 * @return string
277 public function get_site_privacy_string($privacy) {
278 switch ($privacy) {
279 case HUB_SITENOTPUBLISHED:
280 $privacystring = get_string('siteprivacynotpublished', 'hub');
281 break;
282 case HUB_SITENAMEPUBLISHED:
283 $privacystring = get_string('siteprivacypublished', 'hub');
284 break;
285 case HUB_SITELINKPUBLISHED:
286 $privacystring = get_string('siteprivacylinked', 'hub');
287 break;
289 if (empty($privacystring)) {
290 throw new moodle_exception('unknownprivacy');
292 return $privacystring;