Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_reportbuilder\local\aggregation;
20
 
21
use core\{clock, di};
22
use core\lang_string;
23
use core_reportbuilder\local\helpers\format;
24
use core_reportbuilder\local\report\column;
25
 
26
/**
27
 * Column date aggregation type
28
 *
29
 * @package     core_reportbuilder
30
 * @copyright   2024 Paul Holden <paulh@moodle.com>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class date extends base {
34
 
35
    /**
36
     * Return aggregation name
37
     *
38
     * @return lang_string
39
     */
40
    public static function get_name(): lang_string {
41
        return new lang_string('aggregationdate', 'core_reportbuilder');
42
    }
43
 
44
    /**
45
     * This aggregation can be performed on timestamp columns
46
     *
47
     * @param int $columntype
48
     * @return bool
49
     */
50
    public static function compatible(int $columntype): bool {
51
        return $columntype === column::TYPE_TIMESTAMP;
52
    }
53
 
54
    /**
55
     * Return the aggregated field SQL
56
     *
57
     * @param string $field
58
     * @param int $columntype
59
     * @return string
60
     */
61
    public static function get_field_sql(string $field, int $columntype): string {
62
        $datenow = di::get(clock::class)->now();
63
 
64
        // Apply timezone offset for current user.
65
        return "(FLOOR({$field} / " . DAYSECS . ") * " . DAYSECS . ") + " . $datenow->getOffset();
66
    }
67
 
68
    /**
69
     * When applied to a column, we should group by its fields
70
     *
71
     * @return bool
72
     */
73
    public static function column_groupby(): bool {
74
        return true;
75
    }
76
 
77
    /**
78
     * Returns aggregated column type
79
     *
80
     * @param int $columntype
81
     * @return int
82
     */
83
    public static function get_column_type(int $columntype): int {
84
        return column::TYPE_TIMESTAMP;
85
    }
86
 
87
    /**
88
     * Return formatted value for column when applying aggregation
89
     *
90
     * @param mixed $value
91
     * @param array $values
92
     * @param array $callbacks
93
     * @param int $columntype
94
     * @return string
95
     */
96
    public function format_value($value, array $values, array $callbacks, int $columntype): string {
97
        return format::userdate($value, (object) [], get_string('strftimedaydate', 'core_langconfig'));
98
    }
99
}