Skip to content
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
32 changes: 2 additions & 30 deletions classes/importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function import(\stored_file|string $zip, int $draftitemid = 0): void {
$fp->extract_to_storage($zip, $this->contextid, 'tiny_elements', 'import', $draftitemid, '/');
$xmlfile = $fs->get_file($this->contextid, 'tiny_elements', 'import', $draftitemid, '/', constants::FILE_NAME_EXPORT);
if (!$xmlfile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hoppla now there is if (!$xmlfile) { twice

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still there:

$xmlfile = $fs->get_file($this->contextid, 'tiny_elements', 'import', $draftitemid, '/', constants::FILE_NAME_EXPORT);
if (!$xmlfile) {
throw new moodle_exception(get_string('error_import_missing_xml', 'tiny_elements'));
}
if (!$xmlfile) {
throw new moodle_exception(get_string('error_import_missing_xml', 'tiny_elements'));
}
$xmlcontent = $xmlfile->get_content();

$xmlfile = $fs->get_file($this->contextid, 'tiny_elements', 'import', $draftitemid, '/', 'tiny_c4l_export.xml');
throw new moodle_exception(get_string('error_import_missing_xml', 'tiny_elements'));
}
if (!$xmlfile) {
throw new moodle_exception(get_string('error_import_missing_xml', 'tiny_elements'));
Expand Down Expand Up @@ -196,16 +196,13 @@ public function importxml(string $xmlcontent): array {
$componentmap = [];

foreach (constants::TABLES as $table) {
$aliasname = constants::TABLE_ALIASES[$table];
if (!isset($xml->$table) && !isset($xml->$aliasname) && !in_array($table, constants::OPTIONAL_TABLES)) {
if (!isset($xml->$table) && !in_array($table, constants::OPTIONAL_TABLES)) {
throw new moodle_exception(get_string('error_import_missing_table', 'tiny_elements', $table));
}
}

$data = [];

$aliases = array_flip(constants::TABLE_ALIASES);

// Make data usable for further processing.
foreach ($xml as $table => $rows) {
foreach ($rows as $row) {
Expand All @@ -215,8 +212,6 @@ public function importxml(string $xmlcontent): array {
}
if (in_array($table, constants::TABLES)) {
$data[$table][] = $obj;
} else {
$data[$aliases[$table]][] = $obj;
}
}
}
Expand Down Expand Up @@ -247,36 +242,13 @@ public function importxml(string $xmlcontent): array {
self::import_component_variant($componentvariant, $componentmap);
}

self::update_flavor_variant_category();

if (!$this->dryrun) {
local\utils::purge_and_rebuild_caches();
}

return $categorymap;
}

/**
* Updates all flavors and variants that do not have a categoryname yet.
*/
public function update_flavor_variant_category(): void {
global $DB;

$manager = new manager();

$flavors = $DB->get_records('tiny_elements_flavor', ['categoryname' => '']);
foreach ($flavors as $flavor) {
$categoryname = $manager->get_compcatname_for_flavor($flavor->name);
$DB->set_field('tiny_elements_flavor', 'categoryname', $categoryname, ['id' => $flavor->id]);
}

$variants = $DB->get_records('tiny_elements_variant', ['categoryname' => '']);
foreach ($variants as $variant) {
$categoryname = $manager->get_compcatname_for_variant($variant->name);
$DB->set_field('tiny_elements_variant', 'categoryname', $categoryname, ['id' => $variant->id]);
}
}

/**
* Import a component category.
*
Expand Down
9 changes: 0 additions & 9 deletions classes/local/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ class constants {
'flavor' => 'tiny_elements_flavor',
'variant' => 'tiny_elements_variant',
];
/** @var array Table name aliases for compatibility with tiny_c4l exports. */
public const TABLE_ALIASES = [
'tiny_elements_compcat' => 'tiny_c4l_compcat',
'tiny_elements_component' => 'tiny_c4l_component',
'tiny_elements_flavor' => 'tiny_c4l_flavor',
'tiny_elements_variant' => 'tiny_c4l_variant',
'tiny_elements_comp_flavor' => 'tiny_c4l_comp_flavor',
'tiny_elements_comp_variant' => 'tiny_c4l_comp_variant',
];
/** @var array All tables that are optional. */
public const OPTIONAL_TABLES = ['tiny_elements_comp_flavor', 'tiny_elements_comp_variant'];

Expand Down
13 changes: 0 additions & 13 deletions classes/local/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,26 +455,13 @@ public static function button_icon_css(string $buttonclass, string $iconurl, str
* @return string
*/
public static function update_pluginfile_tags_bulk(array $categorymap, string $subject): string {
$subject = self::update_c4l_pluginfile_tags($subject);
foreach ($categorymap as $oldid => $newid) {
$subject = self::update_pluginfile_tags($oldid, $newid, $subject, 'bulk');
}
$subject = self::remove_mark($subject, 'bulk');
return $subject;
}

/**
* Rename the pluginfile tags from tiny_c4l to tiny_elements.
*
* @param string $subject
* @return string
*/
public static function update_c4l_pluginfile_tags(string $subject): string {
$oldstring = '@@PLUGINFILE@@/1/tiny_c4l/';
$newstring = '@@PLUGINFILE@@/1/tiny_elements/';
return str_replace($oldstring, $newstring, $subject);
}

/**
* Update the pluginfile tags in the given subject.
*
Expand Down
82 changes: 0 additions & 82 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,86 +514,4 @@ public function update_component(object $data): bool {

return $result;
}

/**
* Get the name of the category for a variant. If there are multiple categories where the variant is used,
* get the one with the most components.
*
* @param string $variantname
* @return string
*/
public function get_compcatname_for_variant(string $variantname): string {
global $DB;
$compcatname = '';
$compcat = $DB->get_record_sql(
"SELECT c.name, COUNT(c.id) AS cnt
FROM {tiny_elements_compcat} c
JOIN {tiny_elements_component} cp
ON c.name = cp.categoryname
JOIN {tiny_elements_comp_variant} cpv
ON cp.name = cpv.componentname
WHERE cpv.variant = ?
GROUP BY c.name
ORDER BY cnt DESC",
[$variantname],
IGNORE_MULTIPLE
);
if ($compcat) {
$compcatname = $compcat->name;
}
return $compcatname ?? '';
}

/**
* Get the name of the category for a flavor. If there are multiple categories where the flavor is used,
* get the one with the most components.
*
* @param string $flavorname
* @return string
*/
public function get_compcatname_for_flavor(string $flavorname): string {
global $DB;
$compcatname = '';
$compcat = $DB->get_record_sql(
"SELECT c.name, COUNT(c.id) AS cnt
FROM {tiny_elements_compcat} c
JOIN {tiny_elements_component} cp
ON c.name = cp.categoryname
JOIN {tiny_elements_comp_flavor} cpf
ON cp.name = cpf.componentname
WHERE cpf.flavorname = ?
GROUP BY c.name
ORDER BY cnt DESC",
[$flavorname],
IGNORE_MULTIPLE
);
if ($compcat) {
$compcatname = $compcat->name;
}
return $compcatname ?? '';
}

/**
* This function deletes all data from the plugin tables and from the filesystem.
* Use with caution!
*/
public function wipe(): void {
global $DB;
$DB->delete_records(constants::TABLES['compcat']);
$DB->delete_records(constants::TABLES['component']);
$DB->delete_records(constants::TABLES['flavor']);
$DB->delete_records(constants::TABLES['variant']);
$DB->delete_records(constants::TABLES['compflavor']);
$DB->delete_records(constants::TABLES['compvariant']);

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

// Purge CSS and JS cache.
\tiny_elements\local\utils::purge_css_cache();
\tiny_elements\local\utils::rebuild_css_cache();
\tiny_elements\local\utils::purge_js_cache();
\tiny_elements\local\utils::rebuild_js_cache();
}
}
150 changes: 0 additions & 150 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,157 +37,7 @@ function xmldb_tiny_elements_upgrade($oldversion): bool {

$dbman = $DB->get_manager();

if ($oldversion < 2025013100) {
// Define field displayorder to be added to tiny_elements_flavor.
$table = new xmldb_table('tiny_elements_flavor');
$field = new xmldb_field('displayorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'displayname');

// Conditionally launch add field displayorder.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Elements savepoint reached.
upgrade_plugin_savepoint(true, 2025013100, 'tiny', 'elements');
}

if ($oldversion < 2025022402) {
// Define field componentname to be added to tiny_elements_comp_variant.
$table = new xmldb_table('tiny_elements_comp_variant');
$field = new xmldb_field('componentname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'variant');

// Conditionally launch add field componentname.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Move componentid to componentname.
$DB->execute(
"UPDATE {tiny_elements_comp_variant}
SET componentname = (SELECT name FROM {tiny_elements_component} WHERE id = component)"
);

// Delete all rows without componentname.
$DB->execute("DELETE FROM {tiny_elements_comp_variant} WHERE componentname IS NULL");

// Remove old foreign key.
$key = new xmldb_key(
'tinyelementscompvariant_comp_fk',
XMLDB_KEY_FOREIGN,
['component'],
'tiny_elements_component',
['id']
);

// Launch drop key tinyelementscompvariant_comp_fk.
$dbman->drop_key($table, $key);

// Add new foreign key.
$key = new xmldb_key(
'tinyelementscompvariant_comp_fk',
XMLDB_KEY_FOREIGN,
['componentname'],
'tiny_elements_component',
['name']
);

// Launch add key tinyelementscompvariant_comp_fk.
$dbman->add_key($table, $key);

$field = new xmldb_field('component');

// Conditionally launch drop field component.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

// Elements savepoint reached.
upgrade_plugin_savepoint(true, 2025022402, 'tiny', 'elements');
}

if ($oldversion < 2025022409) {
$manager = new tiny_elements\manager();

// Define field categoryname to be added to tiny_elements_component.
$table = new xmldb_table('tiny_elements_component');
$field = new xmldb_field('categoryname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'compcat');

// Conditionally launch add field categoryname.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

$DB->execute(
"UPDATE {tiny_elements_component}
SET categoryname = (SELECT name FROM {tiny_elements_compcat} WHERE id = compcat)"
);

$key = new xmldb_key('compcat', XMLDB_KEY_FOREIGN, ['compcat'], 'tiny_elements_compcat', ['id']);

// Launch drop key compcat.
$dbman->drop_key($table, $key);

// Define key tinyelementscomp_comp_fk (foreign) to be added to tiny_elements_component.
$key = new xmldb_key('tinyelementscomp_comp_fk', XMLDB_KEY_FOREIGN, ['categoryname'], 'tiny_elements_compcat', ['name']);

// Launch add key tinyelementscomp_comp_fk.
$dbman->add_key($table, $key);

// Define field categoryname to be added to tiny_elements_variant.
$table = new xmldb_table('tiny_elements_variant');
$field = new xmldb_field('categoryname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'displayname');

// Conditionally launch add field categoryname.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

$variants = $DB->get_records('tiny_elements_variant');
foreach ($variants as $variant) {
$compcatname = $manager->get_compcatname_for_variant($variant->name);
$DB->set_field('tiny_elements_variant', 'categoryname', $compcatname, ['id' => $variant->id]);
}

$key = new xmldb_key('tinyelementsvari_comp_fk', XMLDB_KEY_FOREIGN, ['categoryname'], 'tiny_elements_compcat', ['name']);

// Launch add key tinyelementsvari_comp_fk.
$dbman->add_key($table, $key);

// Define field categoryname to be added to tiny_elements_glavor.
$table = new xmldb_table('tiny_elements_flavor');
$field = new xmldb_field('categoryname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'displayname');

// Conditionally launch add field categoryname.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

$flavors = $DB->get_records('tiny_elements_flavor');
foreach ($flavors as $flavor) {
$compcatname = $manager->get_compcatname_for_flavor($flavor->name);
$DB->set_field('tiny_elements_flavor', 'categoryname', $compcatname, ['id' => $flavor->id]);
}

$key = new xmldb_key('tinyelementsflav_comp_fk', XMLDB_KEY_FOREIGN, ['categoryname'], 'tiny_elements_compcat', ['name']);

// Launch add key tinyelementsflav_comp_fk.
$dbman->add_key($table, $key);

// Drop old compcat field.
$table = new xmldb_table('tiny_elements_component');
$field = new xmldb_field('compcat');

// Conditionally launch drop field compcat.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

// Elements savepoint reached.
upgrade_plugin_savepoint(true, 2025022409, 'tiny', 'elements');
}

if ($oldversion < 2025081700) {

// Changing precision of field displayname on table tiny_elements_component to (1333).
$table = new xmldb_table('tiny_elements_component');
$field = new xmldb_field('displayname', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'name');
Expand Down