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 * Helper trait buffered_writer 19 * 20 * @package tool_log 21 * @copyright 2014 onwards Ankit Agarwal 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace tool_log\helper; 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * Helper trait buffered_writer. Adds buffer support for the store. 30 * 31 * @package tool_log 32 * @copyright 2014 onwards Ankit Agarwal 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 trait buffered_writer { 36 37 /** @var array $buffer buffer of events. */ 38 protected $buffer = array(); 39 40 /** @var array $buffer buffer size of events. */ 41 protected $buffersize; 42 43 /** @var int $count Counter. */ 44 protected $count = 0; 45 46 /** @var bool If true, writes JSON instead of PHP serialized data for 'other' field */ 47 protected $jsonformat = false; 48 49 /** 50 * Should the event be ignored (== not logged)? 51 * @param \core\event\base $event 52 * @return bool 53 */ 54 abstract protected function is_event_ignored(\core\event\base $event); 55 56 /** 57 * Write event in the store with buffering. Method insert_event_entries() must be 58 * defined. 59 * 60 * @param \core\event\base $event 61 * 62 * @return void 63 */ 64 public function write(\core\event\base $event) { 65 global $PAGE; 66 67 if ($this->is_event_ignored($event)) { 68 return; 69 } 70 71 // We need to capture current info at this moment, 72 // at the same time this lowers memory use because 73 // snapshots and custom objects may be garbage collected. 74 $entry = $event->get_data(); 75 if ($this->jsonformat) { 76 $entry['other'] = json_encode($entry['other']); 77 } else { 78 $entry['other'] = serialize($entry['other']); 79 } 80 $entry['origin'] = $PAGE->requestorigin; 81 $entry['ip'] = $PAGE->requestip; 82 $entry['realuserid'] = \core\session\manager::is_loggedinas() ? $GLOBALS['USER']->realuser : null; 83 84 $this->buffer[] = $entry; 85 $this->count++; 86 87 if (!isset($this->buffersize)) { 88 $this->buffersize = $this->get_config('buffersize', 50); 89 } 90 91 if ($this->count >= $this->buffersize) { 92 $this->flush(); 93 } 94 } 95 96 /** 97 * Flush event buffer. 98 */ 99 public function flush() { 100 if ($this->count == 0) { 101 return; 102 } 103 $events = $this->buffer; 104 $this->count = 0; 105 $this->buffer = array(); 106 $this->insert_event_entries($events); 107 } 108 109 /** 110 * Bulk write a given array of events to the backend. Stores must implement this. 111 * 112 * @param array $evententries raw event data 113 */ 114 abstract protected function insert_event_entries($evententries); 115 116 /** 117 * Get a config value for the store. 118 * 119 * @param string $name Config name 120 * @param mixed $default default value 121 * 122 * @return mixed config value if set, else the default value. 123 */ 124 abstract protected function get_config($name, $default = null); 125 126 /** 127 * Push any remaining events to the database. Insert_events() must be 128 * defined. override in stores if the store doesn't support buffering. 129 * 130 */ 131 public function dispose() { 132 $this->flush(); 133 } 134 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body