From b800b72bef087d40c45672bf6508124d188eefcb Mon Sep 17 00:00:00 2001 From: Jackson Darlow Date: Thu, 12 Nov 2020 15:04:25 +1300 Subject: [PATCH] #27 Customisable Question Identifiers --- .../GridFieldConfig_CustomRelationEditor.php | 2 ++ src/Model/Answer.php | 9 ++++-- src/Model/Pathfinder.php | 16 ++++++++-- src/Model/Question.php | 32 ++++++++++++++++--- tests/units/AnswerTest.php | 5 +-- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/GridField/GridFieldConfig_CustomRelationEditor.php b/src/GridField/GridFieldConfig_CustomRelationEditor.php index 43b00bb..b2a3cbd 100644 --- a/src/GridField/GridFieldConfig_CustomRelationEditor.php +++ b/src/GridField/GridFieldConfig_CustomRelationEditor.php @@ -13,6 +13,7 @@ use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; use SilverStripe\Versioned\VersionedGridFieldStateExtension; +use Symbiote\GridFieldExtensions\GridFieldTitleHeader; /** * A customised relation editor config for managing Pathfinder content @@ -34,6 +35,7 @@ public function __construct($addButtonTitle = 'Add', $orderable = false) $this->addComponents([ new GridFieldButtonRow('after'), new GridFieldToolbarHeader(), + new GridFieldTitleHeader(), (new \Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton('buttons-after-left')) ->setTitle($addButtonTitle), new \Symbiote\GridFieldExtensions\GridFieldEditableColumns(), diff --git a/src/Model/Answer.php b/src/Model/Answer.php index 9f6d05f..b0bfec3 100644 --- a/src/Model/Answer.php +++ b/src/Model/Answer.php @@ -146,9 +146,12 @@ public function getCMSFields() if ($choicesField) { $config = GridFieldConfig_CustomRelationEditor::create('Add Choice', true) ->setDisplayFields([ - 'ChoiceText' => function($record, $column, $grid) { - return TextField::create($column); - }, + 'ChoiceText' => [ + 'title' => 'Choice', + 'callback' => function($record, $column, $grid) { + return TextField::create($column); + }, + ], ]); $choicesField->setConfig($config); diff --git a/src/Model/Pathfinder.php b/src/Model/Pathfinder.php index 4a66281..b10600c 100644 --- a/src/Model/Pathfinder.php +++ b/src/Model/Pathfinder.php @@ -155,13 +155,25 @@ public function getCMSFields() if ($questionsField) { $config = GridFieldConfig_CustomRelationEditor::create('Add Question', true) ->setDisplayFields([ - 'CMSID' => [ - 'field' => ReadonlyField::class, + 'QID' => [ + 'title' => 'Question Identifider', + 'callback' => function($record, $column, $grid) { + $placeholder = 'Leave blank to use default'; + + if ($record->ID) { + $placeholder = sprintf('%s (%s)', $record->ID, $placeholder); + } + + return TextField::create($column) + ->setAttribute('placeholder', $placeholder); + }, ], 'QuestionText' => [ + 'title' => 'Question', 'field' => TextField::class, ], 'FlowTitle' => [ + 'title' => 'Flow', 'field' => ReadonlyField::class, ], ]); diff --git a/src/Model/Question.php b/src/Model/Question.php index 216b891..c6ad0ad 100644 --- a/src/Model/Question.php +++ b/src/Model/Question.php @@ -21,6 +21,8 @@ use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBField; +use SilverStripe\ORM\FieldType\DBString; +use SilverStripe\ORM\FieldType\DBText; use SilverStripe\ORM\HasManyList; use SilverStripe\ORM\ManyManyList; use SilverStripe\Versioned\Versioned; @@ -28,6 +30,7 @@ /** * A question used as a step in a Pathfinder * + * @property string QID * @property string QuestionText * @property int Sort * @method Pathfinder|null Pathfinder() @@ -53,6 +56,7 @@ class Question extends DataObject * @var array */ private static $db = [ + 'QID' => 'Varchar', 'QuestionText' => 'Text', 'Description' => 'HTMLText', 'Sort' => 'Int', @@ -146,6 +150,14 @@ public function getCMSFields() } } + // Question identifider field + $qidField = $fields->dataFieldByName('QID'); + + $qidField + ->setTitle('Question identifider') + ->setAttribute('placeholder', $this->getDefaultQID()) + ->setDescription('Leave blank to use default'); + // Quesiton text field $fields->replaceField( 'QuestionText', @@ -281,7 +293,7 @@ public function recursivePrecedentIDs($precedents = []) */ public function getFlowTitle() { - return sprintf('Flow: %s', $this->Flow()->exists() ? $this->Flow()->Title : 'Default'); + return $this->Flow()->exists() ? $this->Flow()->Title : 'Default'; } /** @@ -291,8 +303,8 @@ public function getCMSTitle() { $parts = [ $this->QuestionText, - ' (Q#', - $this->ID + ' (Q: ', + $this->QID ?: $this->getDefaultQID() ]; if ($this->Flow()) { @@ -304,11 +316,21 @@ public function getCMSTitle() return implode('', $parts); } + /** + * Useful for identifying this question, (e.g {@see Pathfinder::getCMSFields()}) + * + * @return string + */ + public function getCMSID() + { + return sprintf('Q: %s', $this->getDefaultQID()); + } + /** * @return string */ - public function CMSID() + public function getDefaultQID() { - return sprintf('Q#%s', $this->ID); + return $this->ID; } } diff --git a/tests/units/AnswerTest.php b/tests/units/AnswerTest.php index feeab35..14e0f4b 100644 --- a/tests/units/AnswerTest.php +++ b/tests/units/AnswerTest.php @@ -125,6 +125,7 @@ public function testGetGoesToField() $expectedText2 = 'Question 2'; $question2 = Question::create([ + 'QID' => 'Custom', 'QuestionText' => $expectedText2, 'PathfinderID' => $pathfinder->ID, 'FlowID' => $flow->ID, @@ -152,9 +153,9 @@ public function testGetGoesToField() $this->assertSame( [ 'Flow_Default' => 'Default flow', - 3 => 'Question 1 (Q#3, in Flow: Flow 1)', + 3 => 'Question 1 (Q: 3, in Flow: Flow 1)', 'Flow_1' => 'Flow 1', - 4 => 'Question 2 (Q#4, in Flow: Flow 1)', + 4 => 'Question 2 (Q: Custom, in Flow: Flow 1)', ], $field->getSource(), 'Field should have a range of options based on flows and questions'