mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 10:11:49 +01:00
Do not fetch all participants of given survey to get information if participant has filled the survey
This commit is contained in:
81
src/LimeSurvey/Base/Result/BaseParticipant.php
Normal file
81
src/LimeSurvey/Base/Result/BaseParticipant.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
|
||||
|
||||
/**
|
||||
* Base class for participant of survey.
|
||||
* Used as a foundation for short or full participant's data.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
abstract class BaseParticipant extends BaseItem
|
||||
{
|
||||
/**
|
||||
* ID of the participant
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* First name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $firstName;
|
||||
|
||||
/**
|
||||
* Last name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $lastName;
|
||||
|
||||
/**
|
||||
* E-mail of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $email;
|
||||
|
||||
/**
|
||||
* Returns ID of the participant
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns e-mail of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
}
|
||||
142
src/LimeSurvey/Base/Result/BaseParticipantsCollection.php
Normal file
142
src/LimeSurvey/Base/Result/BaseParticipantsCollection.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||
|
||||
/**
|
||||
* Base class for participants' collection
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
abstract class BaseParticipantsCollection extends Collection
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add($element, $index = null)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addParticipant');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMultiple($elements, $useIndexes = false)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addParticipants');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($element)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'hasParticipantsOfSurvey');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participants of given survey
|
||||
*
|
||||
* @param Collection $participants Participants to add. Collection of ParticipantShort or Participant instances.
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipants(Collection $participants, $surveyId)
|
||||
{
|
||||
/*
|
||||
* No participants?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($participants->isEmpty()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->addMultiple($participants);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participants of given survey
|
||||
*
|
||||
* If there are no participants of given survey, adds an empty collection who will store participants.
|
||||
* So, this method will return collection always.
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBySurvey($surveyId)
|
||||
{
|
||||
/*
|
||||
* There are no participants of given survey?
|
||||
* Let's add an empty collection who will store participants
|
||||
*/
|
||||
if (!isset($this[$surveyId])) {
|
||||
$this[$surveyId] = new Collection();
|
||||
}
|
||||
|
||||
return $this[$surveyId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if there are participants of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParticipantsOfSurvey($surveyId)
|
||||
{
|
||||
return false === $this
|
||||
->getBySurvey($surveyId)
|
||||
->isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participant of given survey
|
||||
*
|
||||
* @param BaseParticipant $participant Participant to add
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipant(BaseParticipant $participant, $surveyId)
|
||||
{
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->add($participant);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participant of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $participantEmail E-mail of searched participant
|
||||
* @return BaseParticipant|null
|
||||
*/
|
||||
public function getParticipantOfSurvey($surveyId, $participantEmail)
|
||||
{
|
||||
$participants = $this->getBySurvey($surveyId);
|
||||
|
||||
/*
|
||||
* No participants?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($participants->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($participants as $participant) {
|
||||
if ($participant->getEmail() == $participantEmail) {
|
||||
return $participant;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,7 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Collection;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipantsCollection;
|
||||
|
||||
/**
|
||||
* Collection of participants' short data.
|
||||
@@ -22,132 +20,7 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Participants extends Collection
|
||||
class Participants extends BaseParticipantsCollection
|
||||
{
|
||||
const className = 'Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add($element, $index = null)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addParticipants');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMultiple($elements, $useIndexes = false)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'addParticipants');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has($element)
|
||||
{
|
||||
throw new DisabledMethodException(__METHOD__, 'hasParticipantsOfSurvey');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participants of given survey
|
||||
*
|
||||
* @param Collection $participants Participants to add. Collection of ParticipantShort classes.
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipants(Collection $participants, $surveyId)
|
||||
{
|
||||
/*
|
||||
* No participants?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($participants->isEmpty()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->addMultiple($participants);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participant of given survey
|
||||
*
|
||||
* @param ParticipantShort $participant Participant to add
|
||||
* @param int $surveyId ID of survey
|
||||
* @return $this
|
||||
*/
|
||||
public function addParticipant(ParticipantShort $participant, $surveyId)
|
||||
{
|
||||
$this
|
||||
->getBySurvey($surveyId)
|
||||
->add($participant);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if there are participants of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParticipantsOfSurvey($surveyId)
|
||||
{
|
||||
return false === $this
|
||||
->getBySurvey($surveyId)
|
||||
->isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participants of given survey
|
||||
*
|
||||
* If there are no participants of given survey, adds an empty collection who will store participants.
|
||||
* So, this method will return collection always.
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBySurvey($surveyId)
|
||||
{
|
||||
/*
|
||||
* There are no participants of given survey?
|
||||
* Let's add an empty collection who will store participants
|
||||
*/
|
||||
if (!isset($this[$surveyId])) {
|
||||
$this[$surveyId] = new Collection();
|
||||
}
|
||||
|
||||
return $this[$surveyId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participant of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $participantEmail E-mail of searched participant
|
||||
* @return ParticipantShort|null
|
||||
*/
|
||||
public function getParticipantOfSurvey($surveyId, $participantEmail)
|
||||
{
|
||||
/* @var Collection $participants */
|
||||
$participants = $this->getBySurvey($surveyId);
|
||||
|
||||
if ($participants->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* @var ParticipantShort $participant */
|
||||
foreach ($participants as $participant) {
|
||||
if ($participant->getEmail() == $participantEmail) {
|
||||
return $participant;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
36
src/LimeSurvey/Result/Collection/ParticipantsDetails.php
Normal file
36
src/LimeSurvey/Result/Collection/ParticipantsDetails.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Collection;
|
||||
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipantsCollection;
|
||||
|
||||
/**
|
||||
* Collection of participants' full data.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* It's a collection of participants' collections.
|
||||
* The survey ID is used as an index per each collection of participants, so they are grouped by survey.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ParticipantsDetails extends BaseParticipantsCollection
|
||||
{
|
||||
/**
|
||||
* Returns information if survey with given ID has participant with given e-mail address
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $participantEmail E-mail of searched participant
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParticipantOfSurvey($surveyId, $participantEmail)
|
||||
{
|
||||
return null !== $this->getParticipantOfSurvey($surveyId, $participantEmail);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Result\Item;
|
||||
|
||||
use DateTime;
|
||||
use Meritoo\Common\Utilities\Date;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipant;
|
||||
|
||||
/**
|
||||
* One item of the result/data: full data of one participant
|
||||
@@ -18,17 +18,10 @@ use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Participant extends BaseItem
|
||||
class Participant extends BaseParticipant
|
||||
{
|
||||
const className = 'Meritoo\LimeSurvey\ApiClient\Result\Item\Participant';
|
||||
|
||||
/**
|
||||
* ID of the participant
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* Another ID of the participant?
|
||||
* Don't know where it is used.
|
||||
@@ -44,27 +37,6 @@ class Participant extends BaseItem
|
||||
*/
|
||||
private $mpId;
|
||||
|
||||
/**
|
||||
* First name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $firstName;
|
||||
|
||||
/**
|
||||
* Last name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $lastName;
|
||||
|
||||
/**
|
||||
* E-mail of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* Status of the e-mail
|
||||
*
|
||||
@@ -231,16 +203,6 @@ class Participant extends BaseItem
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ID of the participant
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns another ID of the participant?
|
||||
* Don't know where it is used.
|
||||
@@ -262,36 +224,6 @@ class Participant extends BaseItem
|
||||
return $this->mpId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns e-mail of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status of the e-mail
|
||||
*
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Item;
|
||||
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseParticipant;
|
||||
|
||||
/**
|
||||
* One item of the result/data: short data of one participant
|
||||
@@ -16,38 +16,10 @@ use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ParticipantShort extends BaseItem
|
||||
class ParticipantShort extends BaseParticipant
|
||||
{
|
||||
const className = 'Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort';
|
||||
|
||||
/**
|
||||
* ID of the participant
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* First name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $firstName;
|
||||
|
||||
/**
|
||||
* Last name of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $lastName;
|
||||
|
||||
/**
|
||||
* E-mail of the participant
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -66,46 +38,6 @@ class ParticipantShort extends BaseItem
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns ID of the participant
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last name of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns e-mail of the participant
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns short data of participant created from full data of participant
|
||||
*
|
||||
|
||||
@@ -13,6 +13,7 @@ use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\ParticipantsDetails;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
@@ -43,21 +44,39 @@ class ParticipantService
|
||||
*/
|
||||
private $allParticipants;
|
||||
|
||||
/**
|
||||
* Collection of participants' full data.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* @var Participants
|
||||
*/
|
||||
private $participantsDetails;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param Participants $allParticipants (optional) Collection of participants' short data. All participants
|
||||
* grouped per survey.
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param Participants $allParticipants (optional) Collection of participants' short data. All participants
|
||||
* grouped per survey.
|
||||
* @param ParticipantsDetails $participantsDetails (optional) Collection of participants' full data. All
|
||||
* participants grouped per survey.
|
||||
*/
|
||||
public function __construct(Client $client, Participants $allParticipants = null)
|
||||
{
|
||||
public function __construct(
|
||||
Client $client,
|
||||
Participants $allParticipants = null,
|
||||
ParticipantsDetails $participantsDetails = null
|
||||
) {
|
||||
if (null === $allParticipants) {
|
||||
$allParticipants = new Participants();
|
||||
}
|
||||
|
||||
if (null === $participantsDetails) {
|
||||
$participantsDetails = new ParticipantsDetails();
|
||||
}
|
||||
|
||||
$this->client = $client;
|
||||
$this->allParticipants = $allParticipants;
|
||||
$this->participantsDetails = $participantsDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,13 +114,10 @@ class ParticipantService
|
||||
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||
->getData();
|
||||
} catch (CannotProcessDataException $exception) {
|
||||
$reason = $exception->getReason();
|
||||
|
||||
/*
|
||||
* Reason of the exception is different than "Oops, there is no participants. Everything else is fine."?
|
||||
* Let's throw the exception
|
||||
* Oops, something is broken, because the reason is different than "there are no participants"
|
||||
*/
|
||||
if (ReasonType::NO_PARTICIPANTS_FOUND !== $reason) {
|
||||
if (ReasonType::NO_PARTICIPANTS_FOUND !== $exception->getReason()) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
@@ -127,14 +143,7 @@ class ParticipantService
|
||||
*/
|
||||
public function hasParticipant($surveyId, $email)
|
||||
{
|
||||
/*
|
||||
* I have to get all participants of survey to avoid problem when participants exist but are not loaded
|
||||
*/
|
||||
$this->getSurveyParticipants($surveyId);
|
||||
|
||||
return null !== $this
|
||||
->allParticipants
|
||||
->getParticipantOfSurvey($surveyId, $email);
|
||||
return null !== $this->getParticipantDetails($surveyId, $email);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,9 +203,12 @@ class ParticipantService
|
||||
*/
|
||||
$this->getSurveyParticipants($surveyId);
|
||||
|
||||
return $this
|
||||
$participant = $this
|
||||
->allParticipants
|
||||
->getParticipantOfSurvey($surveyId, $email);
|
||||
|
||||
/* @var ParticipantShort $participant */
|
||||
return $participant;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,22 +217,45 @@ class ParticipantService
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $email E-mail address of the participant
|
||||
* @return Participant|null
|
||||
*
|
||||
* @throws CannotProcessDataException
|
||||
*/
|
||||
public function getParticipantDetails($surveyId, $email)
|
||||
{
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
[
|
||||
'email' => $email,
|
||||
],
|
||||
];
|
||||
if (!$this->participantsDetails->hasParticipantOfSurvey($surveyId, $email)) {
|
||||
$participant = null;
|
||||
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
[
|
||||
'email' => $email,
|
||||
],
|
||||
];
|
||||
|
||||
try {
|
||||
/* @var Participant $participant */
|
||||
$participant = $this
|
||||
->client
|
||||
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||
->getData();
|
||||
} catch (CannotProcessDataException $exception) {
|
||||
/*
|
||||
* Oops, something is broken, because the reason is different than "participant was not found"
|
||||
*/
|
||||
if (ReasonType::NO_PARTICIPANT_PROPERTIES !== $exception->getReason()) {
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $participant) {
|
||||
$this->participantsDetails->addParticipant($participant, $surveyId);
|
||||
}
|
||||
}
|
||||
|
||||
$participant = $this
|
||||
->client
|
||||
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||
->getData();
|
||||
->participantsDetails
|
||||
->getParticipantOfSurvey($surveyId, $email);
|
||||
|
||||
/* @var Participant $participant */
|
||||
return $participant;
|
||||
}
|
||||
|
||||
@@ -236,20 +271,9 @@ class ParticipantService
|
||||
public function hasParticipantFilledSurvey($surveyId, $email)
|
||||
{
|
||||
if ($this->hasParticipant($surveyId, $email)) {
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
[
|
||||
'email' => $email,
|
||||
],
|
||||
];
|
||||
|
||||
/* @var Participant $participant */
|
||||
$participant = $this
|
||||
->client
|
||||
->run(MethodType::GET_PARTICIPANT_PROPERTIES, $arguments)
|
||||
->getData();
|
||||
|
||||
return true === $participant->isCompleted();
|
||||
return true === $this
|
||||
->getParticipantDetails($surveyId, $email)
|
||||
->isCompleted();
|
||||
}
|
||||
|
||||
throw new MissingParticipantOfSurveyException($surveyId, $email);
|
||||
|
||||
@@ -28,6 +28,13 @@ class ReasonType extends BaseType
|
||||
*/
|
||||
const NO_PARTICIPANTS_FOUND = 'No survey participants found.';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no participant's properties/details
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const NO_PARTICIPANT_PROPERTIES = 'Error: No results were found based on your attributes.';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no surveys
|
||||
*
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
@@ -51,7 +52,7 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(ParticipantService::className, OopVisibilityType::IS_PUBLIC, 2, 1);
|
||||
static::assertConstructorVisibilityAndArguments(ParticipantService::className, OopVisibilityType::IS_PUBLIC, 3, 1);
|
||||
}
|
||||
|
||||
public function testGetClient()
|
||||
@@ -111,16 +112,49 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
static::assertCount(0, $this->serviceWithParticipants->getSurveyParticipants(3));
|
||||
}
|
||||
|
||||
public function testHasParticipant()
|
||||
public function testHasParticipantUsingServiceWithoutParticipants()
|
||||
{
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(3);
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(2);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
|
||||
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(1, 'john@scott.com'));
|
||||
static::assertFalse($this->serviceWithoutParticipants->hasParticipant(2, 'john@scott.com'));
|
||||
}
|
||||
|
||||
public function testHasParticipant()
|
||||
{
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
'tid' => '123',
|
||||
'participant_id' => null,
|
||||
'mpid' => null,
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
'emailstatus' => 'OK',
|
||||
'token' => uniqid(),
|
||||
'language' => 'pl',
|
||||
'blacklisted' => 'N',
|
||||
'sent' => 'Y',
|
||||
'remindersent' => 'N',
|
||||
'remindercount' => 0,
|
||||
'completed' => 'N',
|
||||
'usesleft' => 10,
|
||||
'validfrom' => null,
|
||||
'validuntil' => (new DateTime())->format('Y-m-d H:i:s'),
|
||||
],
|
||||
[
|
||||
null,
|
||||
],
|
||||
[
|
||||
null,
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(3, $runMethodCallResults);
|
||||
$sessionManager = $this->getSessionManager();
|
||||
$this->createServiceWithParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
static::assertTrue($this->serviceWithParticipants->hasParticipant(1, 'john@scott.com'));
|
||||
static::assertFalse($this->serviceWithParticipants->hasParticipant(2, 'john@scott.com'));
|
||||
@@ -157,9 +191,11 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
|
||||
$runMethodCallResults = [
|
||||
[
|
||||
'firstname' => $firstName,
|
||||
'lastname' => $lastName,
|
||||
'email' => $email,
|
||||
[
|
||||
'firstname' => $firstName,
|
||||
'lastname' => $lastName,
|
||||
'email' => $email,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -200,14 +236,22 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(1);
|
||||
$this->createServiceWithoutParticipants($rpcClientManager, $sessionManager);
|
||||
|
||||
$id = 1;
|
||||
$firstName = 'John';
|
||||
$lastName = 'Scott';
|
||||
$email = 'john@scott.com';
|
||||
$token = uniqid();
|
||||
|
||||
$runMethodCallResults = [
|
||||
'tid' => 1,
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
'token' => uniqid(),
|
||||
'sent' => 'N',
|
||||
'completed' => 'N',
|
||||
[
|
||||
'tid' => $id,
|
||||
'firstname' => $firstName,
|
||||
'lastname' => $lastName,
|
||||
'email' => $email,
|
||||
'token' => $token,
|
||||
'sent' => 'N',
|
||||
'completed' => 'N',
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
|
||||
@@ -218,11 +262,11 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
|
||||
static::assertNull($participant1);
|
||||
static::assertInstanceOf(Participant::className, $participant2);
|
||||
static::assertEquals($runMethodCallResults['tid'], $participant2->getId());
|
||||
static::assertEquals($runMethodCallResults['firstname'], $participant2->getFirstName());
|
||||
static::assertEquals($runMethodCallResults['lastname'], $participant2->getLastName());
|
||||
static::assertEquals($runMethodCallResults['email'], $participant2->getEmail());
|
||||
static::assertEquals($runMethodCallResults['token'], $participant2->getToken());
|
||||
static::assertEquals($id, $participant2->getId());
|
||||
static::assertEquals($firstName, $participant2->getFirstName());
|
||||
static::assertEquals($lastName, $participant2->getLastName());
|
||||
static::assertEquals($email, $participant2->getEmail());
|
||||
static::assertEquals($token, $participant2->getToken());
|
||||
static::assertFalse($participant2->isSent());
|
||||
static::assertFalse($participant2->isCompleted());
|
||||
static::assertNull($participant2->isBlacklisted());
|
||||
@@ -243,10 +287,12 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
public function testHasParticipantFilledSurveyUsingExistingParticipant()
|
||||
{
|
||||
$runMethodCallResults = [
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
'completed' => 'Y',
|
||||
[
|
||||
'firstname' => 'John',
|
||||
'lastname' => 'Scott',
|
||||
'email' => 'john@scott.com',
|
||||
'completed' => (new DateTime())->format('Y-m-d H:i'),
|
||||
],
|
||||
];
|
||||
|
||||
$rpcClientManager = $this->getJsonRpcClientManager(1, $runMethodCallResults);
|
||||
@@ -298,10 +344,22 @@ class ParticipantServiceTest extends BaseTestCase
|
||||
{
|
||||
$rpcClientManager = $this->createMock(JsonRpcClientManager::className);
|
||||
|
||||
$rpcClientManager
|
||||
$mocker = $rpcClientManager
|
||||
->expects(static::exactly($runMethodCallCount))
|
||||
->method('runMethod')
|
||||
->will(static::returnValue($runMethodCallResults));
|
||||
->method('runMethod');
|
||||
|
||||
if (!empty($runMethodCallResults)) {
|
||||
$function = [
|
||||
$mocker,
|
||||
'willReturnOnConsecutiveCalls',
|
||||
];
|
||||
|
||||
/*
|
||||
* I have to use the call_user_func_array() function to pass elements of $runMethodCallResults array as
|
||||
* arguments of the willReturnOnConsecutiveCalls() method
|
||||
*/
|
||||
call_user_func_array($function, $runMethodCallResults);
|
||||
}
|
||||
|
||||
return $rpcClientManager;
|
||||
}
|
||||
|
||||
@@ -30,10 +30,11 @@ class ReasonTypeTest extends BaseTypeTestCase
|
||||
protected function getAllExpectedTypes()
|
||||
{
|
||||
return [
|
||||
'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID,
|
||||
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND,
|
||||
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
|
||||
'NOT_EXISTING_SURVEY_ID' => ReasonType::NOT_EXISTING_SURVEY_ID,
|
||||
'NO_PARTICIPANTS_FOUND' => ReasonType::NO_PARTICIPANTS_FOUND,
|
||||
'NO_PARTICIPANT_PROPERTIES' => ReasonType::NO_PARTICIPANT_PROPERTIES,
|
||||
'NO_SURVEYS_FOUND' => ReasonType::NO_SURVEYS_FOUND,
|
||||
'NO_TOKEN_TABLE' => ReasonType::NO_TOKEN_TABLE,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user