mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 02:11:45 +01:00
Implement Surveys class, collection of surveys, to work with surveys
This commit is contained in:
@@ -13,7 +13,7 @@ use Meritoo\Common\Exception\Method\DisabledMethodException;
|
|||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Participants of survey.
|
* Collection of participants (of surveys).
|
||||||
* All participants grouped per survey.
|
* All participants grouped per survey.
|
||||||
*
|
*
|
||||||
* It's a collection of participants' collections.
|
* It's a collection of participants' collections.
|
||||||
|
|||||||
58
src/Result/Collection/Surveys.php
Normal file
58
src/Result/Collection/Surveys.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?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\Common\Collection\Collection;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection of surveys (the Survey class instances)
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class Surveys extends Collection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function add($element, $index = null)
|
||||||
|
{
|
||||||
|
if (null === $index) {
|
||||||
|
/* @var Survey $element */
|
||||||
|
$index = $element->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::add($element, $index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all or active only surveys
|
||||||
|
*
|
||||||
|
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function getAll($onlyActive = false)
|
||||||
|
{
|
||||||
|
if ($this->isEmpty() || !$onlyActive) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$all = new static();
|
||||||
|
|
||||||
|
/* @var Survey $survey */
|
||||||
|
foreach ($this as $survey) {
|
||||||
|
if ($survey->isActive()) {
|
||||||
|
$all->add($survey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $all;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,7 +33,7 @@ class ParticipantService
|
|||||||
private $client;
|
private $client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Participants of survey.
|
* Collection of participants (of surveys).
|
||||||
* All participants grouped per survey.
|
* All participants grouped per survey.
|
||||||
*
|
*
|
||||||
* @var Participants
|
* @var Participants
|
||||||
@@ -44,7 +44,8 @@ class ParticipantService
|
|||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
* @param Client $client Client of the LimeSurvey's API
|
* @param Client $client Client of the LimeSurvey's API
|
||||||
* @param Participants $allParticipants (optional) Participants of survey. All participants grouped per survey.
|
* @param Participants $allParticipants (optional) Collection of participants (of surveys). All participants
|
||||||
|
* grouped per survey.
|
||||||
*/
|
*/
|
||||||
public function __construct(Client $client, Participants $allParticipants = null)
|
public function __construct(Client $client, Participants $allParticipants = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Service;
|
|||||||
use Meritoo\Common\Collection\Collection;
|
use Meritoo\Common\Collection\Collection;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
@@ -31,22 +32,23 @@ class SurveyService
|
|||||||
private $client;
|
private $client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All surveys
|
* All surveys.
|
||||||
|
* Collection of surveys (the Survey class instances).
|
||||||
*
|
*
|
||||||
* @var Collection
|
* @var Surveys
|
||||||
*/
|
*/
|
||||||
private $allSurveys;
|
private $allSurveys;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor
|
* Class constructor
|
||||||
*
|
*
|
||||||
* @param Client $client Client of the LimeSurvey's API
|
* @param Client $client Client of the LimeSurvey's API
|
||||||
* @param Collection $allSurveys (optional) All surveys
|
* @param Surveys $allSurveys (optional) All surveys. Collection of surveys (the Survey class instances).
|
||||||
*/
|
*/
|
||||||
public function __construct(Client $client, Collection $allSurveys = null)
|
public function __construct(Client $client, Surveys $allSurveys = null)
|
||||||
{
|
{
|
||||||
if (null === $allSurveys) {
|
if (null === $allSurveys) {
|
||||||
$allSurveys = new Collection();
|
$allSurveys = new Surveys();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
@@ -66,12 +68,16 @@ class SurveyService
|
|||||||
/**
|
/**
|
||||||
* Returns all surveys
|
* Returns all surveys
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
|
||||||
|
* @return Surveys
|
||||||
|
*
|
||||||
* @throws CannotProcessDataException
|
* @throws CannotProcessDataException
|
||||||
*/
|
*/
|
||||||
public function getAllSurveys()
|
public function getAllSurveys($onlyActive = false)
|
||||||
{
|
{
|
||||||
if ($this->allSurveys->isEmpty()) {
|
if ($this->allSurveys->isEmpty()) {
|
||||||
|
$surveys = new Surveys();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$surveys = $this
|
$surveys = $this
|
||||||
->client
|
->client
|
||||||
@@ -87,16 +93,14 @@ class SurveyService
|
|||||||
if (ReasonType::NO_SURVEYS_FOUND !== $reason) {
|
if (ReasonType::NO_SURVEYS_FOUND !== $reason) {
|
||||||
throw $exception;
|
throw $exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
$surveys = new Collection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $surveys) {
|
if (null !== $surveys && $surveys instanceof Collection) {
|
||||||
$this->allSurveys = $surveys;
|
$this->allSurveys = new Surveys($surveys->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->allSurveys;
|
return $this->allSurveys->getAll($onlyActive);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
|||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case of the participants of survey
|
* Test case of the collection of participants (of surveys)
|
||||||
*
|
*
|
||||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
* @copyright Meritoo.pl
|
* @copyright Meritoo.pl
|
||||||
@@ -24,21 +24,21 @@ use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
|||||||
class ParticipantsTest extends BaseTestCase
|
class ParticipantsTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* An empty collection of participants
|
* An empty collection of participants (of surveys)
|
||||||
*
|
*
|
||||||
* @var Participants
|
* @var Participants
|
||||||
*/
|
*/
|
||||||
private $participantsEmpty;
|
private $participantsEmpty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Participants of 1 survey
|
* Collection of participants of 1 survey
|
||||||
*
|
*
|
||||||
* @var Participants
|
* @var Participants
|
||||||
*/
|
*/
|
||||||
private $participantsOfOneSurvey;
|
private $participantsOfOneSurvey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Participants of more than 1 survey
|
* Collection of participants of more than 1 survey
|
||||||
*
|
*
|
||||||
* @var Participants
|
* @var Participants
|
||||||
*/
|
*/
|
||||||
|
|||||||
108
tests/Result/Collection/SurveysTest.php
Normal file
108
tests/Result/Collection/SurveysTest.php
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<?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\Test\ApiClient\Result\Collection;
|
||||||
|
|
||||||
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case of the collection of surveys (the Survey class instances)
|
||||||
|
*
|
||||||
|
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||||
|
* @copyright Meritoo.pl
|
||||||
|
*/
|
||||||
|
class SurveysTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* An empty collection of surveys
|
||||||
|
*
|
||||||
|
* @var Surveys
|
||||||
|
*/
|
||||||
|
private $surveysEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Not empty collection of surveys
|
||||||
|
*
|
||||||
|
* @var Surveys
|
||||||
|
*/
|
||||||
|
private $surveysNotEmpty;
|
||||||
|
|
||||||
|
public function testConstructorVisibilityAndArguments()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(Surveys::class, OopVisibilityType::IS_PUBLIC, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddWithoutIndex()
|
||||||
|
{
|
||||||
|
$survey1 = new Survey([
|
||||||
|
'sid' => 3,
|
||||||
|
'surveyls_title' => 'Test Test Test',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$survey2 = new Survey([
|
||||||
|
'sid' => 4,
|
||||||
|
'surveyls_title' => 'Another Test Test Test',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->surveysEmpty
|
||||||
|
->add($survey1)
|
||||||
|
->add($survey2);
|
||||||
|
|
||||||
|
$this
|
||||||
|
->surveysNotEmpty
|
||||||
|
->add($survey1)
|
||||||
|
->add($survey2);
|
||||||
|
|
||||||
|
static::assertEquals($survey1, $this->surveysEmpty[3]);
|
||||||
|
static::assertEquals($survey2, $this->surveysEmpty[4]);
|
||||||
|
|
||||||
|
static::assertEquals($survey1, $this->surveysNotEmpty[3]);
|
||||||
|
static::assertEquals($survey2, $this->surveysNotEmpty[4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAll()
|
||||||
|
{
|
||||||
|
static::assertCount(0, $this->surveysEmpty->getAll());
|
||||||
|
static::assertCount(0, $this->surveysEmpty->getAll(true));
|
||||||
|
|
||||||
|
static::assertCount(3, $this->surveysNotEmpty->getAll());
|
||||||
|
static::assertCount(2, $this->surveysNotEmpty->getAll(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$surveys = [
|
||||||
|
new Survey([
|
||||||
|
'sid' => 1,
|
||||||
|
'surveyls_title' => 'Test',
|
||||||
|
'active' => 'Y',
|
||||||
|
]),
|
||||||
|
new Survey([
|
||||||
|
'sid' => 2,
|
||||||
|
'surveyls_title' => 'Another Test',
|
||||||
|
'active' => 'Y',
|
||||||
|
]),
|
||||||
|
new Survey([
|
||||||
|
'sid' => 3,
|
||||||
|
'surveyls_title' => 'I am inactive',
|
||||||
|
]),
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->surveysEmpty = new Surveys();
|
||||||
|
$this->surveysNotEmpty = new Surveys($surveys);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
namespace Meritoo\LimeSurvey\Test\ApiClient\Service;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Meritoo\Common\Collection\Collection;
|
|
||||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||||
use Meritoo\Common\Type\OopVisibilityType;
|
use Meritoo\Common\Type\OopVisibilityType;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||||
@@ -17,6 +16,7 @@ use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
|||||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
|
||||||
|
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Service\SurveyService;
|
use Meritoo\LimeSurvey\ApiClient\Service\SurveyService;
|
||||||
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||||
@@ -203,7 +203,7 @@ class SurveyServiceTest extends BaseTestCase
|
|||||||
$configuration = $this->getConnectionConfiguration();
|
$configuration = $this->getConnectionConfiguration();
|
||||||
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
$client = new Client($configuration, $rpcClientManager, $sessionManager);
|
||||||
|
|
||||||
$allSurveys = new Collection([
|
$allSurveys = new Surveys([
|
||||||
new Survey([
|
new Survey([
|
||||||
'sid' => 1,
|
'sid' => 1,
|
||||||
'surveyls_title' => 'Test',
|
'surveyls_title' => 'Test',
|
||||||
|
|||||||
Reference in New Issue
Block a user