| 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_course\reportbuilder\local\entities;
|
|
|
20 |
|
|
|
21 |
use core_reportbuilder\local\entities\base;
|
|
|
22 |
use core_reportbuilder\local\filters\date;
|
|
|
23 |
use core_reportbuilder\local\helpers\format;
|
|
|
24 |
use core_reportbuilder\local\report\column;
|
|
|
25 |
use core_reportbuilder\local\report\filter;
|
|
|
26 |
use lang_string;
|
|
|
27 |
use stdClass;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Course access entity implementation
|
|
|
31 |
*
|
|
|
32 |
* @package core_course
|
|
|
33 |
* @copyright 2022 David Matamoros <davidmc@moodle.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class access extends base {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Database tables that this entity uses
|
|
|
40 |
*
|
|
|
41 |
* @return string[]
|
|
|
42 |
*/
|
|
|
43 |
protected function get_default_tables(): array {
|
|
|
44 |
return [
|
|
|
45 |
'user_lastaccess',
|
|
|
46 |
'user',
|
|
|
47 |
];
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* The default title for this entity in the list of columns/conditions/filters in the report builder
|
|
|
52 |
*
|
|
|
53 |
* @return lang_string
|
|
|
54 |
*/
|
|
|
55 |
protected function get_default_entity_title(): lang_string {
|
|
|
56 |
return new lang_string('courseaccess', 'course');
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Initialise the entity
|
|
|
61 |
*
|
|
|
62 |
* @return base
|
|
|
63 |
*/
|
|
|
64 |
public function initialise(): base {
|
|
|
65 |
foreach ($this->get_all_columns() as $column) {
|
|
|
66 |
$this->add_column($column);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
70 |
foreach ($this->get_all_filters() as $filter) {
|
|
|
71 |
$this
|
|
|
72 |
->add_filter($filter)
|
|
|
73 |
->add_condition($filter);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return $this;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Returns list of all available columns
|
|
|
81 |
*
|
|
|
82 |
* @return column[]
|
|
|
83 |
*/
|
|
|
84 |
protected function get_all_columns(): array {
|
|
|
85 |
$tablealias = $this->get_table_alias('user_lastaccess');
|
|
|
86 |
$user = $this->get_table_alias('user');
|
|
|
87 |
|
|
|
88 |
// Last course access column.
|
|
|
89 |
$columns[] = (new column(
|
|
|
90 |
'timeaccess',
|
|
|
91 |
new lang_string('lastcourseaccess', 'moodle'),
|
|
|
92 |
$this->get_entity_name()
|
|
|
93 |
))
|
|
|
94 |
->add_joins($this->get_joins())
|
|
|
95 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
96 |
->add_field("{$tablealias}.timeaccess")
|
|
|
97 |
->add_field("{$user}.id", 'userid')
|
|
|
98 |
->set_is_sortable(true)
|
| 11 |
efrain |
99 |
->add_callback(static function(?int $value, stdClass $row, $arguments, ?string $aggregation): string {
|
|
|
100 |
if ($row->userid === null && $aggregation === null) {
|
| 1 |
efrain |
101 |
return '';
|
| 11 |
efrain |
102 |
} else if ($value === null) {
|
| 1 |
efrain |
103 |
return get_string('never');
|
|
|
104 |
}
|
| 11 |
efrain |
105 |
return format::userdate($value, $row);
|
| 1 |
efrain |
106 |
});
|
|
|
107 |
|
|
|
108 |
return $columns;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Return list of all available filters
|
|
|
113 |
*
|
|
|
114 |
* @return filter[]
|
|
|
115 |
*/
|
|
|
116 |
protected function get_all_filters(): array {
|
|
|
117 |
$tablealias = $this->get_table_alias('user_lastaccess');
|
|
|
118 |
|
|
|
119 |
// Last course access filter.
|
|
|
120 |
$filters[] = (new filter(
|
|
|
121 |
date::class,
|
|
|
122 |
'timeaccess',
|
|
|
123 |
new lang_string('lastcourseaccess', 'moodle'),
|
|
|
124 |
$this->get_entity_name(),
|
|
|
125 |
"{$tablealias}.timeaccess"
|
|
|
126 |
))
|
|
|
127 |
->add_joins($this->get_joins())
|
|
|
128 |
->set_limited_operators([
|
|
|
129 |
date::DATE_ANY,
|
|
|
130 |
date::DATE_NOT_EMPTY,
|
|
|
131 |
date::DATE_EMPTY,
|
|
|
132 |
date::DATE_RANGE,
|
|
|
133 |
date::DATE_LAST,
|
|
|
134 |
date::DATE_CURRENT,
|
|
|
135 |
]);
|
|
|
136 |
|
|
|
137 |
return $filters;
|
|
|
138 |
}
|
|
|
139 |
}
|