diff --git a/customfield/classes/data_controller.php b/customfield/classes/data_controller.php index 8ec6ac639b1..bd19792bd5f 100644 --- a/customfield/classes/data_controller.php +++ b/customfield/classes/data_controller.php @@ -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'); } diff --git a/customfield/classes/field_controller.php b/customfield/classes/field_controller.php index 07e22b7bac5..5dea8aac877 100644 --- a/customfield/classes/field_controller.php +++ b/customfield/classes/field_controller.php @@ -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 * diff --git a/customfield/field/date/classes/field_controller.php b/customfield/field/date/classes/field_controller.php index bcdf19645b2..f8fb5adc6e0 100644 --- a/customfield/field/date/classes/field_controller.php +++ b/customfield/field/date/classes/field_controller.php @@ -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; + } +} \ No newline at end of file diff --git a/customfield/field/date/tests/plugin_test.php b/customfield/field/date/tests/plugin_test.php index 8445c347f15..749872287e0 100644 --- a/customfield/field/date/tests/plugin_test.php +++ b/customfield/field/date/tests/plugin_test.php @@ -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(); } -} +} \ No newline at end of file diff --git a/customfield/field/select/classes/field_controller.php b/customfield/field/select/classes/field_controller.php index 79fa4083891..c47f05c1022 100644 --- a/customfield/field/select/classes/field_controller.php +++ b/customfield/field/select/classes/field_controller.php @@ -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)); + } +} \ No newline at end of file diff --git a/customfield/field/select/tests/plugin_test.php b/customfield/field/select/tests/plugin_test.php index 0ada2bb4885..6def88cfa66 100644 --- a/customfield/field/select/tests/plugin_test.php +++ b/customfield/field/select/tests/plugin_test.php @@ -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(); } -} +} \ No newline at end of file diff --git a/customfield/field/textarea/classes/data_controller.php b/customfield/field/textarea/classes/data_controller.php index 57b0d584f84..384918c7cec 100644 --- a/customfield/field/textarea/classes/data_controller.php +++ b/customfield/field/textarea/classes/data_controller.php @@ -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'; }