From 5afaea1a769440595829fe23b40570213f9537dd Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Fri, 15 Mar 2024 15:27:29 +0000
Subject: [PATCH] fixes #3801
Closes #3801
Merge request studip/studip!2673
---
app/controllers/course/contentmodules.php | 58 ++++++++++++++++++-----
lib/classes/Icon.class.php | 2 +-
2 files changed, 48 insertions(+), 12 deletions(-)
diff --git a/app/controllers/course/contentmodules.php b/app/controllers/course/contentmodules.php
index adb3055a72b..6dc6c2de2fe 100644
--- a/app/controllers/course/contentmodules.php
+++ b/app/controllers/course/contentmodules.php
@@ -299,18 +299,8 @@ class Course_ContentmodulesController extends AuthenticatedController
'displayname' => $displayname,
'visibility' => $visibility,
'active' => (bool) $tool,
+ 'icon' => $this->getIconFromMetadata($metadata, $plugin),
];
- if (!empty($metadata['icon_clickable'])) {
- $list[$plugin_id]['icon'] = $metadata['icon_clickable'] instanceof Icon
- ? $metadata['icon_clickable']->asImagePath()
- : Icon::create($plugin->getPluginURL().'/'.$metadata['icon_clickable'])->asImagePath();
- } elseif (!empty($metadata['icon'])) {
- $list[$plugin_id]['icon'] = $metadata['icon'] instanceof Icon
- ? $metadata['icon']->asImagePath()
- : Icon::create($plugin->getPluginURL().'/'.$metadata['icon'])->asImagePath();
- } else {
- $list[$plugin_id]['icon'] = null;
- }
$list[$plugin_id]['summary'] = $metadata['summary'] ?? null;
$list[$plugin_id]['mandatory'] = $this->sem_class->isModuleMandatory(get_class($plugin));
$list[$plugin_id]['highlighted'] = (bool) $plugin->isHighlighted();
@@ -320,4 +310,50 @@ class Course_ContentmodulesController extends AuthenticatedController
return $list;
}
+
+ /**
+ * @param array $metadata
+ * @param CorePlugin|StudIPPlugin $plugin
+ */
+ private function getIconFromMetadata(array $metadata, $plugin): ?string
+ {
+ $icon = $metadata['icon_clickable'] ?? $metadata['icon'] ?? null;
+
+ if (!$icon) {
+ return null;
+ }
+
+ if ($plugin instanceof StudIPPlugin) {
+ $path = $GLOBALS['ABSOLUTE_PATH_STUDIP'] . '/' . $plugin->getPluginPath() . '/' . $icon;
+ $icon = $this->getCoreIcon($path) ?? $icon;
+ }
+
+ if (!$icon instanceof Icon) {
+ $icon = Icon::create($plugin->getPluginURL() . '/' . $icon);
+ }
+
+ return $icon->copyWithRole(Icon::ROLE_CLICKABLE)->asImagePath();
+ }
+
+ private function getCoreIcon(string $path): ?Icon
+ {
+ $path = realpath($path);
+
+ if (!file_exists($path)) {
+ return null;
+ }
+
+ try {
+ $icon = basename($path, '.svg');
+ $color = basename(dirname($path));
+ $roles = Icon::colorToRoles($color);
+
+ return Icon::create($icon, $roles[0]);
+ } catch (Exception $e) {
+ return null;
+ }
+
+
+
+ }
}
diff --git a/lib/classes/Icon.class.php b/lib/classes/Icon.class.php
index f2e4fa5dd81..ca0b9e05761 100644
--- a/lib/classes/Icon.class.php
+++ b/lib/classes/Icon.class.php
@@ -66,7 +66,7 @@ class Icon
}
// return the roles! associated to a color
- private static function colorToRoles($color)
+ public static function colorToRoles($color)
{
static $colors_to_roles;
--
GitLab