Skip to content

Commit

Permalink
Improvement: Implement generic classes for customfields in wunderbyte…
Browse files Browse the repository at this point in the history
… table and get the string values to be shown in filter from field controller. (Wunderbyte-GmbH/moodle-mod_booking#752).
  • Loading branch information
ibernhardf committed Dec 19, 2024
1 parent 7613e93 commit 2f54907
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 5 deletions.
6 changes: 3 additions & 3 deletions classes/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
namespace local_wunderbyte_table;

use coding_exception;
use core_component;
use core_date;
use DateTime;
use DateTimeZone;
use dml_exception;
use stdClass;
use local_wunderbyte_table\local\customfield\wbt_field_controller_info;

/**
* Wunderbyte table class is an extension of table_sql.
Expand Down Expand Up @@ -136,8 +135,9 @@ public static function return_filterjson(wunderbyte_table $table, string $cachek

$filterjson = ['categories' => []];

foreach ($filtercolumns as $fckey => $values) {
wbt_field_controller_info::instantiate_by_shortnames(array_keys($filtercolumns));

foreach ($filtercolumns as $fckey => $values) {
// Special treatment for key localizedname.
if (isset($filtersettings[$fckey]['localizedname'])) {
$localizedname = $filtersettings[$fckey]['localizedname'];
Expand Down
9 changes: 8 additions & 1 deletion classes/filters/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace local_wunderbyte_table\filters;

use coding_exception;
use local_wunderbyte_table\local\customfield\wbt_field_controller_info;
use local_wunderbyte_table\filter;
use local_wunderbyte_table\wunderbyte_table;
use moodle_exception;
Expand Down Expand Up @@ -282,9 +283,15 @@ public static function add_to_categoryobject(array &$categoryobject, array $filt
}
$identifierarray[] = $identifier;

// For custom fields, we get the actual string value from field controller.
$fieldcontroller = wbt_field_controller_info::get_instance_by_shortname($fckey);
if (!empty($fieldcontroller)) {
$cfstringvalueforvaluekey = $fieldcontroller->get_option_value_by_key($valuekey);
}

$itemobject = [
// We do not want to show html entities, so replace & with &.
'key' => str_replace("&", "&", $valuekey),
'key' => str_replace("&", "&", $cfstringvalueforvaluekey ?? $valuekey),
'value' => $valuevalue === true ? $valuekey : $valuevalue,
'identifier' => $identifier,
'category' => $fckey,
Expand Down
63 changes: 63 additions & 0 deletions classes/local/customfield/field/dynamic/wbt_field_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_wunderbyte_table\local\customfield\field\dynamic;

// Important: Use the field controller for the right customfield.
use customfield_dynamic\field_controller;
use local_wunderbyte_table\local\customfield\wbt_field_controller_base;

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wbt_field_controller extends field_controller implements wbt_field_controller_base {

/**
* Get the actual string value of the customfield by index.
*
* @param string $key
* @return string the string value for the index
*/
public function get_option_value_by_key(string $key): string {
global $DB;

$sql = $this->get_configdata_property('dynamicsql');
try {
$records = $DB->get_records_sql($sql);
} catch (\Throwable $th) {
return $key;
}
if (isset($records[$key])) {
return format_string($records[$key]->data ?? $key);
} else {
return $key;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_wunderbyte_table\local\customfield\field\dynamicformat;

// Important: Use the field controller for the right customfield.
use local_wunderbyte_table\local\customfield\field\dynamic\wbt_field_controller as wbtfieldcontrollerdynamic;
use local_wunderbyte_table\local\customfield\wbt_field_controller_base;

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wbt_field_controller extends wbtfieldcontrollerdynamic implements wbt_field_controller_base {
}
62 changes: 62 additions & 0 deletions classes/local/customfield/field/select/wbt_field_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_wunderbyte_table\local\customfield\field\select;

// Important: Use the field controller for the right customfield.
use customfield_select\field_controller;
use local_wunderbyte_table\local\customfield\wbt_field_controller_base;
use stdClass;

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wbt_field_controller extends field_controller implements wbt_field_controller_base {

/**
* Get the actual string value of the customfield by index.
*
* @param string $key
* @return string the string value for the index
*/
public function get_option_value_by_key(string $key): string {
$index = (int) $key;
$optionsstring = $this->get_configdata_property('options');
$optionsarray = explode(PHP_EOL, $optionsstring);
if (empty($optionsarray)) {
return '';
}
$i = $index - 1;
if ($i < 0 || $i >= count($optionsarray)) {
return '';
}
return format_string($optionsarray[$i]);
}
}
44 changes: 44 additions & 0 deletions classes/local/customfield/wbt_field_controller_base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Interface for customfield field controllers of Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_wunderbyte_table\local\customfield;

/**
* Interface for customfield field controllers of Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface wbt_field_controller_base {
/**
* Get the actual string value of the customfield by index.
*
* @param string $key
* @return string the string value for the index
*/
public function get_option_value_by_key(string $key): string;
}
98 changes: 98 additions & 0 deletions classes/local/customfield/wbt_field_controller_info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_wunderbyte_table\local\customfield;

use stdClass;
use local_wunderbyte_table\local\customfield\wbt_field_controller_base;

/**
* Extension of the customfield field controller for Wunderbyte table.
*
* @package local_wunderbyte_table
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @author Bernhard Fischer-Sengseis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wbt_field_controller_info {

/**
* Array of instantiated customfield field controllers.
* @var array
*/
private static $instances = [];


/**
* Create a customfield field controller for a customfield db record.
*
* @param stdClass $record customfield record from db table customfield_field
* @return wbt_field_controller_base|null the field controller for the customfield record
*/
public static function create(stdClass $record) {
$class = "\\local_wunderbyte_table\\local\\customfield\\field\\{$record->type}\\wbt_field_controller";
if (class_exists($class)) {
return new $class($record->id, $record);
}
return null;
}

/**
* Create instances of field controllers for all provided customfield shortnames.
*
* @param array $shortnames array of customfield shortnames
* @return void
*/
public static function instantiate_by_shortnames(array $shortnames) {
global $DB;

[$inorequal, $inparams] = $DB->get_in_or_equal($shortnames, SQL_PARAMS_NAMED);

$sql = "SELECT cf.shortname AS filtercolumn, cf.*
FROM {customfield_field} cf
WHERE cf.shortname $inorequal";
$records = $DB->get_records_sql($sql, $inparams);

foreach ($records as $record) {
// We only add the instance, if a field controller exists.
if ($instance = self::create($record)) {
self::$instances[$record->shortname] = $instance;
}
}
}

/**
* Get the field controller from the singleton $instances.
*
* @param string $shortname shortname of field controller customfield
* @return wbt_field_controller_base the field controller for the customfield record
*/
public static function get_instance_by_shortname(string $shortname) {
if (!empty(self::$instances[$shortname])) {
return self::$instances[$shortname];
}
return null;
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$plugin->component = 'local_wunderbyte_table';
$plugin->release = '2.0.34';
$plugin->version = 2024121200;
$plugin->version = 2024121202;
$plugin->requires = 2022112800; // Requires this Moodle version. Current: Moodle 4.1.
$plugin->maturity = MATURITY_STABLE;
$plugin->supported = [401, 405];

0 comments on commit 2f54907

Please sign in to comment.