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
 
1441 ariadna 19
use core\{clock, di};
1 efrain 20
use core_reportbuilder\manager;
21
use core_reportbuilder\local\helpers\report as helper;
22
use core_reportbuilder\local\helpers\schedule as schedule_helper;
23
use core_reportbuilder\local\models\column;
24
use core_reportbuilder\local\models\filter;
25
use core_reportbuilder\local\models\report;
26
use core_reportbuilder\local\models\schedule;
27
use core_reportbuilder\local\audiences\base as audience_base;
28
 
29
/**
30
 * Report builder test generator
31
 *
32
 * @package     core_reportbuilder
33
 * @copyright   2021 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class core_reportbuilder_generator extends component_generator_base {
37
 
38
    /**
39
     * Create report
40
     *
41
     * @param array|stdClass $record
42
     * @return report
43
     * @throws coding_exception
44
     */
45
    public function create_report($record): report {
46
        $record = (array) $record;
47
 
48
        if (!array_key_exists('name', $record)) {
49
            throw new coding_exception('Record must contain \'name\' property');
50
        }
51
        if (!array_key_exists('source', $record)) {
52
            throw new coding_exception('Record must contain \'source\' property');
53
        }
54
 
55
        // Report tags.
56
        $tags = $record['tags'] ?? '';
57
        if (!is_array($tags)) {
58
            $record['tags'] = preg_split('/\s*,\s*/', $tags, -1, PREG_SPLIT_NO_EMPTY);
59
        }
60
 
61
        // Include default setup unless specifically disabled in passed record.
62
        $default = (bool) ($record['default'] ?? true);
63
 
1441 ariadna 64
        // Report custom fields.
65
        \core_reportbuilder\customfield\report_handler::create()->instance_form_before_set_data((object)$record);
66
 
1 efrain 67
        // If setting up default report, purge caches to ensure any default attributes are always loaded in tests.
68
        $report = helper::create_report((object) $record, $default);
69
        if ($default) {
70
            manager::reset_caches();
71
        }
72
 
73
        return $report;
74
    }
75
 
76
    /**
77
     * Create report column
78
     *
79
     * @param array|stdClass $record
80
     * @return column
81
     * @throws coding_exception
82
     */
83
    public function create_column($record): column {
84
        $record = (array) $record;
85
 
86
        if (!array_key_exists('reportid', $record)) {
87
            throw new coding_exception('Record must contain \'reportid\' property');
88
        }
89
        if (!array_key_exists('uniqueidentifier', $record)) {
90
            throw new coding_exception('Record must contain \'uniqueidentifier\' property');
91
        }
92
 
93
        $column = helper::add_report_column($record['reportid'], $record['uniqueidentifier']);
94
 
95
        // Update additional record properties.
96
        unset($record['reportid'], $record['uniqueidentifier']);
97
        if ($properties = column::properties_filter((object) $record)) {
98
            $column->set_many($properties)->update();
99
        }
100
 
101
        return $column;
102
    }
103
 
104
    /**
105
     * Create report filter
106
     *
107
     * @param array|stdClass $record
108
     * @return filter
109
     * @throws coding_exception
110
     */
111
    public function create_filter($record): filter {
112
        $record = (array) $record;
113
 
114
        if (!array_key_exists('reportid', $record)) {
115
            throw new coding_exception('Record must contain \'reportid\' property');
116
        }
117
        if (!array_key_exists('uniqueidentifier', $record)) {
118
            throw new coding_exception('Record must contain \'uniqueidentifier\' property');
119
        }
120
 
121
        $filter = helper::add_report_filter($record['reportid'], $record['uniqueidentifier']);
122
 
123
        // Update additional record properties.
124
        unset($record['reportid'], $record['uniqueidentifier']);
125
        if ($properties = filter::properties_filter((object) $record)) {
126
            $filter->set_many($properties)->update();
127
        }
128
 
129
        return $filter;
130
    }
131
 
132
    /**
133
     * Create report condition
134
     *
135
     * @param array|stdClass $record
136
     * @return filter
137
     * @throws coding_exception
138
     */
139
    public function create_condition($record): filter {
140
        $record = (array) $record;
141
 
142
        if (!array_key_exists('reportid', $record)) {
143
            throw new coding_exception('Record must contain \'reportid\' property');
144
        }
145
        if (!array_key_exists('uniqueidentifier', $record)) {
146
            throw new coding_exception('Record must contain \'uniqueidentifier\' property');
147
        }
148
 
149
        $condition = helper::add_report_condition($record['reportid'], $record['uniqueidentifier']);
150
 
151
        // Update additional record properties.
152
        unset($record['reportid'], $record['uniqueidentifier']);
153
        if ($properties = filter::properties_filter((object) $record)) {
154
            $condition->set_many($properties)->update();
155
        }
156
 
157
        return $condition;
158
    }
159
 
160
    /**
161
     * Create report audience
162
     *
163
     * @param array|stdClass $record
164
     * @return audience_base
165
     * @throws coding_exception
166
     */
167
    public function create_audience($record): audience_base {
168
        $record = (array) $record;
169
 
170
        // Required properties.
171
        if (!array_key_exists('reportid', $record)) {
172
            throw new coding_exception('Record must contain \'reportid\' property');
173
        }
174
        if (!array_key_exists('configdata', $record)) {
175
            throw new coding_exception('Record must contain \'configdata\' property');
176
        }
177
 
178
        // Default to all users if not specified, for convenience.
179
        /** @var audience_base $classname */
180
        $classname = $record['classname'] ??
181
            \core_reportbuilder\reportbuilder\audience\allusers::class;
182
 
183
        return ($classname)::create($record['reportid'], $record['configdata']);
184
    }
185
 
186
    /**
187
     * Create report schedule
188
     *
189
     * @param array|stdClass $record
190
     * @return schedule
191
     * @throws coding_exception
192
     */
193
    public function create_schedule($record): schedule {
194
        $record = (array) $record;
195
 
196
        // Required properties.
197
        if (!array_key_exists('reportid', $record)) {
198
            throw new coding_exception('Record must contain \'reportid\' property');
199
        }
200
        if (!array_key_exists('name', $record)) {
201
            throw new coding_exception('Record must contain \'name\' property');
202
        }
203
 
204
        // Optional properties.
205
        if (!array_key_exists('format', $record)) {
206
            $record['format'] = 'csv';
207
        }
208
        if (!array_key_exists('subject', $record)) {
209
            $record['subject'] = $record['name'] . ' subject';
210
        }
211
        if (!array_key_exists('message', $record)) {
212
            $record['message'] = $record['name'] . ' message';
213
        }
214
        if (!array_key_exists('timescheduled', $record)) {
1441 ariadna 215
            $record['timescheduled'] = usergetmidnight(di::get(clock::class)->time() + DAYSECS);
1 efrain 216
        }
217
 
1441 ariadna 218
        return schedule_helper::create_schedule((object) $record);
1 efrain 219
    }
220
}