Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 46... Línea 46...
46
 
46
 
47
    /** @var array $activeconditions */
47
    /** @var array $activeconditions */
Línea 48... Línea 48...
48
    private $activeconditions;
48
    private $activeconditions;
49
 
-
 
50
    /**
-
 
51
     * Add columns from the given entity name to be available to use in a custom report
-
 
52
     *
-
 
53
     * Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
-
 
54
     *
-
 
55
     * @param string $entityname
-
 
56
     * @param string[] $include Include only these columns, if omitted then include all
-
 
57
     * @param string[] $exclude Exclude these columns, if omitted then exclude none
-
 
58
     * @throws coding_exception If both $include and $exclude are non-empty
-
 
59
     */
-
 
60
    final protected function add_columns_from_entity(string $entityname, array $include = [], array $exclude = []): void {
-
 
61
        if (!empty($include) && !empty($exclude)) {
-
 
62
            throw new coding_exception('Cannot specify columns to include and exclude simultaneously');
-
 
63
        }
-
 
64
 
-
 
65
        $entity = $this->get_entity($entityname);
-
 
66
 
-
 
67
        // Retrieve filtered columns from entity, respecting given $include/$exclude parameters.
-
 
68
        $columns = array_filter($entity->get_columns(), function(column $column) use ($include, $exclude): bool {
-
 
69
            if (!empty($include)) {
-
 
70
                return $this->report_element_search($column->get_name(), $include);
-
 
71
            }
-
 
72
 
-
 
73
            if (!empty($exclude)) {
-
 
74
                return !$this->report_element_search($column->get_name(), $exclude);
-
 
75
            }
-
 
76
 
-
 
77
            return true;
-
 
78
        });
-
 
79
 
-
 
80
        foreach ($columns as $column) {
-
 
81
            $this->add_column($column);
-
 
82
        }
-
 
83
    }
-
 
84
 
49
 
85
    /**
50
    /**
86
     * Add default datasource columns to the report
51
     * Add default datasource columns to the report
87
     *
52
     *
88
     * Uses column data returned by the source {@see get_default_columns} and {@see get_default_column_sorting} methods
53
     * Uses column data returned by the source {@see get_default_columns} and {@see get_default_column_sorting} methods
Línea 167... Línea 132...
167
                if ($instance->get_is_deprecated()) {
132
                if ($instance->get_is_deprecated()) {
168
                    debugging("The column '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
133
                    debugging("The column '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
169
                        " {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
134
                        " {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
170
                }
135
                }
Línea -... Línea 136...
-
 
136
 
-
 
137
                $columnaggregation = $column->get('aggregation');
171
 
138
 
172
                // We should clone the report column to ensure if it's added twice to a report, each operates independently.
139
                // We should clone the report column to ensure if it's added twice to a report, each operates independently.
173
                $this->activecolumns['values'][] = clone $instance
140
                $this->activecolumns['values'][] = clone $instance
174
                    ->set_index($index)
141
                    ->set_index($index)
175
                    ->set_persistent($column)
142
                    ->set_persistent($column)
176
                    ->set_aggregation($column->get('aggregation'));
143
                    ->set_aggregation($columnaggregation, $instance->get_aggregation_options($columnaggregation));
177
            }
144
            }
Línea 178... Línea 145...
178
        }
145
        }
179
 
146
 
Línea 180... Línea 147...
180
        return $this->activecolumns['values'];
147
        return $this->activecolumns['values'];
181
    }
-
 
182
 
-
 
183
    /**
-
 
184
     * Add filters from the given entity name to be available to use in a custom report
-
 
185
     *
-
 
186
     * Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
-
 
187
     *
-
 
188
     * @param string $entityname
-
 
189
     * @param string[] $include Include only these filters, if omitted then include all
-
 
190
     * @param string[] $exclude Exclude these filters, if omitted then exclude none
-
 
191
     * @throws coding_exception If both $include and $exclude are non-empty
-
 
192
     */
-
 
193
    final protected function add_filters_from_entity(string $entityname, array $include = [], array $exclude = []): void {
-
 
194
        if (!empty($include) && !empty($exclude)) {
-
 
195
            throw new coding_exception('Cannot specify filters to include and exclude simultaneously');
-
 
196
        }
-
 
197
 
-
 
198
        $entity = $this->get_entity($entityname);
-
 
199
 
-
 
200
        // Retrieve filtered filters from entity, respecting given $include/$exclude parameters.
-
 
201
        $filters = array_filter($entity->get_filters(), function(filter $filter) use ($include, $exclude): bool {
-
 
202
            if (!empty($include)) {
-
 
203
                return $this->report_element_search($filter->get_name(), $include);
-
 
204
            }
-
 
205
 
-
 
206
            if (!empty($exclude)) {
-
 
207
                return !$this->report_element_search($filter->get_name(), $exclude);
-
 
208
            }
-
 
209
 
-
 
210
            return true;
-
 
211
        });
-
 
212
 
-
 
213
        foreach ($filters as $filter) {
-
 
214
            $this->add_filter($filter);
-
 
215
        }
-
 
216
    }
148
    }
217
 
149
 
