Skip to content

Commit 4340f22

Browse files
WR490984: General fixes.
1 parent 6aa4e71 commit 4340f22

11 files changed

Lines changed: 125 additions & 9 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 tool_cloudmetrics\event;
18+
19+
use core\event\base;
20+
21+
/**
22+
* Collector plugin was disabled
23+
*
24+
* @package tool_cloudmetrics
25+
* @copyright 2026 onwards Catalyst IT EU {@link https://catalyst-eu.net}
26+
* @author Michael Kotlyar <michael.kotlyar@catalyst-eu.net>
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class collector_plugin_disabled extends base {
30+
#[\Override]
31+
public function init() {
32+
$this->data['edulevel'] = self::LEVEL_OTHER;
33+
$this->data['crud'] = 'u';
34+
}
35+
36+
#[\Override]
37+
public static function get_name() {
38+
return get_string('eventcollectorplugindisabled', 'tool_cloudmetrics');
39+
}
40+
41+
#[\Override]
42+
public function get_description() {
43+
return 'User ' . $this->data['userid'] . ' disabled collector plugin ' . $this->data['other']['pluginname'];
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 tool_cloudmetrics\event;
18+
19+
use core\event\base;
20+
21+
/**
22+
* Collector plugin was enabled
23+
*
24+
* @package tool_cloudmetrics
25+
* @copyright 2026 onwards Catalyst IT EU {@link https://catalyst-eu.net}
26+
* @author Michael Kotlyar <michael.kotlyar@catalyst-eu.net>
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class collector_plugin_enabled extends base {
30+
#[\Override]
31+
public function init() {
32+
$this->data['edulevel'] = self::LEVEL_OTHER;
33+
$this->data['crud'] = 'u';
34+
}
35+
36+
#[\Override]
37+
public static function get_name() {
38+
return get_string('eventcollectorpluginenabled', 'tool_cloudmetrics');
39+
}
40+
41+
#[\Override]
42+
public function get_description() {
43+
return 'User ' . $this->data['userid'] . ' enabled collector plugin ' . $this->data['other']['pluginname'];
44+
}
45+
}

classes/metric/base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ abstract public function get_frequency_default(): int;
8989
* @param int $freq
9090
*/
9191
public function set_frequency(int $freq) {
92-
set_config($this->get_name() . '_frequency', $freq, 'tool_cloudmetrics');
92+
set_config($this->get_name() . '_frequency', $freq, 'tool_cloudmetrics', true);
9393
}
9494

9595
/**

classes/metric/manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function get_metrics(bool $enabledonly = true): array {
102102
'dailyusers' => new daily_users_metric(),
103103
'yearlyactiveusers' => new yearly_active_users_metric(),
104104
];
105-
if (defined('PHPUNIT_TEST')) {
105+
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
106106
// Add testing metrics to the list.
107107
$metrics['foobar'] = new test_metric();
108108
}

collector/database/backfill.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@
7474
$range = $collector->get_metric_range($metricname);
7575
$freqretrieved = $collector->get_last_backfilled_frequency($metricname);
7676
if (!empty($range)) {
77-
['mintime' => $mintmptmp, 'maxtime' => $maxtmpstmp] = $range;
78-
$backfilledfrom = userdate($mintmptmp, get_string('strftimedatetime', 'cltr_database'), $CFG->timezone);
79-
$backfilledto = userdate($maxtmpstmp, get_string('strftimedatetime', 'cltr_database'), $CFG->timezone);
77+
$backfilledfrom = userdate($range['mintime'], get_string('strftimedatetime', 'cltr_database'), $CFG->timezone);
78+
$backfilledto = userdate($range['maxtime'], get_string('strftimedatetime', 'cltr_database'), $CFG->timezone);
8079
$backfilledinterval = (int)$freqretrieved;
8180

8281
// Add a comment about different frequencies.

collector/database/chart.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
$graphperiodsec = \cltr_database\lib::period_from_interval($metrics[$displayedmetrics[0]]);
112112
}
113113
} else {
114+
require_login(null, false);
115+
require_capability('moodle/site:config', context_system::instance());
116+
require_sesskey();
114117
set_config('chart_period', $graphperiodsec, 'cltr_database');
115118
\core_plugin_manager::reset_caches();
116119
}

collector/database/classes/form/metric_backfill_form.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ public function definition() {
4747
}
4848
}
4949
$mform->addElement('select', 'periodretrieval', get_string('period_select', 'tool_cloudmetrics'), $periods);
50+
$mform->setType('periodretrieval', PARAM_INT);
51+
5052
$mform->addElement('hidden', 'metric', $metric);
5153
$mform->setType('metric', PARAM_ALPHANUMEXT);
52-
$this->add_action_buttons(false, 'Backfill data');
54+
55+
$this->add_action_buttons(false, get_string('backfilldata', 'tool_cloudmetrics'));
5356
}
5457

5558
/**
@@ -59,6 +62,14 @@ public function definition() {
5962
* @param array $files
6063
*/
6164
public function validation($data, $files) {
62-
return [];
65+
$errors = [];
66+
67+
// Ensure `periodretrieval` is a valid option.
68+
$allowed = array_keys($this->_customdata[1] ?? []);
69+
if (!in_array((int) ($data['periodretrieval'] ?? null), $allowed, false)) {
70+
$errors['periodretrieval'] = get_string('invaliddata', 'error');
71+
}
72+
73+
return $errors;
6374
}
6475
}

collectors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,17 @@
4949
switch ($action) {
5050
case 'disable':
5151
$plugins[$name]->set_enabled(false);
52+
\tool_cloudmetrics\event\collector_plugin_disabled::create([
53+
'context' => $syscontext,
54+
'other' => ['pluginname' => $name],
55+
])->trigger();
5256
break;
5357
case 'enable':
5458
$plugins[$name]->set_enabled(true);
59+
\tool_cloudmetrics\event\collector_plugin_enabled::create([
60+
'context' => $syscontext,
61+
'other' => ['pluginname' => $name],
62+
])->trigger();
5563
break;
5664
}
5765
redirect($return);

lang/en/tool_cloudmetrics.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,13 @@
125125

126126
// Backfill progress.
127127
$string['backfillcomplete'] = 'Backfilling data for {$a} complete.';
128+
$string['backfilldata'] = 'Backfill data';
128129
$string['backfillsaving'] = 'Saving metric items for {$a}.';
129130
$string['backfillgenerating'] = 'Generating metric items for {$a}.';
130131

131132
// Groups.
132133
$string['user_activity'] = 'User activity';
134+
135+
// Event logs.
136+
$string['eventcollectorplugindisabled'] = 'Collector plugin disabled';
137+
$string['eventcollectorpluginenabled'] = 'Collector plugin enabled';

templates/chart_page.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<br>
6666
{{{chart}}}
6767
{{#backfillable}}
68-
<a href="{{{backfillurl}}}">
68+
<a href="{{backfillurl}}">
6969
{{#str}} return_to_backfill, tool_cloudmetrics, {{metriclabeltolower}} {{/str}}
7070
</a>
7171
{{/backfillable}}

0 commit comments

Comments
 (0)