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;
}
}
@@ -0,0 +1,149 @@
<?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/>.
/**
* Privacy helper.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\local\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\request\transform;
/**
* Privacy helper class.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
use \tool_log\helper\reader;
/**
* Returns an event from a standard record.
*
* @see \logstore_standard\log\store::get_log_event()
* @param object $data Log data.
* @return \core\event\base
*/
protected static function restore_event_from_standard_record($data) {
$extra = ['origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid];
$data = (array) $data;
$id = $data['id'];
$data['other'] = self::decode_other($data['other']);
if ($data['other'] === false) {
$data['other'] = [];
}
unset($data['origin']);
unset($data['ip']);
unset($data['realuserid']);
unset($data['id']);
if (!$event = \core\event\base::restore($data, $extra)) {
return null;
}
return $event;
}
/**
* Transform a standard log record for a user.
*
* @param object $record The record.
* @param int $userid The user ID.
* @return array
*/
public static function transform_standard_log_record_for_userid($record, $userid) {
// Restore the event to try to get the name, description and other field.
$restoredevent = static::restore_event_from_standard_record($record);
if ($restoredevent) {
$name = $restoredevent->get_name();
$description = $restoredevent->get_description();
$other = $restoredevent->other;
} else {
$name = $record->eventname;
$description = "Unknown event ({$name})";
$other = self::decode_other($record->other);
}
$realuserid = $record->realuserid;
$isauthor = $record->userid == $userid;
$isrelated = $record->relateduserid == $userid;
$isrealuser = $realuserid == $userid;
$ismasqueraded = $realuserid !== null && $record->userid != $realuserid;
$ismasquerading = $isrealuser && !$isauthor;
$isanonymous = $record->anonymous;
$data = [
'name' => $name,
'description' => $description,
'timecreated' => transform::datetime($record->timecreated),
'origin' => static::transform_origin($record->origin),
'ip' => $isauthor ? $record->ip : '',
'other' => $other ? $other : []
];
if ($isanonymous) {
$data['action_was_done_anonymously'] = transform::yesno($isanonymous);
}
if ($isauthor || !$isanonymous) {
$data['authorid'] = transform::user($record->userid);
$data['author_of_the_action_was_you'] = transform::yesno($isauthor);
}
if ($record->relateduserid) {
$data['relateduserid'] = transform::user($record->relateduserid);
$data['related_user_was_you'] = transform::yesno($isrelated);
}
if ($ismasqueraded) {
$data['author_of_the_action_was_masqueraded'] = transform::yesno(true);
if ($ismasquerading || !$isanonymous) {
$data['masqueradinguserid'] = transform::user($realuserid);
$data['masquerading_user_was_you'] = transform::yesno($ismasquerading);
}
}
return $data;
}
/**
* Transform origin.
*
* @param string $origin The page request origin.
* @return string
*/
public static function transform_origin($origin) {
switch ($origin) {
case 'cli':
case 'restore':
case 'web':
case 'ws':
return get_string('privacy:request:origin:' . $origin, 'tool_log');
break;
}
return $origin;
}
}
@@ -0,0 +1,78 @@
<?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/>.
/**
* Logstore provider interface.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\local\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
/**
* Logstore provider interface.
*
* Logstore subplugins providers must implement this interface.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface logstore_provider extends \core_privacy\local\request\plugin\subplugin_provider {
/**
* Add contexts that contain user information for the specified user.
*
* @param contextlist $contextlist The contextlist to add the contexts to.
* @param int $userid The user to find the contexts for.
* @return void
*/
public static function add_contexts_for_userid(contextlist $contextlist, $userid);
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
* @return void
*/
public static function export_user_data(approved_contextlist $contextlist);
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
* @return void
*/
public static function delete_data_for_all_users_in_context(context $context);
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
* @return void
*/
public static function delete_data_for_user(approved_contextlist $contextlist);
}
@@ -0,0 +1,60 @@
<?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/>.
/**
* Logstore userlist provider interface.
*
* @package tool_log
* @copyright 2018 Adrian Greeve
* @author Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\local\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Logstore userlist provider interface.
*
* Logstore subplugins providers must implement this interface.
*
* @package tool_log
* @copyright 2018 Adrian Greeve
* @author Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface logstore_userlist_provider extends
\core_privacy\local\request\plugin\subplugin_provider,
\core_privacy\local\request\shared_userlist_provider
{
/**
* Add user IDs that contain user information for the specified context.
*
* @param \core_privacy\local\request\userlist $userlist The userlist to add the users to.
* @return void
*/
public static function add_userids_for_context(\core_privacy\local\request\userlist $userlist);
/**
* Delete all data for a list of users in the specified context.
*
* @param \core_privacy\local\request\approved_userlist $userlist The specific context and users to delete data for.
* @return void
*/
public static function delete_data_for_userlist(\core_privacy\local\request\approved_userlist $userlist);
}
@@ -0,0 +1,138 @@
<?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/>.
/**
* Moodle database: export and delete.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\local\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\writer;
/**
* Moodle database: export and delete trait.
*
* This is to be used with logstores which use a database and table with the same columns
* as the core plugin 'logstore_standard'.
*
* This trait expects the following methods to be present in the object:
*
* - public static function get_database_and_table(): [moodle_database|null, string|null]
* - public static function get_export_subcontext(): []
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait moodle_database_export_and_delete {
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
list($db, $table) = static::get_database_and_table();
if (!$db || !$table) {
return;
}
$userid = $contextlist->get_user()->id;
list($insql, $inparams) = $db->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
$sql = "(userid = :userid1 OR relateduserid = :userid2 OR realuserid = :userid3) AND contextid $insql";
$params = array_merge($inparams, [
'userid1' => $userid,
'userid2' => $userid,
'userid3' => $userid,
]);
$path = static::get_export_subcontext();
$flush = function($lastcontextid, $data) use ($path) {
$context = context::instance_by_id($lastcontextid);
writer::with_context($context)->export_data($path, (object) ['logs' => $data]);
};
$lastcontextid = null;
$data = [];
$recordset = $db->get_recordset_select($table, $sql, $params, 'contextid, timecreated, id');
foreach ($recordset as $record) {
if ($lastcontextid && $lastcontextid != $record->contextid) {
$flush($lastcontextid, $data);
$data = [];
}
$data[] = helper::transform_standard_log_record_for_userid($record, $userid);
$lastcontextid = $record->contextid;
}
if ($lastcontextid) {
$flush($lastcontextid, $data);
}
$recordset->close();
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
list($db, $table) = static::get_database_and_table();
if (!$db || !$table) {
return;
}
$db->delete_records($table, ['contextid' => $context->id]);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
list($db, $table) = static::get_database_and_table();
if (!$db || !$table) {
return;
}
list($insql, $inparams) = $db->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
$params = array_merge($inparams, ['userid' => $contextlist->get_user()->id]);
$db->delete_records_select($table, "userid = :userid AND contextid $insql", $params);
}
/**
* Delete all user data for the specified users, in the specified context.
*
* @param \core_privacy\local\request\approved_userlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_userlist(\core_privacy\local\request\approved_userlist $userlist) {
list($db, $table) = static::get_database_and_table();
if (!$db || !$table) {
return;
}
list($insql, $inparams) = $db->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$params = array_merge($inparams, ['contextid' => $userlist->get_context()->id]);
$db->delete_records_select($table, "contextid = :contextid AND userid $insql", $params);
}
}
+202
View File
@@ -0,0 +1,202 @@
<?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/>.
/**
* Log store manager.
*
* @package tool_log
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\log;
defined('MOODLE_INTERNAL') || die();
class manager implements \core\log\manager {
/** @var \core\log\reader[] $readers list of initialised log readers */
protected $readers;
/** @var \tool_log\log\writer[] $writers list of initialised log writers */
protected $writers;
/** @var \tool_log\log\store[] $stores list of all enabled stores */
protected $stores;
/**
* Delayed initialisation of singleton.
*/
protected function init() {
if (isset($this->stores)) {
// Do not bother checking readers and writers here
// because everything is init here.
return;
}
$this->stores = array();
$this->readers = array();
$this->writers = array();
// Register shutdown handler - this may be useful for buffering, file handle closing, etc.
\core_shutdown_manager::register_function(array($this, 'dispose'));
$plugins = get_config('tool_log', 'enabled_stores');
if (empty($plugins)) {
return;
}
$plugins = explode(',', $plugins);
foreach ($plugins as $plugin) {
$classname = "\\$plugin\\log\\store";
if (class_exists($classname)) {
$store = new $classname($this);
$this->stores[$plugin] = $store;
if ($store instanceof \tool_log\log\writer) {
$this->writers[$plugin] = $store;
}
if ($store instanceof \core\log\reader) {
$this->readers[$plugin] = $store;
}
}
}
}
/**
* Called from the observer only.
*
* @param \core\event\base $event
*/
public function process(\core\event\base $event) {
$this->init();
foreach ($this->writers as $plugin => $writer) {
try {
$writer->write($event, $this);
} catch (\Exception $e) {
debugging('Exception detected when logging event ' . $event->eventname . ' in ' . $plugin . ': ' .
$e->getMessage(), DEBUG_NORMAL, $e->getTrace());
}
}
}
/**
* Returns list of available log readers.
*
* This way the reports find out available sources of data.
*
* @param string $interface Returned stores must implement this interface.
*
* @return \core\log\reader[] list of available log data readers
*/
public function get_readers($interface = null) {
$this->init();
$return = array();
foreach ($this->readers as $plugin => $reader) {
if (empty($interface) || ($reader instanceof $interface)) {
$return[$plugin] = $reader;
}
}
return $return;
}
/**
* Get a list of reports that support the given store instance.
*
* @param string $logstore Name of the store.
*
* @return array List of supported reports
*/
public function get_supported_reports($logstore) {
$allstores = self::get_store_plugins();
if (empty($allstores[$logstore])) {
// Store doesn't exist.
return array();
}
$reports = get_plugin_list_with_function('report', 'supports_logstore', 'lib.php');
$enabled = $this->stores;
if (empty($enabled[$logstore])) {
// Store is not enabled, init an instance.
$classname = '\\' . $logstore . '\log\store';
$instance = new $classname($this);
} else {
$instance = $enabled[$logstore];
}
$return = array();
foreach ($reports as $report => $fulldir) {
if (component_callback($report, 'supports_logstore', array($instance), false)) {
$return[$report] = get_string('pluginname', $report);
}
}
return $return;
}
/**
* For a given report, returns a list of log stores that are supported.
*
* @param string $component component.
*
* @return false|array list of logstores that support the given report. It returns false if the given $component doesn't
* require logstores.
*/
public function get_supported_logstores($component) {
$allstores = self::get_store_plugins();
$enabled = $this->stores;
$function = component_callback_exists($component, 'supports_logstore');
if (!$function) {
// The report doesn't define the callback, most probably it doesn't need log stores.
return false;
}
$return = array();
foreach ($allstores as $store => $logclass) {
$instance = empty($enabled[$store]) ? new $logclass($this) : $enabled[$store];
if ($function($instance)) {
$return[$store] = get_string('pluginname', $store);
}
}
return $return;
}
/**
* Intended for store management, do not use from reports.
*
* @return store[] Returns list of available store plugins.
*/
public static function get_store_plugins() {
return \core_component::get_plugin_list_with_class('logstore', 'log\store');
}
/**
* Usually called automatically from shutdown manager,
* this allows us to implement buffering of write operations.
*/
public function dispose() {
if ($this->stores) {
foreach ($this->stores as $store) {
$store->dispose();
}
}
$this->stores = null;
$this->readers = null;
$this->writers = null;
}
}
+43
View File
@@ -0,0 +1,43 @@
<?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/>.
/**
* Event observer.
*
* @package tool_log
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\log;
defined('MOODLE_INTERNAL') || die();
class observer {
/**
* Redirect all events to this log manager, but only if this
* log manager is actually used.
*
* @param \core\event\base $event
*/
public static function store(\core\event\base $event) {
$logmanager = get_log_manager();
if (get_class($logmanager) === 'tool_log\log\manager') {
/** @var \tool_log\log\manager $logmanager */
$logmanager->process($event);
}
}
}
+43
View File
@@ -0,0 +1,43 @@
<?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/>.
/**
* Log store interface.
*
* @package tool_log
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\log;
defined('MOODLE_INTERNAL') || die();
interface store {
/**
* Create new instance of store,
* the calling code must make sure only one instance exists.
*
* @param manager $manager
*/
public function __construct(\tool_log\log\manager $manager);
/**
* Notify store no more events are going to be written/read from it.
* @return void
*/
public function dispose();
}
+37
View File
@@ -0,0 +1,37 @@
<?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/>.
/**
* Log store writer interface.
*
* @package tool_log
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\log;
defined('MOODLE_INTERNAL') || die();
interface writer extends store {
/**
* Write one event to the store.
*
* @param \core\event\base $event
* @return void
*/
public function write(\core\event\base $event);
}
@@ -0,0 +1,122 @@
<?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/>.
/**
* Subplugin info class.
*
* @package tool_log
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\plugininfo;
use admin_settingpage;
use core\plugininfo\base;
use moodle_url;
use part_of_admin_tree;
/**
* Plugin info class for logging store plugins.
*/
class logstore extends base {
public static function plugintype_supports_disabling(): bool {
return true;
}
public function is_enabled() {
$enabled = get_config('tool_log', 'enabled_stores');
if (!$enabled) {
return false;
}
$enabled = array_flip(explode(',', $enabled));
return isset($enabled['logstore_' . $this->name]);
}
public static function enable_plugin(string $pluginname, int $enabled): bool {
$haschanged = false;
$plugins = [];
$oldvalue = get_config('tool_log', 'enabled_stores');
if (!empty($oldvalue)) {
$plugins = array_flip(explode(',', $oldvalue));
}
// Only set visibility if it's different from the current value.
if ($enabled && !array_key_exists($pluginname, $plugins)) {
$plugins[$pluginname] = $pluginname;
$haschanged = true;
} else if (!$enabled && array_key_exists($pluginname, $plugins)) {
unset($plugins[$pluginname]);
$haschanged = true;
}
if ($haschanged) {
$new = implode(',', array_flip($plugins));
add_to_config_log('tool_logstore_visibility', !$enabled, $enabled, $pluginname);
set_config('enabled_stores', $new, 'tool_log');
// Reset caches.
\core_plugin_manager::reset_caches();
}
return $haschanged;
}
public function get_settings_section_name() {
return 'logsetting' . $this->name;
}
public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
/** @var \admin_root $ADMIN */
$ADMIN = $adminroot; // May be used in settings.php.
$section = $this->get_settings_section_name();
if (!$this->is_installed_and_upgraded()) {
return;
}
if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
return;
}
$settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
include($this->full_path('settings.php'));
if ($settings) {
$ADMIN->add($parentnodename, $settings);
}
}
public static function get_manage_url() {
return new moodle_url('/admin/settings.php', array('section' => 'managelogging'));
}
public function is_uninstall_allowed() {
return true;
}
public function uninstall_cleanup() {
$enabled = get_config('tool_log', 'enabled_stores');
if ($enabled) {
$enabled = array_flip(explode(',', $enabled));
unset($enabled['logstore_' . $this->name]);
$enabled = array_flip($enabled);
set_config('enabled_stores', implode(',', $enabled), 'tool_log');
}
parent::uninstall_cleanup();
}
}
+137
View File
@@ -0,0 +1,137 @@
<?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/>.
/**
* Data provider.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_log\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use tool_log\log\manager;
/**
* Data provider class.
*
* @package tool_log
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\subsystem\provider,
\core_privacy\local\request\core_userlist_provider {
/**
* Returns metadata.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_plugintype_link('logstore', [], 'privacy:metadata:logstore');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): \core_privacy\local\request\contextlist {
$contextlist = new \core_privacy\local\request\contextlist();
static::call_subplugins_method_with_args('add_contexts_for_userid', [$contextlist, $userid]);
return $contextlist;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param \core_privacy\local\request\userlist $userlist The userlist containing the list of users who have data in
* this context/plugin combination.
*/
public static function get_users_in_context(\core_privacy\local\request\userlist $userlist) {
$interface = \tool_log\local\privacy\logstore_userlist_provider::class;
static::call_subplugins_method_with_args('add_userids_for_context', [$userlist], $interface);
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
if (get_config('tool_log', 'exportlog')) {
static::call_subplugins_method_with_args('export_user_data', [$contextlist]);
}
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
static::call_subplugins_method_with_args('delete_data_for_all_users_in_context', [$context]);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
static::call_subplugins_method_with_args('delete_data_for_user', [$contextlist]);
}
/**
* Delete multiple users within a single context.
*
* @param \core_privacy\local\request\approved_userlist $userlist The approved context and user information to delete
* information for.
*/
public static function delete_data_for_users(\core_privacy\local\request\approved_userlist $userlist) {
$interface = \tool_log\local\privacy\logstore_userlist_provider::class;
static::call_subplugins_method_with_args('delete_data_for_userlist', [$userlist], $interface);
}
/**
* Invoke the subplugins method with arguments.
*
* @param string $method The method name.
* @param array $args The arguments.
* @param string $interface The interface to use. By default uses the logstore_provider.
* @return void
*/
protected static function call_subplugins_method_with_args($method, array $args = [], string $interface = null) {
if (!isset($interface)) {
$interface = \tool_log\local\privacy\logstore_provider::class;
}
\core_privacy\manager::plugintype_class_callback('logstore', $interface, $method, $args);
}
}
@@ -0,0 +1,243 @@
<?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/>.
/**
* Store management setting.
*
* @package tool_log
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/adminlib.php");
class tool_log_setting_managestores extends admin_setting {
/**
* Calls parent::__construct with specific arguments
*/
public function __construct() {
$this->nosave = true;
parent::__construct('tool_log_manageui', get_string('managelogging', 'tool_log'), '', '');
}
/**
* Always returns true, does nothing.
*
* @return true
*/
public function get_setting() {
return true;
}
/**
* Always returns true, does nothing.
*
* @return true
*/
public function get_defaultsetting() {
return true;
}
/**
* Always returns '', does not write anything.
*
* @param mixed $data ignored
* @return string Always returns ''
*/
public function write_setting($data) {
// Do not write any setting.
return '';
}
/**
* Checks if $query is one of the available log plugins.
*
* @param string $query The string to search for
* @return bool Returns true if found, false if not
*/
public function is_related($query) {
if (parent::is_related($query)) {
return true;
}
$query = core_text::strtolower($query);
$plugins = \tool_log\log\manager::get_store_plugins();
foreach ($plugins as $plugin => $fulldir) {
if (strpos(core_text::strtolower($plugin), $query) !== false) {
return true;
}
$localised = get_string('pluginname', $plugin);
if (strpos(core_text::strtolower($localised), $query) !== false) {
return true;
}
}
return false;
}
/**
* Builds the XHTML to display the control.
*
* @param string $data Unused
* @param string $query
* @return string
*/
public function output_html($data, $query = '') {
global $OUTPUT, $PAGE;
// Display strings.
$strup = get_string('up');
$strdown = get_string('down');
$strsettings = get_string('settings');
$strenable = get_string('enable');
$strdisable = get_string('disable');
$struninstall = get_string('uninstallplugin', 'core_admin');
$strversion = get_string('version');
$pluginmanager = core_plugin_manager::instance();
$logmanager = new \tool_log\log\manager();
$available = $logmanager->get_store_plugins();
$enabled = get_config('tool_log', 'enabled_stores');
if (!$enabled) {
$enabled = array();
} else {
$enabled = array_flip(explode(',', $enabled));
}
$allstores = array();
foreach ($enabled as $key => $store) {
$allstores[$key] = true;
$enabled[$key] = true;
}
foreach ($available as $key => $store) {
$allstores[$key] = true;
$available[$key] = true;
}
$return = $OUTPUT->heading(get_string('actlogshdr', 'tool_log'), 3, 'main', true);
$return .= $OUTPUT->box_start('generalbox loggingui');
$table = new html_table();
$table->head = array(get_string('name'), get_string('reportssupported', 'tool_log'), $strversion, $strenable,
$strup . '/' . $strdown, $strsettings, $struninstall);
$table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign',
'centeralign');
$table->id = 'logstoreplugins';
$table->attributes['class'] = 'admintable generaltable';
$table->data = array();
// Iterate through store plugins and add to the display table.
$updowncount = 1;
$storecount = count($enabled);
$url = new moodle_url('/admin/tool/log/stores.php', array('sesskey' => sesskey()));
$printed = array();
foreach ($allstores as $store => $unused) {
$plugininfo = $pluginmanager->get_plugin_info($store);
$version = get_config($store, 'version');
if ($version === false) {
$version = '';
}
if (get_string_manager()->string_exists('pluginname', $store)) {
$name = get_string('pluginname', $store);
} else {
$name = $store;
}
$reports = $logmanager->get_supported_reports($store);
if (!empty($reports)) {
$supportedreports = implode(', ', $reports);
} else {
$supportedreports = '-';
}
// Hide/show links.
if (isset($enabled[$store])) {
$aurl = new moodle_url($url, array('action' => 'disable', 'store' => $store));
$hideshow = "<a href=\"$aurl\">";
$hideshow .= $OUTPUT->pix_icon('t/hide', $strdisable) . '</a>';
$isenabled = true;
$displayname = "<span>$name</span>";
} else {
if (isset($available[$store])) {
$aurl = new moodle_url($url, array('action' => 'enable', 'store' => $store));
$hideshow = "<a href=\"$aurl\">";
$hideshow .= $OUTPUT->pix_icon('t/show', $strenable) . '</a>';
$isenabled = false;
$displayname = "<span>$name</span>";
} else {
$hideshow = '';
$isenabled = false;
$displayname = '<span class="notifyproblem">' . $name . '</span>';
}
}
if ($PAGE->theme->resolve_image_location('icon', $store, false)) {
$icon = $OUTPUT->pix_icon('icon', '', $store, array('class' => 'icon pluginicon'));
} else {
$icon = $OUTPUT->spacer();
}
// Up/down link (only if store is enabled).
$updown = '';
if ($isenabled) {
if ($updowncount > 1) {
$aurl = new moodle_url($url, array('action' => 'up', 'store' => $store));
$updown .= "<a href=\"$aurl\">";
$updown .= $OUTPUT->pix_icon('t/up', $strup) . '</a>&nbsp;';
} else {
$updown .= $OUTPUT->spacer();
}
if ($updowncount < $storecount) {
$aurl = new moodle_url($url, array('action' => 'down', 'store' => $store));
$updown .= "<a href=\"$aurl\">";
$updown .= $OUTPUT->pix_icon('t/down', $strdown) . '</a>&nbsp;';
} else {
$updown .= $OUTPUT->spacer();
}
++$updowncount;
}
// Add settings link.
if (!$version) {
$settings = '';
} else {
if ($surl = $plugininfo->get_settings_url()) {
$settings = html_writer::link($surl, $strsettings);
} else {
$settings = '';
}
}
// Add uninstall info.
$uninstall = '';
if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url($store, 'manage')) {
$uninstall = html_writer::link($uninstallurl, $struninstall);
}
// Add a row to the table.
$table->data[] = array($icon . $displayname, $supportedreports, $version, $hideshow, $updown, $settings, $uninstall);
$table->rowclasses[] = $isenabled ? '' : 'dimmed_text';
$printed[$store] = true;
}
$return .= html_writer::table($table);
$return .= get_string('configlogplugins', 'tool_log') . '<br />' . get_string('tablenosave', 'admin');
$return .= $OUTPUT->box_end();
return highlight($query, $return);
}
}