Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_ai\reportbuilder\local\systemreports;
18
 
19
use core_reportbuilder\system_report;
20
use core_ai\reportbuilder\local\entities\ai_action_register;
21
use core\reportbuilder\local\entities\context;
22
use core_reportbuilder\local\entities\user;
23
 
24
/**
25
 * AI usage system report.
26
 *
27
 * @package    core_ai
28
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class usage extends system_report {
32
 
33
    #[\Override]
34
    protected function initialise(): void {
35
        $entitymain = new ai_action_register();
36
        $entitymainalias = $entitymain->get_table_alias('ai_action_register');
37
 
38
        $this->set_main_table('ai_action_register', $entitymainalias);
39
        $this->add_entity($entitymain);
40
 
41
        // Join the 'user' entity to our main entity.
42
        $entityuser = new user();
43
        $entituseralias = $entityuser->get_table_alias('user');
44
        $this->add_entity($entityuser->add_join(
45
            "LEFT JOIN {user} {$entituseralias} ON {$entituseralias}.id = {$entitymainalias}.userid"
46
        ));
47
 
48
        // Join the 'context' entity to our main entity.
49
        $entitycontext = new context();
50
        $entitycontextalias = $entitycontext->get_table_alias('context');
51
        $this->add_entity($entitycontext->add_join(
52
            "LEFT JOIN {context} {$entitycontextalias} ON {$entitycontextalias}.id = {$entitymainalias}.contextid"
53
        ));
54
 
55
        // Now we can call our helper methods to add the content we want to include in the report.
56
        $this->add_columns();
57
        $this->add_filters();
58
 
59
        // Set if report can be downloaded.
60
        $this->set_downloadable(true, get_string('aiusage', 'core_ai'));
61
    }
62
 
63
    #[\Override]
64
    protected function can_view(): bool {
65
        return has_capability('moodle/ai:viewaiusagereport', $this->get_context());
66
    }
67
 
68
    #[\Override]
69
    public static function get_name(): string {
70
        return get_string('aiusage', 'core_ai');
71
    }
72
 
73
    /**
74
     * Adds the columns we want to display in the report.
75
     *
76
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
77
     * unique identifier.
78
     */
79
    public function add_columns(): void {
80
        $this->add_columns_from_entities([
81
            'ai_action_register:provider',
82
            'ai_action_register:actionname',
83
            'ai_action_register:timecreated',
84
            'ai_action_register:prompttokens',
85
            'ai_action_register:completiontokens',
86
            'ai_action_register:success',
87
            'context:name',
88
            'user:fullnamewithlink',
89
        ]);
90
 
91
        // It's possible to set a default initial sort direction for one column.
92
        $this->set_initial_sort_column('ai_action_register:timecreated', SORT_DESC);
93
    }
94
 
95
    /**
96
     * Adds the filters we want to display in the report.
97
     *
98
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
99
     * unique identifier.
100
     */
101
    protected function add_filters(): void {
102
        $this->add_filters_from_entities([
103
            'ai_action_register:actionname',
104
            'ai_action_register:provider',
105
            'ai_action_register:timecreated',
106
            'ai_action_register:prompttokens',
107
            'ai_action_register:completiontokens',
108
            'ai_action_register:success',
109
            'context:level',
110
            'user:fullname',
111
        ]);
112
    }
113
}