Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 21... Línea 21...
21
use coding_exception;
21
use coding_exception;
22
use context;
22
use context;
23
use lang_string;
23
use lang_string;
24
use core_reportbuilder\local\entities\base as entity_base;
24
use core_reportbuilder\local\entities\base as entity_base;
25
use core_reportbuilder\local\filters\base as filter_base;
25
use core_reportbuilder\local\filters\base as filter_base;
26
use core_reportbuilder\local\helpers\database;
-
 
27
use core_reportbuilder\local\helpers\user_filter_manager;
26
use core_reportbuilder\local\helpers\{database, user_filter_manager};
28
use core_reportbuilder\local\models\report;
27
use core_reportbuilder\local\models\report;
-
 
28
use core_reportbuilder\output\report_action;
Línea 29... Línea 29...
29
 
29
 
30
/**
30
/**
31
 * Base class for all reports
31
 * Base class for all reports
32
 *
32
 *
Línea 85... Línea 85...
85
    private $downloadfilename = '';
85
    private $downloadfilename = '';
Línea 86... Línea 86...
86
 
86
 
87
    /** @var int Default paging size */
87
    /** @var int Default paging size */
Línea -... Línea 88...
-
 
88
    private $defaultperpage = self::DEFAULT_PAGESIZE;
-
 
89
 
-
 
90
    /** @var report_action $reportaction */
-
 
91
    private report_action|null $reportaction = null;
-
 
92
 
-
 
93
    /** @var string $reportinfocontainer */
88
    private $defaultperpage = self::DEFAULT_PAGESIZE;
94
    private string $reportinfocontainer = '';
89
 
95
 
Línea 90... Línea 96...
90
    /** @var array $attributes */
96
    /** @var array $attributes */
91
    private $attributes = [];
97
    private $attributes = [];
Línea 373... Línea 379...
373
 
379
 
374
        return $this->add_column($this->get_entity($entityname)->get_column($columnname));
380
        return $this->add_column($this->get_entity($entityname)->get_column($columnname));
Línea 375... Línea 381...
375
    }
381
    }
-
 
382
 
-
 
383
    /**
-
 
384
     * Add columns from the given entity name to be available to use in a custom report
-
 
385
     *
-
 
386
     * Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
-
 
387
     *
-
 
388
     * @param string $entityname
-
 
389
     * @param string[] $include Include only these columns, if omitted then include all
-
 
390
     * @param string[] $exclude Exclude these columns, if omitted then exclude none
-
 
391
     * @throws coding_exception If both $include and $exclude are non-empty
-
 
392
     */
-
 
393
    final protected function add_columns_from_entity(string $entityname, array $include = [], array $exclude = []): void {
-
 
394
        if (!empty($include) && !empty($exclude)) {
-
 
395
            throw new coding_exception('Cannot specify columns to include and exclude simultaneously');
-
 
396
        }
-
 
397
 
-
 
398
        $entity = $this->get_entity($entityname);
-
 
399
 
-
 
400
        // Retrieve filtered columns from entity, respecting given $include/$exclude parameters.
-
 
401
        $columns = array_filter($entity->get_columns(), function(column $column) use ($include, $exclude): bool {
-
 
402
            if (!empty($include)) {
-
 
403
                return $this->report_element_search($column->get_name(), $include);
-
 
404
            }
-
 
405
 
-
 
406
            if (!empty($exclude)) {
-
 
407
                return !$this->report_element_search($column->get_name(), $exclude);
-
 
408
            }
-
 
409
 
-
 
410
            return true;
-
 
411
        });
-
 
412
 
-
 
413
        foreach ($columns as $column) {
-
 
414
            $this->add_column($column);
-
 
415
        }
-
 
416
    }
376
 
417
 
377
    /**
418
    /**
378
     * Add given columns to the report from one or more entities
419
     * Add given columns to the report from one or more entities
379
     *
420
     *
380
     * Each entity must have already been added to the report before calling this method
421
     * Each entity must have already been added to the report before calling this method
Línea 516... Línea 557...
516
    }
557
    }
Línea 517... Línea 558...
517
 
558
 
518
    /**
559
    /**
519
     * Return all active report conditions (by default, all available conditions)
560
     * Return all active report conditions (by default, all available conditions)
-
 
561
     *
520
     *
562
     * @param bool $checkavailable
521
     * @return filter[]
563
     * @return filter[]
522
     */
564
     */
