-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement: Implement generic classes for customfields in wunderbyte…
… table and get the string values to be shown in filter from field controller. (Wunderbyte-GmbH/moodle-mod_booking#752).
- Loading branch information
1 parent
7613e93
commit 2f54907
Showing
8 changed files
with
320 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
classes/local/customfield/field/dynamic/wbt_field_controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
classes/local/customfield/field/dynamicformat/wbt_field_controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
classes/local/customfield/field/select/wbt_field_controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters