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
 * Transform data by renaming delimited IDs to fields. Such as course name or group name.
19
 *
20
 * @package    block_dash
21
 * @copyright  2020 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\field\attribute;
26
 
27
use dml_exception;
28
 
29
/**
30
 * Transform data by renaming delimited IDs to fields. Such as course name or group name.
31
 *
32
 * @package block_dash
33
 */
34
class rename_ids_attribute extends abstract_field_attribute {
35
 
36
    /** @var array stored the fields used in current table.*/
37
    private static $fieldstore = [];
38
 
39
    /**
40
     * After records are relieved from database each field has a chance to transform the data.
41
     * Example: Convert unix timestamp into a human readable date format
42
     *
43
     * @param mixed $data
44
     * @param \stdClass $record Entire row
45
     * @return mixed
46
     * @throws \moodle_exception
47
     */
48
    final public function transform_data($data, \stdClass $record) {
49
        $groups = [];
50
        if ($data) {
51
            foreach (explode($this->get_option('delimiter'), $data) as $id) {
52
                if ($this->check_id($id) && $id != '') {
53
                    $fields = self::get_fields($this->get_option('table'), $this->get_option('field'));
54
                    if (isset($fields[$id])) {
55
                        $groups[] = $fields[$id];
56
                    }
57
                }
58
            }
59
        }
60
        return implode($this->get_option('delimiter') . ' ', $groups);
61
    }
62
 
63
    /**
64
     * Override in child class to add additional checks per ID.
65
     *
66
     * @param int $id
67
     * @return bool
68
     */
69
    public function check_id($id) {
70
        return true;
71
    }
72
 
73
    /**
74
     * Get the fields used in this table.
75
     *
76
     * @param string $table
77
     * @param string $field
78
     * @return mixed
79
     * @throws dml_exception
80
     */
81
    protected static function get_fields($table, $field) {
82
        global $DB;
83
 
84
        if (!isset(self::$fieldstore[$table][$field])) {
85
            if (!isset(self::$fieldstore[$table])) {
86
                self::$fieldstore[$table] = [];
87
            }
88
            self::$fieldstore[$table][$field] = [];
89
 
90
            foreach ($DB->get_recordset($table, null, '', 'id, ' . $field) as $record) {
91
                self::$fieldstore[$table][$field][$record->id] = $record->$field;
92
            }
93
        }
94
 
95
        return self::$fieldstore[$table][$field];
96
    }
97
}