First, initial commit

This commit is contained in:
Meritoo
2017-09-19 20:46:11 +02:00
commit 08fd482dd9
42 changed files with 8956 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
<?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\Base\Result;
/**
* Base class for one item of result/data fetched while talking to the LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
abstract class BaseItem
{
/**
* Sets values in each property of the item
*
* @param array $data Data to set in properties of the item
* @return $this
*/
public function setValues($data)
{
/*
* Oops, no data
*/
if (empty($data)) {
return $this;
}
/*
* Let's iterate through each property
*/
foreach ($data as $property => $value) {
$this->setValue($property, $value);
}
return $this;
}
/**
* Sets given value in given property
*
* @param string $property Name of property to set the value
* @param mixed $value Value to set in property
* @return mixed
*/
abstract public function setValue($property, $value);
}

View File

@@ -0,0 +1,137 @@
<?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\Client;
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
use Meritoo\LimeSurvey\ApiClient\Manager\JsonRpcClientManager;
use Meritoo\LimeSurvey\ApiClient\Manager\SessionManager;
use Meritoo\LimeSurvey\ApiClient\Result\Result;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* Client of the LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Client
{
/**
* Configuration used while connecting to LimeSurvey's API
*
* @var ConnectionConfiguration
*/
private $configuration;
/**
* Manager of the JsonRPC client used while connecting to LimeSurvey's API
*
* @var JsonRpcClientManager
*/
private $rpcClientManager;
/**
* Manager of session started while connecting to LimeSurvey's API
*
* @var SessionManager
*/
private $sessionManager;
/**
* Class constructor
*
* @param ConnectionConfiguration $configuration Configuration used while connecting to LimeSurvey's API
* @param JsonRpcClientManager $rpcClientManager (optional) Manager of the JsonRPC client used while
* connecting to LimeSurvey's API
* @param SessionManager $sessionManager (optional) Manager of session started while connecting to
* LimeSurvey's API
*/
public function __construct(
ConnectionConfiguration $configuration,
JsonRpcClientManager $rpcClientManager = null,
SessionManager $sessionManager = null
) {
$this->configuration = $configuration;
$this->rpcClientManager = $rpcClientManager;
$this->sessionManager = $sessionManager;
}
/**
* Runs method with given name, arguments and returns result
*
* @param string $method Name of method to call. One of the MethodType class constants.
* @param array $arguments (optional) Arguments of the method to call
* @return Result
*
* @throws UnknownMethodException
* @throws UnknownInstanceOfResultItem
*/
public function run($method, $arguments = [])
{
/*
* Let's validate method
*/
$method = MethodType::getValidatedMethod($method);
/*
* Prepare key of session
*/
$username = $this->configuration->getUsername();
$password = $this->configuration->getPassword();
$sessionKey = $this
->getSessionManager()
->getSessionKey($username, $password);
/*
* Use the session's key as of the method's arguments
*/
array_unshift($arguments, $sessionKey);
/*
* Run the method, fetch raw data and finally prepare result
*/
$rawData = $this
->getRpcClientManager()
->runMethod($method, $arguments);
return new Result($method, $rawData);
}
/**
* Returns manager of the JsonRPC client used while connecting to LimeSurvey's API
*
* @return JsonRpcClientManager
*/
private function getRpcClientManager()
{
if (null === $this->rpcClientManager) {
$this->rpcClientManager = new JsonRpcClientManager($this->configuration);
}
return $this->rpcClientManager;
}
/**
* Returns manager of session started while connecting to LimeSurvey's API
*
* @return SessionManager
*/
private function getSessionManager()
{
if (null === $this->sessionManager) {
$rpcClientManager = $this->getRpcClientManager();
$this->sessionManager = new SessionManager($rpcClientManager);
}
return $this->sessionManager;
}
}

View File

