Skip to content

Commit

Permalink
Merge branch 'textFieldOnResponseOptions-706' into 'main'
Browse files Browse the repository at this point in the history
Adiciona campo de texto às opções de resposta

See merge request softwares-pkp/plugins_ojs/demographicData!13
  • Loading branch information
JhonathanLepidus committed Oct 16, 2024
2 parents 1031e16 + 3e5b275 commit 5c9fe3e
Show file tree
Hide file tree
Showing 32 changed files with 827 additions and 125 deletions.
1 change: 1 addition & 0 deletions DemographicDataPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function register($category, $path, $mainContextId = null): bool
Hook::add('Schema::get::author', [$this, 'editAuthorSchema']);
Hook::add('Schema::get::demographicQuestion', [$this, 'addCustomSchema']);
Hook::add('Schema::get::demographicResponse', [$this, 'addCustomSchema']);
Hook::add('Schema::get::demographicResponseOption', [$this, 'addCustomSchema']);
Hook::add('Decision::add', [$this, 'requestDataExternalContributors']);
Hook::add('User::edit', [$this, 'checkMigrateResponsesOrcid']);

Expand Down
80 changes: 62 additions & 18 deletions classes/DemographicDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ public function retrieveAllQuestions(int $contextId, bool $shouldRetrieveRespons
'inputType' => $demographicQuestion->getQuestionInputType(),
'title' => $demographicQuestion->getLocalizedQuestionText(),
'description' => $demographicQuestion->getLocalizedQuestionDescription(),
'possibleResponses' => $demographicQuestion->getLocalizedPossibleResponses()
'responseOptions' => $demographicQuestion->getResponseOptions()
];

if ($demographicQuestion->getQuestionType() == DemographicQuestion::TYPE_DROP_DOWN_BOX) {
$questionData['responseOptions'] = [];
foreach ($demographicQuestion->getResponseOptions() as $responseOption) {
$questionData['responseOptions'][$responseOption->getId()] = $responseOption->getLocalizedOptionText();
}
}

if ($shouldRetrieveResponses) {
$user = $request->getUser();
$response = $this->getUserResponse($demographicQuestion, $user->getId());

$questionData['response'] = $response;
$questionData['response'] = $this->getUserResponse($demographicQuestion, $user->getId());
}

$questions[] = $questionData;
Expand All @@ -56,17 +61,17 @@ private function getUserResponse(DemographicQuestion $question, int $userId)
$question->getQuestionType() == DemographicQuestion::TYPE_CHECKBOXES
|| $question->getQuestionType() == DemographicQuestion::TYPE_RADIO_BUTTONS
) {
return [];
return ['value' => [], 'optionsInputValue' => []];
}

return null;
return ['value' => null];
}

$firstResponse = array_shift($demographicResponses);
return $firstResponse->getValue();
return ['value' => $firstResponse->getValue(), 'optionsInputValue' => $firstResponse->getOptionsInputValue()];
}

public function registerUserResponses(int $userId, array $responses)
public function registerUserResponses(int $userId, array $responses, array $responseOptionsInputs)
{
foreach ($responses as $question => $responseInput) {
$questionId = explode("-", $question)[1];
Expand All @@ -77,19 +82,47 @@ public function registerUserResponses(int $userId, array $responses)
->getMany()
->toArray();
$demographicResponse = array_shift($demographicResponses);

$optionsInputValue = $this->getResponseOptionsInputValue($questionId, $responseOptionsInputs, $responseInput);

if ($demographicResponse) {
Repo::demographicResponse()->edit($demographicResponse, ['responseValue' => $responseInput]);
Repo::demographicResponse()->edit($demographicResponse, [
'responseValue' => $responseInput,
'optionsInputValue' => $optionsInputValue
]);
} else {
$response = Repo::demographicResponse()->newDataObject();
$response->setUserId($userId);
$response->setDemographicQuestionId($questionId);
$response->setData('responseValue', $responseInput);
$response->setOptionsInputValue($optionsInputValue);
Repo::demographicResponse()->add($response);
}
}
}

