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 |
namespace gradereport_summary\local\entities;
|
|
|
18 |
|
|
|
19 |
use core_reportbuilder\local\filters\select;
|
|
|
20 |
use grade_item;
|
|
|
21 |
use grade_plugin_return;
|
|
|
22 |
use grade_report_summary;
|
|
|
23 |
use lang_string;
|
|
|
24 |
use stdClass;
|
|
|
25 |
use core_reportbuilder\local\entities\base;
|
|
|
26 |
use core_reportbuilder\local\report\column;
|
|
|
27 |
use core_reportbuilder\local\report\filter;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die;
|
|
|
30 |
|
|
|
31 |
require_once($CFG->dirroot . '/grade/report/summary/lib.php');
|
|
|
32 |
require_once($CFG->dirroot . '/grade/lib.php');
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Grade summary entity class implementation
|
|
|
36 |
*
|
|
|
37 |
* @package gradereport_summary
|
|
|
38 |
* @copyright 2022 Ilya Tregubov <ilya@moodle.com>
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class grade_items extends base {
|
|
|
42 |
|
|
|
43 |
/** @var stdClass Course */
|
|
|
44 |
public $course;
|
|
|
45 |
|
|
|
46 |
/** @var grade_report_summary Grade report. */
|
|
|
47 |
public $report;
|
|
|
48 |
|
|
|
49 |
/** @var array Ungraded grade items counts with sql info. */
|
|
|
50 |
public $ungradedcounts;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Entity constructor
|
|
|
54 |
*
|
|
|
55 |
* @param stdClass $course
|
|
|
56 |
*/
|
|
|
57 |
public function __construct(stdClass $course) {
|
|
|
58 |
$this->course = $course;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Database tables that this entity uses
|
|
|
63 |
*
|
|
|
64 |
* @return string[]
|
|
|
65 |
*/
|
|
|
66 |
protected function get_default_tables(): array {
|
|
|
67 |
return [
|
|
|
68 |
'grade_items',
|
|
|
69 |
];
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* The default title for this entity in the list of columns/conditions/filters in the report builder
|
|
|
74 |
*
|
|
|
75 |
* @return lang_string
|
|
|
76 |
*/
|
|
|
77 |
protected function get_default_entity_title(): lang_string {
|
|
|
78 |
return new lang_string('gradeitem', 'grades');
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Initialise the entity
|
|
|
83 |
*
|
|
|
84 |
* @return base
|
|
|
85 |
*/
|
|
|
86 |
public function initialise(): base {
|
|
|
87 |
$context = \context_course::instance($this->course->id);
|
|
|
88 |
|
|
|
89 |
$gpr = new grade_plugin_return(
|
|
|
90 |
[
|
|
|
91 |
'type' => 'report',
|
|
|
92 |
'plugin' => 'summary',
|
|
|
93 |
'course' => $this->course,
|
|
|
94 |
]
|
|
|
95 |
);
|
|
|
96 |
|
|
|
97 |
$this->report = new grade_report_summary($this->course->id, $gpr, $context);
|
|
|
98 |
$showonlyactiveenrol = $this->report->show_only_active();
|
|
|
99 |
$this->ungradedcounts = $this->report->ungraded_counts(false, false, $showonlyactiveenrol);
|
|
|
100 |
|
|
|
101 |
$columns = $this->get_all_columns();
|
|
|
102 |
foreach ($columns as $column) {
|
|
|
103 |
$this->add_column($column);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$filters = $this->get_all_filters();
|
|
|
107 |
foreach ($filters as $filter) {
|
|
|
108 |
$this->add_filter($filter);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
return $this;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Returns list of all available columns
|
|
|
116 |
*
|
|
|
117 |
* @return column[]
|
|
|
118 |
*/
|
|
|
119 |
protected function get_all_columns(): array {
|
|
|
120 |
|
|
|
121 |
$tablealias = $this->get_table_alias('grade_items');
|
|
|
122 |
$selectsql = "$tablealias.id, $tablealias.itemname, $tablealias.iteminstance, $tablealias.calculation,
|
|
|
123 |
$tablealias.itemnumber, $tablealias.itemmodule, $tablealias.hidden, $tablealias.courseid";
|
|
|
124 |
|
|
|
125 |
// Grade item name column.
|
|
|
126 |
$columns[] = (new column(
|
|
|
127 |
'name',
|
|
|
128 |
null,
|
|
|
129 |
$this->get_entity_name()
|
|
|
130 |
))
|
|
|
131 |
->add_joins($this->get_joins())
|
|
|
132 |
->set_type(column::TYPE_TEXT)
|
|
|
133 |
->add_fields($selectsql)
|
|
|
134 |
->add_callback(static function($value, $row): string {
|
|
|
135 |
$gradeitem = grade_item::fetch(['id' => $row->id, 'courseid' => $row->courseid]);
|
|
|
136 |
$element = ['type' => 'item', 'object' => $gradeitem, 'modinfo' => get_fast_modinfo($row->courseid)];
|
|
|
137 |
$fullname = \grade_helper::get_element_header($element, true, false, true, true, true);
|
|
|
138 |
|
|
|
139 |
$icon = \grade_helper::get_element_icon($element);
|
|
|
140 |
$elementtype = \grade_helper::get_element_type_string($element);
|
|
|
141 |
$itemtype = \html_writer::span($elementtype, 'd-block text-uppercase small dimmed_text',
|
|
|
142 |
['title' => $elementtype]);
|
|
|
143 |
$content = \html_writer::div($itemtype . $fullname);
|
|
|
144 |
$dimmed = '';
|
|
|
145 |
if ($row->hidden) {
|
|
|
146 |
$dimmed = ' dimmed_text';
|
|
|
147 |
}
|
|
|
148 |
return \html_writer::div($icon . $content, "item d-flex align-items-center" . $dimmed);
|
|
|
149 |
});
|
|
|
150 |
|
|
|
151 |
$report = [
|
|
|
152 |
'report' => $this->report,
|
|
|
153 |
'ungradedcounts' => $this->ungradedcounts
|
|
|
154 |
];
|
|
|
155 |
|
|
|
156 |
// Average column.
|
|
|
157 |
$columns[] = (new column(
|
|
|
158 |
'average',
|
|
|
159 |
new lang_string('average', 'grades'),
|
|
|
160 |
$this->get_entity_name()
|
|
|
161 |
))
|
|
|
162 |
->add_joins($this->get_joins())
|
|
|
163 |
->set_type(column::TYPE_TEXT)
|
|
|
164 |
->add_field("$tablealias.id")
|
|
|
165 |
->add_callback(static function($value) use ($report): string {
|
|
|
166 |
|
|
|
167 |
$gradeitem = grade_item::fetch(['id' => $value]);
|
|
|
168 |
if (!empty($gradeitem->avg)) {
|
|
|
169 |
$averageformatted = '-';
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
if ($gradeitem->needsupdate) {
|
|
|
173 |
$averageformatted = get_string('error');
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if (empty($averageformatted)) {
|
|
|
177 |
$ungradedcounts = $report['ungradedcounts'];
|
|
|
178 |
$aggr = $report['report']->calculate_average($gradeitem, $ungradedcounts);
|
|
|
179 |
|
|
|
180 |
if (empty($aggr['average'])) {
|
|
|
181 |
$averageformatted = '-';
|
|
|
182 |
} else {
|
|
|
183 |
$averagesdisplaytype = $ungradedcounts['report']['averagesdisplaytype'];
|
|
|
184 |
$averagesdecimalpoints = $ungradedcounts['report']['averagesdecimalpoints'];
|
|
|
185 |
$shownumberofgrades = $ungradedcounts['report']['shownumberofgrades'];
|
|
|
186 |
|
|
|
187 |
// Determine which display type to use for this average.
|
|
|
188 |
// No ==0 here, please resave the report and user preferences.
|
|
|
189 |
if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) {
|
|
|
190 |
$displaytype = $gradeitem->get_displaytype();
|
|
|
191 |
} else {
|
|
|
192 |
$displaytype = $averagesdisplaytype;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
// Override grade_item setting if a display preference (not inherit) was set for the averages.
|
|
|
196 |
if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) {
|
|
|
197 |
$decimalpoints = $gradeitem->get_decimals();
|
|
|
198 |
} else {
|
|
|
199 |
$decimalpoints = $averagesdecimalpoints;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
$gradehtml = grade_format_gradevalue($aggr['average'],
|
|
|
203 |
$gradeitem, true, $displaytype, $decimalpoints);
|
|
|
204 |
|
|
|
205 |
if ($shownumberofgrades) {
|
|
|
206 |
$numberofgrades = $aggr['meancount'];
|
|
|
207 |
$gradehtml .= " (" . $numberofgrades . ")";
|
|
|
208 |
}
|
|
|
209 |
$averageformatted = $gradehtml;
|
|
|
210 |
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
return $averageformatted;
|
|
|
214 |
});
|
|
|
215 |
|
|
|
216 |
return $columns;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Return list of all available filters
|
|
|
221 |
*
|
|
|
222 |
* @return filter[]
|
|
|
223 |
*/
|
|
|
224 |
protected function get_all_filters(): array {
|
|
|
225 |
$tablealias = $this->get_table_alias('grade_items');
|
|
|
226 |
|
|
|
227 |
// Activity type filter (for performance only load options on demand).
|
|
|
228 |
$filters[] = (new filter(
|
|
|
229 |
select::class,
|
|
|
230 |
'name',
|
|
|
231 |
new lang_string('activitytype', 'format_singleactivity'),
|
|
|
232 |
$this->get_entity_name(),
|
|
|
233 |
"coalesce({$tablealias}.itemmodule,{$tablealias}.itemtype)"
|
|
|
234 |
))
|
|
|
235 |
->add_joins($this->get_joins())
|
|
|
236 |
->set_options_callback([$this->report, 'item_types']);
|
|
|
237 |
|
|
|
238 |
return $filters;
|
|
|
239 |
}
|
|
|
240 |
}
|