Skip to content

Commit 3efa823

Browse files
malteschmitzdaniil-berg
authored andcommitted
fix: rename tag itemtype to table name
This closes #79
1 parent 6cb15da commit 3efa823

10 files changed

Lines changed: 89 additions & 17 deletions

File tree

classes/form/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function definition(): void {
8686
label: new lang_string('settings:metric_enabled', 'tool_monitoring'),
8787
);
8888
$this->add_tags_field(
89-
itemtype: 'metrics',
89+
itemtype: registered_metric::TABLE,
9090
component: 'tool_monitoring',
9191
);
9292
if (!is_null($this->metric->configclass)) {

classes/metrics_manager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function fetch(bool $collect = true, bool|null $enabled = true, array $ta
148148
$where = '(m.component,m.name) IN (' . implode(',', $inplaceholders) . ')';
149149
$join = '';
150150
// Filter by tags.
151-
$tagsenabled = core_tag_tag::is_enabled('tool_monitoring', 'metrics');
151+
$tagsenabled = core_tag_tag::is_enabled('tool_monitoring', registered_metric::TABLE);
152152
if ($tagsenabled) {
153153
if (!empty($tagnames)) {
154154
$tagcollid = $DB->get_field('tag_coll', 'id', ['name' => 'monitoring', 'component' => 'tool_monitoring']);
@@ -163,7 +163,7 @@ public function fetch(bool $collect = true, bool|null $enabled = true, array $ta
163163
$params += $inparams;
164164
$params += [
165165
'tagcomponent' => 'tool_monitoring',
166-
'tagitemtype' => 'metrics',
166+
'tagitemtype' => registered_metric::TABLE,
167167
'tagcount' => count($tagids),
168168
];
169169
$subselect = "SELECT ti.itemid
@@ -252,7 +252,7 @@ public function sync(bool $collect = true, bool $delete = false): self {
252252
// Optionally, delete the former and insert the latter.
253253
if ($delete) {
254254
foreach ($existingrecords as $record) {
255-
core_tag_tag::remove_all_item_tags('tool_monitoring', 'metrics', $record->id);
255+
core_tag_tag::remove_all_item_tags('tool_monitoring', registered_metric::TABLE, $record->id);
256256
}
257257
[$oprphansql, $orphanparams] = $DB->get_in_or_equal(array_column($existingrecords, 'id'), onemptyitems: null);
258258
$DB->delete_records_select(registered_metric::TABLE, "id $oprphansql", $orphanparams);

classes/output/overview.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function remove_tag_url(core_tag_tag $tag): moodle_url {
108108
public function export_for_template(renderer_base $output): array {
109109
global $DB;
110110
$tagcollid = $DB->get_field('tag_coll', 'id', ['name' => 'monitoring', 'component' => 'tool_monitoring']);
111-
$tagsenabled = core_tag_tag::is_enabled('tool_monitoring', 'metrics');
111+
$tagsenabled = core_tag_tag::is_enabled('tool_monitoring', registered_metric::TABLE);
112112
$managetagsurl = new moodle_url('/tag/manage.php', ['tc' => $tagcollid]);
113113
$lines = [];
114114
foreach ($this->metrics as $qualifiedname => $metric) {
@@ -123,7 +123,7 @@ public function export_for_template(renderer_base $output): array {
123123
'config_url' => $configurl->out(escaped: false),
124124
];
125125
if ($tagsenabled) {
126-
$tags = core_tag_tag::get_item_tags('tool_monitoring', 'metrics', $metric->id);
126+
$tags = core_tag_tag::get_item_tags('tool_monitoring', registered_metric::TABLE, $metric->id);
127127
$line['tags'] = array_map(
128128
fn (core_tag_tag $tag): array => [
129129
'id' => $tag->id,

classes/registered_metric.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function to_form_data(): array {
255255
$formdata = [];
256256
}
257257
$formdata['enabled'] = $this->enabled;
258-
$formdata['tags'] = core_tag_tag::get_item_tags_array('tool_monitoring', 'metrics', $this->id);
258+
$formdata['tags'] = core_tag_tag::get_item_tags_array('tool_monitoring', self::TABLE, $this->id);
259259
return $formdata;
260260
}
261261

@@ -292,7 +292,7 @@ public function update_with_form_data(stdClass $formdata): void {
292292
// This only actually performs DB queries if tags were either added, removed, or their order changed.
293293
core_tag_tag::set_item_tags(
294294
component: 'tool_monitoring',
295-
itemtype: 'metrics',
295+
itemtype: self::TABLE,
296296
itemid: $this->id,
297297
context: context_system::instance(),
298298
tagnames: $formdata->tags

db/tag.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131

3232
defined('MOODLE_INTERNAL') || die();
3333

34+
use tool_monitoring\registered_metric;
35+
3436
$tagareas = [
3537
[
36-
'itemtype' => 'metrics',
38+
'itemtype' => registered_metric::TABLE,
3739
'customurl' => '/admin/tool/monitoring/',
3840
'collection' => 'monitoring',
3941
'searchable' => false,

db/upgrade.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Upgrade steps for tool_monitoring.
19+
*
20+
* @package tool_monitoring
21+
* @copyright 2025 MootDACH DevCamp
22+
* Daniel Fainberg <d.fainberg@tu-berlin.de>
23+
* Martin Gauk <martin.gauk@tu-berlin.de>
24+
* Sebastian Rupp <sr@artcodix.com>
25+
* Malte Schmitz <mal.schmitz@uni-luebeck.de>
26+
* Melanie Treitinger <melanie.treitinger@ruhr-uni-bochum.de>
27+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
30+
defined('MOODLE_INTERNAL') || die();
31+
32+
use tool_monitoring\registered_metric;
33+
34+
/**
35+
* Upgrade code for the monitoring tool.
36+
*
37+
* @param int $oldversion
38+
* @return bool
39+
* @throws ddl_exception
40+
* @throws dml_exception
41+
*/
42+
function xmldb_tool_monitoring_upgrade(int $oldversion): bool {
43+
global $DB;
44+
45+
if ($oldversion < 2026041000) {
46+
$transaction = $DB->start_delegated_transaction();
47+
48+
// The tag itemtype must match an existing DB table name. Older versions used "metrics",
49+
// but the actual records live in "tool_monitoring_metrics", so migrate both area and instances.
50+
$DB->set_field(
51+
'tag_area',
52+
'itemtype',
53+
registered_metric::TABLE,
54+
['component' => 'tool_monitoring', 'itemtype' => 'metrics']
55+
);
56+
$DB->set_field(
57+
'tag_instance',
58+
'itemtype',
59+
registered_metric::TABLE,
60+
['component' => 'tool_monitoring', 'itemtype' => 'metrics']
61+
);
62+
63+
$transaction->allow_commit();
64+
65+
upgrade_plugin_savepoint(true, 2026041000, 'tool', 'monitoring');
66+
}
67+
68+
return true;
69+
}

lang/en/tool_monitoring.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
$string['subplugintype_monitoringexporter_plural'] = 'Exporter types';
7979

80-
$string['tagarea_metrics'] = 'Metrics';
80+
$string['tagarea_tool_monitoring_metrics'] = 'Metrics';
8181
$string['tagcollection_monitoring'] = 'Monitoring';
8282

8383
$string['testing:metric:testing_simple_metric_config:notpromotedstring'] = 'String with a default; not promoted to any property.';

tests/behat/behat_tool_monitoring.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131

3232
use Behat\Step\Given;
33+
use tool_monitoring\registered_metric;
3334

3435
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
3536

@@ -66,7 +67,7 @@ public function the_tag_area_is(string $state): void {
6667
};
6768
$area = $DB->get_record(
6869
table: 'tag_area',
69-
conditions: ['itemtype' => 'metrics', 'component' => 'tool_monitoring'],
70+
conditions: ['itemtype' => registered_metric::TABLE, 'component' => 'tool_monitoring'],
7071
strictness: MUST_EXIST,
7172
);
7273
core_tag_area::update($area, ['enabled' => $enabled]);

tests/metrics_manager_test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public function test_sync_deletes_tag_instances_for_deleted_metrics(): void {
553553
// Add tags alpha and beta to metric foo.
554554
core_tag_tag::set_item_tags(
555555
component: 'tool_monitoring',
556-
itemtype: 'metrics',
556+
itemtype: registered_metric::TABLE,
557557
itemid: $metricid,
558558
context: context_system::instance(),
559559
tagnames: ['alpha', 'beta'],
@@ -562,7 +562,7 @@ public function test_sync_deletes_tag_instances_for_deleted_metrics(): void {
562562
2,
563563
$DB->count_records('tag_instance', [
564564
'component' => 'tool_monitoring',
565-
'itemtype' => 'metrics',
565+
'itemtype' => registered_metric::TABLE,
566566
'itemid' => $metricid,
567567
]),
568568
);
@@ -577,10 +577,10 @@ public function test_sync_deletes_tag_instances_for_deleted_metrics(): void {
577577
0,
578578
$DB->count_records('tag_instance', [
579579
'component' => 'tool_monitoring',
580-
'itemtype' => 'metrics',
580+
'itemtype' => registered_metric::TABLE,
581581
'itemid' => $metricid,
582582
]),
583583
);
584-
self::assertSame([], core_tag_tag::get_item_tags_array('tool_monitoring', 'metrics', $metricid));
584+
self::assertSame([], core_tag_tag::get_item_tags_array('tool_monitoring', registered_metric::TABLE, $metricid));
585585
}
586586
}

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
defined('MOODLE_INTERNAL') || die();
3333

3434
$plugin->component = 'tool_monitoring';
35-
$plugin->release = '0.2.1';
36-
$plugin->version = 2026030200;
35+
$plugin->release = '0.2.2';
36+
$plugin->version = 2026041000;
3737
$plugin->requires = 2025041400; // Moodle 5.0.
3838
$plugin->maturity = MATURITY_ALPHA;

0 commit comments

Comments
 (0)