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_notes\reportbuilder\local\entities;
20
 
21
use lang_string;
22
use stdClass;
23
use core_reportbuilder\local\entities\base;
24
use core_reportbuilder\local\filters\{date, select, text};
25
use core_reportbuilder\local\helpers\format;
26
use core_reportbuilder\local\report\{column, filter};
27
 
28
defined('MOODLE_INTERNAL') || die;
29
 
30
global $CFG;
31
require_once("{$CFG->dirroot}/notes/lib.php");
32
 
33
/**
34
 * Note entity
35
 *
36
 * @package     core_notes
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 note 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
        ];
51
    }
52
 
53
    /**
54
     * The default title for this entity
55
     *
56
     * @return lang_string
57
     */
58
    protected function get_default_entity_title(): lang_string {
59
        return new lang_string('note', 'core_notes');
60
    }
61
 
62
    /**
63
     * Initialise the entity
64
     *
65
     * @return base
66
     */
67
    public function initialise(): base {
68
        $columns = $this->get_all_columns();
69
        foreach ($columns as $column) {
70
            $this->add_column($column);
71
        }
72
 
73
        // All the filters defined by the entity can also be used as conditions.
74
        $filters = $this->get_all_filters();
75
        foreach ($filters as $filter) {
76
            $this
77
                ->add_filter($filter)
78
                ->add_condition($filter);
79
        }
80
 
81
        return $this;
82
    }
83
 
84
    /**
85
     * Returns list of all available columns
86
     *
87
     * @return column[]
88
     */
89
    protected function get_all_columns(): array {
90
        $postalias = $this->get_table_alias('post');
91
 
92
        // Content.
93
        $columns[] = (new column(
94
            'content',
95
            new lang_string('content', 'core_notes'),
96
            $this->get_entity_name()
97
        ))
98
            ->add_joins($this->get_joins())
99
            ->set_type(column::TYPE_LONGTEXT)
1441 ariadna 100
            ->add_fields("{$postalias}.content, {$postalias}.format")
101
            ->set_is_sortable(true)
1 efrain 102
            ->add_callback(static function(?string $content, stdClass $note): string {
103
                if ($content === null) {
104
                    return '';
105
                }
106
                return format_text($content, $note->format);
107
            });
108
 
109
        // Publish state.
110
        $columns[] = (new column(
111
            'publishstate',
112
            new lang_string('publishstate', 'core_notes'),
113
            $this->get_entity_name()
114
        ))
115
            ->add_joins($this->get_joins())
116
            ->set_type(column::TYPE_TEXT)
117
            ->add_fields("{$postalias}.publishstate")
118
            ->set_is_sortable(true)
119
            ->add_callback(static function(string $publishstate): string {
120
                $states = [
121
                    NOTES_STATE_SITE => new lang_string('sitenotes', 'core_notes'),
122
                    NOTES_STATE_PUBLIC => new lang_string('coursenotes', 'core_notes'),
123
                    NOTES_STATE_DRAFT => new lang_string('personalnotes', 'core_notes'),
124
                ];
125
 
126
                return (string) ($states[$publishstate] ?? $publishstate);
127
            });
128
 
129
        // Time created.
130
        $columns[] = (new column(
131
            'timecreated',
132
            new lang_string('timecreated', 'core_reportbuilder'),
133
            $this->get_entity_name()
134
        ))
135
            ->add_joins($this->get_joins())
136
            ->set_type(column::TYPE_TIMESTAMP)
137
            ->add_fields("{$postalias}.created")
138
            ->set_is_sortable(true)
139
            ->add_callback([format::class, 'userdate']);
140
 
141
        // Time modified.
142
        $columns[] = (new column(
143
            'timemodified',
144
            new lang_string('timemodified', 'core_reportbuilder'),
145
            $this->get_entity_name()
146
        ))
147
            ->add_joins($this->get_joins())
148
            ->set_type(column::TYPE_TIMESTAMP)
149
            ->add_fields("{$postalias}.lastmodified")
150
            ->set_is_sortable(true)
151
            ->add_callback([format::class, 'userdate']);
152
 
153
        return $columns;
154
    }
155
 
156
    /**
157
     * Return list of all available filters
158
     *
159
     * @return filter[]
160
     */
161
    protected function get_all_filters(): array {
162
        $postalias = $this->get_table_alias('post');
163
 
164
        // Content.
165
        $filters[] = (new filter(
166
            text::class,
167
            'content',
168
            new lang_string('content', 'core_notes'),
169
            $this->get_entity_name(),
1441 ariadna 170
            "{$postalias}.content"
1 efrain 171
        ))
172
            ->add_joins($this->get_joins());
173
 
174
        // Publish state.
175
        $filters[] = (new filter(
176
            select::class,
177
            'publishstate',
178
            new lang_string('publishstate', 'core_notes'),
179
            $this->get_entity_name(),
180
            "{$postalias}.publishstate"
181
        ))
182
            ->add_joins($this->get_joins())
183
            ->set_options([
184
                NOTES_STATE_SITE => new lang_string('sitenotes', 'core_notes'),
185
                NOTES_STATE_PUBLIC => new lang_string('coursenotes', 'core_notes'),
186
                NOTES_STATE_DRAFT => new lang_string('personalnotes', 'core_notes'),
187
            ]);
188
 
189
        // Time created.
190
        $filters[] = (new filter(
191
            date::class,
192
            'timecreated',
193
            new lang_string('timecreated', 'core_reportbuilder'),
194
            $this->get_entity_name(),
195
            "{$postalias}.created"
196
        ))
197
            ->add_joins($this->get_joins())
198
            ->set_limited_operators([
199
                date::DATE_ANY,
200
                date::DATE_CURRENT,
201
                date::DATE_LAST,
202
                date::DATE_RANGE,
203
            ]);
204
 
205
        // Time modified.
206
        $filters[] = (new filter(
207
            date::class,
208
            'timemodified',
209
            new lang_string('timemodified', 'core_reportbuilder'),
210
            $this->get_entity_name(),
211
            "{$postalias}.lastmodified"
212
        ))
213
            ->add_joins($this->get_joins())
214
            ->set_limited_operators([
215
                date::DATE_ANY,
216
                date::DATE_CURRENT,
217
                date::DATE_LAST,
218
                date::DATE_RANGE,
219
            ]);
220
 
221
        return $filters;
222
    }
223
}