Skip to content

Commit

Permalink
Coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-csg committed Dec 12, 2024
1 parent 7a58005 commit fa9bf1f
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 81 deletions.
1 change: 0 additions & 1 deletion classes/external/get_c4l_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public static function execute_returns(): external_single_structure {
'name' => new external_value(PARAM_TEXT, 'the name of the component'),
'displayname' => new external_value(PARAM_TEXT, 'the display name of the component'),
'compcat' => new external_value(PARAM_INT, 'the id of the component category'),
'imageclass' => new external_value(PARAM_TEXT, 'the image class'), // not in db table? delete?
'code' => new external_value(PARAM_RAW, 'the code'),
'text' => new external_value(PARAM_TEXT, 'the text'),
'variants' => new external_multiple_structure(new external_value(PARAM_TEXT, 'the variants')),
Expand Down
42 changes: 30 additions & 12 deletions classes/form/base_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_form extends dynamic_form {
/** @var string $formtype */
protected string $formtype;

/**
* Form definition.
*/
abstract public function definition();

/**
Expand Down Expand Up @@ -94,12 +97,10 @@ public function process_dynamic_submission(): array {

$formdata = $this->get_data();

/**if (is_array($formdata->variants)) {
$formdata->variants = implode(',', $formdata->variants);
} DELETE?*/

if (($this->formtype == 'flavor' || $this->formtype == 'component')
&& !isset($formdata->hideforstudents)) {
if (
($this->formtype == 'flavor' || $this->formtype == 'component')
&& !isset($formdata->hideforstudents)
) {
$formdata->hideforstudents = 0;
}

Expand Down Expand Up @@ -136,7 +137,12 @@ public function process_dynamic_submission(): array {
if ($this->formtype === 'component') {
// Update component flavors.
if ($oldrecord) {
$records = $DB->get_records('tiny_c4l_comp_flavor', ['componentname' => $oldrecord->name], '', 'flavorname, iconurl');
$records = $DB->get_records(
'tiny_c4l_comp_flavor',
['componentname' => $oldrecord->name],
'',
'flavorname, iconurl'
);
$DB->delete_records('tiny_c4l_comp_flavor', ['componentname' => $oldrecord->name]);
}
if (count($formdata->flavors) > 0) {
Expand Down Expand Up @@ -165,10 +171,12 @@ public function process_dynamic_submission(): array {
}

// Purge CSS to show new one.
if (($newrecord && !(empty($formdata->css) && empty($formdata->iconurl)))
if (
($newrecord && !(empty($formdata->css) && empty($formdata->iconurl)))
|| ($oldrecord->css != $formdata->css)
|| ($oldrecord->iconurl != $formdata->iconurl)
|| (isset($oldrecord->hideforstudents) && $oldrecord->hideforstudents != $formdata->hideforstudents)) {
|| (isset($oldrecord->hideforstudents) && $oldrecord->hideforstudents != $formdata->hideforstudents)
) {
\tiny_c4l\local\utils::purge_css_cache();
\tiny_c4l\local\utils::rebuild_css_cache();
}
Expand Down Expand Up @@ -214,9 +222,19 @@ public function set_data_for_dynamic_submission(): void {

if ($this->formtype === 'component') {
if (isset($source->name)) {
$flavors = $DB->get_fieldset_select('tiny_c4l_comp_flavor', 'flavorname', 'componentname = ?', ['componentname' => $source->name]);
$flavors = $DB->get_fieldset_select(
'tiny_c4l_comp_flavor',
'flavorname',
'componentname = ?',
['componentname' => $source->name]
);
$source->flavors = $flavors;;
$variants = $DB->get_fieldset_select('tiny_c4l_comp_variant', 'variant', 'component = ?', ['component' => $source->id]);
$variants = $DB->get_fieldset_select(
'tiny_c4l_comp_variant',
'variant',
'component = ?',
['component' => $source->id]
);
$source->variants = $variants;
}
}
Expand Down
4 changes: 2 additions & 2 deletions classes/form/management_comp_flavor_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function process_dynamic_submission(): array {

$result = true;

foreach($formdata->id as $key => $id) {
foreach ($formdata->id as $key => $id) {
$record = new \stdClass();
$record->id = $id;
$record->iconurl = utils::replace_pluginfile_urls($formdata->iconurl[$key] ?? '');
Expand All @@ -111,7 +111,7 @@ public function set_data_for_dynamic_submission(): void {
$compflavor = $DB->get_records('tiny_c4l_comp_flavor');

$data = [];
foreach($compflavor as $item) {
foreach ($compflavor as $item) {
$data['id'][] = $item->id;
$data['name'][] = $item->componentname . '/' . $item->flavorname;
$data['iconurl'][] = utils::replace_pluginfile_urls($item->iconurl ?? '', true);
Expand Down
3 changes: 3 additions & 0 deletions classes/form/management_compcat_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class management_compcat_form extends base_form {
/**
* Form definition.
*/
public function definition() {
$mform =& $this->_form;

Expand Down
3 changes: 3 additions & 0 deletions classes/form/management_component_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class management_component_form extends base_form {
/**
* Form definition.
*/
public function definition() {
global $DB;

Expand Down
3 changes: 3 additions & 0 deletions classes/form/management_flavor_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class management_flavor_form extends base_form {
/**
* Form definition.
*/
public function definition() {
$mform =& $this->_form;

Expand Down
13 changes: 12 additions & 1 deletion classes/form/management_import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class management_import_form extends base_form {
/**
* Form definition.
*/
public function definition() {
$mform =& $this->_form;

Expand Down Expand Up @@ -86,7 +89,15 @@ public function process_dynamic_submission(): array {
$manager->importxml($xmlcontent);
$categories = $DB->get_records('tiny_c4l_compcat');
foreach ($categories as $category) {
$categoryfiles = $fs->get_directory_files(SYSCONTEXTID, 'tiny_c4l', 'import', $draftitemid, '/' . $category->name . '/', true, false);
$categoryfiles = $fs->get_directory_files(
SYSCONTEXTID,
'tiny_c4l',
'import',
$draftitemid,
'/' . $category->name . '/',
true,
false
);
$manager->importfiles($categoryfiles, $category->id, $category->name);
}

Expand Down
3 changes: 3 additions & 0 deletions classes/form/management_variant_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class management_variant_form extends base_form {
/**
* Form definition.
*/
public function definition() {
$mform =& $this->_form;

Expand Down
7 changes: 4 additions & 3 deletions classes/local/hook_callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public static function add_c4l_data_to_dom(\core\hook\output\before_http_headers
}

// Don't run if we are in the management page. This should help to remove invalid css/js.
if ($beforehttpheadershook->renderer->get_page()->has_set_url() &&
$beforehttpheadershook->renderer->get_page()->url->get_path() == '/lib/editor/tiny/plugins/c4l/management.php') {
if (
$beforehttpheadershook->renderer->get_page()->has_set_url() &&
$beforehttpheadershook->renderer->get_page()->url->get_path() == '/lib/editor/tiny/plugins/c4l/management.php'
) {
return;
}

Expand All @@ -65,7 +67,6 @@ public static function add_c4l_data_to_dom(\core\hook\output\before_http_headers

$cache = \cache::make('tiny_c4l', utils::TINY_C4L_CACHE_AREA);
$rev = $cache->get(utils::TINY_C4L_CSS_CACHE_REV);
// var_dump($rev);
if (!$rev) {
$rev = utils::rebuild_css_cache();
}
Expand Down
16 changes: 10 additions & 6 deletions classes/local/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ public static function get_all_components(bool $isstudent = false): array {
'name' => $record->name,
'displayname' => $record->displayname,
'compcat' => $record->compcat,
'imageclass' => $record->imageclass, // not in db table? delete?
'code' => self::replace_pluginfile_urls($record->code, true),
'text' => $record->text,
'displayorder' => $record->displayorder,
'flavors' => explode(',', $record->flavors), // not in db table? maybe 'flavors' => '';
'variants' => explode(',', $record->variants), // not in db table? maybe 'variants' => '';
'js' => self::replace_pluginfile_urls($record->js, true),
];
}
Expand All @@ -83,7 +80,7 @@ public static function get_all_variants(bool $isstudent = false): array {

/**
* Get all component variants.
*
*
* @param bool $isstudent
* @return array all component variants
*/
Expand Down Expand Up @@ -119,7 +116,10 @@ public static function get_all_comp_flavors(bool $isstudent = false): array {
$compflavors = $DB->get_records('tiny_c4l_comp_flavor', null, '', 'id, componentname, flavorname');
$components = [];
foreach ($compflavors as $compflavor) {
$components[$compflavor->componentname] = array_merge([$compflavor->flavorname], $components[$compflavor->componentname] ?? []);
$components[$compflavor->componentname] = array_merge(
[$compflavor->flavorname],
$components[$compflavor->componentname] ?? []
);
}
return $components;
}
Expand Down Expand Up @@ -219,7 +219,11 @@ public static function rebuild_css_cache(): int {
if (empty($componentflavor->iconurl)) {
continue;
}
$iconcssentries[] .= self::button_icon_css($componentflavor->componentname, self::replace_pluginfile_urls($componentflavor->iconurl, true), $componentflavor->flavorname);
$iconcssentries[] .= self::button_icon_css(
$componentflavor->componentname,
self::replace_pluginfile_urls($componentflavor->iconurl, true),
$componentflavor->flavorname
);
}
foreach ($components as $component) {
if ($component->hideforstudents) {
Expand Down
41 changes: 31 additions & 10 deletions classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,16 @@ public function importfiles($files, $categoryid, $categoryname = '') {
continue;
}
$newfilepath = ($categoryname ? str_replace('/' . $categoryname, '', $file->get_filepath()) : $file->get_filepath());
if ($oldfile = $fs->get_file($this->contextid, 'tiny_c4l', 'images', $categoryid, $newfilepath, $file->get_filename())) {
if (
$oldfile = $fs->get_file(
$this->contextid,
'tiny_c4l',
'images',
$categoryid,
$newfilepath,
$file->get_filename()
)
) {
if ($oldfile->get_contenthash() != $file->get_contenthash()) {
$oldfile->replace_file_with($file);
}
Expand Down Expand Up @@ -289,12 +298,12 @@ public static function import_component(array|object $record, array $categorymap
if (isset($categorymap[$record['compcat']])) {
$record['compcat'] = $categorymap[$record['compcat']];
}

$record['css'] = self::update_pluginfile_tags_bulk($categorymap, $record['css'] ?? '');
$record['code'] = self::update_pluginfile_tags_bulk($categorymap, $record['code'] ?? '');
$record['js'] = self::update_pluginfile_tags_bulk($categorymap, $record['js'] ?? '');
$record['iconurl'] = self::update_pluginfile_tags_bulk($categorymap, $record['iconurl'] ?? '');

$current = $DB->get_record('tiny_c4l_component', ['name' => $record['name']]);
if ($current) {
$record['id'] = $current->id;
Expand Down Expand Up @@ -347,10 +356,10 @@ public static function import_flavor(array|object $record, array $categorymap):
global $DB;
$record = (array) $record;
$current = $DB->get_record('tiny_c4l_flavor', ['name' => $record['name']]);

$record['css'] = self::update_pluginfile_tags_bulk($categorymap, $record['css'], 'import');
$record['content'] = self::update_pluginfile_tags_bulk($categorymap, $record['content'], 'import');

if ($current) {
$record['id'] = $current->id;
$DB->update_record('tiny_c4l_flavor', $record);
Expand All @@ -371,7 +380,7 @@ public static function import_variant(array|object $record, array $categorymap):
global $DB;
$record = (array) $record;
$current = $DB->get_record('tiny_c4l_variant', ['name' => $record['name']]);

$record['css'] = self::update_pluginfile_tags_bulk($categorymap, $record['css'] ?? '');
$record['content'] = self::update_pluginfile_tags_bulk($categorymap, $record['content'] ?? '');
$record['iconurl'] = self::update_pluginfile_tags_bulk($categorymap, $record['iconurl'] ?? '');
Expand All @@ -395,8 +404,11 @@ public static function import_variant(array|object $record, array $categorymap):
public static function import_component_flavor(array|object $record, array $categorymap): int {
global $DB;
$record = (array) $record;
$current = $DB->get_record('tiny_c4l_comp_flavor', ['componentname' => $record['componentname'], 'flavorname' => $record['flavorname']]);

$current = $DB->get_record(
'tiny_c4l_comp_flavor',
['componentname' => $record['componentname'], 'flavorname' => $record['flavorname']]
);

$record['iconurl'] = self::update_pluginfile_tags_bulk($categorymap, $record['iconurl'] ?? '');

if ($current) {
Expand Down Expand Up @@ -467,7 +479,8 @@ public static function update_pluginfile_tags(int $oldid, int $newid, string $su
* @return string
*/
public static function remove_mark(string $subject, string $mark): string {
$newstring = '@@PLUGINFILE@@/1/tiny_c4l/images/';;
$newstring = '@@PLUGINFILE@@/1/tiny_c4l/images/';
;
$oldstring = '@@PLUGINFILE@@/1/tiny_c4l/' . $mark . 'images/';
return str_replace($oldstring, $newstring, $subject);
}
Expand Down Expand Up @@ -520,7 +533,15 @@ public function import(stored_file|string $zip, $draftitemid = 0) {
$manager->importxml($xmlcontent);
$categories = $DB->get_records('tiny_c4l_compcat');
foreach ($categories as $category) {
$categoryfiles = $fs->get_directory_files($this->contextid, 'tiny_c4l', 'import', $draftitemid, '/' . $category->name . '/', true, false);
$categoryfiles = $fs->get_directory_files(
$this->contextid,
'tiny_c4l',
'import',
$draftitemid,
'/' . $category->name . '/',
true,
false
);
$manager->importfiles($categoryfiles, $category->id, $category->name);
}
$fs->delete_area_files($this->contextid, 'tiny_c4l', 'import', $draftitemid);
Expand Down
2 changes: 0 additions & 2 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace tiny_c4l\privacy;

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

use core_privacy\local\metadata\collection;
use core_privacy\local\request\writer;

Expand Down
4 changes: 0 additions & 4 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* @copyright 2024 ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

/**
* Execute the plugin upgrade steps from the given old version.
Expand All @@ -40,7 +39,6 @@ function xmldb_tiny_c4l_upgrade($oldversion): bool {
$dbman = $DB->get_manager();

if ($oldversion < 2024110500) {

// Define table tiny_c4l_compcat to be created.
$table = new xmldb_table('tiny_c4l_compcat');

Expand Down Expand Up @@ -157,7 +155,6 @@ function xmldb_tiny_c4l_upgrade($oldversion): bool {
}

if ($oldversion < 2024120401) {

// Define table tiny_c4l_compcat and field to be added.
$table = new xmldb_table('tiny_c4l_component');
$field = new xmldb_field('hideforstudents', XMLDB_TYPE_INTEGER, '1', null, null, null, 0, null);
Expand All @@ -181,7 +178,6 @@ function xmldb_tiny_c4l_upgrade($oldversion): bool {
}

if ($oldversion < 2024120405) {

// Insert junction table component_variant.
tiny_c4l_insert_comp_variant();

Expand Down
Loading

0 comments on commit fa9bf1f

Please sign in to comment.