mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 18:11:50 +01:00
composer.json - move tests-related classes to "autoload-dev" section (used for development purposes only and avoid polluting the autoloader in production)
This commit is contained in:
152
src/Service/ParticipantService.php
Normal file
152
src/Service/ParticipantService.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?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\Service;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
|
||||
/**
|
||||
* Serves participants
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ParticipantService
|
||||
{
|
||||
/**
|
||||
* Client of the LimeSurvey's API
|
||||
*
|
||||
* @var Client
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* Participants of survey.
|
||||
* All participants grouped per survey.
|
||||
*
|
||||
* @var Participants
|
||||
*/
|
||||
private $allParticipants;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param Participants $allParticipants (optional) Participants of survey. All participants grouped per survey.
|
||||
*/
|
||||
public function __construct(Client $client, Participants $allParticipants = null)
|
||||
{
|
||||
if (null === $allParticipants) {
|
||||
$allParticipants = new Participants();
|
||||
}
|
||||
|
||||
$this->client = $client;
|
||||
$this->allParticipants = $allParticipants;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns client of the LimeSurvey's API
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns participants of given survey
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return Collection
|
||||
*/
|
||||
public function getSurveyParticipants($surveyId)
|
||||
{
|
||||
$hasSurvey = $this
|
||||
->allParticipants
|
||||
->hasParticipantsOfSurvey($surveyId);
|
||||
|
||||
if (!$hasSurvey) {
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
];
|
||||
|
||||
$participants = $this
|
||||
->client
|
||||
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||
->getData();
|
||||
|
||||
$this
|
||||
->allParticipants
|
||||
->addParticipants($participants, $surveyId);
|
||||
}
|
||||
|
||||
return $this
|
||||
->allParticipants
|
||||
->getBySurvey($surveyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if given survey has participant with given e-mail
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $email E-mail address of the participant
|
||||
* @return bool
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds participant with given data to survey with given ID
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @param string $firstName First name of the participant to add
|
||||
* @param string $lastName Last ame of the participant to add
|
||||
* @param string $email E-mail address of the participant to add
|
||||
* @return Participant
|
||||
*/
|
||||
public function addParticipant($surveyId, $firstName, $lastName, $email)
|
||||
{
|
||||
$participantsData = [
|
||||
[
|
||||
'firstname' => $firstName,
|
||||
'lastname' => $lastName,
|
||||
'email' => $email,
|
||||
],
|
||||
];
|
||||
|
||||
$arguments = [
|
||||
$surveyId,
|
||||
$participantsData,
|
||||
];
|
||||
|
||||
$participantCollection = $this
|
||||
->client
|
||||
->run(MethodType::ADD_PARTICIPANTS, $arguments)
|
||||
->getData();
|
||||
|
||||
$this
|
||||
->allParticipants
|
||||
->addParticipants($participantCollection, $surveyId);
|
||||
|
||||
return $participantCollection->getFirst();
|
||||
}
|
||||
}
|
||||
114
src/Service/SurveyService.php
Normal file
114
src/Service/SurveyService.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?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\Service;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
|
||||
/**
|
||||
* Service that serves surveys
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SurveyService
|
||||
{
|
||||
/**
|
||||
* Client of the LimeSurvey's API
|
||||
*
|
||||
* @var Client
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* All surveys
|
||||
*
|
||||
* @var Collection
|
||||
*/
|
||||
private $allSurveys;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param Client $client Client of the LimeSurvey's API
|
||||
* @param Collection $allSurveys (optional) All surveys
|
||||
*/
|
||||
public function __construct(Client $client, Collection $allSurveys = null)
|
||||
{
|
||||
if (null === $allSurveys) {
|
||||
$allSurveys = new Collection();
|
||||
}
|
||||
|
||||
$this->client = $client;
|
||||
$this->allSurveys = $allSurveys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns client of the LimeSurvey's API
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all surveys
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAllSurveys()
|
||||
{
|
||||
if ($this->allSurveys->isEmpty()) {
|
||||
$surveys = $this
|
||||
->client
|
||||
->run(MethodType::LIST_SURVEYS)
|
||||
->getData();
|
||||
|
||||
if (null !== $surveys) {
|
||||
$this->allSurveys = $surveys;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->allSurveys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if survey with given ID exists
|
||||
*
|
||||
* @param int $surveyId ID of survey to verify
|
||||
* @return bool
|
||||
*/
|
||||
public function isExistingSurvey($surveyId)
|
||||
{
|
||||
$allSurveys = $this->getAllSurveys();
|
||||
|
||||
/*
|
||||
* No surveys?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($allSurveys->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$surveyId = (int)$surveyId;
|
||||
|
||||
/* @var Survey $survey */
|
||||
foreach ($allSurveys as $survey) {
|
||||
if ($survey->getId() == $surveyId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user