[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/log/store/database/classes/log/ -> store.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * External database store.
  19   *
  20   * @package    logstore_database
  21   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace logstore_database\log;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  class store implements \tool_log\log\writer, \core\log\sql_select_reader {
  29      use \tool_log\helper\store,
  30          \tool_log\helper\reader,
  31          \tool_log\helper\buffered_writer {
  32          dispose as helper_dispose;
  33      }
  34  
  35      /** @var \moodle_database $extdb */
  36      protected $extdb;
  37  
  38      /** @var bool $logguests true if logging guest access */
  39      protected $logguests;
  40  
  41      /** @var array $includelevels An array of education levels to include */
  42      protected $includelevels = array();
  43  
  44      /** @var array $includeactions An array of actions types to include */
  45      protected $includeactions = array();
  46  
  47      /**
  48       * Construct
  49       *
  50       * @param \tool_log\log\manager $manager
  51       */
  52      public function __construct(\tool_log\log\manager $manager) {
  53          $this->helper_setup($manager);
  54          $this->buffersize = $this->get_config('buffersize', 50);
  55          $this->logguests = $this->get_config('logguests', 1);
  56          $actions = $this->get_config('includeactions', '');
  57          $levels = $this->get_config('includelevels', '');
  58          $this->includeactions = $actions === '' ? array() : explode(',', $actions);
  59          $this->includelevels = $levels === '' ? array() : explode(',', $levels);
  60      }
  61  
  62      /**
  63       * Setup the Database.
  64       *
  65       * @return bool
  66       */
  67      protected function init() {
  68          if (isset($this->extdb)) {
  69              return !empty($this->extdb);
  70          }
  71  
  72          $dbdriver = $this->get_config('dbdriver');
  73          if (empty($dbdriver)) {
  74              $this->extdb = false;
  75              return false;
  76          }
  77          list($dblibrary, $dbtype) = explode('/', $dbdriver);
  78  
  79          if (!$db = \moodle_database::get_driver_instance($dbtype, $dblibrary, true)) {
  80              debugging("Unknown driver $dblibrary/$dbtype", DEBUG_DEVELOPER);
  81              $this->extdb = false;
  82              return false;
  83          }
  84  
  85          $dboptions = array();
  86          $dboptions['dbpersist'] = $this->get_config('dbpersist', '0');
  87          $dboptions['dbsocket'] = $this->get_config('dbsocket', '');
  88          $dboptions['dbport'] = $this->get_config('dbport', '');
  89          $dboptions['dbschema'] = $this->get_config('dbschema', '');
  90          $dboptions['dbcollation'] = $this->get_config('dbcollation', '');
  91          try {
  92              $db->connect($this->get_config('dbhost'), $this->get_config('dbuser'), $this->get_config('dbpass'),
  93                  $this->get_config('dbname'), false, $dboptions);
  94              $tables = $db->get_tables();
  95              if (!in_array($this->get_config('dbtable'), $tables)) {
  96                  debugging('Cannot find the specified table', DEBUG_DEVELOPER);
  97                  $this->extdb = false;
  98                  return false;
  99              }
 100          } catch (\moodle_exception $e) {
 101              debugging('Cannot connect to external database: ' . $e->getMessage(), DEBUG_DEVELOPER);
 102              $this->extdb = false;
 103              return false;
 104          }
 105  
 106          $this->extdb = $db;
 107          return true;
 108      }
 109  
 110      /**
 111       * Should the event be ignored (== not logged)?
 112       * @param \core\event\base $event
 113       * @return bool
 114       */
 115      protected function is_event_ignored(\core\event\base $event) {
 116          if (!in_array($event->crud, $this->includeactions) &&
 117              !in_array($event->edulevel, $this->includelevels)
 118          ) {
 119              // Ignore event if the store settings do not want to store it.
 120              return true;
 121          }
 122          if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) {
 123              // Always log inside CLI scripts because we do not login there.
 124              if (!isloggedin() or isguestuser()) {
 125                  return true;
 126              }
 127          }
 128          return false;
 129      }
 130  
 131      /**
 132       * Insert events in bulk to the database.
 133       *
 134       * @param array $evententries raw event data
 135       */
 136      protected function insert_event_entries($evententries) {
 137          if (!$this->init()) {
 138              return;
 139          }
 140          if (!$dbtable = $this->get_config('dbtable')) {
 141              return;
 142          }
 143          try {
 144              $this->extdb->insert_records($dbtable, $evententries);
 145          } catch (\moodle_exception $e) {
 146              debugging('Cannot write to external database: ' . $e->getMessage(), DEBUG_DEVELOPER);
 147          }
 148      }
 149  
 150      /**
 151       * Get an array of events based on the passed on params.
 152       *
 153       * @param string $selectwhere select conditions.
 154       * @param array $params params.
 155       * @param string $sort sortorder.
 156       * @param int $limitfrom limit constraints.
 157       * @param int $limitnum limit constraints.
 158       *
 159       * @return array|\core\event\base[] array of events.
 160       */
 161      public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
 162          if (!$this->init()) {
 163              return array();
 164          }
 165  
 166          if (!$dbtable = $this->get_config('dbtable')) {
 167              return array();
 168          }
 169  
 170          $sort = self::tweak_sort_by_id($sort);
 171  
 172          $events = array();
 173          $records = $this->extdb->get_records_select($dbtable, $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);
 174  
 175          foreach ($records as $data) {
 176              $extra = array('origin' => $data->origin, 'realuserid' => $data->realuserid, 'ip' => $data->ip);
 177              $data = (array)$data;
 178              $id = $data['id'];
 179              $data['other'] = unserialize($data['other']);
 180              if ($data['other'] === false) {
 181                  $data['other'] = array();
 182              }
 183              unset($data['origin']);
 184              unset($data['ip']);
 185              unset($data['realuserid']);
 186              unset($data['id']);
 187  
 188              $event = \core\event\base::restore($data, $extra);
 189              // Add event to list if it's valid.
 190              if ($event) {
 191                  $events[$id] = $event;
 192              }
 193          }
 194  
 195          return $events;
 196      }
 197  
 198      /**
 199       * Get number of events present for the given select clause.
 200       *
 201       * @param string $selectwhere select conditions.
 202       * @param array $params params.
 203       *
 204       * @return int Number of events available for the given conditions
 205       */
 206      public function get_events_select_count($selectwhere, array $params) {
 207          if (!$this->init()) {
 208              return 0;
 209          }
 210  
 211          if (!$dbtable = $this->get_config('dbtable')) {
 212              return 0;
 213          }
 214  
 215          return $this->extdb->count_records_select($dbtable, $selectwhere, $params);
 216      }
 217  
 218      /**
 219       * Are the new events appearing in the reader?
 220       *
 221       * @return bool true means new log events are being added, false means no new data will be added
 222       */
 223      public function is_logging() {
 224          if (!$this->init()) {
 225              return false;
 226          }
 227          return true;
 228      }
 229  
 230      /**
 231       * Dispose off database connection after pushing any buffered events to the database.
 232       */
 233      public function dispose() {
 234          $this->helper_dispose();
 235          if ($this->extdb) {
 236              $this->extdb->dispose();
 237          }
 238          $this->extdb = null;
 239      }
 240  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1