diff --git a/app/controllers/course/contentmodules.php b/app/controllers/course/contentmodules.php index adb3055a72b62ae6dc0de28dc4dc418da87f2dbd..6dc6c2de2fe9988a1420f55a05ccf1210080c354 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 f2e4fa5dd81803bd4b993d9b0ed33c049987bc50..ca0b9e05761cc1a9051bb46f0faf4c3595263c17 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;