Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
declare(strict_types=1);
18
 
19
namespace core_comment\reportbuilder\local\entities;
20
 
21
use context;
22
use context_helper;
23
use lang_string;
24
use stdClass;
25
use core_reportbuilder\local\entities\base;
26
use core_reportbuilder\local\filters\{date, text};
27
use core_reportbuilder\local\helpers\format;
28
use core_reportbuilder\local\report\{column, filter};
29
 
30
/**
31
 * Comment entity
32
 *
33
 * @package     core_comment
34
 * @copyright   2022 Paul Holden <paulh@moodle.com>
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class comment extends base {
38
 
39
    /**
40
     * Database tables that this entity uses
41
     *
42
     * @return string[]
43
     */
44
    protected function get_default_tables(): array {
45
        return [
46
            'comments',
47
            'context',
48
        ];
49
    }
50
 
51
    /**
52
     * The default title for this entity
53
     *
54
     * @return lang_string
55
     */
56
    protected function get_default_entity_title(): lang_string {
57
        return new lang_string('comment', 'core_comment');
58
    }
59
 
60
    /**
61
     * Initialise the entity
62
     *
63
     * @return base
64
     */
65
    public function initialise(): base {
66
        $columns = $this->get_all_columns();
67
        foreach ($columns as $column) {
68
            $this->add_column($column);
69
        }
70
 
71
        // All the filters defined by the entity can also be used as conditions.
72
        $filters = $this->get_all_filters();
73
        foreach ($filters as $filter) {
74
            $this
75
                ->add_filter($filter)
76
                ->add_condition($filter);
77
        }
78
 
79
        return $this;
80
    }
81
 
82
    /**
83
     * Returns list of all available columns
84
     *
85
     * @return column[]
86
     */
87
    protected function get_all_columns(): array {
88
        $commentalias = $this->get_table_alias('comments');
89
        $contextalias = $this->get_table_alias('context');
90
 
91
        // Content.
92
        $columns[] = (new column(
93
            'content',
94
            new lang_string('content'),
95
            $this->get_entity_name()
96
        ))
97
            ->add_joins($this->get_joins())
98
            ->set_type(column::TYPE_LONGTEXT)
99
            ->add_join($this->get_context_join())
1441 ariadna 100
            ->add_fields("{$commentalias}.content, {$commentalias}.format, {$commentalias}.contextid")
101
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
102
            ->set_is_sortable(true)
1 efrain 103
            ->add_callback(static function($content, stdClass $comment): string {
104
                if ($content === null) {
105
                    return '';
106
                }
107
 
108
                context_helper::preload_from_record($comment);
109
                $context = context::instance_by_id($comment->contextid);
110
 
111
                return format_text($content, $comment->format, ['context' => $context]);
112
            });
113
 
114
        // Component.
115
        $columns[] = (new column(
116
            'component',
117
            new lang_string('plugin'),
118
            $this->get_entity_name()
119
        ))
120
            ->add_joins($this->get_joins())
121
            ->set_type(column::TYPE_TEXT)
122
            ->add_fields("{$commentalias}.component")
123
            ->set_is_sortable(true);
124
 
125
        // Area.
126
        $columns[] = (new column(
127
            'area',
128
            new lang_string('pluginarea'),
129
            $this->get_entity_name()
130
        ))
131
            ->add_joins($this->get_joins())
132
            ->set_type(column::TYPE_TEXT)
133
            ->add_fields("{$commentalias}.commentarea")
134
            ->set_is_sortable(true);
135
 
136
        // Item ID.
137
        $columns[] = (new column(
138
            'itemid',
139
            new lang_string('pluginitemid'),
140
            $this->get_entity_name()
141
        ))
142
            ->add_joins($this->get_joins())
143
            ->add_fields("{$commentalias}.itemid")
1441 ariadna 144
            ->set_is_sortable(true);
1 efrain 145
 
146
        // Time created.
147
        $columns[] = (new column(
148
            'timecreated',
149
            new lang_string('timecreated', 'core_reportbuilder'),
150
            $this->get_entity_name()
151
        ))
152
            ->add_joins($this->get_joins())
153
            ->set_type(column::TYPE_TIMESTAMP)
154
            ->add_fields("{$commentalias}.timecreated")
155
            ->set_is_sortable(true)
156
            ->add_callback([format::class, 'userdate']);
157
 
158
        return $columns;
159
    }
160
 
161
    /**
162
     * Return list of all available filters
163
     *
164
     * @return filter[]
165
     */
166
    protected function get_all_filters(): array {
167
        $commentalias = $this->get_table_alias('comments');
168
 
169
        // Content.
170
        $filters[] = (new filter(
171
            text::class,
172
            'content',
173
            new lang_string('content'),
174
            $this->get_entity_name(),
1441 ariadna 175
            "{$commentalias}.content"
1 efrain 176
        ))
177
            ->add_joins($this->get_joins());
178
 
179
        // Time created.
180
        $filters[] = (new filter(
181
            date::class,
182
            'timecreated',
183
            new lang_string('timecreated', 'core_reportbuilder'),
184
            $this->get_entity_name(),
185
            "{$commentalias}.timecreated"
186
        ))
187
            ->add_joins($this->get_joins())
188
            ->set_limited_operators([
189
                date::DATE_ANY,
190
                date::DATE_RANGE,
191
                date::DATE_LAST,
192
                date::DATE_CURRENT,
193
            ]);
194
 
195
        return $filters;
196
    }
197
 
198
    /**
199
     * Return syntax for joining on the context table
200
     *
201
     * @return string
202
     */
203
    public function get_context_join(): string {
204
        $commentalias = $this->get_table_alias('comments');
205
        $contextalias = $this->get_table_alias('context');
206
 
207
        return "LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$commentalias}.contextid";
208
    }
209
}