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
 
24
/**
25
 * Column group concatenation distinct aggregation type
26
 *
1441 ariadna 27
 * The value used for the separator between aggregated items can be specified by passing the 'separator' option
28
 * via {@see column::set_aggregation} or {@see column::set_aggregation_options} methods
29
 *
1 efrain 30
 * @package     core_reportbuilder
31
 * @copyright   2021 Paul Holden <paulh@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class groupconcatdistinct extends groupconcat {
35
 
36
    /**
37
     * Return aggregation name
38
     *
39
     * @return lang_string
40
     */
41
    public static function get_name(): lang_string {
42
        return new lang_string('aggregationgroupconcatdistinct', 'core_reportbuilder');
43
    }
44
 
45
    /**
1441 ariadna 46
     * This aggregation can be performed on all non-timestamp columns in supported DBs
1 efrain 47
     *
48
     * @param int $columntype
49
     * @return bool
50
     */
51
    public static function compatible(int $columntype): bool {
52
        global $DB;
53
 
54
        $dbsupportedtype = in_array($DB->get_dbfamily(), [
55
            'mysql',
56
            'postgres',
57
        ]);
58
 
59
        return $dbsupportedtype && parent::compatible($columntype);
60
    }
61
 
62
    /**
63
     * Return the aggregated field SQL
64
     *
65
     * @param string $field
66
     * @param int $columntype
67
     * @return string
68
     */
69
    public static function get_field_sql(string $field, int $columntype): string {
70
        global $DB;
71
 
72
        $fieldsort = database::sql_group_concat_sort($field);
73
 
74
        // Postgres handles group concatenation differently in that it requires the expression to be cast to char, so we can't
75
        // simply pass "DISTINCT {$field}" to the {@see \moodle_database::sql_group_concat} method in all cases.
76
        if ($DB->get_dbfamily() === 'postgres') {
77
            $field = $DB->sql_cast_to_char($field);
78
            if ($fieldsort !== '') {
79
                $fieldsort = "ORDER BY {$fieldsort}";
80
            }
81
 
82
            return "STRING_AGG(DISTINCT {$field}, '" . self::FIELD_VALUE_DELIMETER . "' {$fieldsort})";
83
        } else {
84
            return $DB->sql_group_concat("DISTINCT {$field}", self::FIELD_VALUE_DELIMETER, $fieldsort);
85
        }
86
    }
87
}