| 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\models;
|
|
|
20 |
|
|
|
21 |
use context;
|
|
|
22 |
use lang_string;
|
|
|
23 |
use core\persistent;
|
|
|
24 |
use core_reportbuilder\datasource;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Persistent class to represent a report column
|
|
|
28 |
*
|
|
|
29 |
* @package core_reportbuilder
|
|
|
30 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class column extends persistent {
|
|
|
34 |
|
|
|
35 |
/** @var string The table name. */
|
|
|
36 |
public const TABLE = 'reportbuilder_column';
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Return the definition of the properties of this model.
|
|
|
40 |
*
|
|
|
41 |
* @return array
|
|
|
42 |
*/
|
|
|
43 |
protected static function define_properties(): array {
|
|
|
44 |
return [
|
|
|
45 |
'reportid' => [
|
|
|
46 |
'type' => PARAM_INT,
|
|
|
47 |
],
|
|
|
48 |
'uniqueidentifier' => [
|
|
|
49 |
'type' => PARAM_RAW,
|
|
|
50 |
],
|
|
|
51 |
'aggregation' => [
|
|
|
52 |
'type' => PARAM_ALPHANUMEXT,
|
|
|
53 |
'default' => null,
|
|
|
54 |
'null' => NULL_ALLOWED,
|
|
|
55 |
],
|
|
|
56 |
'heading' => [
|
|
|
57 |
'type' => PARAM_TEXT,
|
|
|
58 |
'default' => null,
|
|
|
59 |
'null' => NULL_ALLOWED,
|
|
|
60 |
],
|
|
|
61 |
'columnorder' => [
|
|
|
62 |
'type' => PARAM_INT,
|
|
|
63 |
],
|
|
|
64 |
'sortenabled' => [
|
|
|
65 |
'type' => PARAM_BOOL,
|
|
|
66 |
'default' => false,
|
|
|
67 |
],
|
|
|
68 |
'sortdirection' => [
|
|
|
69 |
'type' => PARAM_INT,
|
|
|
70 |
'choices' => [SORT_ASC, SORT_DESC],
|
|
|
71 |
'default' => SORT_ASC,
|
|
|
72 |
],
|
|
|
73 |
'sortorder' => [
|
|
|
74 |
'type' => PARAM_INT,
|
|
|
75 |
'default' => null,
|
|
|
76 |
'null' => NULL_ALLOWED,
|
|
|
77 |
],
|
|
|
78 |
'usercreated' => [
|
|
|
79 |
'type' => PARAM_INT,
|
|
|
80 |
'default' => static function(): int {
|
|
|
81 |
global $USER;
|
|
|
82 |
|
|
|
83 |
return (int) $USER->id;
|
|
|
84 |
},
|
|
|
85 |
],
|
|
|
86 |
];
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Validate reportid property
|
|
|
91 |
*
|
|
|
92 |
* @param int $reportid
|
|
|
93 |
* @return bool|lang_string
|
|
|
94 |
*/
|
|
|
95 |
protected function validate_reportid(int $reportid) {
|
|
|
96 |
if (!report::record_exists($reportid)) {
|
|
|
97 |
return new lang_string('invaliddata', 'error');
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
return true;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Return the report this column belongs to
|
|
|
105 |
*
|
|
|
106 |
* @return report
|
|
|
107 |
*/
|
|
|
108 |
public function get_report(): report {
|
|
|
109 |
return new report($this->get('reportid'));
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Ensure report source is notified of new column
|
|
|
114 |
*/
|
|
|
115 |
protected function after_create(): void {
|
|
|
116 |
datasource::report_elements_modified($this->get('reportid'));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Ensure report source is notified of updated column
|
|
|
121 |
*
|
|
|
122 |
* @param bool $result
|
|
|
123 |
*/
|
|
|
124 |
protected function after_update($result): void {
|
|
|
125 |
if ($result) {
|
|
|
126 |
datasource::report_elements_modified($this->get('reportid'));
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Ensure report source is notified of deleted column
|
|
|
132 |
*
|
|
|
133 |
* @param bool $result
|
|
|
134 |
*/
|
|
|
135 |
protected function after_delete($result): void {
|
|
|
136 |
if ($result) {
|
|
|
137 |
datasource::report_elements_modified($this->get('reportid'));
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Helper method to return the current maximum column order value for a report
|
|
|
143 |
*
|
|
|
144 |
* @param int $reportid
|
|
|
145 |
* @param string $columnname
|
|
|
146 |
* @return int
|
|
|
147 |
*/
|
|
|
148 |
public static function get_max_columnorder(int $reportid, string $columnname): int {
|
|
|
149 |
global $DB;
|
|
|
150 |
|
|
|
151 |
return (int) $DB->get_field(static::TABLE, "MAX({$columnname})", ['reportid' => $reportid], MUST_EXIST);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Return formatted column name
|
|
|
156 |
*
|
|
|
157 |
* @param context|null $context If the context of the report is already known, it should be passed here
|
|
|
158 |
* @return string
|
|
|
159 |
*/
|
|
|
160 |
public function get_formatted_heading(?context $context = null): string {
|
|
|
161 |
if ($context === null) {
|
|
|
162 |
$context = $this->get_report()->get_context();
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
return format_string($this->raw_get('heading'), true, ['context' => $context]);
|
|
|
166 |
}
|
|
|
167 |
}
|