Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Display query info to admin for better debugging and troubleshooting.
19
 *
20
 * @package    block_dash
21
 * @copyright  2020 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\output;
26
 
27
use renderer_base;
28
use stdClass;
29
/**
30
 * Display query info to admin for better debugging and troubleshooting.
31
 *
32
 * @package block_dash
33
 */
34
final class query_debug implements \renderable, \templatable {
35
 
36
    /**
37
     * @var string SQL compiled raw query.
38
     */
39
    private $query;
40
 
41
    /**
42
     * @var array parameters used in query.
43
     */
44
    private $params;
45
 
46
    /**
47
     * Constructor.
48
     *
49
     * @param string $query Full SQL query to display to user for debug purposes.
50
     * @param array $params Any parameters that will be used in query.
51
     */
52
    public function __construct(string $query, array $params) {
53
        $this->query = $query;
54
        $this->params = $params;
55
    }
56
 
57
    /**
58
     * Check if user can view query debug.
59
     *
60
     * @param int $userid
61
     * @return bool
62
     */
63
    public function can_view(int $userid): bool {
64
        return is_siteadmin($userid);
65
    }
66
 
67
    /**
68
     * Function to export the renderer data in a format that is suitable for a
69
     * mustache template. This means:
70
     * 1. No complex types - only stdClass, array, int, string, float, bool
71
     * 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
72
     *
73
     * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
74
     * @return stdClass|array
75
     */
76
    public function export_for_template(renderer_base $output): array {
77
        return [
78
            'query' => $this->query,
79
            'params' => json_encode($this->params, true),
80
            'uniqueid' => uniqid(),
81
        ];
82
    }
83
}