Proyectos de Subversion Moodle

Rev

| 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
/**
18
 * Group data by a certain field.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local\data_grid\data\strategy;
26
 
27
use block_dash\local\data_grid\data\data_collection;
28
use block_dash\local\data_grid\data\data_collection_interface;
29
use block_dash\local\data_grid\data\field;
30
use block_dash\local\dash_framework\structure\field_interface;
31
/**
32
 * Group data by a certain field.
33
 *
34
 * @package block_dash
35
 */
36
class grouped_strategy implements data_strategy_interface {
37
 
38
    /**
39
     * @var field_interface
40
     */
41
    private $groupbyfielddefinition;
42
 
43
    /**
44
     * @var field_interface
45
     */
46
    private $grouplabelfielddefinition;
47
 
48
    /**
49
     * Create new grouped strategy.
50
     *
51
     * @param field_interface $groupbyfielddefinition
52
     * @param field_interface $grouplabelfielddefinition
53
     */
54
    public function __construct(field_interface $groupbyfielddefinition,
55
                                field_interface $grouplabelfielddefinition) {
56
        $this->groupbyfielddefinition = $groupbyfielddefinition;
57
        $this->grouplabelfielddefinition = $grouplabelfielddefinition;
58
    }
59
 
60
    /**
61
     * Convert records.
62
     *
63
     * @param \stdClass[] $records
64
     * @param field_interface[] $fielddefinitions
65
     * @return data_collection_interface
66
     */
67
    public function convert_records_to_data_collection($records, array $fielddefinitions) {
68
        $griddata = block_dash_get_data_collection();
69
 
70
        $sections = [];
71
 
72
        foreach ($records as $fullrecord) {
73
            $record = clone $fullrecord;
74
            $row = block_dash_get_data_collection();
75
 
76
            $label = $record->{$this->grouplabelfielddefinition->get_alias()};
77
            if (!$groupby = $record->{$this->groupbyfielddefinition->get_alias()}) {
78
                continue;
79
            }
80
 
81
            if (isset($record->unique_id)) {
82
                unset($record->unique_id);
83
            }
84
 
85
            foreach ($fielddefinitions as $fielddefinition) {
86
                $alias = $fielddefinition->get_alias();
87
 
88
                $row->add_data(new field($alias, $fielddefinition->transform_data($record->$alias, $fullrecord),
89
                    $fielddefinition->get_visibility(), $fielddefinition->get_title()));
90
            }
91
 
92
            if (!isset($sections[$groupby])) {
93
                $sections[$groupby] = block_dash_get_data_collection();
94
                $sections[$groupby]->add_data(new field('groupby', $groupby, $fielddefinition->get_visibility()));
95
                $sections[$groupby]->add_data(new field('label', $label, $fielddefinition->get_visibility()));
96
            }
97
            $sections[$groupby]->add_child_collection('rows', $row);
98
        }
99
 
100
        foreach ($sections as $section) {
101
            $griddata->add_child_collection('sections', $section);
102
        }
103
 
104
        return $griddata;
105
    }
106
}