From 6281661476aab04807a5c96cf1e7ae4abb6b4752 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms Date: Mon, 25 Aug 2025 14:27:28 +0200 Subject: adjust according to elmar's review --- app/controllers/admin/courses.php | 8 +++---- lib/classes/DataFieldSelectboxEntry.php | 41 +++++++++++++-------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/app/controllers/admin/courses.php b/app/controllers/admin/courses.php index 876abf3..da0e6d3 100644 --- a/app/controllers/admin/courses.php +++ b/app/controllers/admin/courses.php @@ -32,8 +32,7 @@ class Admin_CoursesController extends AuthenticatedController /** * This method returns the appropriate widget for the given datafield. * - * @param DataField datafield The datafield whose widget is requested. - * + * @param DataField $datafield The datafield whose widget is requested. * @return SidebarWidget|null Returns a SidebarWidget derivative or null in case of an error. */ private function getDatafieldWidget(DataField $datafield) @@ -45,6 +44,7 @@ class Admin_CoursesController extends AuthenticatedController $datafields_filters = $GLOBALS['user']->cfg->ADMIN_COURSES_DATAFIELDS_FILTERS; $type = $datafield->type; + $entry = DataFieldDateEntry::createDataFieldEntry($datafield); if ($type == 'bool') { //bool fields just need a checkbox for the states TRUE and FALSE @@ -62,8 +62,8 @@ class Admin_CoursesController extends AuthenticatedController ['onclick' => "$(this).toggleClass(['options-checked', 'options-unchecked']); STUDIP.AdminCourses.App.changeFilter({'df_".$datafield->id."': $(this).hasClass('options-checked') ? 1 : 0}); return false;"] ); return $checkboxWidget; - } elseif (in_array($type, ['selectbox', 'radio', 'selectboxmultiple'])) { - $options = DataFieldSelectboxEntry::convertTypeParamToChoiceList($datafield->typeparam, $is_assoc); + } elseif ($entry instanceof DataFieldSelectboxEntry) { + [$options, $is_assoc] = $entry->getParameters(); if ($options) { $selectWidget = new SelectWidget( diff --git a/lib/classes/DataFieldSelectboxEntry.php b/lib/classes/DataFieldSelectboxEntry.php index aeb8ec2..4c9c890 100644 --- a/lib/classes/DataFieldSelectboxEntry.php +++ b/lib/classes/DataFieldSelectboxEntry.php @@ -25,7 +25,7 @@ class DataFieldSelectboxEntry extends DataFieldEntry { parent::__construct($struct, $range_id, $value); - list($values, $is_assoc) = $this->getParameters(); + [$values, $is_assoc] = $this->getParameters(); $this->is_assoc_param = $is_assoc; $this->type_param = $values; @@ -65,28 +65,7 @@ class DataFieldSelectboxEntry extends DataFieldEntry */ public function getParameters() { - $choices = self::convertTypeParamToChoiceList($this->model->typeparam, $is_assoc); - - return [$choices, $is_assoc]; - } - - /** - * Returns the display/rendered value of this datafield - * - * @param bool $entities Should html entities be encoded (defaults to true) - * @return String containg the rendered value - */ - public function getDisplayValue($entities = true) - { - $value = $this->is_assoc_param - ? $this->type_param[$this->getValue()] - : $this->getValue(); - return $entities ? htmlReady($value) : $value; - } - - public static function convertTypeParamToChoiceList(string $typeparam, ?bool &$is_assoc = null): array - { - $params = explode("\n", rtrim($typeparam)); + $params = explode("\n", rtrim($this->model->typeparam)); $params = array_map('trim', $params); $ret = []; @@ -95,14 +74,26 @@ class DataFieldSelectboxEntry extends DataFieldEntry foreach ($params as $i => $p) { if (mb_strpos($p, '=>') !== false) { $is_assoc = true; - [$key, $value] = array_map('trim', explode('=>', $p, 2)); $ret[$key] = $value; } else { $ret[$i] = $p; } } + return [$ret, $is_assoc]; + } - return $ret; + /** + * Returns the display/rendered value of this datafield + * + * @param bool $entities Should html entities be encoded (defaults to true) + * @return String containg the rendered value + */ + public function getDisplayValue($entities = true) + { + $value = $this->is_assoc_param + ? $this->type_param[$this->getValue()] + : $this->getValue(); + return $entities ? htmlReady($value) : $value; } } -- cgit v1.0