public function registerExternalAuthorResponses(string $externalId, string $externalType, array $responses)
private function getResponseOptionsInputValue($questionId, $responseOptionsInputs, $responseInput)
{
$demographicQuestion = Repo::demographicQuestion()->get($questionId);

if ($demographicQuestion->getQuestionType() == DemographicQuestion::TYPE_CHECKBOXES
|| $demographicQuestion->getQuestionType() == DemographicQuestion::TYPE_RADIO_BUTTONS
) {
$responseOptionsInputValue = [];
foreach ($responseInput as $responseOptionId) {
$responseOptionInputName = "responseOptionInput-$responseOptionId";
if (isset($responseOptionsInputs[$responseOptionInputName])) {
$responseOptionsInputValue[$responseOptionId] = $responseOptionsInputs[$responseOptionInputName];
}
}

return $responseOptionsInputValue;
}

return null;
}

public function registerExternalAuthorResponses(string $externalId, string $externalType, array $responses, array $responseOptionsInputs)
{
$locale = Locale::getLocale();

Expand All @@ -98,10 +131,13 @@ public function registerExternalAuthorResponses(string $externalId, string $exte
$questionId = $questionParts[1];
$questionType = $questionParts[2];

$optionsInputValue = $this->getResponseOptionsInputValue($questionId, $responseOptionsInputs, $responseInput);

$response = Repo::demographicResponse()->newDataObject();
$response->setDemographicQuestionId($questionId);
$response->setExternalId($externalId);
$response->setExternalType($externalType);
$response->setOptionsInputValue($optionsInputValue);

if ($questionType == 'text' or $questionType == 'textarea') {
$response->setData('responseValue', $responseInput, $locale);
Expand Down Expand Up @@ -144,21 +180,29 @@ private function getResponseValueForDisplay($question, $response): string
$question->getQuestionType() == DemographicQuestion::TYPE_CHECKBOXES
|| $question->getQuestionType() == DemographicQuestion::TYPE_RADIO_BUTTONS
) {
$possibleResponses = $question->getLocalizedPossibleResponses();
$selectedResponsesValues = [];
$responseOptions = $question->getResponseOptions();
$selectedResponseOptionsTexts = [];

foreach ($response->getValue() as $selectedResponseOptionId) {
$selectedResponseOption = $responseOptions[$selectedResponseOptionId];
$selectedResponseOptionsText = $selectedResponseOption->getLocalizedOptionText();

if ($selectedResponseOption->hasInputField()) {
$optionsInputValue = $response->getOptionsInputValue();
$selectedResponseOptionsText .= ' "' . $optionsInputValue[$selectedResponseOptionId] . '"';
}

foreach ($response->getValue() as $selectedResponse) {
$selectedResponsesValues[] = $possibleResponses[$selectedResponse];
$selectedResponseOptionsTexts[] = $selectedResponseOptionsText;
}

return implode(', ', $selectedResponsesValues);
return implode(', ', $selectedResponseOptionsTexts);
}

if ($question->getQuestionType() == DemographicQuestion::TYPE_DROP_DOWN_BOX) {
$possibleResponses = $question->getLocalizedPossibleResponses();
$selectedResponse = $response->getValue();
$responseOptions = $question->getResponseOptions();
$selectedResponseOption = $responseOptions[$response->getValue()];

return $possibleResponses[$selectedResponse];
return $selectedResponseOption->getLocalizedOptionText();
}

return '';
Expand Down
25 changes: 15 additions & 10 deletions classes/demographicQuestion/DemographicQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace APP\plugins\generic\demographicData\classes\demographicQuestion;

use APP\plugins\generic\demographicData\classes\facades\Repo;

class DemographicQuestion extends \PKP\core\DataObject
{
public const TYPE_SMALL_TEXT_FIELD = 1;
Expand Down Expand Up @@ -77,18 +79,21 @@ public function setQuestionDescription($descriptionText, $locale)
$this->setData('questionDescription', $descriptionText, $locale);
}

public function getPossibleResponses($locale)
public function getResponseOptions()
{
return $this->getData('possibleResponses', $locale);
}
if (is_null($this->getData('responseOptions'))) {
$responseOptions = Repo::demographicResponseOption()->getCollector()
->filterByQuestionIds([$this->getId()])
->getMany();

public function setPossibleResponses($possibleResponses, $locale)
{
$this->setData('possibleResponses', $possibleResponses, $locale);
}
$mappedResponseOptions = [];
foreach ($responseOptions as $responseOption) {
$mappedResponseOptions[$responseOption->getId()] = $responseOption;
}

public function getLocalizedPossibleResponses()
{
return $this->getLocalizedData('possibleResponses');
$this->setData('responseOptions', $mappedResponseOptions);
}

return $this->getData('responseOptions');
}
}
5 changes: 5 additions & 0 deletions classes/demographicResponse/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public function fromRow(object $row): DemographicResponse
$demographicResponse->setValue(unserialize($serializedValue));
}

if (@unserialize($demographicResponse->getOptionsInputValue())) {
$serializedValue = $demographicResponse->getOptionsInputValue();
$demographicResponse->setOptionsInputValue(unserialize($serializedValue));
}

return $demographicResponse;
}
}
10 changes: 10 additions & 0 deletions classes/demographicResponse/DemographicResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ public function setValue($responseValue)
{
$this->setData('responseValue', $responseValue);
}

