Skip to content

Commit 33042de

Browse files
TobiGash-csg
authored andcommitted
MBS-10145: add a previewall page
1 parent 1ad27ec commit 33042de

14 files changed

+180
-15
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/build/previewall.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/previewall.min.js.map

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

amd/src/management.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,10 @@ const wipeModal = (event) => {
396396
try {
397397
await wipe();
398398
reload();
399+
return;
399400
} catch (error) {
400401
displayException(error);
402+
return;
401403
}
402404
}).catch((err) => {
403405
if (err.message) {

amd/src/previewall.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
/**
17+
* Elements preview all.
18+
*
19+
* @module tiny_elements/previewall
20+
* @copyright 2025 Tobias Garske
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
24+
import {add as addToast} from 'core/toast';
25+
import {get_string as getString} from "core/str";
26+
27+
export const init = async() => {
28+
// Add listener to copy all elements as string.
29+
document.getElementById("elements_to_clipboard").addEventListener("click", () => {
30+
const allelements = document.getElementById("elements_output");
31+
navigator.clipboard.writeText(allelements.value)
32+
.then(() => {
33+
addToast(getString('copysuccess', 'tiny_elements'), {
34+
type: 'info',
35+
});
36+
return;
37+
})
38+
.catch(() => {
39+
addToast(getString('copyfail', 'tiny_elements'), {
40+
type: 'warning',
41+
});
42+
return;
43+
});
44+
});
45+
};

classes/external/wipe.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static function execute_parameters(): external_function_parameters {
4545

4646
/**
4747
* Implementation of web service tiny_elements_wipe
48+
* @param int $contextid
4849
* @return array
4950
*/
5051
public static function execute(int $contextid): array {

lang/de/tiny_elements.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
$string['componentname_help'] = 'Name der Komponente zur internen Verwendung (auch als Klassenname in CSS)';
4848
$string['components'] = 'Komponenten';
4949
$string['content'] = 'Inhalt';
50+
$string['copyasstring'] = 'Alle Elemente als String in den Zwischenspeicher laden';
51+
$string['copyfail'] = 'Fehler beim Kopieren aufgetreten';
5052
$string['copyof'] = 'Kopie von {$a}';
53+
$string['copysuccess'] = 'Elemente in den Zwischenspeicher kopiert';
5154
$string['css'] = 'CSS';
5255
$string['delete'] = 'Element "{$a}" löschen';
5356
$string['deletewarning'] = 'Sind Sie sicher, dass Sie dieses Element löschen möchten?';
@@ -94,6 +97,8 @@
9497
$string['js'] = 'JS';
9598
$string['linktomanagerdesc'] = 'Gehen Sie zu <a href="{$a}">Verwaltung</a>, um Änderungen vorzunehmen.';
9699
$string['linktomanagername'] = 'Link zur Verwaltung';
100+
$string['linktopreviewall'] = 'Link zur Vorschau';
101+
$string['linktopreviewall_desc'] = 'Gehen Sie zu <a href="{$a}">Vorschau</a>, um alle Elements anzusehen und als String zu exportieren.';
97102
$string['manage'] = 'Verwalten';
98103
$string['management'] = 'Verwaltung';
99104
$string['menuitem_elements'] = 'Kurselemente';

lang/en/tiny_elements.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
$string['componentname_help'] = 'Name of the component use for internal use (also as a class name in CSS)';
4646
$string['components'] = 'Components';
4747
$string['content'] = 'Content';
48+
$string['copyasstring'] = 'Save all elements to clipboard';
49+
$string['copyfail'] = 'Error copying elements';
4850
$string['copyof'] = 'Copy of {$a}';
51+
$string['copysuccess'] = 'Elements copied to clipboard';
4952
$string['css'] = 'CSS';
5053
$string['delete'] = 'Delete item "{$a}"';
5154
$string['deletewarning'] = 'Are you sure you want to delete this item.';
@@ -91,6 +94,9 @@
9194
$string['js'] = 'JS';
9295
$string['linktomanagerdesc'] = 'Go to <a href="{$a}">management page</a> to edit categories, components, flavors and variants.';
9396
$string['linktomanagername'] = 'Link to management';
97+
$string['linktopreviewall'] = 'Link to preview';
98+
$string['linktopreviewall_desc'] = 'Go to <a href="{$a}">preview</a> to watch all elements and export as string.';
99+
94100
$string['manage'] = 'Manage';
95101
$string['management'] = 'Management';
96102
$string['menuitem_elements'] = 'Course elements';

preview.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@
5353

5454
$componentdata->code = tiny_elements\local\utils::replace_pluginfile_urls($componentdata->code, true);
5555

56-
if (empty($flavordata)) {
57-
echo str_replace('{{FLAVOR}}', '', $componentdata->code);
58-
} else {
59-
echo str_replace('{{FLAVOR}}', $flavordata->name, $componentdata->code);
60-
}
56+
echo $componentdata->code;
6157

6258
echo $OUTPUT->footer();

previewall.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
/**
18+
* Creates a preview for all elements components in all flavors and variants.
19+
*
20+
* @package tiny_elements
21+
* @copyright 2024 Tobias Garske, ISB Bayern
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
require('../../../../../config.php');
26+
27+
require_login();
28+
29+
$url = new moodle_url('/lib/editor/tiny/plugins/elements/previewall.php', []);
30+
$PAGE->set_url($url);
31+
$PAGE->set_context(context_system::instance());
32+
$PAGE->set_heading($SITE->fullname);
33+
34+
echo $OUTPUT->header();
35+
36+
// Iterate over every category.
37+
$categorydata = $DB->get_records('tiny_elements_compcat');
38+
$outputbuffer = '';
39+
foreach ($categorydata as $category) {
40+
$outputbuffer .= '<h2>' . $category->name . '</h2>';
41+
$category = $category->name;
42+
$componentdata = $DB->get_records('tiny_elements_component', ['categoryname' => $category]);
43+
// Iterate over every component.
44+
foreach ($componentdata as $component) {
45+
$outputbuffer .= '<h3>' . $component->name . '</h3>';
46+
// Select corresponding flavors and variants.
47+
$sql = "SELECT * FROM {tiny_elements_flavor} fla
48+
JOIN {tiny_elements_comp_flavor} comfla ON comfla.flavorname = fla.name
49+
WHERE fla.categoryname = :categoryname AND comfla.componentname = :componentname";
50+
$allflavors = $DB->get_records_sql($sql, ['categoryname' => $component->categoryname, 'componentname' => $component->name]);
51+
$sql = "SELECT * FROM {tiny_elements_variant} var
52+
JOIN {tiny_elements_comp_variant} covar ON var.name = covar.variant
53+
WHERE covar.componentname = :componentname";
54+
$allvariants = $DB->get_records_sql($sql, ['componentname' => $component->name]);
55+
// Add default "variant".
56+
array_unshift($allvariants, (object) ['name' => '']);
57+
foreach ($allflavors as $flavordata) {
58+
foreach ($allvariants as $variantdata) {
59+
$tmpcomponent = clone $component;
60+
$variant = '';
61+
if (strlen($variantdata->name) > 0) {
62+
$variant = 'elements-' . $variantdata->name . '-variant';
63+
}
64+
$varianthtml = '';
65+
// Build elements by replacing placeholders.
66+
$tmpcomponent->code = str_replace('{{CATEGORY}}', 'elements-' . $category, $tmpcomponent->code);
67+
$tmpcomponent->code = str_replace('{{COMPONENT}}', 'elements-' . $tmpcomponent->name, $tmpcomponent->code);
68+
$tmpcomponent->code = str_replace(
69+
'{{FLAVOR}}',
70+
'elements-' . $flavordata->name . '-flavor',
71+
$tmpcomponent->code
72+
);
73+
$tmpcomponent->code = str_replace('{{VARIANTS}}', $variant, $tmpcomponent->code);
74+
$tmpcomponent->code = str_replace('{{VARIANTSHTML}}', $varianthtml, $tmpcomponent->code);
75+
$tmpcomponent->code = str_replace(
76+
'{{PLACEHOLDER}}',
77+
$flavordata->name . ' ' . $variantdata->name,
78+
$tmpcomponent->code
79+
);
80+
// Output element.
81+
$tmpcomponent->code = tiny_elements\local\utils::replace_pluginfile_urls($tmpcomponent->code, true);
82+
$outputbuffer .= $tmpcomponent->code;
83+
}
84+
}
85+
}
86+
}
87+
88+
// Create button to copy elements to clipboard.
89+
echo "<button id='elements_to_clipboard' type='button' class='btn btn-primary mb-3'>"
90+
. get_string('copyasstring', 'tiny_elements') . "</button>";
91+
echo "<input type='hidden' id='elements_output' value='$outputbuffer' />";
92+
$PAGE->requires->js_call_amd('tiny_elements/previewall', 'init');
93+
94+
echo $outputbuffer;
95+
96+
echo $OUTPUT->footer();

settings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@
5151
(new moodle_url('/lib/editor/tiny/plugins/elements/management.php'))->out()
5252
)
5353
));
54+
55+
// Add text with link to previewpage as setting.
56+
$settings->add(new admin_setting_description(
57+
'tiny_elements/previewall',
58+
get_string('linktopreviewall', 'tiny_elements'),
59+
get_string(
60+
'linktopreviewall_desc',
61+
'tiny_elements',
62+
(new moodle_url('/lib/editor/tiny/plugins/elements/previewall.php'))->out()
63+
)
64+
));
5465
}

templates/management.mustache

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<h2>{{#str}} compcat, tiny_elements {{/str}}</h2>
7777
<div class="row row-cols-8">
7878
{{#compcats}}
79-
<div
79+
<div
8080
class="col mb-4 item compcat {{^id}}addcontainer{{/id}}" data-compcat="{{name}}" data-id="{{id}}"
8181
data-categoryname="{{categoryname}}"
8282
>
@@ -154,11 +154,11 @@
154154
</div>
155155

156156
<div class="row{{^showbulkedit}} hidden{{/showbulkedit}}">
157-
<div class="col mb-4">
157+
<div class="col mb-4">
158158
<button id="elements_displaynames_flavor_button" class="btn btn-secondary">{{#str}} bulk_edit_flavor_displaynames, tiny_elements {{/str}}</button>
159159
</div>
160160
</div>
161-
161+
162162
<h2>{{#str}} components, tiny_elements {{/str}} </h2>
163163

164164
<div class="row row-cols-12">
@@ -207,15 +207,15 @@
207207
</div>
208208

209209
<div class="row{{^showbulkedit}} hidden{{/showbulkedit}}">
210-
<div class="col mb-4">
210+
<div class="col mb-4">
211211
<button id="elements_displaynames_button" class="btn btn-secondary">{{#str}} bulk_edit_displaynames, tiny_elements {{/str}}</button>
212212
</div>
213213
</div>
214214

215215
<h2>{{#str}} variants, tiny_elements {{/str}}</h2>
216216
<div class="row row-cols-12">
217217
{{#variant}}
218-
<div
218+
<div
219219
class="col mb-4 item variant {{name}} {{#id}}hidden{{/id}}{{^id}}addcontainer{{/id}}" data-categoryname="{{categoryname}}">
220220
<div class="card h-100">
221221
<div class="card-body">
@@ -246,9 +246,8 @@
246246
</div>
247247

248248
<div class="row{{^showbulkedit}} hidden{{/showbulkedit}}">
249-
<div class="col mb-4">
249+
<div class="col mb-4">
250250
<button id="elements_displaynames_variant_button" class="btn btn-secondary">{{#str}} bulk_edit_variant_displaynames, tiny_elements {{/str}}</button>
251251
</div>
252252
</div>
253-
254253
</div>

templates/management_preview.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
</iframe>
3939
{{/flavors}}
4040
{{/body}}
41-
{{/ core/modal }}
41+
{{/ core/modal }}

0 commit comments

Comments
 (0)