523
    public function get_active_conditions(): array {
565
    public function get_active_conditions(bool $checkavailable = true): array {
524
        $conditions = $this->get_conditions();
566
        $conditions = $this->get_conditions();
525
        foreach ($conditions as $condition) {
567
        foreach ($conditions as $condition) {
526
            if ($condition->get_is_deprecated()) {
568
            if ($condition->get_is_deprecated()) {
527
                debugging("The condition '{$condition->get_unique_identifier()}' is deprecated, please do not use it any more." .
569
                debugging("The condition '{$condition->get_unique_identifier()}' is deprecated, please do not use it any more." .
Línea 635... Línea 677...
635
 
677
 
636
        return $this->add_filter($this->get_entity($entityname)->get_filter($filtername));
678
        return $this->add_filter($this->get_entity($entityname)->get_filter($filtername));
Línea 637... Línea 679...
637
    }
679
    }
-
 
680
 
-
 
681
    /**
-
 
682
     * Add filters from the given entity name to be available to use in a custom report
-
 
683
     *
-
 
684
     * Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
-
 
685
     *
-
 
686
     * @param string $entityname
-
 
687
     * @param string[] $include Include only these filters, if omitted then include all
-
 
688
     * @param string[] $exclude Exclude these filters, if omitted then exclude none
-
 
689
     * @throws coding_exception If both $include and $exclude are non-empty
-
 
690
     */
-
 
691
    final protected function add_filters_from_entity(string $entityname, array $include = [], array $exclude = []): void {
-
 
692
        if (!empty($include) && !empty($exclude)) {
-
 
693
            throw new coding_exception('Cannot specify filters to include and exclude simultaneously');
-
 
694
        }
-
 
695
 
-
 
696
        $entity = $this->get_entity($entityname);
-
 
697
 
-
 
698
        // Retrieve filtered filters from entity, respecting given $include/$exclude parameters.
-
 
699
        $filters = array_filter($entity->get_filters(), function(filter $filter) use ($include, $exclude): bool {
-
 
700
            if (!empty($include)) {
-
 
701
                return $this->report_element_search($filter->get_name(), $include);
-
 
702
            }
-
 
703
 
-
 
704
            if (!empty($exclude)) {
-
 
705
                return !$this->report_element_search($filter->get_name(), $exclude);
-
 
706
            }
-
 
707
 
-
 
708
            return true;
-
 
709
        });
-
 
710
 
-
 
711
        foreach ($filters as $filter) {
-
 
712
            $this->add_filter($filter);
-
 
713
        }
-
 
714
    }
638
 
715
 
639
    /**
716
    /**
640
     * Add given filters to the report from one or more entities
717
     * Add given filters to the report from one or more entities
641
     *
718
     *
642
     * Each entity must have already been added to the report before calling this method
719
     * Each entity must have already been added to the report before calling this method
Línea 783... Línea 860...
783
    }
860
    }
Línea 784... Línea 861...
784
 
861
 
785
    /**
862
    /**
786
     * Set the default lang string for the notice used when no results are found.
863
     * Set the default lang string for the notice used when no results are found.
-
 
864
     *
-
 
865
     * Note this should be called from within the report class instance itself (ideally it would be protected)
787
     *
866
     *
788
     * @param lang_string|null $notice string, or null to tell the report to omit the notice entirely.
-
 
789
     * @return void
867
     * @param lang_string|null $notice string, or null to tell the report to omit the notice entirely.
790
     */
868
     */
791
    public function set_default_no_results_notice(?lang_string $notice): void {
869
    public function set_default_no_results_notice(?lang_string $notice): void {
792
        $this->noresultsnotice = $notice;
870
        $this->noresultsnotice = $notice;
Línea 809... Línea 887...
809
    public function get_default_per_page(): int {
887
    public function get_default_per_page(): int {
810
        return $this->defaultperpage;
888
        return $this->defaultperpage;
811
    }
889
    }
Línea 812... Línea 890...
812
 
890
 
-
 
891
    /**
-
 
892
     * Sets the report action to be rendered above the table
-
 
893
     *
-
 
894
     * @param report_action $reportaction
-
 
895
     */
-
 
896
    final public function set_report_action(report_action $reportaction): void {
-
 
897
        $this->reportaction = $reportaction;
-
 
898
    }
-
 
899
 
-
 
900
    /**
-
 
901
     * Gets the report action to be rendered abover the table
-
 
902
     *
-
 
903
     * @return report_action|null
-
 
904
     */
-
 
905
    final public function get_report_action(): ?report_action {
-
 
906
        return $this->reportaction;
-
 
907
    }
-
 
908
 
-
 
909
    /**
-
 
910
     * Sets the report info container content to be rendered between action buttons and table
-
 
911
     *
-
 
912
     * @param string $reportinfocontainer
-
 
913
     */
-
 
914
    final public function set_report_info_container(string $reportinfocontainer): void {
-
 
915
        $this->reportinfocontainer = $reportinfocontainer;
-
 
916
    }
-
 
917
 
-
 
918
    /**
-
 
919
     * Gets the report info container content to be rendered between action buttons and table
-
 
920
     *
-
 
921
     * @return string
-
 
922
     */
-
 
923
    final public function get_report_info_container(): string {
-
 
924
        return $this->reportinfocontainer;
-
 
925
    }
-
 
926
 
813
    /**
927
    /**
814
     * Add report attributes (data-, class, etc.) that will be included in HTML when report is displayed
928
     * Add report attributes (data-, class, etc.) that will be included in HTML when report is displayed
815
     *
929
     *
816
     * @param array $attributes
930
     * @param array $attributes
817
     * @return self
931
     * @return self
Línea 827... Línea 941...
827
     * @return array
941
     * @return array
828
     */
942
     */
829
    public function get_attributes(): array {
943
    public function get_attributes(): array {
830
        return $this->attributes;
944
        return $this->attributes;
831
    }
945
    }
-
 
946
 
-
 
947
    /**
-
 
948
     * Search for given element within list of search items, supporting '*' wildcards
-
 
949
     *
-
 
950
     * @param string $element
-
 
951
     * @param string[] $search
-
 
952
     * @return bool
-
 
953
     */
-
 
954
    final protected function report_element_search(string $element, array $search): bool {
-
 
955
        foreach ($search as $item) {
-
 
956
            // Simple matching.
-
 
957
            if ($element === $item) {
-
 
958
                return true;
-
 
959
            }
-
 
960
 
-
 
961
            // Wildcard matching.
-
 
962
            if (strpos($item, '*') !== false) {
-
 
963
                $pattern = '/^' . str_replace('\*', '.*', preg_quote($item)) . '$/';
-
 
964
                return (bool) preg_match($pattern, $element);
-
 
965
            }
-
 
966
        }
-
 
967
 
-
 
968
        return false;
-
 
969
    }
832
}
970
}