public function getOptionsInputValue()
{
return $this->getData('optionsInputValue');
}

public function setOptionsInputValue($optionsInputValue)
{
$this->setData('optionsInputValue', $optionsInputValue);
}
}
52 changes: 52 additions & 0 deletions classes/demographicResponseOption/Collector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace APP\plugins\generic\demographicData\classes\demographicResponseOption;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use PKP\core\interfaces\CollectorInterface;
use Illuminate\Support\LazyCollection;

class Collector implements CollectorInterface
{
public DAO $dao;
public ?array $questionIds = null;

public function __construct(DAO $dao)
{
$this->dao = $dao;
}

public function filterByQuestionIds(?array $questionIds): Collector
{
$this->questionIds = $questionIds;
return $this;
}

public function getQueryBuilder(): Builder
{
$queryBuilder = DB::table($this->dao->table . ' AS dro')
->select(['dro.*']);

if (isset($this->questionIds)) {
$queryBuilder->whereIn('dro.demographic_question_id', $this->questionIds);
}

return $queryBuilder;
}

public function getCount(): int
{
return $this->dao->getCount($this);
}

public function getIds(): Collection
{
return $this->dao->getIds($this);
}

public function getMany(): LazyCollection
{
return $this->dao->getMany($this);
}
}
71 changes: 71 additions & 0 deletions classes/demographicResponseOption/DAO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace APP\plugins\generic\demographicData\classes\demographicResponseOption;

use PKP\core\EntityDAO;
use Illuminate\Support\LazyCollection;
use PKP\core\traits\EntityWithParent;

class DAO extends EntityDAO
{
use EntityWithParent;

public $schema = 'demographicResponseOption';
public $table = 'demographic_response_options';
public $primaryKeyColumn = 'demographic_response_option_id';
public $settingsTable = 'demographic_response_option_settings';
public $primaryTableColumns = [
'id' => 'demographic_response_option_id',
'demographicQuestionId' => 'demographic_question_id',
];

public function getParentColumn(): string
{
return 'demographic_question_id';
}

public function newDataObject(): DemographicResponseOption
{
return app(DemographicResponseOption::class);
}

public function insert(DemographicResponseOption $demographicResponseOption): int
{
return parent::_insert($demographicResponseOption);
}

public function delete(DemographicResponseOption $demographicResponseOption)
{
return parent::_delete($demographicResponseOption);
}

public function update(DemographicResponseOption $demographicResponseOption)
{
return parent::_update($demographicResponseOption);
}

public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->count();
}

public function getMany(Collector $query): LazyCollection
{
$rows = $query
->getQueryBuilder()
->get();

return LazyCollection::make(function () use ($rows) {
foreach ($rows as $row) {
yield $row->demographic_response_option_id => $this->fromRow($row);
}
});
}

public function fromRow(object $row): DemographicResponseOption
{
return parent::fromRow($row);
}
}
36 changes: 36 additions & 0 deletions classes/demographicResponseOption/DemographicResponseOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace APP\plugins\generic\demographicData\classes\demographicResponseOption;

class DemographicResponseOption extends \PKP\core\DataObject
{
public function getDemographicQuestionId(): int
{
return $this->getData('demographicQuestionId');
}

public function setDemographicQuestionId($demographicQuestionId)
{
$this->setData('demographicQuestionId', $demographicQuestionId);
}

public function getLocalizedOptionText()
{
return $this->getLocalizedData('optionText');
}

public function setOptionText(string $text, string $locale)
{
$this->setData('optionText', $text, $locale);
}

public function hasInputField(): bool
{
return $this->getData('hasInputField');
}

public function setHasInputField(bool $hasInputField)
{
$this->setData('hasInputField', $hasInputField);
}
}
Loading

0 comments on commit 5c9fe3e

Please sign in to comment.