Skip to content

Commit

Permalink
attempt to fix the multiple_choice dichotomous bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Mette committed Mar 29, 2023
1 parent 66e915b commit b8ec326
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Resources/contao/classes/SurveyQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function __get($name)
case 'title':
case 'question':
case 'questiontype':
case 'multiplechoice_subtype':
return $this->arrData[$name];
break;

Expand Down
59 changes: 31 additions & 28 deletions src/Resources/contao/classes/SurveyQuestionMultiplechoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function getResultData(): array

$result['choices'] = $this->arrData['multiplechoice_subtype'] !== 'mc_dichotomous' ?
StringUtil::deserialize($this->arrData['choices'], true) :
[0 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'], 1 => $GLOBALS['TL_LANG']['tl_survey_question']['no']];
[
1 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'],
2 => $GLOBALS['TL_LANG']['tl_survey_question']['no']
];
$result['categories'] = [];
$counter = 1;

Expand Down Expand Up @@ -156,7 +159,12 @@ public function exportDataToExcel(& $exporter, $sheet, & $row): void
$exporter->setCellValue($sheet, $row, 0, [Exporter::DATA => $GLOBALS['TL_LANG']['tl_survey_question']['answers'], Exporter::BGCOLOR => $this->titlebgcolor, Exporter::COLOR => $this->titlecolor, Exporter::FONTWEIGHT => Exporter::FONTWEIGHT_BOLD]);
$exporter->setCellValue($sheet, $row + 1, 0, [Exporter::DATA => $GLOBALS['TL_LANG']['tl_survey_question']['nrOfSelections'], Exporter::BGCOLOR => $this->titlebgcolor, Exporter::COLOR => $this->titlecolor, Exporter::FONTWEIGHT => Exporter::FONTWEIGHT_BOLD]);

$arrChoices = 0 !== strcmp($this->arrData['multiplechoice_subtype'], 'mc_dichotomous') ? StringUtil::deserialize($this->arrData['choices'], true) : [0 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'], 1 => $GLOBALS['TL_LANG']['tl_survey_question']['no']];
$arrChoices = $this->arrData['multiplechoice_subtype'] !== 'mc_dichotomous' ?
StringUtil::deserialize($this->arrData['choices'], true) :
[
1 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'],
2 => $GLOBALS['TL_LANG']['tl_survey_question']['no']
];
$col = 2;

foreach ($arrChoices as $id => $choice) {
Expand Down Expand Up @@ -229,18 +237,29 @@ public function exportDetailsToExcel(& $exporter, $sheet, & $row, & $col, $quest
public function resultAsString($res)
{
$arrAnswer = StringUtil::deserialize($res, true);
$arrChoices = 0 !== strcmp($this->arrData['multiplechoice_subtype'], 'mc_dichotomous') ? StringUtil::deserialize($this->arrData['choices'], true) : [0 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'], 1 => $GLOBALS['TL_LANG']['tl_survey_question']['no']];
$arrChoices = $this->arrData['multiplechoice_subtype'] !== 'mc_dichotomous' ?
StringUtil::deserialize($this->arrData['choices'], true) :
[
1 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'],
2 => $GLOBALS['TL_LANG']['tl_survey_question']['no']
];
$selections = [];

if (\is_array($arrAnswer['value'])) {
foreach ($arrAnswer['value'] as $key => $val) $selections[] = $arrChoices[$val]['choice'];
if (\is_array($arrAnswer['value']))
{
foreach ($arrAnswer['value'] as $key => $val)
{
// ToDo: what todo with 'other' here?
$selections[] = array_key_exists($val, $arrChoices) ? $arrChoices[$val]['choice'] : 'other';
};
return implode(', ', $selections);
}

if(is_array(array_values($arrChoices)[0])) {
$result = $arrChoices[$arrAnswer['value']]['choice'];
// ToDo: what todo with 'other' here?
$result = array_key_exists($arrAnswer['value'], $arrChoices) ? $arrChoices[$arrAnswer['value']]['choice'] : 'other';
} else {
$result = $arrChoices[((int)$arrAnswer['value'])-1];
$result = $arrChoices[((int)$arrAnswer['value'])];
}
// the following line should never have worked like this??
//return $arrChoices[is_numeric($arrAnswer['value']) ? $arrAnswer['value'] : -1]['choice'];
Expand Down Expand Up @@ -312,7 +331,8 @@ protected function calculateCumulated(): void
foreach ($this->arrStatistics['answers'] as $answer) {
$arrAnswer = StringUtil::deserialize($answer, true);

if (\is_array($arrAnswer['value'])) {
if (\is_array($arrAnswer['value']))
{
foreach ($arrAnswer['value'] as $answervalue) {
if (!empty($answervalue)) {
$cumulated[$answervalue] = array_key_exists($answervalue, $cumulated) ? ++$cumulated[$answervalue] : $cumulated[$answervalue] = 1;
Expand All @@ -328,6 +348,7 @@ protected function calculateCumulated(): void
array_push($cumulated['other'], $arrAnswer['other']);
}
}

$this->arrStatistics['cumulated'] = $cumulated;
}

Expand All @@ -350,8 +371,8 @@ protected function exportQuestionHeadersToExcel(& $exporter, $sheet, & $row, & $
{
$this->choices = 'mc_dichotomous' === $this->arrData['multiplechoice_subtype']
? [
0 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'],
1 => $GLOBALS['TL_LANG']['tl_survey_question']['no'],
1 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'],
2 => $GLOBALS['TL_LANG']['tl_survey_question']['no'],
]
: StringUtil::deserialize($this->arrData['choices'], true);

Expand Down Expand Up @@ -570,22 +591,4 @@ protected function exportDetailResults(& $exporter, $sheet, & $row, & $col, $par

return $cells;
}

/*public function resultAsString($res)
{
$arrAnswer = deserialize($res, true);
if (is_array($arrAnswer['value']))
{
return implode (", ", $arrAnswer['value']);
}
else
{
$arrChoices = (strcmp($this->arrData['multiplechoice_subtype'], 'mc_dichotomous') != 0) ? deserialize($this->arrData['choices'], true) : array(0 => $GLOBALS['TL_LANG']['tl_survey_question']['yes'], 1 => $GLOBALS['TL_LANG']['tl_survey_question']['no']);
return $arrChoices[$arrAnswer['value']-1];
}
if (strlen($arrAnswer['other']))
{
return $arrAnswer['other'];
}
}*/
}

1 comment on commit b8ec326

@cmette
Copy link

@cmette cmette commented on b8ec326 Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is intended to fix a strange behavior in the detection and calculation of dichotomous questions. The PR should be tested thoroughly.

Please sign in to comment.