@@ -0,0 +1,220 @@
<?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\Configuration;
use Meritoo\Common\Exception\Regex\InvalidUrlException;
use Meritoo\Common\Utilities\Regex;
/**
* Configuration used while connecting to LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ConnectionConfiguration
{
/**
* Base url.
* Protocol & domain.
*
* Example:
* http://my-domain.com
*
* @var string
*/
private $baseUrl;
/**
* Url of the LimeSurvey's remote control (without starting slash)
*
* @var string
*/
private $remoteControlUrl = 'admin/remotecontrol';
/**
* Name of user used to authenticate to LimeSurvey
*
* @var string
*/
private $username;
/**
* Password used to authenticate to LimeSurvey
*
* @var string
*/
private $password;
/**
* If is set to true, the "debug" mode is turned on. Otherwise - turned off.
* If turned on, more information is displayed.
*
* @var bool
*/
private $debugMode = false;
/**
* Class constructor
*
* @param string $baseUrl Base url. Protocol & domain.
* @param string $username Name of user used to authenticate to LimeSurvey
* @param string $password Password used to authenticate to LimeSurvey
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off.
*/
public function __construct($baseUrl, $username, $password, $debugMode = false)
{
$this
->setBaseUrl($baseUrl)
->setUsername($username)
->setPassword($password)
->setDebugMode($debugMode);
}
/**
* Returns the base url, protocol & domain
*
* @return string
*/
public function getBaseUrl()
{
return $this->baseUrl;
}
/**
* Sets the base url, protocol & domain
*
* @param string $baseUrl The base url, protocol & domain
* @return $this
*
* @throws InvalidUrlException
*/
public function setBaseUrl($baseUrl)
{
if (!Regex::isValidUrl($baseUrl)) {
throw new InvalidUrlException($baseUrl);
}
if (Regex::endsWith($baseUrl, '/')) {
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
}
$this->baseUrl = $baseUrl;
return $this;
}
/**
* Returns the url of the LimeSurvey's remote control
*
* @return string
*/
public function getRemoteControlUrl()
{
return $this->remoteControlUrl;
}
/**
* Sets the url of the LimeSurvey's remote control
*
* @param string $remoteControlUrl Url of the LimeSurvey's remote control
* @return $this
*/
public function setRemoteControlUrl($remoteControlUrl)
{
$this->remoteControlUrl = $remoteControlUrl;
return $this;
}
/**
* Returns the name of user used to authenticate to LimeSurvey
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Sets the name of user used to authenticate to LimeSurvey
*
* @param string $username The name of user used to authenticate to LimeSurvey
* @return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Returns the password used to authenticate to LimeSurvey
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Sets the password used to authenticate to LimeSurvey
*
* @param string $password The password used to authenticate to LimeSurvey
* @return $this
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Returns information if the "debug" mode is turned on
*
* @return bool
*/
public function isDebugModeOn()
{
return $this->debugMode;
}
/**
* Sets information if the "debug" mode is turned on
*
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off.
* @return $this
*/
public function setDebugMode($debugMode = false)
{
$this->debugMode = $debugMode;
return $this;
}
/*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Additional / extra methods (neither getters, nor setters)
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
/**
* Returns full url of the LimeSurvey's API.
* It's a base url with part related to remote control.
*
* @return string
*/
public function getFullUrl()
{
return sprintf('%s/%s', $this->baseUrl, $this->remoteControlUrl);
}
}

View 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\Exception;
use Exception;
/**
* An exception used while create of the session key has failed
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class CreateSessionKeyFailedException extends Exception
{
/**
* Class constructor
*
* @param string $reason (optional) Reason of failure, e.g. "Invalid user name or password"
*/
public function __construct($reason = '')
{
$message = 'Create of the session key has failed';
if (!empty($reason)) {
$message = sprintf('%s. Reason: \'%s\'.', $message, $reason);
}
parent::__construct($message);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Meritoo\LimeSurvey\ApiClient\Exception;
use Exception;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
/**
* An exception used while instance of one item used by result, with data fetched from the LimeSurvey's API, is unknown
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class UnknownInstanceOfResultItem extends Exception
{
/**
* Class constructor
*
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType class
* constants.
*/
public function __construct($method)
{
$template = 'Instance of one item used by result the of \'%s\' LimeSurvey API\'s method is unknown. Proper'
. ' class is not mapped in %s::%s() method. Did you forget about this?';
$message = sprintf($template, $method, ResultProcessor::class, 'getItemInstance');
parent::__construct($message);
}
}

