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
declare(strict_types=1);
18
 
19
namespace core_blog\reportbuilder\local\entities;
20
 
21
use blog_entry_attachment;
22
use context_system;
23
use core_collator;
24
use html_writer;
25
use lang_string;
26
use moodle_url;
27
use stdClass;
28
use core_reportbuilder\local\entities\base;
29
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
30
use core_reportbuilder\local\helpers\format;
31
use core_reportbuilder\local\report\{column, filter};
32
 
33
/**
34
 * Blog entity
35
 *
36
 * @package     core_blog
37
 * @copyright   2022 Paul Holden <paulh@moodle.com>
38
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class blog extends base {
41
 
42
    /**
43
     * Database tables that this entity uses
44
     *
45
     * @return string[]
46
     */
47
    protected function get_default_tables(): array {
48
        return [
49
            'post',
50
            'tag_instance',
51
            'tag',
52
        ];
53
    }
54
 
55
    /**
56
     * The default title for this entity
57
     *
58
     * @return lang_string
59
     */
60
    protected function get_default_entity_title(): lang_string {
61
        return new lang_string('blog', 'core_blog');
62
    }
63
 
64
    /**
65
     * Initialise the entity
66
     *
67
     * @return base
68
     */
69
    public function initialise(): base {
70
        $columns = $this->get_all_columns();
71
        foreach ($columns as $column) {
72
            $this->add_column($column);
73
        }
74
 
75
        // All the filters defined by the entity can also be used as conditions.
76
        $filters = $this->get_all_filters();
77
        foreach ($filters as $filter) {
78
            $this
79
                ->add_filter($filter)
80
                ->add_condition($filter);
81
        }
82
 
83
        return $this;
84
    }
85
 
86
    /**
87
     * Returns list of all available columns
88
     *
89
     * @return column[]
90
     */
91
    protected function get_all_columns(): array {
92
        global $DB;
93
 
94
        $postalias = $this->get_table_alias('post');
95
 
96
        // Title.
97
        $columns[] = (new column(
98
            'title',
99
            new lang_string('entrytitle', 'core_blog'),
100
            $this->get_entity_name()
101
        ))
102
            ->add_joins($this->get_joins())
103
            ->set_type(column::TYPE_TEXT)
104
            ->add_fields("{$postalias}.subject")
105
            ->set_is_sortable(true);
106
 
107
        // Title with link.
108
        $columns[] = (new column(
109
            'titlewithlink',
110
            new lang_string('entrytitlewithlink', 'core_blog'),
111
            $this->get_entity_name()
112
        ))
113
            ->add_joins($this->get_joins())
114
            ->set_type(column::TYPE_TEXT)
115
            ->add_fields("{$postalias}.subject, {$postalias}.id")
116
            ->set_is_sortable(true)
117
            ->add_callback(static function(?string $subject, stdClass $post): string {
118
                if ($subject === null) {
119
                    return '';
120
                }
121
                return html_writer::link(new moodle_url('/blog/index.php', ['entryid' => $post->id]), $subject);
122
            });
123
 
124
        // Body.
125
        $summaryfieldsql = "{$postalias}.summary";
126
        if ($DB->get_dbfamily() === 'oracle') {
127
            $summaryfieldsql = $DB->sql_order_by_text($summaryfieldsql, 1024);
128
        }
129
 
130
        $columns[] = (new column(
131
            'body',
132
            new lang_string('entrybody', 'core_blog'),
133
            $this->get_entity_name()
134
        ))
135
            ->add_joins($this->get_joins())
136
            ->set_type(column::TYPE_LONGTEXT)
137
            ->add_field($summaryfieldsql, 'summary')
138
            ->add_fields("{$postalias}.summaryformat, {$postalias}.id")
139
            ->add_callback(static function(?string $summary, stdClass $post): string {
140
                global $CFG;
141
                require_once("{$CFG->libdir}/filelib.php");
142
 
143
                if ($summary === null) {
144
                    return '';
145
                }
146
 
147
                // All blog files are stored in system context.
148
                $context = context_system::instance();
149
                $summary = file_rewrite_pluginfile_urls($summary, 'pluginfile.php', $context->id, 'blog', 'post', $post->id);
150
 
151
                return format_text($summary, $post->summaryformat, ['context' => $context->id]);
152
            });
153
 
154
        // Attachment.
155
        $columns[] = (new column(
156
            'attachment',
157
            new lang_string('attachment', 'core_repository'),
158
            $this->get_entity_name()
159
        ))
160
            ->add_joins($this->get_joins())
161
            ->set_type(column::TYPE_BOOLEAN)
162
            ->add_fields("{$postalias}.attachment, {$postalias}.id")
163
            ->add_callback(static function(?bool $attachment, stdClass $post): string {
164
                global $CFG, $PAGE;
165
                require_once("{$CFG->dirroot}/blog/locallib.php");
166
 
167
                if (!$attachment) {
168
                    return '';
169
                }
170
 
171
                $renderer = $PAGE->get_renderer('core_blog');
172
                $attachments = '';
173
 
174
                // Loop over attached files, use blog renderer to generate appropriate content.
175
                $files = get_file_storage()->get_area_files(context_system::instance()->id, 'blog', 'attachment', $post->id,
176
                    'filename', false);
177
                foreach ($files as $file) {
178
                    $attachments .= $renderer->render(new blog_entry_attachment($file, $post->id));
179
                }
180
 
181
                return $attachments;
182
            })
183
            ->set_disabled_aggregation_all();
184
 
185
        // Publish state.
186
        $columns[] = (new column(
187
            'publishstate',
188
            new lang_string('published', 'core_blog'),
189
            $this->get_entity_name()
190
        ))
191
            ->add_joins($this->get_joins())
192
            ->set_type(column::TYPE_TEXT)
193
            ->add_field("{$postalias}.publishstate")
194
            ->set_is_sortable(true)
195
            ->add_callback(static function(?string $publishstate): string {
196
                $states = [
197
                    'draft' => new lang_string('publishtodraft', 'core_blog'),
198
                    'site' => new lang_string('publishtosite', 'core_blog'),
199
                    'public' => new lang_string('publishtoworld', 'core_blog'),
200
                ];
201
 
202
                if ($publishstate === null || !array_key_exists($publishstate, $states)) {
203
                    return (string) $publishstate;
204
                }
205
 
206
                return (string) $states[$publishstate];
207
            });
208
 
209
        // Time created.
210
        $columns[] = (new column(
211
            'timecreated',
212
            new lang_string('timecreated', 'core_reportbuilder'),
213
            $this->get_entity_name()
214
        ))
215
            ->add_joins($this->get_joins())
216
            ->set_type(column::TYPE_TIMESTAMP)
217
            ->add_fields("{$postalias}.created")
218
            ->set_is_sortable(true)
219
            ->add_callback([format::class, 'userdate']);
220
 
221
        // Time modified.
222
        $columns[] = (new column(
223
            'timemodified',
224
            new lang_string('timemodified', 'core_reportbuilder'),
225
            $this->get_entity_name()
226
        ))
227
            ->add_joins($this->get_joins())
228
            ->set_type(column::TYPE_TIMESTAMP)
229
            ->add_fields("{$postalias}.lastmodified")
230
            ->set_is_sortable(true)
231
            ->add_callback([format::class, 'userdate']);
232
 
233
        return $columns;
234
    }
235
 
236
    /**
237
     * Return list of all available filters
238
     *
239
     * @return filter[]
240
     */
241
    protected function get_all_filters(): array {
242
        global $DB;
243
 
244
        $postalias = $this->get_table_alias('post');
245
 
246
        // Title.
247
        $filters[] = (new filter(
248
            text::class,
249
            'title',
250
            new lang_string('entrytitle', 'core_blog'),
251
            $this->get_entity_name(),
252
            "{$postalias}.subject"
253
        ))
254
            ->add_joins($this->get_joins());
255
 
256
        // Body.
257
        $filters[] = (new filter(
258
            text::class,
259
            'body',
260
            new lang_string('entrybody', 'core_blog'),
261
            $this->get_entity_name(),
262
            $DB->sql_cast_to_char("{$postalias}.summary")
263
        ))
264
            ->add_joins($this->get_joins());
265
 
266
        // Attachment.
267
        $filters[] = (new filter(
268
            boolean_select::class,
269
            'attachment',
270
            new lang_string('attachment', 'core_repository'),
271
            $this->get_entity_name(),
272
            $DB->sql_cast_char2int("{$postalias}.attachment")
273
        ))
274
            ->add_joins($this->get_joins());
275
 
276
        // Publish state.
277
        $filters[] = (new filter(
278
            select::class,
279
            'publishstate',
280
            new lang_string('published', 'core_blog'),
281
            $this->get_entity_name(),
282
            "{$postalias}.publishstate"
283
        ))
284
            ->add_joins($this->get_joins())
285
            ->set_options_callback(static function(): array {
286
                $states = [
287
                    'draft' => new lang_string('publishtodraft', 'core_blog'),
288
                    'site' => new lang_string('publishtosite', 'core_blog'),
289
                    'public' => new lang_string('publishtoworld', 'core_blog'),
290
                ];
291
 
292
                core_collator::asort($states);
293
                return $states;
294
            });
295
 
296
        // Time created.
297
        $filters[] = (new filter(
298
            date::class,
299
            'timecreated',
300
            new lang_string('timecreated', 'core_reportbuilder'),
301
            $this->get_entity_name(),
302
            "{$postalias}.created"
303
        ))
304
            ->add_joins($this->get_joins())
305
            ->set_limited_operators([
306
                date::DATE_ANY,
307
                date::DATE_CURRENT,
308
                date::DATE_LAST,
309
                date::DATE_RANGE,
310
            ]);
311
 
312
        // Time modified.
313
        $filters[] = (new filter(
314
            date::class,
315
            'timemodified',
316
            new lang_string('timemodified', 'core_reportbuilder'),
317
            $this->get_entity_name(),
318
            "{$postalias}.lastmodified"
319
        ))
320
            ->add_joins($this->get_joins())
321
            ->set_limited_operators([
322
                date::DATE_ANY,
323
                date::DATE_CURRENT,
324
                date::DATE_LAST,
325
                date::DATE_RANGE,
326
            ]);
327
 
328
        return $filters;
329
    }
330
 
331
    /**
332
     * Return joins necessary for retrieving tags
333
     *
334
     * @return string[]
335
     */
336
    public function get_tag_joins(): array {
337
        return $this->get_tag_joins_for_entity('core', 'post', $this->get_table_alias('post') . '.id');
338
    }
339
}