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