Skip to content

Commit 43b975c

Browse files
committed
MBS-10005: Wipe
1 parent 5e17a51 commit 43b975c

File tree

9 files changed

+190
-7
lines changed

9 files changed

+190
-7
lines changed

amd/build/management.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/management.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/management.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ export const init = async(params) => {
110110
});
111111
});
112112

113+
// Add listener to wipe all items.
114+
let wipebutton = document.getElementById('elements_wipe');
115+
if (wipebutton) {
116+
wipebutton.addEventListener('click', async(e) => {
117+
wipeModal(e);
118+
});
119+
}
120+
113121
// Add image and text to item setting click area.
114122
let enlargeItems = document.querySelectorAll(
115123
'.flavor .card-body > .clickingextended, .component .card-body > .clickingextended, .variant .card-body > .clickingextended'
@@ -346,6 +354,29 @@ const deleteModal = (event, id, title, table) => {
346354
});
347355
};
348356

357+
/**
358+
* Show a modal to confirm wiping all items.
359+
* @param {*} event
360+
*/
361+
const wipeModal = (event) => {
362+
event.preventDefault();
363+
364+
deleteCancelPromise(
365+
getString('wipe', 'tiny_elements'),
366+
getString('wipewarning', 'tiny_elements')
367+
).then(async() => {
368+
try {
369+
await wipe();
370+
reload();
371+
} catch (error) {
372+
displayException(error);
373+
}
374+
}).catch((err) => {
375+
Log.error(err.message);
376+
return;
377+
});
378+
};
379+
349380
/**
350381
* Delete elements items.
351382
* @param {*} id
@@ -364,6 +395,18 @@ export const deleteItem = (
364395
}
365396
}])[0];
366397

398+
/**
399+
* Wipe all elements items.
400+
* @returns {mixed}
401+
*/
402+
export const wipe = () => fetchMany(
403+
[{
404+
methodname: 'tiny_elements_wipe',
405+
args: {
406+
"contextid": 1,
407+
}
408+
}])[0];
409+
367410
/**
368411
* Show items after clicking a compcat.
369412
* @param {*} event

classes/external/management.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 tiny_elements\external;
18+
19+
use core\external\external_single_structure;
20+
use core\external\external_value;
21+
use core\external\external_api;
22+
23+
/**
24+
* Class management
25+
*
26+
* @package tiny_elements
27+
* @copyright 2025 ISB Bayern
28+
* @author Stefan Hanauska <[email protected]>
29+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30+
*/
31+
class management extends external_api {
32+
/**
33+
* Return all tiny_elements data that has been modified since the given timestamp.
34+
* @param int $contextid the context id (currently only system context is supported)
35+
* @param int $timestamp the timestamp of the last update
36+
*/
37+
public static function get_update(int $contextid = SYSCONTEXTID, int $timestamp = 0): array {
38+
global $DB;
39+
$params = self::validate_parameters(self::get_update_parameters(), [
40+
'timestamp' => $timestamp,
41+
]);
42+
$timestamp = $params['timestamp'];
43+
$contextid = $params['contextid'];
44+
$context = \core\context::instance_by_id($contextid);
45+
self::validate_context($context);
46+
47+
require_capability('tiny/elements:manage', $context);
48+
49+
$data = [];
50+
51+
$categories = $DB->get_records_sql(
52+
'SELECT * FROM {tiny_elements_compcat} WHERE timemodified >= ?',
53+
[$timestamp]
54+
);
55+
$data['categories'] = $categories;
56+
57+
$categorynames = array_map(function ($a) {
58+
return $a->name;
59+
}, $categories);
60+
61+
[$sql, $params] = $DB->get_in_or_equal($categorynames, SQL_PARAMS_NAMED);
62+
$params['timestamp'] = $timestamp;
63+
64+
$components = $DB->get_records_sql(
65+
'SELECT * FROM {tiny_elements_component} WHERE timemodified >= ? AND categoryname ' . $sql,
66+
$params
67+
);
68+
$data['components'] = $components;
69+
70+
$componentnames = array_map(function ($a) {
71+
return $a->name;
72+
}, $components);
73+
74+
[$sql, $params] = $DB->get_in_or_equal($componentnames, SQL_PARAMS_NAMED);
75+
76+
$flavors = $DB->get_records_sql(
77+
'SELECT * FROM {tiny_elements_flavor} WHERE timemodified >= ? AND categoryname ' . $sql,
78+
$params
79+
);
80+
$data['flavors'] = $flavors;
81+
82+
$variants = $DB->get_records_sql(
83+
'SELECT * FROM {tiny_elements_variant} WHERE timemodified >= ? AND categoryname ' . $sql,
84+
$params
85+
);
86+
$data['variants'] = $variants;
87+
88+
$params['componentnames'] = $componentnames;
89+
[$sql, $params] = $DB->get_in_or_equal($componentnames, SQL_PARAMS_NAMED);
90+
91+
$compflavors = $DB->get_records_sql(
92+
'SELECT * FROM {tiny_elements_comp_flavor} WHERE timemodified >= ? AND componentname ' . $sql,
93+
$params
94+
);
95+
$data['compflavors'] = $compflavors;
96+
97+
$compvariants = $DB->get_records_sql(
98+
'SELECT * FROM {tiny_elements_comp_variant} WHERE timemodified >= ? AND componentname ' . $sql,
99+
$params
100+
);
101+
$data['compvariants'] = $compvariants;
102+
103+
return $data;
104+
}
105+
106+
/**
107+
* Returns the description of the parameters for the get_update function.
108+
*
109+
* @return array
110+
*/
111+
public static function get_update_parameters(): array {
112+
return [
113+
'timestamp' => new external_value(PARAM_INT, 'Timestamp of the last update', VALUE_OPTIONAL, 0),
114+
];
115+
}
116+
117+
/**
118+
* Returns the description of the result for the get_update function.
119+
*
120+
* @return external_single_structure
121+
*/
122+
public static function get_update_returns(): external_single_structure {
123+
return new external_single_structure([
124+
'timestamp' => new external_value(PARAM_INT, 'Timestamp of the last update'),
125+
'data' => new external_single_structure(PARAM_RAW, 'Data of the update', VALUE_OPTIONAL, []),
126+
]);
127+
}
128+
}

