Skip to content

MBS-10145: add a previewall page #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: rebase
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion amd/build/management.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/management.min.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions amd/build/previewall.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/previewall.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions amd/src/management.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,10 @@ const wipeModal = (event) => {
try {
await wipe();
reload();
return;
} catch (error) {
displayException(error);
return;
}
}).catch((err) => {
if (err.message) {
Expand Down
45 changes: 45 additions & 0 deletions amd/src/previewall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// This file is part of Moodle - http://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/>.

/**
* Elements preview all.
*
* @module tiny_elements/previewall
* @copyright 2025 Tobias Garske
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import {add as addToast} from 'core/toast';
import {get_string as getString} from "core/str";

export const init = async() => {
// Add listener to copy all elements as string.
document.getElementById("elements_to_clipboard").addEventListener("click", () => {
const allelements = document.getElementById("elements_output");
navigator.clipboard.writeText(allelements.value)
.then(() => {
addToast(getString('copysuccess', 'tiny_elements'), {
type: 'info',
});
return;
})
.catch(() => {
addToast(getString('copyfail', 'tiny_elements'), {
type: 'warning',
});
return;
});
});
};
2 changes: 1 addition & 1 deletion classes/exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function export_flavors_and_variants(
$params
);
$this->write_elements($xmlwriter, constants::TABLES['compflavor'], $compflavors);
$flavornames = array_unique(array_column($compflavors, 'flavor'));
$flavornames = array_unique(array_column($compflavors, 'flavorname'));

$sql = ' = name';
if (!empty($categoryname)) {
Expand Down
1 change: 1 addition & 0 deletions classes/external/wipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function execute_parameters(): external_function_parameters {

/**
* Implementation of web service tiny_elements_wipe
* @param int $contextid
* @return array
*/
public static function execute(int $contextid): array {
Expand Down
1 change: 1 addition & 0 deletions classes/form/management_editlicense_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function definition(): void {
$mform->removeElement('adddummy');

$mform->setAttributes(['data-formtype' => 'tiny_elements_editlicense']);
$mform->disable_form_change_checker();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lang/de/tiny_elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
$string['componentname_help'] = 'Name der Komponente zur internen Verwendung (auch als Klassenname in CSS)';
$string['components'] = 'Komponenten';
$string['content'] = 'Inhalt';
$string['copyasstring'] = 'Alle Elemente als String in den Zwischenspeicher laden';
$string['copyfail'] = 'Fehler beim Kopieren aufgetreten';
$string['copyof'] = 'Kopie von {$a}';
$string['copysuccess'] = 'Elemente in den Zwischenspeicher kopiert';
$string['css'] = 'CSS';
$string['delete'] = 'Element "{$a}" löschen';
$string['deletewarning'] = 'Sind Sie sicher, dass Sie dieses Element löschen möchten?';
Expand Down Expand Up @@ -94,6 +97,8 @@
$string['js'] = 'JS';
$string['linktomanagerdesc'] = 'Gehen Sie zu <a href="{$a}">Verwaltung</a>, um Änderungen vorzunehmen.';
$string['linktomanagername'] = 'Link zur Verwaltung';
$string['linktopreviewall'] = 'Link zur Vorschau';
$string['linktopreviewall_desc'] = 'Gehen Sie zu <a href="{$a}">Vorschau</a>, um alle Elements anzusehen und als String zu exportieren.';
$string['manage'] = 'Verwalten';
$string['management'] = 'Verwaltung';
$string['menuitem_elements'] = 'Kurselemente';
Expand Down
6 changes: 6 additions & 0 deletions lang/en/tiny_elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
$string['componentname_help'] = 'Name of the component use for internal use (also as a class name in CSS)';
$string['components'] = 'Components';
$string['content'] = 'Content';
$string['copyasstring'] = 'Save all elements to clipboard';
$string['copyfail'] = 'Error copying elements';
$string['copyof'] = 'Copy of {$a}';
$string['copysuccess'] = 'Elements copied to clipboard';
$string['css'] = 'CSS';
$string['delete'] = 'Delete item "{$a}"';
$string['deletewarning'] = 'Are you sure you want to delete this item.';
Expand Down Expand Up @@ -91,6 +94,9 @@
$string['js'] = 'JS';
$string['linktomanagerdesc'] = 'Go to <a href="{$a}">management page</a> to edit categories, components, flavors and variants.';
$string['linktomanagername'] = 'Link to management';
$string['linktopreviewall'] = 'Link to preview';
$string['linktopreviewall_desc'] = 'Go to <a href="{$a}">preview</a> to watch all elements and export as string.';

$string['manage'] = 'Manage';
$string['management'] = 'Management';
$string['menuitem_elements'] = 'Course elements';
Expand Down
6 changes: 1 addition & 5 deletions preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@

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

if (empty($flavordata)) {
echo str_replace('{{FLAVOR}}', '', $componentdata->code);
} else {
echo str_replace('{{FLAVOR}}', $flavordata->name, $componentdata->code);
}
echo $componentdata->code;

echo $OUTPUT->footer();
96 changes: 96 additions & 0 deletions previewall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
// This file is part of Moodle - http://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/>.

/**
* Creates a preview for all elements components in all flavors and variants.
*
* @package tiny_elements
* @copyright 2024 Tobias Garske, ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require('../../../../../config.php');

require_login();

$url = new moodle_url('/lib/editor/tiny/plugins/elements/previewall.php', []);
$PAGE->set_url($url);
$PAGE->set_context(context_system::instance());
$PAGE->set_heading($SITE->fullname);

echo $OUTPUT->header();

// Iterate over every category.
$categorydata = $DB->get_records('tiny_elements_compcat');
$outputbuffer = '';
foreach ($categorydata as $category) {
$outputbuffer .= '<h2>' . $category->name . '</h2>';
$category = $category->name;
$componentdata = $DB->get_records('tiny_elements_component', ['categoryname' => $category]);
// Iterate over every component.
foreach ($componentdata as $component) {
$outputbuffer .= '<h3>' . $component->name . '</h3>';
// Select corresponding flavors and variants.
$sql = "SELECT * FROM {tiny_elements_flavor} fla
JOIN {tiny_elements_comp_flavor} comfla ON comfla.flavorname = fla.name
WHERE fla.categoryname = :categoryname AND comfla.componentname = :componentname";
$allflavors = $DB->get_records_sql($sql, ['categoryname' => $component->categoryname, 'componentname' => $component->name]);
$sql = "SELECT * FROM {tiny_elements_variant} var
JOIN {tiny_elements_comp_variant} covar ON var.name = covar.variant
WHERE covar.componentname = :componentname";
$allvariants = $DB->get_records_sql($sql, ['componentname' => $component->name]);
// Add default "variant".
array_unshift($allvariants, (object) ['name' => '']);
foreach ($allflavors as $flavordata) {
foreach ($allvariants as $variantdata) {
$tmpcomponent = clone $component;
$variant = '';
if (strlen($variantdata->name) > 0) {
$variant = 'elements-' . $variantdata->name . '-variant';
}
$varianthtml = '';
// Build elements by replacing placeholders.
$tmpcomponent->code = str_replace('{{CATEGORY}}', 'elements-' . $category, $tmpcomponent->code);
$tmpcomponent->code = str_replace('{{COMPONENT}}', 'elements-' . $tmpcomponent->name, $tmpcomponent->code);
$tmpcomponent->code = str_replace(
'{{FLAVOR}}',
'elements-' . $flavordata->name . '-flavor',
$tmpcomponent->code
);
$tmpcomponent->code = str_replace('{{VARIANTS}}', $variant, $tmpcomponent->code);
$tmpcomponent->code = str_replace('{{VARIANTSHTML}}', $varianthtml, $tmpcomponent->code);
$tmpcomponent->code = str_replace(
'{{PLACEHOLDER}}',
$flavordata->name . ' ' . $variantdata->name,
$tmpcomponent->code
);
// Output element.
$tmpcomponent->code = tiny_elements\local\utils::replace_pluginfile_urls($tmpcomponent->code, true);
$outputbuffer .= $tmpcomponent->code;
}
}
}
}

// Create button to copy elements to clipboard.
echo "<button id='elements_to_clipboard' type='button' class='btn btn-primary mb-3'>"
. get_string('copyasstring', 'tiny_elements') . "</button>";
echo "<input type='hidden' id='elements_output' value='$outputbuffer' />";
$PAGE->requires->js_call_amd('tiny_elements/previewall', 'init');

echo $outputbuffer;

echo $OUTPUT->footer();
11 changes: 11 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@
(new moodle_url('/lib/editor/tiny/plugins/elements/management.php'))->out()
)
));

// Add text with link to previewpage as setting.
$settings->add(new admin_setting_description(
'tiny_elements/previewall',
get_string('linktopreviewall', 'tiny_elements'),
get_string(
'linktopreviewall_desc',
'tiny_elements',
(new moodle_url('/lib/editor/tiny/plugins/elements/previewall.php'))->out()
)
));
}
13 changes: 6 additions & 7 deletions templates/management.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<h2>{{#str}} compcat, tiny_elements {{/str}}</h2>
<div class="row row-cols-8">
{{#compcats}}
<div
<div
class="col mb-4 item compcat {{^id}}addcontainer{{/id}}" data-compcat="{{name}}" data-id="{{id}}"
data-categoryname="{{categoryname}}"
>
Expand Down Expand Up @@ -154,11 +154,11 @@
</div>

<div class="row{{^showbulkedit}} hidden{{/showbulkedit}}">
<div class="col mb-4">
<div class="col mb-4">
<button id="elements_displaynames_flavor_button" class="btn btn-secondary">{{#str}} bulk_edit_flavor_displaynames, tiny_elements {{/str}}</button>
</div>
</div>

<h2>{{#str}} components, tiny_elements {{/str}} </h2>

<div class="row row-cols-12">
Expand Down Expand Up @@ -207,15 +207,15 @@
</div>

<div class="row{{^showbulkedit}} hidden{{/showbulkedit}}">
<div class="col mb-4">
<div class="col mb-4">
<button id="elements_displaynames_button" class="btn btn-secondary">{{#str}} bulk_edit_displaynames, tiny_elements {{/str}}</button>
</div>
</div>

<h2>{{#str}} variants, tiny_elements {{/str}}</h2>
<div class="row row-cols-12">
{{#variant}}
<div
<div
class="col mb-4 item variant {{name}} {{#id}}hidden{{/id}}{{^id}}addcontainer{{/id}}" data-categoryname="{{categoryname}}">
<div class="card h-100">
<div class="card-body">
Expand Down Expand Up @@ -246,9 +246,8 @@
</div>

<div class="row{{^showbulkedit}} hidden{{/showbulkedit}}">
<div class="col mb-4">
<div class="col mb-4">
<button id="elements_displaynames_variant_button" class="btn btn-secondary">{{#str}} bulk_edit_variant_displaynames, tiny_elements {{/str}}</button>
</div>
</div>

</div>
2 changes: 1 addition & 1 deletion templates/management_preview.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
</iframe>
{{/flavors}}
{{/body}}
{{/ core/modal }}
{{/ core/modal }}