Skip to content

Commit

Permalink
Merge pull request #12 from pixelant/dev
Browse files Browse the repository at this point in the history
Added FE mode to show survey results
  • Loading branch information
anjeylink authored Jul 19, 2018
2 parents cf337bf + cfe3bc2 commit 79e6bf3
Show file tree
Hide file tree
Showing 20 changed files with 610 additions and 474 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist: precise

matrix:
include:
- php: 7.2
- php: 7.1
- php: 7.0

Expand Down Expand Up @@ -32,8 +33,8 @@ script:
- >
echo;
echo "Running PHP codesniffer";
.Build/bin/phpcs --standard=PSR2 --extensions=php --ignore=*./Tests/* --ignore=*./Configuration/* --ignore=*./.Build/* .
.Build/bin/phpcs -n --standard=PSR2 --extensions=php --ignore=*./Tests/* --ignore=*./Classes/* --ignore=*./.Build/* .
.Build/bin/phpcs --standard=PSR2 --extensions=php Classes/
.Build/bin/phpcs -n --standard=PSR2 --extensions=php --ignore=*./Tests/*,*./Classes/*,*./.Build/* .
- >
echo;
Expand Down
141 changes: 141 additions & 0 deletions Classes/Controller/AbstractController.php
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;
}
}
98 changes: 1 addition & 97 deletions Classes/Controller/SurveyAnalysisController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;

/**
* Class SurveyAnalysisController
* @package Pixelant\PxaSurvey\Controller
*/
class SurveyAnalysisController extends ActionController
class SurveyAnalysisController extends AbstractController
{
/**
* BackendTemplateContainer
Expand All @@ -47,22 +46,6 @@ class SurveyAnalysisController extends ActionController
*/
protected $defaultViewObjectName = BackendTemplateView::class;

/**
* 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;

/**
* Current page
*
Expand Down Expand Up @@ -168,85 +151,6 @@ public function exportCsvAction(Survey $survey)
exit(0);
}

/**
* Generate data for Charts.js
*
* @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;
}

/**
* Set up view
*
Expand Down
63 changes: 22 additions & 41 deletions Classes/Controller/SurveyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,12 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

/**
* SurveyController
*/
class SurveyController extends ActionController
class SurveyController extends AbstractController
{
/**
* 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;

/**
* Include reCAPTCHA api js
*/
Expand Down Expand Up @@ -88,14 +55,12 @@ public function initializeShowAction()
/**
* action show
*
* @param Survey $survey
* @return void
*/
public function showAction(Survey $survey = null)
public function showAction()
{
if ($survey === null && ($surveyUid = (int)$this->settings['survey'])) {
$survey = $this->surveyRepository->findByUid($surveyUid);
}
/** @var Survey $survey */
$survey = $this->surveyRepository->findByUid((int)$this->settings['survey']);

if ($survey !== null && !$this->isSurveyAllowed($survey)) {
/** @noinspection PhpUnhandledExceptionInspection */
Expand Down Expand Up @@ -141,8 +106,7 @@ public function answerAction(Survey $survey, Question $currentQuestion = null)
SurveyMainUtility::addAnswerToSessionData($survey->getUid(), $answers);

// Show next question
/** @noinspection PhpUnhandledExceptionInspection */
$this->forward('show', null, null, ['survey' => $survey]);
$this->forward('show');
}
}

Expand All @@ -159,6 +123,23 @@ public function finishAction(Survey $survey, bool $alreadyFinished = false)
->assign('alreadyFinished', $alreadyFinished);
}

/**
* Show survey results
*/
public function showResultsAction()
{
/** @var Survey $survey */
$survey = $this->surveyRepository->findByUid((int)$this->settings['survey']);

$data = $survey !== null
? $this->generateAnalysisData($survey)
: [];

$this->view
->assign('survey', $survey)
->assign('data', $data);
}

/**
* Get answers from request
*
Expand Down
Loading

0 comments on commit 79e6bf3

Please sign in to comment.