source: moodle/trunk/fuentes/admin/tool/log/store/standard/classes/log/store.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 3.9 KB
Line 
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
25namespace logstore_standard\log;
26
27defined('MOODLE_INTERNAL') || die();
28
29class store implements \tool_log\log\writer, \core\log\sql_internal_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    }
42
43    /**
44     * Should the event be ignored (== not logged)?
45     * @param \core\event\base $event
46     * @return bool
47     */
48    protected function is_event_ignored(\core\event\base $event) {
49        if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) {
50            // Always log inside CLI scripts because we do not login there.
51            if (!isloggedin() or isguestuser()) {
52                return true;
53            }
54        }
55        return false;
56    }
57
58    /**
59     * Finally store the events into the database.
60     *
61     * @param array $evententries raw event data
62     */
63    protected function insert_event_entries($evententries) {
64        global $DB;
65
66        $DB->insert_records('logstore_standard_log', $evententries);
67    }
68
69    public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
70        global $DB;
71
72        $sort = self::tweak_sort_by_id($sort);
73
74        $events = array();
75        $records = $DB->get_records_select('logstore_standard_log', $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);
76
77        foreach ($records as $data) {
78            $extra = array('origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid);
79            $data = (array)$data;
80            $id = $data['id'];
81            $data['other'] = unserialize($data['other']);
82            if ($data['other'] === false) {
83                $data['other'] = array();
84            }
85            unset($data['origin']);
86            unset($data['ip']);
87            unset($data['realuserid']);
88            unset($data['id']);
89
90            $event = \core\event\base::restore($data, $extra);
91            // Add event to list if it's valid.
92            if ($event) {
93                $events[$id] = $event;
94            }
95        }
96
97        return $events;
98    }
99
100    public function get_events_select_count($selectwhere, array $params) {
101        global $DB;
102        return $DB->count_records_select('logstore_standard_log', $selectwhere, $params);
103    }
104
105    public function get_internal_log_table_name() {
106        return 'logstore_standard_log';
107    }
108
109    /**
110     * Are the new events appearing in the reader?
111     *
112     * @return bool true means new log events are being added, false means no new data will be added
113     */
114    public function is_logging() {
115        // Only enabled stpres are queried,
116        // this means we can return true here unless store has some extra switch.
117        return true;
118    }
119}
Note: See TracBrowser for help on using the repository browser.