Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/registration/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00019 
00020 
00022 
00026 define('HUB_SITENOTPUBLISHED', 'notdisplayed');
00027 
00031 define('HUB_SITENAMEPUBLISHED', 'named');
00032 
00036 define('HUB_SITELINKPUBLISHED', 'linked');
00037 
00048 class registration_manager {
00049 
00053     public function cron() {
00054         global $CFG;
00055         if (extension_loaded('xmlrpc')) {
00056             //check if the last registration cron update was less than a week ago
00057             $lastcron = get_config('registration', 'crontime');
00058             if ($lastcron === false or $lastcron < strtotime("-7 day")) { //set to a week, see MDL-23704
00059                 $function = 'hub_update_site_info';
00060                 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
00061 
00062                 //update all hub where the site is registered on
00063                 $hubs = $this->get_registered_on_hubs();
00064                 foreach ($hubs as $hub) {
00065                     //update the registration
00066                     $siteinfo = $this->get_site_info($hub->huburl);
00067                     $params = array('siteinfo' => $siteinfo);
00068                     $serverurl = $hub->huburl . "/local/hub/webservice/webservices.php";
00069                     $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
00070                     try {
00071                         $result = $xmlrpcclient->call($function, $params);
00072                         mtrace(get_string('siteupdatedcron', 'hub', $hub->hubname));
00073                     } catch (Exception $e) {
00074                         $errorparam = new stdClass();
00075                         $errorparam->errormessage = $e->getMessage();
00076                         $errorparam->hubname = $hub->hubname;
00077                         mtrace(get_string('errorcron', 'hub', $errorparam));
00078                     }
00079                 }
00080                 set_config('crontime', time(), 'registration');
00081             }
00082         } else {
00083             mtrace(get_string('errorcronnoxmlrpc', 'hub'));
00084         }
00085     }
00086 
00094     public function get_site_secret_for_hub($huburl) {
00095         global $DB;
00096 
00097         $existingregistration = $DB->get_record('registration_hubs',
00098                     array('huburl' => $huburl));
00099 
00100         if (!empty($existingregistration)) {
00101             return $existingregistration->secret;
00102         }
00103 
00104         if ($huburl == HUB_MOODLEORGHUBURL) {
00105             $siteidentifier =  get_site_identifier();
00106         } else {
00107             $siteidentifier = random_string(32) . $_SERVER['HTTP_HOST'];
00108         }
00109 
00110         return $siteidentifier;
00111 
00112     }
00113 
00119     public function add_registeredhub($hub) {
00120         global $DB;
00121         $id = $DB->insert_record('registration_hubs', $hub);
00122         return $id;
00123     }
00124 
00129     public function delete_registeredhub($huburl) {
00130         global $DB;
00131         $DB->delete_records('registration_hubs', array('huburl' => $huburl));
00132     }
00133 
00141     public function get_registeredhub($huburl = null, $token = null) {
00142         global $DB;
00143 
00144         $params = array();
00145         if (!empty($huburl)) {
00146             $params['huburl'] = $huburl;
00147         }
00148         if (!empty($token)) {
00149             $params['token'] = $token;
00150         }
00151         $params['confirmed'] = 1;
00152         $token = $DB->get_record('registration_hubs', $params);
00153         return $token;
00154     }
00155 
00162     public function get_unconfirmedhub($huburl) {
00163         global $DB;
00164 
00165         $params = array();
00166         $params['huburl'] = $huburl;
00167         $params['confirmed'] = 0;
00168         $token = $DB->get_record('registration_hubs', $params);
00169         return $token;
00170     }
00171 
00176     public function update_registeredhub($communication) {
00177         global $DB;
00178         $DB->update_record('registration_hubs', $communication);
00179     }
00180 
00184     public function get_registered_on_hubs() {
00185         global $DB;
00186         $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
00187         return $hubs;
00188     }
00189 
00195     public function get_site_info($huburl) {
00196         global $CFG, $DB;
00197 
00198         $siteinfo = array();
00199         $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
00200         $siteinfo['name'] = get_config('hub', 'site_name_' . $cleanhuburl);
00201         $siteinfo['description'] = get_config('hub', 'site_description_' . $cleanhuburl);
00202         $siteinfo['contactname'] = get_config('hub', 'site_contactname_' . $cleanhuburl);
00203         $siteinfo['contactemail'] = get_config('hub', 'site_contactemail_' . $cleanhuburl);
00204         $siteinfo['contactphone'] = get_config('hub', 'site_contactphone_' . $cleanhuburl);
00205         $siteinfo['imageurl'] = get_config('hub', 'site_imageurl_' . $cleanhuburl);
00206         $siteinfo['privacy'] = get_config('hub', 'site_privacy_' . $cleanhuburl);
00207         $siteinfo['street'] = get_config('hub', 'site_address_' . $cleanhuburl);
00208         $siteinfo['regioncode'] = get_config('hub', 'site_region_' . $cleanhuburl);
00209         $siteinfo['countrycode'] = get_config('hub', 'site_country_' . $cleanhuburl);
00210         $siteinfo['geolocation'] = get_config('hub', 'site_geolocation_' . $cleanhuburl);
00211         $siteinfo['contactable'] = get_config('hub', 'site_contactable_' . $cleanhuburl);
00212         $siteinfo['emailalert'] = get_config('hub', 'site_emailalert_' . $cleanhuburl);
00213         if (get_config('hub', 'site_coursesnumber_' . $cleanhuburl) == -1) {
00214             $coursecount = -1;
00215         } else {
00216             $coursecount = $DB->count_records('course') - 1;
00217         }
00218         $siteinfo['courses'] = $coursecount;
00219         if (get_config('hub', 'site_usersnumber_' . $cleanhuburl) == -1) {
00220             $usercount = -1;
00221         } else {
00222             $usercount = $DB->count_records('user', array('deleted' => 0));
00223         }
00224         $siteinfo['users'] = $usercount;
00225 
00226         if (get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl) == -1) {
00227             $roleassigncount = -1;
00228         } else {
00229             $roleassigncount = $DB->count_records('role_assignments');
00230         }
00231         $siteinfo['enrolments'] = $roleassigncount;
00232         if (get_config('hub', 'site_postsnumber_' . $cleanhuburl) == -1) {
00233             $postcount = -1;
00234         } else {
00235             $postcount = $DB->count_records('forum_posts');
00236         }
00237         $siteinfo['posts'] = $postcount;
00238         if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) {
00239             $questioncount = -1;
00240         } else {
00241             $questioncount = $DB->count_records('question');
00242         }
00243         $siteinfo['questions'] = $questioncount;
00244         if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) {
00245             $resourcecount = -1;
00246         } else {
00247             $resourcecount = $DB->count_records('resource');
00248         }
00249         $siteinfo['resources'] = $resourcecount;
00250         //TODO
00251         require_once($CFG->dirroot . "/course/lib.php");
00252         if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) {
00253             $participantnumberaverage = -1;
00254         } else {
00255             $participantnumberaverage = average_number_of_participants();
00256         }
00257         $siteinfo['participantnumberaverage'] = $participantnumberaverage;
00258         if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) {
00259             $modulenumberaverage = -1;
00260         } else {
00261             $modulenumberaverage = average_number_of_courses_modules();
00262         }
00263         $siteinfo['modulenumberaverage'] = $modulenumberaverage;
00264         $siteinfo['language'] = get_config('hub', 'site_language_' . $cleanhuburl);
00265         $siteinfo['moodleversion'] = $CFG->version;
00266         $siteinfo['moodlerelease'] = $CFG->release;
00267         $siteinfo['url'] = $CFG->wwwroot;
00268 
00269         return $siteinfo;
00270     }
00271 
00277     public function get_site_privacy_string($privacy) {
00278         switch ($privacy) {
00279             case HUB_SITENOTPUBLISHED:
00280                 $privacystring = get_string('siteprivacynotpublished', 'hub');
00281                 break;
00282             case HUB_SITENAMEPUBLISHED:
00283                 $privacystring = get_string('siteprivacypublished', 'hub');
00284                 break;
00285             case HUB_SITELINKPUBLISHED:
00286                 $privacystring = get_string('siteprivacylinked', 'hub');
00287                 break;
00288         }
00289         if (empty($privacystring)) {
00290             throw new moodle_exception('unknownprivacy');
00291         }
00292         return $privacystring;
00293     }
00294 
00295 }
00296 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations