MDL-66135 customfield: method to allow fields to parse values.

This is useful when the value is coming from an external source (e.g. course upload tool).
This commit is contained in:
Paul Holden 2019-10-02 10:10:44 +01:00
parent 788dfb9c7d
commit 1084f8bf79
7 changed files with 106 additions and 6 deletions

View File

@ -122,7 +122,7 @@ abstract class data_controller {
*
* @return string
*/
protected function get_form_element_name() : string {
public function get_form_element_name() : string {
return 'customfield_' . $this->get_field()->get('shortname');
}

View File

@ -123,6 +123,18 @@ abstract class field_controller {
return $fieldcontroller;
}
/**
* Perform pre-processing of field values, for example those that originate from an external source (e.g. upload course tool)
*
* Override in plugin classes as necessary
*
* @param string $value
* @return mixed
*/
public function parse_value(string $value) {
return $value;
}
/**
* Validate the data on the field configuration form
*

View File

@ -114,4 +114,17 @@ class field_controller extends \core_customfield\field_controller {
$this->get_formatted_name());
return $ret;
}
}
/**
* Convert given value into appropriate timestamp
*
* @param string $value
* @return int
*/
public function parse_value(string $value) {
$timestamp = strtotime($value);
// If we have a valid, positive timestamp then return it.
return $timestamp > 0 ? $timestamp : 0;
}
}

View File

@ -171,10 +171,39 @@ class customfield_date_plugin_testcase extends advanced_testcase {
$this->assertEquals(null, $d->export_value());
}
/**
* Data provider for {@see test_parse_value}
*
* @return array
*/
public function parse_value_provider() : array {
return [
// Valid times.
['2019-10-01', strtotime('2019-10-01')],
['2019-10-01 14:00', strtotime('2019-10-01 14:00')],
// Invalid times.
['ZZZZZ', 0],
['202-04-01', 0],
['2019-15-15', 0],
];
}
/**
* Test field parse_value method
*
* @param string $value
* @param int $expected
* @return void
*
* @dataProvider parse_value_provider
*/
public function test_parse_value(string $value, int $expected) {
$this->assertSame($expected, $this->cfields[1]->parse_value($value));
}
/**
* Deleting fields and data
*/
public function test_delete() {
$this->cfcat->get_handler()->delete_all();
}
}
}

View File

@ -119,4 +119,14 @@ class field_controller extends \core_customfield\field_controller {
$this->get_formatted_name());
return $ret;
}
}
/**
* Locate the value parameter in the field options array, and return it's index
*
* @param string $value
* @return int
*/
public function parse_value(string $value) {
return (int) array_search($value, self::get_options_array($this));
}
}

View File

@ -157,10 +157,46 @@ class customfield_select_plugin_testcase extends advanced_testcase {
$this->assertEquals('b', $d->export_value());
}
/**
* Data provider for {@see test_parse_value}
*
* @return array
*/
public function parse_value_provider() : array {
return [
['Red', 1],
['Blue', 2],
['Green', 3],
['Mauve', 0],
];
}
/**
* Test field parse_value method
*
* @param string $value
* @param int $expected
* @return void
*
* @dataProvider parse_value_provider
*/
public function test_parse_value(string $value, int $expected) {
$field = $this->get_generator()->create_field([
'categoryid' => $this->cfcat->get('id'),
'type' => 'select',
'shortname' => 'myselect',
'configdata' => [
'options' => "Red\nBlue\nGreen",
],
]);
$this->assertSame($expected, $field->parse_value($value));
}
/**
* Deleting fields and data
*/
public function test_delete() {
$this->cfcat->get_handler()->delete_all();
}
}
}

View File

@ -59,7 +59,7 @@ class data_controller extends \core_customfield\data_controller {
*
* @return string
*/
protected function get_form_element_name() : string {
public function get_form_element_name() : string {
return parent::get_form_element_name() . '_editor';
}