View File

@@ -0,0 +1,29 @@
<?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\Exception;
use Meritoo\Common\Exception\Base\UnknownTypeException;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* An exception used while name of method used while talking to the LimeSurvey's API is unknown
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class UnknownMethodException extends UnknownTypeException
{
/**
* {@inheritdoc}
*/
public function __construct($unknownType)
{
parent::__construct($unknownType, new MethodType(), 'name of method used while talking to the LimeSurvey\'s API');
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Meritoo\LimeSurvey\ApiClient\Manager;
use JsonRPC\Client as RpcClient;
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* Manager of the JsonRPC client used while connecting to LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class JsonRpcClientManager
{
/**
* Configuration used while connecting to LimeSurvey's API
*
* @var ConnectionConfiguration
*/
private $connectionConfiguration;
/**
* The JsonRPC client used while connecting to LimeSurvey's API
*
* @var RpcClient
*/
private $rpcClient;
/**
* Class constructor
*
* @param ConnectionConfiguration $configuration Configuration used while connecting to LimeSurvey's API
*/
public function __construct(ConnectionConfiguration $configuration)
{
$this->connectionConfiguration = $configuration;
}
/**
* Runs given method with given arguments and returns raw data
*
* @param string $method Name of method to call. One of the MethodType class constants.
* @param array $arguments (optional) Arguments of the method to call
* @return mixed
*
* @throws UnknownMethodException
*/
public function runMethod($method, $arguments = [])
{
$method = MethodType::getValidatedMethod($method);
return $this
->getRpcClient()
->execute($method, $arguments);
}
/**
* Returns the JsonRPC client used while connecting to LimeSurvey's API
*
* @return RpcClient
*/
protected function getRpcClient()
{
if (null === $this->rpcClient) {
/*
* Let's prepare the JsonRPC Client
*/
$url = $this->connectionConfiguration->getFullUrl();
$this->rpcClient = new RpcClient($url);
/*
* The "debug" mode is turned on?
*/
if ($this->connectionConfiguration->isDebugModeOn()) {
$this->rpcClient->getHttpClient()->withDebug();
}
}
return $this->rpcClient;
}
}

View File

@@ -0,0 +1,115 @@
<?php
namespace Meritoo\LimeSurvey\ApiClient\Manager;
use Meritoo\LimeSurvey\ApiClient\Exception\CreateSessionKeyFailedException;
use Meritoo\LimeSurvey\ApiClient\Type\SystemMethodType;
/**
* Manager of session started while connecting to LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SessionManager
{
/**
* The session key.
* Used to authenticate user while connecting to LimeSurvey's API.
*
* @var string
*/
private $sessionKey;
/**
* Manager of the JsonRPC client used while connecting to LimeSurvey's API
*
* @var JsonRpcClientManager
*/
private $rpcClientManager;
/**
* Class constructor
*
* @param JsonRpcClientManager $rpcClientManager Manager of the JsonRPC client used while connecting to
* LimeSurvey's API
*/
public function __construct(JsonRpcClientManager $rpcClientManager)
{
$this->rpcClientManager = $rpcClientManager;
}
/**
* Class destructor
*/
public function __destruct()
{
$this->releaseSessionKey();
}
/**
* Returns key/id of session used while connecting to LimeSurvey's API
*
* @param string $username Name of user used to authenticate to LimeSurvey
* @param string $password Password used to authenticate to LimeSurvey
* @return string
*
* @throws CreateSessionKeyFailedException
*/
public function getSessionKey($username, $password)
{
if (null === $this->sessionKey) {
$arguments = [
$username,
$password,
];
/*
* Let's fetch the key/id of session
*/
$this->sessionKey = $this
->rpcClientManager
->runMethod(SystemMethodType::GET_SESSION_KEY, $arguments);
/*
* Oops, something is broken
*/
if (is_array($this->sessionKey)) {
$reason = '';
/*
* The "status" is provided?
* It's a reason of failure
*/
if (isset($this->sessionKey['status'])) {
$reason = $this->sessionKey['status'];
}
throw new CreateSessionKeyFailedException($reason);
}
}
return $this->sessionKey;
}
/**
* Releases key/id of session and closes the RPC session
*
* @return $this
*/
public function releaseSessionKey()
{
$arguments = [
$this->sessionKey,
];
/*
* Let's release the key/id of session
*/
$this
->rpcClientManager
->runMethod(SystemMethodType::RELEASE_SESSION_KEY, $arguments);
return $this;
}
}

View File

@@ -0,0 +1,397 @@
<?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\Item;
use DateTime;
use Meritoo\Common\Utilities\Date;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
* One item of the result/data: full data of participant
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Participant extends BaseItem
{
/**
* ID of the participant
*
* @var int
*/
private $id;
/**
* Another ID of the participant?
* Don't know where it is used.
*
* @var int
*/
private $participantId;
/**
* MP ID
*
* @var int
*/
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
*
* @var string
*/
private $emailStatus;
/**
* Token of the participant
*
* @var string
*/
private $token;
/**
* Language of the participant
*
* @var string
*/
private $language;
/**
* Information if participant is blacklisted
*
* @var bool
*/
private $blacklisted;
/**
* Information if token was sent to participant/user
*
* @var bool
*/
private $sent;
/**
* Information if reminder about token was sent to participant/user
*
* @var bool
*/
private $reminderSent;
/**
* Count of sent reminders to participant/user
*
* @var int
*/
private $reminderCount;
/**
* Information if the participant has completed the survey
*
* @var bool
*/
private $completed;
/**
* Count of left uses of the token
*
* @var int
*/
private $usesLeft;
/**
* Information from which day the token is valid
*
* @var DateTime
*/
private $validFrom;
/**
* Information until which day the token is valid
*
* @var DateTime
*/
private $validUntil;
/**
* {@inheritdoc}
*/
public function setValue($property, $value)
{
switch ($property) {
case 'tid':
$this->id = (int)$value;
break;
case 'participant_id':
$this->participantId = (int)$value;
break;
case 'mpid':
$this->mpId = (int)$value;
break;
case 'firstname':
$this->firstName = trim($value);
break;
case 'lastname':
$this->lastName = trim($value);
break;
case 'email':
$this->email = trim($value);
break;
case 'emailstatus':
$this->emailStatus = trim($value);
break;
case 'token':
$this->token = trim($value);
break;
case 'language':
$this->language = trim($value);
break;
case 'blacklisted':
$this->blacklisted = 'Y' === trim($value);
break;
case 'sent':
$this->sent = 'Y' === trim($value);
break;
case 'remindersent':
$this->reminderSent = 'Y' === trim($value);
break;
case 'remindercount':
$this->reminderCount = (int)$value;
break;
case 'completed':
$this->completed = 'Y' === trim($value);
break;
case 'usesleft':
$this->usesLeft = (int)$value;
break;
case 'validfrom':
if (null === $value) {
break;
}
$this->validFrom = Date::getDateTime($value, false, 'Y-m-d H:i:s');
break;
case 'validuntil':
if (null === $value) {
break;
}
$this->validUntil = Date::getDateTime($value, false, 'Y-m-d H:i:s');
break;
}
}
/**
* 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.
*
* @return int
*/
public function getParticipantId()
{
return $this->participantId;
}
/**
* Returns MP ID
*
* @return int
*/
public function getMpId()
{
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
*
* @return string
*/
public function getEmailStatus()
{
return $this->emailStatus;
}
/**
* Returns token of the participant
*
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* Returns language of the participant
*
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Returns information if participant is blacklisted
*
* @return bool
*/
public function isBlacklisted()
{
return $this->blacklisted;
}
/**
* Returns information if token was sent to participant/user
*
* @return bool
*/
public function isSent()
{
return $this->sent;
}
/**
* Returns information if reminder about token was sent to participant/user
*
* @return bool
*/
public function isReminderSent()
{
return $this->reminderSent;
}
/**
* Returns count of sent reminders to participant/user
*
* @return int
*/
public function getReminderCount()
{
return $this->reminderCount;
}
/**
* Returns information if the participant has completed the survey
*
* @return bool
*/
public function isCompleted()
{
return $this->completed;
}
/**
* Returns count of left uses of the token
*
* @return int
*/
public function getUsesLeft()
{
return $this->usesLeft;
}
/**
* Returns information from which day the token is valid
*
* @return DateTime
*/
public function getValidFrom()
{
return $this->validFrom;
}
/**
* Returns information until which day the token is valid
*
* @return DateTime
*/
public function getValidUntil()
{
return $this->validUntil;
}
}

View File

@@ -0,0 +1,106 @@
<?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\Item;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
* One item of the result/data: short data of one participant
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ParticipantShort extends BaseItem
{
/**
* 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}
*/
public function setValue($property, $value)
{
switch ($property) {
case 'tid':
$this->id = (int)$value;
break;
case 'participant_info':
$this->firstName = trim($value['firstname']);
$this->lastName = trim($value['lastname']);
$this->email = trim($value['email']);
break;
}
}
/**
* 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;
}
}

View File

@@ -0,0 +1,153 @@
<?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\Item;
/**
* One item of the result/data: full data of one question of survey
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Question extends QuestionShort
{
/**
* Available answers
*
* @var array
*/
private $availableAnswers;
/**
* Sub-questions
* @var array
*/
private $subQuestions;
/**
* Attributes
*
* @var array
*/
private $attributes;
/**
* Attributes in languages
*
* @var array
*/
private $attributesLanguages;
/**
* Answer's options
*
* @var array
*/
private $answerOptions;
/**
* Default value
*
* @var string
*/
private $defaultValue;
/**
* {@inheritdoc}
*/
public function setValue($property, $value)
{
parent::setValue($property, $value);
switch ($property) {
case 'available_answers':
$this->availableAnswers = $value;
break;
case 'subquestions':
$this->subQuestions = $value;
break;
case 'attributes':
$this->attributes = $value;
break;
case 'attributes_lang':
$this->attributesLanguages = $value;
break;
case 'answeroptions':
$this->answerOptions = $value;
break;
case 'defaultvalue':
$this->defaultValue = $value;
break;
}
}
/**
* Returns available answers
*
* @return array
*/
public function getAvailableAnswers()
{
return $this->availableAnswers;
}
/**
* Returns sub-questions
*
* @return array
*/
public function getSubQuestions()
{
return $this->subQuestions;
}
/**
* Returns attributes
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* Returns attributes in languages
*
* @return array
*/
public function getAttributesLanguages()
{
return $this->attributesLanguages;
}
/**
* Returns answer's options
*
* @return array
*/
public function getAnswerOptions()
{
return $this->answerOptions;
}
/**
* Returns default value
*
* @return string
*/
public function getDefaultValue()
{
return $this->defaultValue;
}
}

View File

@@ -0,0 +1,398 @@
<?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\Item;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
* One item of the result/data: short data of one question of survey
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class QuestionShort extends BaseItem
{
/**
* ID of the question
*
* @var int
*/
private $id;
/**
* ID of the parent question
*
* @var int
*/
private $parentId;
/**
* ID of the survey
*
* @var int
*/
private $surveyId;
/**
* ID of the group of questions
*
* @var int
*/
private $groupId;
/**
* ID of the scale
* (what does it mean?)
*
* @var int
*/
private $scaleId;
/**
* Type of the question
*
* @var string
*/
private $type;
/**
* Title of the question
*
* @var string
*/
private $title;
/**
* Content of the question
*
* @var string
*/
private $content;
/**
* Explanation of the question
*
* @var string
*/
private $contentHelp;
/**
* Regular expression
* (what does it mean? used to validate answer?)
*
* @var string
*/
private $regularExpression;
/**
* Information if type of question is other
* (what does it mean?)
*
* @var bool
*/
private $other;
/**
* Information if the question mandatory
*
* @var bool
*/
private $mandatory;
/**
* Position/Order of the question
*
* @var int
*/
private $position;
/**
* Language of the question
*
* @var string
*/
private $language;
/**
* Same as default
* (what does it mean?)
*
* @var int
*/
private $sameDefault;
/**
* Relevant equation
*
* @var string
*/
private $relevance;
/**
* Name of module
* (what does it mean?)
*
* @var string
*/
private $moduleName;
/**
* {@inheritdoc}
*/
public function setValue($property, $value)
{
switch ($property) {
case 'qid':
$this->id = (int)$value;
break;
case 'parent_qid':
$this->parentId = (int)$value;
break;
case 'sid':
$this->surveyId = (int)$value;
break;
case 'gid':
$this->groupId = (int)$value;
break;
case 'scale_id':
$this->scaleId = (int)$value;
break;
case 'type':
$this->type = trim($value);
break;
case 'title':
$this->title = trim($value);
break;
case 'question':
$this->content = trim($value);
break;
case 'help':
$this->contentHelp = trim($value);
break;
case 'preg':
if (null === $value) {
break;
}
$this->regularExpression = trim($value);
break;
case 'other':
$this->other = 'Y' === trim($value);
break;
case 'mandatory':
$this->mandatory = 'Y' === trim($value);
break;
case 'question_order':
$this->position = (int)$value;
break;
case 'language':
$this->language = trim($value);
break;
case 'same_default':
$this->sameDefault = (int)$value;
break;
case 'relevance':
$this->relevance = trim($value);
break;
case 'modulename':
if (null === $value) {
break;
}
$this->moduleName = trim($value);
break;
}
}
/**
* Returns ID of the question
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Returns ID of the parent question
*
* @return int
*/
public function getParentId()
{
return $this->parentId;
}
/**
* Returns ID of the survey
*
* @return int
*/
public function getSurveyId()
{
return $this->surveyId;
}
/**
* Returns ID of the group of questions
*
* @return int
*/
public function getGroupId()
{
return $this->groupId;
}
/**
* Returns ID of the scale
*
* @return int
*/
public function getScaleId()
{
return $this->scaleId;
}
/**
* Returns type of the question
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Returns title of the question
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Returns content of the question
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Returns explanation of the question
*
* @return string
*/
public function getContentHelp()
{
return $this->contentHelp;
}
/**
* Returns regular expression
*
* @return string
*/
public function getRegularExpression()
{
return $this->regularExpression;
}
/**
* Returns information if type of question is other
*
* @return bool
*/
public function isOther()
{
return $this->other;
}
/**
* Returns information if the question mandatory
*
* @return bool
*/
public function isMandatory()
{
return $this->mandatory;
}
/**
* Returns position/Order of the question
*
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* Returns language of the question
*
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Returns information related to same as default
*
* @return int
*/
public function getSameDefault()
{
return $this->sameDefault;
}
/**
* Returns relevant equation
*
* @return string
*/
public function getRelevance()
{
return $this->relevance;
}
/**
* Returns name of module
*
* @return string
*/
public function getModuleName()
{
return $this->moduleName;
}
}

View File

@@ -0,0 +1,143 @@
<?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\Item;
use DateTime;
use Meritoo\Common\Utilities\Date;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
/**
* One item of the result/data: survey
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Survey extends BaseItem
{
/**
* ID of the survey
*
* @var int
*/
private $id;
/**
* Title of the survey
*
* @var string
*/
private $title;
/**
* Date when the survey starts
*
* @var DateTime
*/
private $startsAt;
/**
* Date when the survey expires
*
* @var DateTime
*/
private $expiresAt;
/**
* Information if the survey is active
*
* @var bool
*/
private $active = false;
/**
* {@inheritdoc}
*/
public function setValue($property, $value)
{
switch ($property) {
case 'sid':
$this->id = (int)$value;
break;
case 'surveyls_title':
$this->title = trim($value);
break;
case 'startdate':
if (null === $value) {
break;
}
$this->startsAt = Date::getDateTime($value, false, 'Y-m-d H:i:s');
break;
case 'expires':
if (null === $value) {
break;
}
$this->expiresAt = Date::getDateTime($value, false, 'Y-m-d H:i:s');
break;
case 'active':
$this->active = 'Y' === trim($value);
break;
}
}
/**
* Returns ID of the survey
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Returns title of the survey
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Returns date when the survey starts
*
* @return DateTime|null
*/
public function getStartsAt()
{
return $this->startsAt;
}
/**
* Returns date when the survey expires
*
* @return DateTime|null
*/
public function getExpiresAt()
{
return $this->expiresAt;
}
/**
* Returns information if the survey is active
*
* @return bool
*/
public function isActive()
{
return $this->active;
}
}

View File

@@ -0,0 +1,118 @@
<?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\Processor;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Question;
use Meritoo\LimeSurvey\ApiClient\Result\Item\QuestionShort;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* Processor of the raw data fetched while talking to the LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class ResultProcessor
{
/**
* Returns processed data based on the raw data returned by the LimeSurvey's API
*
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
* class constants.
* @param array $rawData Data returned by the LimeSurvey's API
* @return null|BaseItem|array
*/
public function process($method, array $rawData)
{
$method = MethodType::getValidatedMethod($method);
/*
* No data?
* Nothing to do
*/
if (empty($rawData)) {
return null;
}
/*
* Prepare instance of one item
*/
$item = $this->getItemInstance($method);
/*
* The raw data is or, actually, should be iterable?
*/
if (MethodType::isResultIterable($method)) {
$items = [];
$emptyItem = clone $item;
foreach ($rawData as $itemData) {
$items[] = $emptyItem->setValues($itemData);
}
return $items;
}
return $item->setValues($rawData);
}
/**
* Returns instance of one item of the result
*
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
* class constants.
* @return BaseItem
* @throws UnknownInstanceOfResultItem
*/
private function getItemInstance($method)
{
$item = null;
$method = MethodType::getValidatedMethod($method);
switch ($method) {
case MethodType::GET_PARTICIPANT_PROPERTIES:
$item = new Participant();
break;
case MethodType::GET_QUESTION_PROPERTIES:
$item = new Question();
break;
case MethodType::LIST_PARTICIPANTS:
$item = new ParticipantShort();
break;
case MethodType::LIST_QUESTIONS:
$item = new QuestionShort();
break;
case MethodType::LIST_SURVEYS:
$item = new Survey();
break;
/*
* todo: Use other types of methods and create proper classes (used to get instances of one item)
*/
}
/*
* Instance of the item is unknown?
*/
if (null === $item) {
throw new UnknownInstanceOfResultItem($method);
}
return $item;
}
}

View File

@@ -0,0 +1,122 @@
<?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;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
/**
* Result with data fetched while talking to the LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Result
{
/**
* Name of called method while talking to the LimeSurvey's API. One of the MethodType class constants.
*
* @var string
*/
private $method;
/**
* Raw data returned by the LimeSurvey's API
*
* @var array
*/
private $rawData;
/**
* Processor of the raw data fetched while talking to the LimeSurvey's API
*
* @var ResultProcessor
*/
private $resultProcessor;
/**
* Class constructor
*
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
* class constants.
* @param array $rawData Raw data returned by the LimeSurvey's API
*/
public function __construct($method, array $rawData)
{
$this->method = MethodType::getValidatedMethod($method);
$this->rawData = $rawData;
}
/**
* Returns information if the result contains any data
*
* @return bool
*/
public function isEmpty()
{
return empty($this->rawData);
}
/**
* Returns data returned by the LimeSurvey's API
*
* @param bool $raw (optional) If is set to true, raw data provided by the LimeSurvey's API will be returned.
* Otherwise - prepared/processed.
* @return array|Collection|BaseItem
*/
public function getData($raw = false)
{
if ($raw) {
return $this->rawData;
}
return $this->getProcessedData($this->rawData);
}
/**
* Returns processed data based on the raw data returned by the LimeSurvey's API
*
* @param array $rawData Raw data returned by the LimeSurvey's API
* @return Collection|BaseItem
*/
private function getProcessedData(array $rawData)
{
$processed = $this
->getResultProcessor()
->process($this->method, $rawData);
if (null === $processed || is_array($processed)) {
$collection = new Collection();
if (is_array($processed)) {
$collection->addMultiple($processed);
}
return $collection;
}
return $processed;
}
/**
* Returns processor of the raw data fetched while talking to the LimeSurvey's API
*
* @return ResultProcessor
*/
private function getResultProcessor()
{
if (null === $this->resultProcessor) {
$this->resultProcessor = new ResultProcessor();
}
return $this->resultProcessor;
}
}

