protected function buildAnswer(SurveyAnswer $answer, PlDict $data)
{
$content = $data->v($this->qid);
- $value = $content['answer'];
+ $value = $content['answers'];
if (empty($value)) {
$answer->answer = null;
return true;
}
- $id = to_integer($value);
- if ($id === false) {
- if ($value != 'other') {
- $answer->answers = null;
- return false;
+ if ($this->parameters['subtype'] == 'radio') {
+ if (count($value) > 1) {
+ throw new Exception("You cannot select more than one answer");
}
- if (@$this->parameters['allow_other']) {
- $answer->answer = array('other' => $content['text']);
+ }
+ $answers = array();
+ $answers['answers'] = array();
+ foreach ($value as $key=>$text) {
+ if (can_convert_to_integer($key)) {
+ $key = to_integer($key);
+ if ($text != $this->parameters['answers'][$key]) {
+ throw new Exception("Answer text does not match");
+ }
+ $answers['answers'][] = $key;
+ } else if ($key != 'other') {
+ throw new Exception("Unsupported answer id $key");
+ } else if (!$this->parameters['allow_other']) {
+ throw new Exception("Got 'Other' answer while not supported");
+ } else if (!isset($content['other'])) {
+ $answers['other'] = '';
+ } else {
+ $answers['other'] = $content['other'];
}
+ }
+ if (empty($value)) {
+ $answer->answer = null;
+ return false;
} else {
- if ($id >= count($this->parameters['answers'])) {
- $answer->answers = null;
- return false;
- }
- $answer->answer = array('answer' => $id);
+ $answer->answer = $answers;
}
return true;
}
<div>
Permettre la sélection de plusieurs réponses ?
<select name="q_edit[${qid}][subtype]">
- <option value="checkbox">Oui</option>
- <option value="radio" selected="selected">Non</option>
+ <option value="checkbox" {{if subtype}}{{if subtype == 'checkbox'}}selected="selected"{{/if}}{{/if}}>
+ Oui
+ </option>
+ <option value="radio" {{if subtype}}{{if subtype == 'radio'}}selected="selected"{{/if}}{{/if}}>
+ Non
+ </option>
</select>
</div>
<div class="add_answer">
<div>
<div><strong>${label}</strong></div>
{{each answers}}
- <input type="${subtype}" name="qid[${qid}][]" value="${$index}" /> ${$value}<br />
+ <input type="${subtype}" name="qid[${qid}][answers][${$index}]" value="${$value}" /> ${$value}<br />
{{/each}}
{{if allow_other}}
- <input type="${subtype}" name="qid[${qid}][other][checked]" value="1" /> Autre, préciser :
- <input type="text" name="qid[${qid}][other][text]" />
+ <input type="${subtype}" name="qid[${qid}][answers][other]" value="other" /> Autre, préciser :
+ <input type="text" name="qid[${qid}][other]" />
{{/if}}
</div>
</script>