[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 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 20 21 //// SITE PRIVACY ///// 22 23 /** 24 * Site privacy: private 25 */ 26 define('HUB_SITENOTPUBLISHED', 'notdisplayed'); 27 28 /** 29 * Site privacy: public 30 */ 31 define('HUB_SITENAMEPUBLISHED', 'named'); 32 33 /** 34 * Site privacy: public and global 35 */ 36 define('HUB_SITELINKPUBLISHED', 'linked'); 37 38 /** 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 47 */ 48 class registration_manager { 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); 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)); 78 } 79 } 80 set_config('crontime', time(), 'registration'); 81 } 82 } else { 83 mtrace(get_string('errorcronnoxmlrpc', 'hub')); 84 } 85 } 86 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 93 */ 94 public function get_site_secret_for_hub($huburl) { 95 global $DB; 96 97 $existingregistration = $DB->get_record('registration_hubs', 98 array('huburl' => $huburl)); 99 100 if (!empty($existingregistration)) { 101 return $existingregistration->secret; 102 } 103 104 if ($huburl == HUB_MOODLEORGHUBURL) { 105 $siteidentifier = get_site_identifier(); 106 } else { 107 $siteidentifier = random_string(32) . $_SERVER['HTTP_HOST']; 108 } 109 110 return $siteidentifier; 111 112 } 113 114 /** 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 118 */ 119 public function add_registeredhub($hub) { 120 global $DB; 121 $id = $DB->insert_record('registration_hubs', $hub); 122 return $id; 123 } 124 125 /** 126 * When a site unregister from a hub, he must call this function 127 * @param string $huburl the huburl to delete 128 */ 129 public function delete_registeredhub($huburl) { 130 global $DB; 131 $DB->delete_records('registration_hubs', array('huburl' => $huburl)); 132 } 133 134 /** 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 140 */ 141 public function get_registeredhub($huburl = null, $token = null) { 142 global $DB; 143 144 $params = array(); 145 if (!empty($huburl)) { 146 $params['huburl'] = $huburl; 147 } 148 if (!empty($token)) { 149 $params['token'] = $token; 150 } 151 $params['confirmed'] = 1; 152 $token = $DB->get_record('registration_hubs', $params); 153 return $token; 154 } 155 156 /** 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 161 */ 162 public function get_unconfirmedhub($huburl) { 163 global $DB; 164 165 $params = array(); 166 $params['huburl'] = $huburl; 167 $params['confirmed'] = 0; 168 $token = $DB->get_record('registration_hubs', $params); 169 return $token; 170 } 171 172 /** 173 * Update a registered hub (mostly use to update the confirmation status) 174 * @param object $communication the hub 175 */ 176 public function update_registeredhub($communication) { 177 global $DB; 178 $DB->update_record('registration_hubs', $communication); 179 } 180 181 /** 182 * Return all hubs where the site is registered on 183 */ 184 public function get_registered_on_hubs() { 185 global $DB; 186 $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1)); 187 return $hubs; 188 } 189 190 /** 191 * Return site information for a specific hub 192 * @param string $huburl 193 * @return array site info 194 */ 195 public function get_site_info($huburl) { 196 global $CFG, $DB; 197 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; 217 } 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)); 223 } 224 $siteinfo['users'] = $usercount; 225 226 if (get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl) == -1) { 227 $roleassigncount = -1; 228 } else { 229 $roleassigncount = $DB->count_records('role_assignments'); 230 } 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'); 236 } 237 $siteinfo['posts'] = $postcount; 238 if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) { 239 $questioncount = -1; 240 } else { 241 $questioncount = $DB->count_records('question'); 242 } 243 $siteinfo['questions'] = $questioncount; 244 if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) { 245 $resourcecount = -1; 246 } else { 247 $resourcecount = $DB->count_records('resource'); 248 } 249 $siteinfo['resources'] = $resourcecount; 250 // Badge statistics. 251 require_once($CFG->libdir . '/badgeslib.php'); 252 if (get_config('hub', 'site_badges_' . $cleanhuburl) == -1) { 253 $badges = -1; 254 } else { 255 $badges = $DB->count_records_select('badge', 'status <> ' . BADGE_STATUS_ARCHIVED); 256 } 257 $siteinfo['badges'] = $badges; 258 if (get_config('hub', 'site_issuedbadges_' . $cleanhuburl) == -1) { 259 $issuedbadges = -1; 260 } else { 261 $issuedbadges = $DB->count_records('badge_issued'); 262 } 263 $siteinfo['issuedbadges'] = $issuedbadges; 264 //TODO 265 require_once($CFG->dirroot . "/course/lib.php"); 266 if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) { 267 $participantnumberaverage = -1; 268 } else { 269 $participantnumberaverage = average_number_of_participants(); 270 } 271 $siteinfo['participantnumberaverage'] = $participantnumberaverage; 272 if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) { 273 $modulenumberaverage = -1; 274 } else { 275 $modulenumberaverage = average_number_of_courses_modules(); 276 } 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; 282 283 return $siteinfo; 284 } 285 286 /** 287 * Retrieve the site privacy string matching the define value 288 * @param string $privacy must match the define into moodlelib.php 289 * @return string 290 */ 291 public function get_site_privacy_string($privacy) { 292 switch ($privacy) { 293 case HUB_SITENOTPUBLISHED: 294 $privacystring = get_string('siteprivacynotpublished', 'hub'); 295 break; 296 case HUB_SITENAMEPUBLISHED: 297 $privacystring = get_string('siteprivacypublished', 'hub'); 298 break; 299 case HUB_SITELINKPUBLISHED: 300 $privacystring = get_string('siteprivacylinked', 'hub'); 301 break; 302 } 303 if (empty($privacystring)) { 304 throw new moodle_exception('unknownprivacy'); 305 } 306 return $privacystring; 307 } 308 309 } 310 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |