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_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
        global $DB;
91
 
92
        $postalias = $this->get_table_alias('post');
93
 
94
        // Content.
95
        $contentfieldsql = "{$postalias}.content";
96
        if ($DB->get_dbfamily() === 'oracle') {
97
            $contentfieldsql = $DB->sql_order_by_text($contentfieldsql, 1024);
98
        }
99
        $columns[] = (new column(
100
            'content',
101
            new lang_string('content', 'core_notes'),
102
            $this->get_entity_name()
103
        ))
104
            ->add_joins($this->get_joins())
105
            ->set_type(column::TYPE_LONGTEXT)
106
            ->add_field($contentfieldsql, 'content')
107
            ->add_field("{$postalias}.format")
108
            ->add_callback(static function(?string $content, stdClass $note): string {
109
                if ($content === null) {
110
                    return '';
111
                }
112
                return format_text($content, $note->format);
113
            });
114
 
115
        // Publish state.
116
        $columns[] = (new column(
117
            'publishstate',
118
            new lang_string('publishstate', 'core_notes'),
119
            $this->get_entity_name()
120
        ))
121
            ->add_joins($this->get_joins())
122
            ->set_type(column::TYPE_TEXT)
123
            ->add_fields("{$postalias}.publishstate")
124
            ->set_is_sortable(true)
125
            ->add_callback(static function(string $publishstate): string {
126
                $states = [
127
                    NOTES_STATE_SITE => new lang_string('sitenotes', 'core_notes'),
128
                    NOTES_STATE_PUBLIC => new lang_string('coursenotes', 'core_notes'),
129
                    NOTES_STATE_DRAFT => new lang_string('personalnotes', 'core_notes'),
130
                ];
131
 
132
                return (string) ($states[$publishstate] ?? $publishstate);
133
            });
134
 
135
        // Time created.
136
        $columns[] = (new column(
137
            'timecreated',
138
            new lang_string('timecreated', 'core_reportbuilder'),
139
            $this->get_entity_name()
140
        ))
141
            ->add_joins($this->get_joins())
142
            ->set_type(column::TYPE_TIMESTAMP)
143
            ->add_fields("{$postalias}.created")
144
            ->set_is_sortable(true)
145
            ->add_callback([format::class, 'userdate']);
146
 
147
        // Time modified.
148
        $columns[] = (new column(
149
            'timemodified',
150
            new lang_string('timemodified', 'core_reportbuilder'),
151
            $this->get_entity_name()
152
        ))
153
            ->add_joins($this->get_joins())
154
            ->set_type(column::TYPE_TIMESTAMP)
155
            ->add_fields("{$postalias}.lastmodified")
156
            ->set_is_sortable(true)
157
            ->add_callback([format::class, 'userdate']);
158
 
159
        return $columns;
160
    }
161
 
162
    /**
163
     * Return list of all available filters
164
     *
165
     * @return filter[]
166
     */
167
    protected function get_all_filters(): array {
168
        global $DB;
169
 
170
        $postalias = $this->get_table_alias('post');
171
 
172
        // Content.
173
        $filters[] = (new filter(
174
            text::class,
175
            'content',
176
            new lang_string('content', 'core_notes'),
177
            $this->get_entity_name(),
178
            $DB->sql_cast_to_char("{$postalias}.content")
179
        ))
180
            ->add_joins($this->get_joins());
181
 
182
        // Publish state.
183
        $filters[] = (new filter(
184
            select::class,
185
            'publishstate',
186
            new lang_string('publishstate', 'core_notes'),
187
            $this->get_entity_name(),
188
            "{$postalias}.publishstate"
189
        ))
190
            ->add_joins($this->get_joins())
191
            ->set_options([
192
                NOTES_STATE_SITE => new lang_string('sitenotes', 'core_notes'),
193
                NOTES_STATE_PUBLIC => new lang_string('coursenotes', 'core_notes'),
194
                NOTES_STATE_DRAFT => new lang_string('personalnotes', 'core_notes'),
195
            ]);
196
 
197
        // Time created.
198
        $filters[] = (new filter(
199
            date::class,
200
            'timecreated',
201
            new lang_string('timecreated', 'core_reportbuilder'),
202
            $this->get_entity_name(),
203
            "{$postalias}.created"
204
        ))
205
            ->add_joins($this->get_joins())
206
            ->set_limited_operators([
207
                date::DATE_ANY,
208
                date::DATE_CURRENT,
209
                date::DATE_LAST,
210
                date::DATE_RANGE,
211
            ]);
212
 
213
        // Time modified.
214
        $filters[] = (new filter(
215
            date::class,
216
            'timemodified',
217
            new lang_string('timemodified', 'core_reportbuilder'),
218
            $this->get_entity_name(),
219
            "{$postalias}.lastmodified"
220
        ))
221
            ->add_joins($this->get_joins())
222
            ->set_limited_operators([
223
                date::DATE_ANY,
224
                date::DATE_CURRENT,
225
                date::DATE_LAST,
226
                date::DATE_RANGE,
227
            ]);
228
 
229
        return $filters;
230
    }
231
}