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_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
        $postalias = $this->get_table_alias('post');
93
 
94
        // Title.
95
        $columns[] = (new column(
96
            'title',
97
            new lang_string('entrytitle', 'core_blog'),
98
            $this->get_entity_name()
99
        ))
100
            ->add_joins($this->get_joins())
101
            ->set_type(column::TYPE_TEXT)
102
            ->add_fields("{$postalias}.subject")
103
            ->set_is_sortable(true);
104
 
105
        // Title with link.
106
        $columns[] = (new column(
107
            'titlewithlink',
108
            new lang_string('entrytitlewithlink', 'core_blog'),
109
            $this->get_entity_name()
110
        ))
111
            ->add_joins($this->get_joins())
112
            ->set_type(column::TYPE_TEXT)
113
            ->add_fields("{$postalias}.subject, {$postalias}.id")
114
            ->set_is_sortable(true)
115
            ->add_callback(static function(?string $subject, stdClass $post): string {
116
                if ($subject === null) {
117
                    return '';
118
                }
119
                return html_writer::link(new moodle_url('/blog/index.php', ['entryid' => $post->id]), $subject);
120
            });
121
 
122
        // Body.
123
        $columns[] = (new column(
124
            'body',
125
            new lang_string('entrybody', 'core_blog'),
126
            $this->get_entity_name()
127
        ))
128
            ->add_joins($this->get_joins())
129
            ->set_type(column::TYPE_LONGTEXT)
1441 ariadna 130
            ->add_fields("{$postalias}.summary, {$postalias}.summaryformat, {$postalias}.id")
131
            ->set_is_sortable(true)
1 efrain 132
            ->add_callback(static function(?string $summary, stdClass $post): string {
133
                global $CFG;
134
                require_once("{$CFG->libdir}/filelib.php");
135
 
136
                if ($summary === null) {
137
                    return '';
138
                }
139
 
140
                // All blog files are stored in system context.
141
                $context = context_system::instance();
142
                $summary = file_rewrite_pluginfile_urls($summary, 'pluginfile.php', $context->id, 'blog', 'post', $post->id);
143
 
144
                return format_text($summary, $post->summaryformat, ['context' => $context->id]);
145
            });
146
 
147
        // Attachment.
148
        $columns[] = (new column(
149
            'attachment',
150
            new lang_string('attachment', 'core_repository'),
151
            $this->get_entity_name()
152
        ))
153
            ->add_joins($this->get_joins())
154
            ->set_type(column::TYPE_BOOLEAN)
155
            ->add_fields("{$postalias}.attachment, {$postalias}.id")
156
            ->add_callback(static function(?bool $attachment, stdClass $post): string {
157
                global $CFG, $PAGE;
158
                require_once("{$CFG->dirroot}/blog/locallib.php");
159
 
160
                if (!$attachment) {
161
                    return '';
162
                }
163
 
164
                $renderer = $PAGE->get_renderer('core_blog');
165
                $attachments = '';
166
 
167
                // Loop over attached files, use blog renderer to generate appropriate content.
168
                $files = get_file_storage()->get_area_files(context_system::instance()->id, 'blog', 'attachment', $post->id,
169
                    'filename', false);
170
                foreach ($files as $file) {
171
                    $attachments .= $renderer->render(new blog_entry_attachment($file, $post->id));
172
                }
173
 
174
                return $attachments;
175
            })
176
            ->set_disabled_aggregation_all();
177
 
178
        // Publish state.
179
        $columns[] = (new column(
180
            'publishstate',
181
            new lang_string('published', 'core_blog'),
182
            $this->get_entity_name()
183
        ))
184
            ->add_joins($this->get_joins())
185
            ->set_type(column::TYPE_TEXT)
186
            ->add_field("{$postalias}.publishstate")
187
            ->set_is_sortable(true)
188
            ->add_callback(static function(?string $publishstate): string {
189
                $states = [
190
                    'draft' => new lang_string('publishtodraft', 'core_blog'),
191
                    'site' => new lang_string('publishtosite', 'core_blog'),
192
                    'public' => new lang_string('publishtoworld', 'core_blog'),
193
                ];
194
 
195
                if ($publishstate === null || !array_key_exists($publishstate, $states)) {
196
                    return (string) $publishstate;
197
                }
198
 
199
                return (string) $states[$publishstate];
200
            });
