Skip to content

Commit 5f7c8ea

Browse files
authored
Updated Extra Question Types (#155)
* added is_default to values Change-Id: Iee5fe2e429bdd869bccf4112889c9fe1f996b640
1 parent ba02cff commit 5f7c8ea

7 files changed

Lines changed: 104 additions & 6 deletions

File tree

app/Http/Controllers/Apis/Protected/Summit/Factories/ExtraQuestionTypeValueValidationRulesFactory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?php namespace App\Http\Controllers;
2-
use App\Http\ValidationRulesFactories\AbstractValidationRulesFactory;
3-
42
/**
53
* Copyright 2021 OpenStack Foundation
64
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +11,7 @@
1311
* See the License for the specific language governing permissions and
1412
* limitations under the License.
1513
**/
16-
17-
14+
use App\Http\ValidationRulesFactories\AbstractValidationRulesFactory;
1815
/**
1916
* Class ExtraQuestionTypeValueValidationRulesFactory
2017
* @package App\Http\Controllers
@@ -27,6 +24,7 @@ public static function buildForAdd(array $payload = []): array
2724
return [
2825
'label' => 'sometimes|string',
2926
'value' => 'required|string|max:255',
27+
'is_default' => 'sometimes|boolean',
3028
];
3129
}
3230

@@ -35,7 +33,8 @@ public static function buildForUpdate(array $payload = []): array
3533
return [
3634
'label' => 'sometimes|string',
3735
'value' => 'sometimes|string|max:255',
38-
'order' => 'sometimes|integer|min:1'
36+
'order' => 'sometimes|integer|min:1',
37+
'is_default' => 'sometimes|boolean',
3938
];
4039
}
4140
}

app/ModelSerializers/ExtraQuestionTypeValueSerializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ExtraQuestionTypeValueSerializer extends SilverStripeSerializer
2525
'Value' => 'value:json_string',
2626
'Order' => 'order:json_int',
2727
'QuestionId' => 'question_id:json_int',
28+
'Default' => 'is_default:json_boolean',
2829
];
2930

3031
protected static $expand_mappings = [

app/Models/Foundation/Main/ExtraQuestions/ExtraQuestionType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,10 @@ public function seed():void{
558558
}
559559
}
560560
}
561+
562+
public function resetDefaultValues():void{
563+
foreach ($this->values as $value){
564+
$value->resetDefaultValue();
565+
}
566+
}
561567
}

app/Models/Foundation/Main/ExtraQuestions/ExtraQuestionTypeValue.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class ExtraQuestionTypeValue extends SilverstripeBaseModel
5151
*/
5252
protected $order;
5353

54+
/**
55+
* @ORM\Column(name="IsDefault", type="boolean")
56+
* @var boolean
57+
*/
58+
protected $is_default;
59+
5460
/**
5561
* @ORM\ManyToOne(targetEntity="ExtraQuestionType", inversedBy="values")
5662
* @ORM\JoinColumn(name="QuestionID", referencedColumnName="ID")
@@ -125,12 +131,35 @@ public function setQuestion(ExtraQuestionType $question): void
125131
/**
126132
* @param string $value
127133
* @param string $label
134+
* @param bool $is_default
128135
*/
129-
public function __construct(string $value = '', string $label = '')
136+
public function __construct(string $value = '', string $label = '', bool $is_default = false)
130137
{
131138
parent::__construct();
132139
$this->value = $value;
133140
$this->label = $label;
134141
$this->order = 1;
142+
$this->is_default = $is_default;
143+
}
144+
145+
/**
146+
* @return bool
147+
*/
148+
public function isDefault(): bool
149+
{
150+
return $this->is_default;
135151
}
152+
153+
/**
154+
* @param bool $is_default
155+
*/
156+
public function setIsDefault(bool $is_default): void
157+
{
158+
$this->is_default = $is_default;
159+
}
160+
161+
public function resetDefaultValue():void{
162+
$this->is_default = false;
163+
}
164+
136165
}

app/Models/Foundation/Summit/Factories/SummitOrderExtraQuestionValueFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static function populate(ExtraQuestionTypeValue $value, array $data):Extr
3939
if(isset($data['value']))
4040
$value->setValue(trim($data['value']));
4141

42+
if(isset($data['is_default']))
43+
$value->setIsDefault(boolval($data['is_default']));
44+
4245
return $value;
4346
}
4447
}

app/Services/Model/Imp/ExtraQuestionTypeService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ protected function _addOrderExtraQuestionValue(ExtraQuestionType $question, arra
6363

6464
$value = ExtraQuestionTypeValueFactory::build($payload);
6565

66+
if($value->isDefault()){
67+
// clear the rest of values
68+
$question->resetDefaultValues();
69+
}
70+
6671
$question->addValue($value);
6772

6873
return $value;
@@ -105,6 +110,11 @@ protected function _updateOrderExtraQuestionValue(ExtraQuestionType $question, i
105110
$question->recalculateValueOrder($value, intval($payload['order']) );
106111
}
107112

113+
if(isset($payload['is_default']) && boolval($payload['is_default'])){
114+
// clear the rest of values
115+
$question->resetDefaultValues();
116+
}
117+
108118
return ExtraQuestionTypeValueFactory::populate($value, $payload);
109119
});
110120
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php namespace Database\Migrations\Model;
2+
/**
3+
* Copyright 2023 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use Doctrine\Migrations\AbstractMigration;
15+
use Doctrine\DBAL\Schema\Schema as Schema;
16+
use LaravelDoctrine\Migrations\Schema\Builder;
17+
use LaravelDoctrine\Migrations\Schema\Table;
18+
19+
/**
20+
* Class Version20230608185457
21+
* @package Database\Migrations\Model
22+
*/
23+
final class Version20230608185457 extends AbstractMigration
24+
{
25+
/**
26+
* @param Schema $schema
27+
*/
28+
public function up(Schema $schema): void
29+
{
30+
$builder = new Builder($schema);
31+
if ($schema->hasTable("ExtraQuestionTypeValue") && !$schema->getTable("ExtraQuestionTypeValue")->hasColumn("IsDefault")) {
32+
$builder->table('ExtraQuestionTypeValue', function (Table $table) {
33+
$table->boolean("IsDefault")->setNotnull(true)->setDefault(false);
34+
});
35+
}
36+
}
37+
38+
/**
39+
* @param Schema $schema
40+
*/
41+
public function down(Schema $schema): void
42+
{
43+
$builder = new Builder($schema);
44+
if ($schema->hasTable("ExtraQuestionTypeValue") && $schema->getTable("ExtraQuestionTypeValue")->hasColumn("IsDefault")) {
45+
$builder->table('ExtraQuestionTypeValue', function (Table $table) {
46+
$table->dropColumn("IsDefault");
47+
});
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)