View File

@@ -0,0 +1,113 @@
<?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\Type;
use Meritoo\Common\Type\Base\BaseType;
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
/**
* Type of method used while talking with LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class MethodType extends BaseType
{
/**
* Add a response to the survey responses collection.
* Returns the id of the inserted survey response.
*
* @var string
*/
const ADD_RESPONSE = 'add_response';
/**
* Export statistics of a survey to a user
*
* @var string
*/
const EXPORT_STATISTICS = 'export_statistics';
/**
* Get settings of a token/participant of a survey
*
* @var string
*/
const GET_PARTICIPANT_PROPERTIES = 'get_participant_properties';
/**
* Get properties of a question in a survey
*
* @var string
*/
const GET_QUESTION_PROPERTIES = 'get_question_properties';
/**
* Return the IDs and properties of token/participants of a survey
*
* @var string
*/
const LIST_PARTICIPANTS = 'list_participants';
/**
* Return the ids and info of (sub-)questions of a survey/group
*
* @var string
*/
const LIST_QUESTIONS = 'list_questions';
/**
* List the surveys belonging to a user
*
* @var string
*/
const LIST_SURVEYS = 'list_surveys';
/**
* Get list the ids and info of users
*
* @var string
*/
const LIST_USERS = 'list_users';
/**
* Returns validated name of method to call or throws an exception (if method is incorrect)
*
* @param string $method Name of method to call. One of this class constants.
* @return string
*
* @throws UnknownMethodException
*/
public static function getValidatedMethod($method)
{
if ((new static())->isCorrectType($method)) {
return $method;
}
throw new UnknownMethodException($method);
}
/**
* Returns information if result provided by the API is iterable
*
* @param string $method Name of called method while talking to the LimeSurvey's API. One of this class constants.
* @return bool
*/
public static function isResultIterable($method)
{
$method = static::getValidatedMethod($method);
return in_array($method, [
static::LIST_PARTICIPANTS,
static::LIST_QUESTIONS,
static::LIST_SURVEYS,
static::LIST_USERS,
]);
}
}

View File

@@ -0,0 +1,34 @@
<?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\Type;
use Meritoo\Common\Type\Base\BaseType;
/**
* Type of system-related method used while talking with LimeSurvey's API
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class SystemMethodType extends BaseType
{
/**
* Create and return a session key
*
* @var string
*/
const GET_SESSION_KEY = 'get_session_key';
/**
* Close the RPC session
*
* @var string
*/
const RELEASE_SESSION_KEY = 'release_session_key';
}