-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from pixelant/dev
Added FE mode to show survey results
- Loading branch information
Showing
20 changed files
with
610 additions
and
474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pixelant\PxaSurvey\Controller; | ||
|
||
/*** | ||
* | ||
* This file is part of the "Simple Survey" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* (c) 2017 Andriy Oprysko | ||
* | ||
***/ | ||
|
||
use Pixelant\PxaSurvey\Domain\Model\Answer; | ||
use Pixelant\PxaSurvey\Domain\Model\Question; | ||
use Pixelant\PxaSurvey\Domain\Model\Survey; | ||
use Pixelant\PxaSurvey\Domain\Model\UserAnswer; | ||
use Pixelant\PxaSurvey\Utility\SurveyMainUtility; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
|
||
/** | ||
* Class AbstractController | ||
* @package Pixelant\PxaSurvey\Controller | ||
*/ | ||
abstract class AbstractController extends ActionController | ||
{ | ||
/** | ||
* Survey Repository | ||
* | ||
* @var \Pixelant\PxaSurvey\Domain\Repository\SurveyRepository | ||
* @inject | ||
*/ | ||
protected $surveyRepository = null; | ||
|
||
/** | ||
* User Answer Repository | ||
* | ||
* @var \Pixelant\PxaSurvey\Domain\Repository\UserAnswerRepository | ||
* @inject | ||
*/ | ||
protected $userAnswerRepository = null; | ||
|
||
/** | ||
* Answer Repository | ||
* | ||
* @var \Pixelant\PxaSurvey\Domain\Repository\AnswerRepository | ||
* @inject | ||
*/ | ||
protected $answerRepository = null; | ||
|
||
/** | ||
* Frontend User Repository | ||
* | ||
* @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository | ||
* @inject | ||
*/ | ||
protected $frontendUserRepository = null; | ||
|
||
/** | ||
* Generate data for Charts.js and FE show results | ||
* | ||
* @param Survey $survey | ||
* @return array | ||
*/ | ||
protected function generateAnalysisData(Survey $survey): array | ||
{ | ||
$data = []; | ||
|
||
/** @var Question $question */ | ||
foreach ($survey->getQuestions() as $question) { | ||
$questionData = []; | ||
$allAnswersCount = 0; | ||
|
||
/** @noinspection PhpUndefinedMethodInspection */ | ||
$userAnswers = $this->userAnswerRepository->findByQuestion($question); | ||
|
||
/** @var UserAnswer $userAnswer */ | ||
foreach ($userAnswers as $userAnswer) { | ||
// if check box or radio | ||
if ($userAnswer->getAnswers()->count() > 0) { | ||
/** @var Answer $answer */ | ||
foreach ($userAnswer->getAnswers() as $answer) { | ||
if (!is_array($questionData[$answer->getUid()])) { | ||
$questionData[$answer->getUid()] = [ | ||
'label' => $answer->getText(), | ||
'count' => 1 | ||
]; | ||
} else { | ||
$questionData[$answer->getUid()]['count'] += 1; | ||
} | ||
|
||
$allAnswersCount++; | ||
} | ||
} elseif (!empty($userAnswer->getCustomValue())) { // custom value | ||
$identifier = GeneralUtility::shortMD5($userAnswer->getCustomValue()); | ||
|
||
if (!is_array($questionData[$identifier])) { | ||
$questionData[$identifier] = [ | ||
'label' => $userAnswer->getCustomValue(), | ||
'count' => 1 | ||
]; | ||
} else { | ||
$questionData[$identifier]['count'] += 1; | ||
} | ||
|
||
$allAnswersCount++; | ||
} | ||
} | ||
|
||
// add to data array | ||
$data[$question->getUid()] = [ | ||
'questionData' => $this->calculatePercentsForQuestionData($questionData, $allAnswersCount), | ||
'labelChart' => SurveyMainUtility::translate('module.percentages'), | ||
'label' => $question->getText(), | ||
'allAnswersCount' => $allAnswersCount | ||
]; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Count in percents user answers | ||
* | ||
* @param array $questionData | ||
* @param int $allAnswersCount | ||
* @return array | ||
*/ | ||
protected function calculatePercentsForQuestionData(array $questionData, int $allAnswersCount): array | ||
{ | ||
foreach ($questionData as &$questionItem) { | ||
$questionItem['percents'] = (string)(round($questionItem['count'] / $allAnswersCount, 3) * 100); | ||
} | ||
|
||
return $questionData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.