1 |
efrain |
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 |
* Standard log reader/writer.
|
|
|
19 |
*
|
|
|
20 |
* @package logstore_standard
|
|
|
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_standard\log;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
class store implements \tool_log\log\writer, \core\log\sql_internal_table_reader {
|
|
|
30 |
use \tool_log\helper\store,
|
|
|
31 |
\tool_log\helper\buffered_writer,
|
|
|
32 |
\tool_log\helper\reader;
|
|
|
33 |
|
|
|
34 |
/** @var string $logguests true if logging guest access */
|
|
|
35 |
protected $logguests;
|
|
|
36 |
|
|
|
37 |
public function __construct(\tool_log\log\manager $manager) {
|
|
|
38 |
$this->helper_setup($manager);
|
|
|
39 |
// Log everything before setting is saved for the first time.
|
|
|
40 |
$this->logguests = $this->get_config('logguests', 1);
|
|
|
41 |
// JSON writing defaults to false (table format compatibility with older versions).
|
|
|
42 |
// Note: This variable is defined in the buffered_writer trait.
|
|
|
43 |
$this->jsonformat = (bool)$this->get_config('jsonformat', false);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Should the event be ignored (== not logged)?
|
|
|
48 |
* @param \core\event\base $event
|
|
|
49 |
* @return bool
|
|
|
50 |
*/
|
|
|
51 |
protected function is_event_ignored(\core\event\base $event) {
|
|
|
52 |
if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) {
|
|
|
53 |
// Always log inside CLI scripts because we do not login there.
|
|
|
54 |
if (!isloggedin() or isguestuser()) {
|
|
|
55 |
return true;
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
return false;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Finally store the events into the database.
|
|
|
63 |
*
|
|
|
64 |
* @param array $evententries raw event data
|
|
|
65 |
*/
|
|
|
66 |
protected function insert_event_entries($evententries) {
|
|
|
67 |
global $DB;
|
|
|
68 |
|
|
|
69 |
$DB->insert_records('logstore_standard_log', $evententries);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
|
|
|
73 |
global $DB;
|
|
|
74 |
|
|
|
75 |
$sort = self::tweak_sort_by_id($sort);
|
|
|
76 |
|
|
|
77 |
$events = array();
|
|
|
78 |
$records = $DB->get_recordset_select('logstore_standard_log', $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);
|
|
|
79 |
|
|
|
80 |
foreach ($records as $data) {
|
|
|
81 |
if ($event = $this->get_log_event($data)) {
|
|
|
82 |
$events[$data->id] = $event;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$records->close();
|
|
|
87 |
|
|
|
88 |
return $events;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Fetch records using given criteria returning a Traversable object.
|
|
|
93 |
*
|
|
|
94 |
* Note that the traversable object contains a moodle_recordset, so
|
|
|
95 |
* remember that is important that you call close() once you finish
|
|
|
96 |
* using it.
|
|
|
97 |
*
|
|
|
98 |
* @param string $selectwhere
|
|
|
99 |
* @param array $params
|
|
|
100 |
* @param string $sort
|
|
|
101 |
* @param int $limitfrom
|
|
|
102 |
* @param int $limitnum
|
|
|
103 |
* @return \core\dml\recordset_walk|\core\event\base[]
|
|
|
104 |
*/
|
|
|
105 |
public function get_events_select_iterator($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
|
|
|
106 |
global $DB;
|
|
|
107 |
|
|
|
108 |
$sort = self::tweak_sort_by_id($sort);
|
|
|
109 |
|
|
|
110 |
$recordset = $DB->get_recordset_select('logstore_standard_log', $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);
|
|
|
111 |
|
|
|
112 |
return new \core\dml\recordset_walk($recordset, array($this, 'get_log_event'));
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Returns an event from the log data.
|
|
|
117 |
*
|
|
|
118 |
* @param stdClass $data Log data
|
|
|
119 |
* @return \core\event\base
|
|
|
120 |
*/
|
|
|
121 |
public function get_log_event($data) {
|
|
|
122 |
|
|
|
123 |
$extra = array('origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid);
|
|
|
124 |
$data = (array)$data;
|
|
|
125 |
$id = $data['id'];
|
|
|
126 |
$data['other'] = self::decode_other($data['other']);
|
|
|
127 |
if ($data['other'] === false) {
|
|
|
128 |
$data['other'] = array();
|
|
|
129 |
}
|
|
|
130 |
unset($data['origin']);
|
|
|
131 |
unset($data['ip']);
|
|
|
132 |
unset($data['realuserid']);
|
|
|
133 |
unset($data['id']);
|
|
|
134 |
|
|
|
135 |
if (!$event = \core\event\base::restore($data, $extra)) {
|
|
|
136 |
return null;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
return $event;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Get number of events present for the given select clause.
|
|
|
144 |
*
|
|
|
145 |
* @param string $selectwhere select conditions.
|
|
|
146 |
* @param array $params params.
|
|
|
147 |
*
|
|
|
148 |
* @return int Number of events available for the given conditions
|
|
|
149 |
*/
|
|
|
150 |
public function get_events_select_count($selectwhere, array $params) {
|
|
|
151 |
global $DB;
|
|
|
152 |
return $DB->count_records_select('logstore_standard_log', $selectwhere, $params);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Get whether events are present for the given select clause.
|
|
|
157 |
*
|
|
|
158 |
* @param string $selectwhere select conditions.
|
|
|
159 |
* @param array $params params.
|
|
|
160 |
*
|
|
|
161 |
* @return bool Whether events available for the given conditions
|
|
|
162 |
*/
|
|
|
163 |
public function get_events_select_exists(string $selectwhere, array $params): bool {
|
|
|
164 |
global $DB;
|
|
|
165 |
return $DB->record_exists_select('logstore_standard_log', $selectwhere, $params);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
public function get_internal_log_table_name() {
|
|
|
169 |
return 'logstore_standard_log';
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Are the new events appearing in the reader?
|
|
|
174 |
*
|
|
|
175 |
* @return bool true means new log events are being added, false means no new data will be added
|
|
|
176 |
*/
|
|
|
177 |
public function is_logging() {
|
|
|
178 |
// Only enabled stpres are queried,
|
|
|
179 |
// this means we can return true here unless store has some extra switch.
|
|
|
180 |
return true;
|
|
|
181 |
}
|
|
|
182 |
}
|