-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpage_group.php
147 lines (121 loc) · 4.98 KB
/
page_group.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Page group meta data.
*
* @package tool_excimer
* @author Jason den Dulk <[email protected]>
* @copyright 2022, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\chart_line;
use core\chart_series;
use tool_excimer\helper;
use tool_excimer\monthint;
use tool_excimer\output\tabs;
use tool_excimer\page_group;
require_once('../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
$expiry = get_config('tool_excimer', 'expiry_fuzzy_counts');
$pagegroupid = required_param('id', PARAM_INT);
$monthstodisplay = optional_param('monthstodisplay', $expiry, PARAM_INT);
$url = new \moodle_url('/admin/tool/excimer/page_group.php');
$context = context_system::instance();
$pagegroup = new page_group($pagegroupid);
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->navbar->add($pagegroup->get('name'));
admin_externalpage_setup('tool_excimer_report_page_groups');
$output = $PAGE->get_renderer('tool_excimer');
$data = $pagegroup->to_record();
// Data for table.
$data->month = helper::monthint_formatted($data->month);
$data->approxcount = pow(2, $data->fuzzycount - 1) . ' - ' . pow(2, $data->fuzzycount);
$data->approxduration = pow(2, $data->fuzzydurationsum);
$data->histogram = helper::make_histogram($data);
// Data for charts.
// Each duration range forms its own line on the chart.
$firstmonth = monthint::from_timestamp(strtotime("$monthstodisplay months ago"));
// We do the following to ensure we include the current month.
$firstmonth = monthint::increment_month($firstmonth);
$history = $DB->get_records_select(
page_group::TABLE,
"name = ? and month >= ?",
[$data->name, $firstmonth],
'',
'month, id, fuzzydurationcounts'
);
$highest = 0;
// Each of these is an array holding counts for each month for a particular duration range.
$durationseries = [];
$histograms = [];
// We make a list of histograms, while finding the highest duration range that a histogram has a count for.
foreach ($history as $month => $record) {
$histograms[$month] = helper::make_histogram($record);
$highest = max($highest, count($histograms[$month]));
}
// Labels for the axis.
$labels = [];
// Label for each line.
$linelabels = [];
// Prepare values and labels for the chart.
$month = $firstmonth;
// We use a for loop to include months that have no record in the database.
for ($i = 0; $i < $monthstodisplay; ++$i) {
$labels[] = helper::monthint_formatted($month);
// For each histogram, the duration counts are stored in a simple array. We count from 0 to highest here to
// ensure that we have the same number of entries for each month.
for ($rangeindex = 0; $rangeindex < $highest; ++$rangeindex) {
$value = 0;
if (isset($histograms[$month][$rangeindex])) {
$value = $histograms[$month][$rangeindex]['value'];
$linelabels[$rangeindex] = get_string('fuzzydurationrange', 'tool_excimer', $histograms[$month][$rangeindex]);
}
$durationseries[$rangeindex][] = $value;
}
$month = monthint::increment_month($month);
}
$chart = new chart_line();
$chart->set_labels($labels);
// Add each duration range to the chart, but only those that have non-zero counts.
foreach ($durationseries as $idx => $series) {
if (max($series) != 0) {
$chart->add_series(new chart_series($linelabels[$idx], $series));
}
}
// Select element to choose how many months in the past to show in the chart.
$monthstodisplayurl = clone $url;
$monthstodisplayurl->params(['id' => $pagegroupid]);
$monthstodisplayselect = new \single_select(
$monthstodisplayurl,
'monthstodisplay',
// We do the following line to get a 1 to $expiry array with matching indexes.
array_slice(range(0, $expiry), 1, null, true),
$monthstodisplay
);
$monthstodisplayselect->set_label(get_string('months_to_display', 'tool_excimer'));
$pluginname = get_string('pluginname', 'tool_excimer');
$PAGE->set_title($pluginname);
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($pluginname);
echo $output->header();
$tabs = new tabs($url);
echo $output->render_tabs($tabs);
echo $output->render_from_template('tool_excimer/page_group', $data);
echo html_writer::tag('h3', get_string('histogram_history', 'tool_excimer'));
echo $output->render($monthstodisplayselect);
echo $output->render($chart);
echo $output->footer();