first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,134 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper trait buffered_writer
*
* @package tool_log
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\helper;
defined('MOODLE_INTERNAL') || die();
/**
* Helper trait buffered_writer. Adds buffer support for the store.
*
* @package tool_log
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait buffered_writer {
/** @var array $buffer buffer of events. */
protected $buffer = array();
/** @var array $buffer buffer size of events. */
protected $buffersize;
/** @var int $count Counter. */
protected $count = 0;
/** @var bool If true, writes JSON instead of PHP serialized data for 'other' field */
protected $jsonformat = false;
/**
* Should the event be ignored (== not logged)?
* @param \core\event\base $event
* @return bool
*/
abstract protected function is_event_ignored(\core\event\base $event);
/**
* Write event in the store with buffering. Method insert_event_entries() must be
* defined.
*
* @param \core\event\base $event
*
* @return void
*/
public function write(\core\event\base $event) {
global $PAGE;
if ($this->is_event_ignored($event)) {
return;
}
// We need to capture current info at this moment,
// at the same time this lowers memory use because
// snapshots and custom objects may be garbage collected.
$entry = $event->get_data();
if ($this->jsonformat) {
$entry['other'] = json_encode($entry['other']);
} else {
$entry['other'] = serialize($entry['other']);
}
$entry['origin'] = $PAGE->requestorigin;
$entry['ip'] = $PAGE->requestip;
$entry['realuserid'] = \core\session\manager::is_loggedinas() ? $GLOBALS['USER']->realuser : null;
$this->buffer[] = $entry;
$this->count++;
if (!isset($this->buffersize)) {
$this->buffersize = $this->get_config('buffersize', 50);
}
if ($this->count >= $this->buffersize) {
$this->flush();
}
}
/**
* Flush event buffer.
*/
public function flush() {
if ($this->count == 0) {
return;
}
$events = $this->buffer;
$this->count = 0;
$this->buffer = array();
$this->insert_event_entries($events);
}
/**
* Bulk write a given array of events to the backend. Stores must implement this.
*
* @param array $evententries raw event data
*/
abstract protected function insert_event_entries($evententries);
/**
* Get a config value for the store.
*
* @param string $name Config name
* @param mixed $default default value
*
* @return mixed config value if set, else the default value.
*/
abstract protected function get_config($name, $default = null);
/**
* Push any remaining events to the database. Insert_events() must be
* defined. override in stores if the store doesn't support buffering.
*
*/
public function dispose() {
$this->flush();
}
}
+112
View File
@@ -0,0 +1,112 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Reader helper trait.
*
* @package tool_log
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\helper;
defined('MOODLE_INTERNAL') || die();
/**
* Reader helper trait.
* \tool_log\helper\store must be included before using this trait.
*
* @package tool_log
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @property string $component Frankenstyle plugin name initialised in store trait.
* @property string $store short plugin name initialised in store trait.
*/
trait reader {
/** @var string Frankenstyle plugin name initialised in store trait. */
protected $component;
/** @var string short plugin name initialised in store trait. */
protected $store;
/**
* Default get name api.
*
* @return string name of the store.
*/
public function get_name() {
if (get_string_manager()->string_exists('pluginname', $this->component)) {
return get_string('pluginname', $this->component);
}
return $this->store;
}
/**
* Default get description method.
*
* @return string description of the store.
*/
public function get_description() {
if (get_string_manager()->string_exists('pluginname_desc', $this->component)) {
return get_string('pluginname_desc', $this->component);
}
return $this->store;
}
/**
* Function decodes the other field into an array using either PHP serialisation or JSON.
*
* Note that this does not rely on the config setting, it supports both formats, so you can
* use it for data before/after making a change to the config setting.
*
* The return value is usually an array but it can also be null or a boolean or something.
*
* @param string $other Other value
* @return mixed Decoded value
*/
public static function decode_other(?string $other) {
if ($other === 'N;' || preg_match('~^.:~', $other ?? '')) {
return unserialize($other, ['allowed_classes' => [stdClass::class]]);
} else {
return json_decode($other ?? '', true);
}
}
/**
* Adds ID column to $sort to make sure events from one request
* within 1 second are returned in the same order.
*
* @param string $sort
* @return string sort string
*/
protected static function tweak_sort_by_id($sort) {
if (empty($sort)) {
// Mysql does this - unlikely to be used in real life because $sort is always expected.
$sort = "id ASC";
} else if (stripos($sort, 'timecreated') === false) {
$sort .= ", id ASC";
} else if (stripos($sort, 'timecreated DESC') !== false) {
$sort .= ", id DESC";
} else {
$sort .= ", id ASC";
}
return $sort;
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper trait store.
*
* @package tool_log
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\helper;
defined('MOODLE_INTERNAL') || die();
/**
* Helper trait store. Adds some helper methods for stores.
*
* @package tool_log
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait store {
/** @var \tool_log\log\manager $manager manager instance. */
protected $manager;
/** @var string $component Frankenstyle store name. */
protected $component;
/** @var string $store name of the store. */
protected $store;
/**
* Setup store specific variables.
*
* @param \tool_log\log\manager $manager manager instance.
*/
protected function helper_setup(\tool_log\log\manager $manager) {
$this->manager = $manager;
$called = get_called_class();
$parts = explode('\\', $called);
if (!isset($parts[0]) || strpos($parts[0], 'logstore_') !== 0) {
throw new \coding_exception("Store $called doesn't define classes in correct namespaces.");
}
$this->component = $parts[0];
$this->store = str_replace('logstore_', '', $this->component);
}
/**
* Api to get plugin config
*
* @param string $name name of the config.
* @param null|mixed $default default value to return.
*
* @return mixed|null return config value.
*/
protected function get_config($name, $default = null) {
$value = get_config($this->component, $name);
if ($value !== false) {
return $value;
}
return $default;
}
}