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
namespace core\output;
18
 
1441 ariadna 19
use core\context;
1 efrain 20
use stdClass;
21
 
22
/**
23
 * The filter renderable class.
24
 *
25
 * @package    core
26
 * @copyright  2021 Catalyst IT Australia Pty Ltd
27
 * @author     Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
abstract class datafilter implements renderable, templatable {
31
    /** @var int None of the following match */
32
    public const JOINTYPE_NONE = 0;
33
 
34
    /** @var int Any of the following match */
35
    public const JOINTYPE_ANY = 1;
36
 
37
    /** @var int All of the following match */
38
    public const JOINTYPE_ALL = 2;
39
 
40
    /** @var context $context The context where the filters are being rendered. */
41
    protected $context;
42
 
43
    /** @var string $tableregionid Container of the table to be updated by this filter, is used to retrieve the table */
44
    protected $tableregionid;
45
 
46
    /** @var stdClass $course The course shown */
47
    protected $course;
48
 
49
    /**
50
     * Filter constructor.
51
     *
52
     * @param context $context The context where the filters are being rendered
53
     * @param string|null $tableregionid Container of the table which will be updated by this filter
54
     */
55
    public function __construct(context $context, ?string $tableregionid = null) {
56
        $this->context = $context;
57
        $this->tableregionid = $tableregionid;
58
 
59
        if ($context instanceof \context_course) {
60
            $this->course = get_course($context->instanceid);
61
        }
62
    }
63
 
64
    /**
65
     * Get data for all filter types.
66
     *
67
     * @return array
68
     */
69
    abstract protected function get_filtertypes(): array;
70
 
71
    /**
72
     * Get a standardised filter object.
73
     *
74
     * @param string $name
75
     * @param string $title
76
     * @param bool $custom
77
     * @param bool $multiple
78
     * @param string|null $filterclass
79
     * @param array $values
80
     * @param bool $allowempty
81
     * @return stdClass|null
82
     */
83
    protected function get_filter_object(
84
        string $name,
85
        string $title,
86
        bool $custom,
87
        bool $multiple,
88
        ?string $filterclass,
89
        array $values,
90
        bool $allowempty = false,
91
        ?stdClass $filteroptions = null,
92
        bool $required = false,
93
        array $joinlist = [self::JOINTYPE_NONE, self::JOINTYPE_ANY, self::JOINTYPE_ALL]
94
    ): ?stdClass {
95
 
96
        if (!$allowempty && empty($values)) {
97
            // Do not show empty filters.
98
            return null;
99
        }
100
 
101
        return (object) [
102
            'name' => $name,
103
            'title' => $title,
104
            'allowcustom' => $custom,
105
            'allowmultiple' => $multiple,
106
            'filtertypeclass' => $filterclass,
107
            'values' => $values,
108
            'filteroptions' => $filteroptions,
109
            'required' => $required,
1441 ariadna 110
            'joinlist' => json_encode($joinlist),
1 efrain 111
        ];
112
    }
113
}