classes/manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ public function wipe(): void {
583583
$DB->delete_records(constants::TABLES['component']);
584584
$DB->delete_records(constants::TABLES['flavor']);
585585
$DB->delete_records(constants::TABLES['variant']);
586-
$DB->delete_records(constants::TABLES['comp_flavor']);
587-
$DB->delete_records(constants::TABLES['comp_variant']);
586+
$DB->delete_records(constants::TABLES['compflavor']);
587+
$DB->delete_records(constants::TABLES['compvariant']);
588588

589589
$fs = get_file_storage();
590590
$fs->delete_area_files($this->contextid, 'tiny_elements', 'images');

db/services.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@
7070
'ajax' => true,
7171
'capabilities' => 'tiny/elements:manage',
7272
],
73+
'tiny_elements_wipe' => [
74+
'classname' => 'tiny_elements\external\wipe',
75+
'description' => 'Wipe all elements data',
76+
'type' => 'write',
77+
'ajax' => true,
78+
'capabilities' => 'tiny/elements:manage',
79+
],
7380
];

lang/en/tiny_elements.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,5 @@
112112
$string['validclassname'] = 'The name has to be a valid css class name. It may only contain letters, numbers and the characters "-" and "_". It must start with a letter or "_". Using only lowercase letters is recommended.';
113113
$string['variant'] = 'Variant';
114114
$string['variants'] = 'Variants';
115+
$string['wipe'] = 'Wipe all elements';
116+
$string['wipewarning'] = 'This will remove all categories, files, components, flavors and variants. There is no way to undo this step. Make sure, you have a backup!';

templates/management.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@
6363
}}
6464

6565
<div class="container-fluid">
66-
<div class="row row-cols-1 mb-2">
66+
<div class="row mb-2">
6767
<div class="col">
6868
<a href="{{exportlink}}" class="btn btn-primary"> {{#str}} export, tiny_elements {{/str}}</a>
69-
<a href="#" id="elements_import" class="ml-2 btn btn-primary"> {{#str}} import, tiny_elements {{/str}}</a>
69+
<a href="#" id="elements_import" class="ml-2 btn btn-primary"> {{#str}} import, tiny_elements {{/str}}</a>
70+
</div>
71+
<div class="col">
72+
<a href="#" id="elements_wipe" class="ml-2 btn btn-secondary">{{#pix}} req, core {{/pix}} {{#str}} wipe, tiny_elements {{/str}}</a>
7073
</div>
7174
</div>
7275

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
$plugin->release = '1.0.0';
3030
$plugin->requires = 2024042200;
3131
$plugin->maturity = MATURITY_BETA;
32-
$plugin->version = 2025041501;
32+
$plugin->version = 2025052001;

0 commit comments

Comments
 (0)