Skip to content

Commit 5fa15cf

Browse files
committed
If a Response object is passed to the Question's render method, pre-check the radio input if applicable
1 parent 474be0b commit 5fa15cf

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Models/Question.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ public function choices()
1717
return $this->hasMany('DivineOmega\LaravelMultipleChoice\Models\Choice');
1818
}
1919

20-
public function responseItem()
20+
public function responseItems()
2121
{
2222
return $this->hasMany('DivineOmega\LaravelMultipleChoice\Models\ResponseItem');
2323
}
2424

25-
public function render()
25+
public function render(Response $response = null)
2626
{
27-
$view = View::make('lmc::question', ['question' => $this]);
27+
$selectedChoice = null;
28+
29+
if ($response) {
30+
$selectedChoice = $this->responseItems()->where('response_id', $response->id)->first();
31+
}
32+
33+
$view = View::make('lmc::question', ['question' => $this, 'selectedChoice' => $selectedChoice]);
2834
return $view->render();
2935
}
3036
}

src/Resources/Views/question.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@foreach($question->choices as $choice)
1010
<div class="lmc-choice" id="lmc-choice-{{ $choice->id }}">
1111
<input type="radio" class="lmc-choice-input" id="lmc-choice-input-{{ $choice->id }}" name="lmc-question-input-{{ $question->id }}" value="{{ $choice->id }}">
12-
<label class="lmc-choice-label" id="lmc-choice-label-{{ $choice->id }}" for="lmc-choice-input-{{ $choice->id }}">
12+
<label class="lmc-choice-label" id="lmc-choice-label-{{ $choice->id }}" for="lmc-choice-input-{{ $choice->id }}" {{ $choice->id == $selectedChoice->id ? 'checked' : '' }}>
1313
<span class="lmc-choice-text" id="lmc-choice-text-{{ $choice->id }}">{{ $choice->text }}</span>
1414
<span class="lmc-choice-description" id="lmc-choice-description-{{ $choice->id }}">{{ $choice->description }}</span>
1515
</label>

0 commit comments

Comments
 (0)