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
declare(strict_types=1);
18
 
19
namespace block_dedication\local\entities;
20
 
21
use lang_string;
22
use core_reportbuilder\local\entities\base;
23
use core_reportbuilder\local\filters\date;
24
use core_reportbuilder\local\helpers\format;
25
use core_reportbuilder\local\report\column;
26
use core_reportbuilder\local\report\filter;
27
use block_dedication\lib\utils;
28
use core_reportbuilder\local\filters\duration;
29
 
30
/**
31
 * Dedication entity
32
 * @package block_dedication
33
 * @copyright 2022 University of Canterbury
34
 * @author Pramith Dayananda <pramithd@catalyst.net.nz>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class dedication extends base {
38
 
39
    /**
40
     * Database tables that this entity uses and their default aliases
41
     *
42
     * @return array
43
     */
44
    protected function get_default_table_aliases(): array {
45
        return [
46
            'block_dedication' => 'td',
47
            'user' => 'auser',
48
            'course' => 'c',
49
        ];
50
    }
51
 
52
    /**
53
     * The default title for this entity
54
     *
55
     * @return lang_string
56
     */
57
    protected function get_default_entity_title(): lang_string {
58
        return new lang_string('entity_dedication', 'block_dedication');
59
    }
60
 
61
    /**
62
     * Initialise the entity, add all user fields and all 'visible' user profile fields
63
     *
64
     * @return base
65
     */
66
    public function initialise(): base {
67
 
68
        $columns = $this->get_all_columns();
69
 
70
        foreach ($columns as $column) {
71
            $this->add_column($column);
72
        }
73
 
74
        $filters = $this->get_all_filters();
75
        foreach ($filters as $filter) {
76
            $this
77
                ->add_filter($filter)
78
                ->add_condition($filter);
79
        }
80
 
81
        return $this;
82
    }
83
 
84
    /**
85
     * Add extra columns to course report.
86
     * @return array
87
     * @throws \coding_exception
88
     */
89
    protected function get_all_columns(): array {
90
        $dedicationalias = $this->get_table_alias('block_dedication');
91
 
92
        $columns[] = (new column(
93
            'timespent',
94
            new lang_string('sessionduration', 'block_dedication'),
95
            $this->get_entity_name()
96
        ))
97
            ->add_joins($this->get_joins())
98
            ->add_fields("$dedicationalias.timespent")
99
            ->set_type(column::TYPE_INTEGER)
100
            ->add_callback(static function(?int $value) {
101
                $format = utils::format_dedication($value);
102
                return $format;
103
            });
104
 
105
        $columns[] = (new column(
106
            'timestart',
107
            new lang_string('sessionstart', 'block_dedication'),
108
            $this->get_entity_name()
109
        ))
110
            ->add_joins($this->get_joins())
111
            ->add_fields("$dedicationalias.timestart")
112
            ->set_type(column::TYPE_TIMESTAMP)
113
            ->set_callback([format::class, 'userdate']);
114
 
115
        return $columns;
116
    }
117
 
118
    /**
119
     * Return list of all available filters
120
     *
121
     * @return filter[]
122
     */
123
    protected function get_all_filters(): array {
124
 
125
        $filters = [];
126
        $dedicationalias = $this->get_table_alias('block_dedication');
127
 
128
        // Module name filter.
129
        $filters[] = (new filter(
130
            duration::class,
131
            'timespent',
132
            new lang_string('sessionduration', 'block_dedication'),
133
            $this->get_entity_name(),
134
            "$dedicationalias.timespent"
135
        ))
136
            ->add_joins($this->get_joins());
137
 
138
        $filters[] = (new filter(
139
            date::class,
140
            'timestart',
141
            new lang_string('sessionstart', 'block_dedication'),
142
            $this->get_entity_name(),
143
            "$dedicationalias.timestart"
144
        ))
145
            ->add_joins($this->get_joins());
146
 
147
        return $filters;
148
    }
149
 
150
}