218
    /**
150
    /**
219
     * Add default datasource filters to the report
151
     * Add default datasource filters to the report
220
     *
152
     *
Línea 262... Línea 194...
262
                if ($instance->get_is_deprecated()) {
194
                if ($instance->get_is_deprecated()) {
263
                    debugging("The filter '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
195
                    debugging("The filter '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
264
                        " {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
196
                        " {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
265
                }
197
                }
Línea 266... Línea 198...
266
 
198
 
267
                $this->activefilters['values'][$instance->get_unique_identifier()] =
-
 
268
                    $instance->set_persistent($filter);
199
                $this->activefilters['values'][$instance->get_unique_identifier()] = $instance->set_persistent($filter);
269
            }
200
            }
Línea 270... Línea 201...
270
        }
201
        }
271
 
202
 
Línea 345... Línea 276...
345
 
276
 
346
    /**
277
    /**
347
     * Override parent method, returning only those conditions specifically added to the custom report (rather than all that are
278
     * Override parent method, returning only those conditions specifically added to the custom report (rather than all that are
348
     * available)
279
     * available)
-
 
280
     *
349
     *
281
     * @param bool $checkavailable
350
     * @return filter[]
282
     * @return filter[]
351
     */
283
     */
352
    public function get_active_conditions(): array {
284
    public function get_active_conditions(bool $checkavailable = true): array {
Línea 353... Línea 285...
353
        $reportid = $this->get_report_persistent()->get('id');
285
        $reportid = $this->get_report_persistent()->get('id');
354
 
286
 
355
        // Determine whether we already retrieved the conditions since the report was last modified.
287
        // Determine whether we already retrieved the conditions since the report was last modified.
Línea 362... Línea 294...
362
 
294
 
363
        $activeconditions = filter_model::get_condition_records($reportid, 'filterorder');
295
        $activeconditions = filter_model::get_condition_records($reportid, 'filterorder');
364
        foreach ($activeconditions as $condition) {
296
        foreach ($activeconditions as $condition) {
Línea 365... Línea 297...
365
            $instance = $this->get_condition($condition->get('uniqueidentifier'));
297
            $instance = $this->get_condition($condition->get('uniqueidentifier'));
366
 
298
 
367
            // Ensure the condition is still present and available.
299
            // Ensure the condition is still present and available (if checking available status).
368
            if ($instance !== null && $instance->get_is_available()) {
300
            if ($instance !== null && (!$checkavailable || $instance->get_is_available())) {
369
                if ($instance->get_is_deprecated()) {
301
                if ($instance->get_is_deprecated()) {
370
                    debugging("The condition '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
302
                    debugging("The condition '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
Línea 371... Línea 303...
371
                        " {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
303
                        " {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
372
                }
-
 
373
 
304
                }
374
                $this->activeconditions['values'][$instance->get_unique_identifier()] =
305
 
Línea 375... Línea 306...
375
                    $instance->set_persistent($condition);
306
                $this->activeconditions['values'][$instance->get_unique_identifier()] = $instance->set_persistent($condition);
376
            }
307
            }
Línea 398... Línea 329...
398
        $this->add_conditions_from_entity($entityname, $limitconditions);
329
        $this->add_conditions_from_entity($entityname, $limitconditions);
399
    }
330
    }
Línea 400... Línea 331...
400
 
331
 
401
    /**
332
    /**
-
 
333
     * Adds all columns/filters/conditions from all the entities added to the report at once
-
 
334
     *
402
     * Adds all columns/filters/conditions from all the entities added to the report at once
335
     * @param string[] $entitynames If specified, then only these entity elements are added (otherwise all)
403
     */
336
     */
404
    final protected function add_all_from_entities(): void {
337
    final protected function add_all_from_entities(array $entitynames = []): void {
-
 
338
        foreach ($this->get_entities() as $entity) {
-
 
339
            $entityname = $entity->get_entity_name();
-
 
340
            if (!empty($entitynames) && array_search($entityname, $entitynames) === false) {
-
 
341
                continue;
405
        foreach ($this->get_entities() as $entity) {
342
            }
406
            $this->add_all_from_entity($entity->get_entity_name());
343
            $this->add_all_from_entity($entityname);
407
        }
344
        }
Línea 408... Línea 345...
408
    }
345
    }
409
 
346
 
Línea 413... Línea 350...
413
     * @param int $reportid
350
     * @param int $reportid
414
     */
351
     */
415
    final public static function report_elements_modified(int $reportid): void {
352
    final public static function report_elements_modified(int $reportid): void {
416
        self::$elementsmodified[$reportid] = microtime(true);
353
        self::$elementsmodified[$reportid] = microtime(true);
417
    }
354
    }
418
 
-
 
419
    /**
-
 
420
     * Search for given element within list of search items, supporting '*' wildcards
-
 
421
     *
-
 
422
     * @param string $element
-
 
423
     * @param string[] $search
-
 
424
     * @return bool
-
 
425
     */
-
 
426
    private function report_element_search(string $element, array $search): bool {
-
 
427
        foreach ($search as $item) {
-
 
428
            // Simple matching.
-
 
429
            if ($element === $item) {
-
 
430
                return true;
-
 
431
            }
-
 
432
 
-
 
433
            // Wildcard matching.
-
 
434
            if (strpos($item, '*') !== false) {
-
 
435
                $pattern = '/^' . str_replace('\*', '.*', preg_quote($item)) . '$/';
-
 
436
                return (bool) preg_match($pattern, $element);
-
 
437
            }
-
 
438
        }
-
 
439
 
-
 
440
        return false;
-
 
441
    }
-
 
442
}
355
}