Merge branch 'MDL-79365-401' of https://github.com/paulholden/moodle into MOODLE_401_STABLE

This commit is contained in:
Ilya Tregubov 2023-10-16 14:08:45 +08:00
commit c4c514ee46
No known key found for this signature in database
GPG Key ID: 0F58186F748E55C1
2 changed files with 29 additions and 3 deletions

View File

@ -238,7 +238,13 @@ abstract class data_controller {
*/
protected function is_unique($value) : bool {
global $DB;
// Ensure the "value" datafield can be safely compared across all databases.
$datafield = $this->datafield();
if ($datafield === 'value') {
$datafield = $DB->sql_cast_to_char($datafield);
}
$where = "fieldid = ? AND {$datafield} = ?";
$params = [$this->get_field()->get('id'), $value];
if ($this->get('id')) {

View File

@ -136,6 +136,29 @@ class data_controller extends \core_customfield\data_controller {
$instance->{$this->get_form_element_name()} = $value;
}
/**
* Checks if the value is empty, overriding the base method to ensure it's the "text" element of our value being compared
*
* @param mixed $value
* @return bool
*/
protected function is_empty($value): bool {
if (is_array($value)) {
$value = $value['text'];
}
return html_is_blank($value);
}
/**
* Checks if the value is unique, overriding the base method to ensure it's the "text" element of our value being compared
*
* @param mixed $value
* @return bool
*/
protected function is_unique($value): bool {
return parent::is_unique($value['text']);
}
/**
* Delete data
*
@ -166,9 +189,6 @@ class data_controller extends \core_customfield\data_controller {
require_once($CFG->libdir . '/filelib.php');
$value = $this->get_value();
if ($this->is_empty($value)) {
return null;
}
if ($dataid = $this->get('id')) {
$context = $this->get_context();