201
 
202
        // Time created.
203
        $columns[] = (new column(
204
            'timecreated',
205
            new lang_string('timecreated', 'core_reportbuilder'),
206
            $this->get_entity_name()
207
        ))
208
            ->add_joins($this->get_joins())
209
            ->set_type(column::TYPE_TIMESTAMP)
210
            ->add_fields("{$postalias}.created")
211
            ->set_is_sortable(true)
212
            ->add_callback([format::class, 'userdate']);
213
 
214
        // Time modified.
215
        $columns[] = (new column(
216
            'timemodified',
217
            new lang_string('timemodified', 'core_reportbuilder'),
218
            $this->get_entity_name()
219
        ))
220
            ->add_joins($this->get_joins())
221
            ->set_type(column::TYPE_TIMESTAMP)
222
            ->add_fields("{$postalias}.lastmodified")
223
            ->set_is_sortable(true)
224
            ->add_callback([format::class, 'userdate']);
225
 
226
        return $columns;
227
    }
228
 
229
    /**
230
     * Return list of all available filters
231
     *
232
     * @return filter[]
233
     */
234
    protected function get_all_filters(): array {
235
        global $DB;
236
 
237
        $postalias = $this->get_table_alias('post');
238
 
239
        // Title.
240
        $filters[] = (new filter(
241
            text::class,
242
            'title',
243
            new lang_string('entrytitle', 'core_blog'),
244
            $this->get_entity_name(),
245
            "{$postalias}.subject"
246
        ))
247
            ->add_joins($this->get_joins());
248
 
249
        // Body.
250
        $filters[] = (new filter(
251
            text::class,
252
            'body',
253
            new lang_string('entrybody', 'core_blog'),
254
            $this->get_entity_name(),
1441 ariadna 255
            "{$postalias}.summary"
1 efrain 256
        ))
257
            ->add_joins($this->get_joins());
258
 
259
        // Attachment.
260
        $filters[] = (new filter(
261
            boolean_select::class,
262
            'attachment',
263
            new lang_string('attachment', 'core_repository'),
264
            $this->get_entity_name(),
265
            $DB->sql_cast_char2int("{$postalias}.attachment")
266
        ))
267
            ->add_joins($this->get_joins());
268
 
269
        // Publish state.
270
        $filters[] = (new filter(
271
            select::class,
272
            'publishstate',
273
            new lang_string('published', 'core_blog'),
274
            $this->get_entity_name(),
275
            "{$postalias}.publishstate"
276
        ))
277
            ->add_joins($this->get_joins())
278
            ->set_options_callback(static function(): array {
279
                $states = [
280
                    'draft' => new lang_string('publishtodraft', 'core_blog'),
281
                    'site' => new lang_string('publishtosite', 'core_blog'),
282
                    'public' => new lang_string('publishtoworld', 'core_blog'),
283
                ];
284
 
285
                core_collator::asort($states);
286
                return $states;
287
            });
288
 
289
        // Time created.
290
        $filters[] = (new filter(
291
            date::class,
292
            'timecreated',
293
            new lang_string('timecreated', 'core_reportbuilder'),
294
            $this->get_entity_name(),
295
            "{$postalias}.created"
296
        ))
297
            ->add_joins($this->get_joins())
298
            ->set_limited_operators([
299
                date::DATE_ANY,
300
                date::DATE_CURRENT,
301
                date::DATE_LAST,
302
                date::DATE_RANGE,
303
            ]);
304
 
305
        // Time modified.
306
        $filters[] = (new filter(
307
            date::class,
308
            'timemodified',
309
            new lang_string('timemodified', 'core_reportbuilder'),
310
            $this->get_entity_name(),
311
            "{$postalias}.lastmodified"
312
        ))
313
            ->add_joins($this->get_joins())
314
            ->set_limited_operators([
315
                date::DATE_ANY,
316
                date::DATE_CURRENT,
317
                date::DATE_LAST,
318
                date::DATE_RANGE,
319
            ]);
320
 
321
        return $filters;
322
    }
323
 
324
    /**
325
     * Return joins necessary for retrieving tags
326
     *
327
     * @return string[]
328
     */
329
    public function get_tag_joins(): array {
330
        return $this->get_tag_joins_for_entity('core', 'post', $this->get_table_alias('post') . '.id');
331
    }
332
}