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
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\local\aggregation;
20
 
21
use lang_string;
22
use core_reportbuilder\local\helpers\database;
23
use core_reportbuilder\local\report\column;
24
 
25
/**
26
 * Column group concatenation aggregation type
27
 *
1441 ariadna 28
 * The value used for the separator between aggregated items can be specified by passing the 'separator' option
29
 * via {@see column::set_aggregation} or {@see column::set_aggregation_options} methods
30
 *
1 efrain 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 groupconcat extends base {
36
 
37
    /** @var string Character to use as a delimeter between column fields */
38
    protected const COLUMN_FIELD_DELIMETER = '<|>';
39
 
40
    /** @var string Character to use a null coalesce value */
41
    protected const COLUMN_NULL_COALESCE = '<^>';
42
 
43
    /** @var string Character to use as a delimeter between field values */
44
    protected const FIELD_VALUE_DELIMETER = '<,>';
45
 
46
    /**
47
     * Return aggregation name
48
     *
49
     * @return lang_string
50
     */
51
    public static function get_name(): lang_string {
52
        return new lang_string('aggregationgroupconcat', 'core_reportbuilder');
53
    }
54
 
55
    /**
56
     * This aggregation can be performed on all non-timestamp columns
57
     *
58
     * @param int $columntype
59
     * @return bool
60
     */
61
    public static function compatible(int $columntype): bool {
62
        return !in_array($columntype, [
63
            column::TYPE_TIMESTAMP,
64
        ]);
65
    }
66
 
67
    /**
68
     * Override base method to ensure all SQL fields are concatenated together if there are multiple
69
     *
70
     * @param array $sqlfields
71
     * @return string
72
     */
73
    public static function get_column_field_sql(array $sqlfields): string {
74
        if (count($sqlfields) === 1) {
75
            return parent::get_column_field_sql($sqlfields);
76
        }
77
 
78
        return self::get_column_fields_concat($sqlfields, self::COLUMN_FIELD_DELIMETER, self::COLUMN_NULL_COALESCE);
79
    }
80
 
81
    /**
82
     * Return the aggregated field SQL
83
     *
84
     * @param string $field
85
     * @param int $columntype
86
     * @return string
87
     */
88
    public static function get_field_sql(string $field, int $columntype): string {
89
        global $DB;
90
 
91
        $fieldsort = database::sql_group_concat_sort($field);
92
 
93
        return $DB->sql_group_concat($field, self::FIELD_VALUE_DELIMETER, $fieldsort);
94
    }
95
 
96
    /**
97
     * Return formatted value for column when applying aggregation, note we need to split apart the concatenated string
98
     * and apply callbacks to each concatenated value separately
99
     *
100
     * @param mixed $value
101
     * @param array $values
102
     * @param array $callbacks
103
     * @param int $columntype
104
     * @return mixed
105
     */
1441 ariadna 106
    public function format_value($value, array $values, array $callbacks, int $columntype) {
1 efrain 107
        $firstvalue = reset($values);
108
        if ($firstvalue === null) {
109
            return '';
110
        }
111
 
112
        $formattedvalues = [];
113
 
114
        // Store original names of all values that would be present without aggregation.
115
        $valuenames = array_keys($values);
116
        $valuenamescount = count($valuenames);
117
 
118
        // Loop over each extracted value from the concatenated string.
119
        $values = explode(self::FIELD_VALUE_DELIMETER, (string)$firstvalue);
120
        foreach ($values as $value) {
121
 
122
            // Ensure we have equal number of value names/data, account for truncation by DB.
123
            $valuedata = explode(self::COLUMN_FIELD_DELIMETER, $value);
124
            if ($valuenamescount !== count($valuedata)) {
125
                continue;
126
            }
127
 
128
            // Re-construct original values, also ensuring any nulls contained within are restored.
129
            $originalvalues = array_map(static function(string $value): ?string {
130
                return $value === self::COLUMN_NULL_COALESCE ? null : $value;
131
            }, array_combine($valuenames, $valuedata));
132
 
133
            $originalvalue = column::get_default_value($originalvalues, $columntype);
134
 
135
            // Once we've re-constructed each value, we can apply callbacks to it.
136
            $formattedvalues[] = parent::format_value($originalvalue, $originalvalues, $callbacks, $columntype);
137
        }
138
 
1441 ariadna 139
        // Determine separator based on passed options, defaulting to language pack list separator.
140
        $separator = array_key_exists('separator', $this->options)
141
            ? (string) $this->options['separator']
142
            : get_string('listsep', 'langconfig') . ' ';
143
 
144
        return implode($separator, $formattedvalues);
1 efrain 145
    }
146
}