[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/registration/ -> lib.php (source)

   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              $function = 'hub_update_site_info';
  57              require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
  58  
  59              // Update all hubs where the site is registered.
  60              $hubs = $this->get_registered_on_hubs();
  61              if (empty($hubs)) {
  62                  mtrace(get_string('registrationwarning', 'admin'));
  63              }
  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                      $this->update_registeredhub($hub); // To update timemodified.
  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          } else {
  82              mtrace(get_string('errorcronnoxmlrpc', 'hub'));
  83          }
  84      }
  85  
  86      /**
  87       * Return the site secret for a given hub
  88       * site identifier is assigned to Mooch
  89       * each hub has a unique and personal site secret.
  90       * @param string $huburl
  91       * @return string site secret
  92       */
  93      public function get_site_secret_for_hub($huburl) {
  94          global $DB;
  95  
  96          $existingregistration = $DB->get_record('registration_hubs',
  97                      array('huburl' => $huburl));
  98  
  99          if (!empty($existingregistration)) {
 100              return $existingregistration->secret;
 101          }
 102  
 103          if ($huburl == HUB_MOODLEORGHUBURL) {
 104              $siteidentifier =  get_site_identifier();
 105          } else {
 106              $siteidentifier = random_string(32) . $_SERVER['HTTP_HOST'];
 107          }
 108  
 109          return $siteidentifier;
 110  
 111      }
 112  
 113      /**
 114       * When the site register on a hub, he must call this function
 115       * @param object $hub where the site is registered on
 116       * @return integer id of the record
 117       */
 118      public function add_registeredhub($hub) {
 119          global $DB;
 120          $hub->timemodified = time();
 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 $hub the hub
 175       */
 176      public function update_registeredhub($hub) {
 177          global $DB;
 178          $hub->timemodified = time();
 179          $DB->update_record('registration_hubs', $hub);
 180      }
 181  
 182      /**
 183       * Return all hubs where the site is registered
 184       */
 185      public function get_registered_on_hubs() {
 186          global $DB;
 187          $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
 188          return $hubs;
 189      }
 190  
 191      /**
 192       * Return site information for a specific hub
 193       * @param string $huburl
 194       * @return array site info
 195       */
 196      public function get_site_info($huburl) {
 197          global $CFG, $DB;
 198  
 199          $siteinfo = array();
 200          $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
 201          $siteinfo['name'] = get_config('hub', 'site_name_' . $cleanhuburl);
 202          $siteinfo['description'] = get_config('hub', 'site_description_' . $cleanhuburl);
 203          $siteinfo['contactname'] = get_config('hub', 'site_contactname_' . $cleanhuburl);
 204          $siteinfo['contactemail'] = get_config('hub', 'site_contactemail_' . $cleanhuburl);
 205          $siteinfo['contactphone'] = get_config('hub', 'site_contactphone_' . $cleanhuburl);
 206          $siteinfo['imageurl'] = get_config('hub', 'site_imageurl_' . $cleanhuburl);
 207          $siteinfo['privacy'] = get_config('hub', 'site_privacy_' . $cleanhuburl);
 208          $siteinfo['street'] = get_config('hub', 'site_address_' . $cleanhuburl);
 209          $siteinfo['regioncode'] = get_config('hub', 'site_region_' . $cleanhuburl);
 210          $siteinfo['countrycode'] = get_config('hub', 'site_country_' . $cleanhuburl);
 211          $siteinfo['geolocation'] = get_config('hub', 'site_geolocation_' . $cleanhuburl);
 212          $siteinfo['contactable'] = get_config('hub', 'site_contactable_' . $cleanhuburl);
 213          $siteinfo['emailalert'] = get_config('hub', 'site_emailalert_' . $cleanhuburl);
 214          if (get_config('hub', 'site_coursesnumber_' . $cleanhuburl) == -1) {
 215              $coursecount = -1;
 216          } else {
 217              $coursecount = $DB->count_records('course') - 1;
 218          }
 219          $siteinfo['courses'] = $coursecount;
 220          if (get_config('hub', 'site_usersnumber_' . $cleanhuburl) == -1) {
 221              $usercount = -1;
 222          } else {
 223              $usercount = $DB->count_records('user', array('deleted' => 0));
 224          }
 225          $siteinfo['users'] = $usercount;
 226  
 227          if (get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl) == -1) {
 228              $roleassigncount = -1;
 229          } else {
 230              $roleassigncount = $DB->count_records('role_assignments');
 231          }
 232          $siteinfo['enrolments'] = $roleassigncount;
 233          if (get_config('hub', 'site_postsnumber_' . $cleanhuburl) == -1) {
 234              $postcount = -1;
 235          } else {
 236              $postcount = $DB->count_records('forum_posts');
 237          }
 238          $siteinfo['posts'] = $postcount;
 239          if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) {
 240              $questioncount = -1;
 241          } else {
 242              $questioncount = $DB->count_records('question');
 243          }
 244          $siteinfo['questions'] = $questioncount;
 245          if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) {
 246              $resourcecount = -1;
 247          } else {
 248              $resourcecount = $DB->count_records('resource');
 249          }
 250          $siteinfo['resources'] = $resourcecount;
 251          // Badge statistics.
 252          require_once($CFG->libdir . '/badgeslib.php');
 253          if (get_config('hub', 'site_badges_' . $cleanhuburl) == -1) {
 254              $badges = -1;
 255          } else {
 256              $badges = $DB->count_records_select('badge', 'status <> ' . BADGE_STATUS_ARCHIVED);
 257          }
 258          $siteinfo['badges'] = $badges;
 259          if (get_config('hub', 'site_issuedbadges_' . $cleanhuburl) == -1) {
 260              $issuedbadges = -1;
 261          } else {
 262              $issuedbadges = $DB->count_records('badge_issued');
 263          }
 264          $siteinfo['issuedbadges'] = $issuedbadges;
 265          //TODO
 266          require_once($CFG->dirroot . "/course/lib.php");
 267          if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) {
 268              $participantnumberaverage = -1;
 269          } else {
 270              $participantnumberaverage = average_number_of_participants();
 271          }
 272          $siteinfo['participantnumberaverage'] = $participantnumberaverage;
 273          if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) {
 274              $modulenumberaverage = -1;
 275          } else {
 276              $modulenumberaverage = average_number_of_courses_modules();
 277          }
 278          $siteinfo['modulenumberaverage'] = $modulenumberaverage;
 279          $siteinfo['language'] = get_config('hub', 'site_language_' . $cleanhuburl);
 280          $siteinfo['moodleversion'] = $CFG->version;
 281          $siteinfo['moodlerelease'] = $CFG->release;
 282          $siteinfo['url'] = $CFG->wwwroot;
 283  
 284          return $siteinfo;
 285      }
 286  
 287      /**
 288       * Retrieve the site privacy string matching the define value
 289       * @param string $privacy must match the define into moodlelib.php
 290       * @return string
 291       */
 292      public function get_site_privacy_string($privacy) {
 293          switch ($privacy) {
 294              case HUB_SITENOTPUBLISHED:
 295                  $privacystring = get_string('siteprivacynotpublished', 'hub');
 296                  break;
 297              case HUB_SITENAMEPUBLISHED:
 298                  $privacystring = get_string('siteprivacypublished', 'hub');
 299                  break;
 300              case HUB_SITELINKPUBLISHED:
 301                  $privacystring = get_string('siteprivacylinked', 'hub');
 302                  break;
 303          }
 304          if (empty($privacystring)) {
 305              throw new moodle_exception('unknownprivacy');
 306          }
 307          return $privacystring;
 308      }
 309  
 310  }
 311  ?>


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1