2021-08-11 17:26:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace LDC\LDCCRM\Models;
|
|
|
|
|
|
|
|
|
|
use LDC\LDCCRM\Enums\ApiStatus;
|
|
|
|
|
use LDC\LDCCRM\Utils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class represents an answer
|
|
|
|
|
* It has a method to submit answers
|
|
|
|
|
*
|
|
|
|
|
* @property string $id The answer id
|
|
|
|
|
* @property string $question The actual question as a string
|
|
|
|
|
* @property \LDC\LDCCRM\Enums\QuestionType $type
|
|
|
|
|
* The question type to display,
|
|
|
|
|
* it also hints wether it can accept one or multiple responses,
|
|
|
|
|
* so only checkbox allows multiple responses
|
|
|
|
|
* @property \LDC\LDCCRM\Models\Answer[] $answers An array with answers
|
|
|
|
|
*
|
|
|
|
|
* @category Api Models
|
|
|
|
|
* @package LDCCrmClient
|
|
|
|
|
* @subpackage models
|
|
|
|
|
*/
|
|
|
|
|
class Answer extends CrmClientModel
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is true if this model was not filled from crm
|
|
|
|
|
*
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
private $isNew = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The question id
|
|
|
|
|
*
|
|
|
|
|
* Required
|
|
|
|
|
* Field Type bigInt
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The actual answer as a string
|
|
|
|
|
*
|
|
|
|
|
* Required
|
|
|
|
|
* Max length 255
|
|
|
|
|
* Field Type varchar
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $answer = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The crm customer fields
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $modelFields = [
|
|
|
|
|
'id', 'answer'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validation rules
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $validation = [
|
|
|
|
|
'id' => 'required|int',
|
|
|
|
|
'answer' => 'required|string|max:255'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function __construct(array $properties = [])
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->fill($properties);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUBLIC METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-12 14:49:27 +02:00
|
|
|
* Set unanswered question for a specific customer
|
2021-08-11 17:26:30 +02:00
|
|
|
*
|
2021-08-12 14:49:27 +02:00
|
|
|
* @param string $hashcode The customer hashcode
|
|
|
|
|
* @param integer $questionId The question id
|
|
|
|
|
* @param array $answersId An array with answers id
|
|
|
|
|
* @return bool true if succeeded
|
|
|
|
|
*
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException
|
|
|
|
|
* if an error occurs on server side or on getting response
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
|
|
|
|
|
* If any error or \LDC\LDCCRM\Enums\ApiError occurs
|
2021-08-11 17:26:30 +02:00
|
|
|
*/
|
|
|
|
|
public static function setQuestionAnswers(string $hashcode, int $questionId, array $answersId): bool
|
|
|
|
|
{
|
|
|
|
|
self::assertValidHascode($hashcode);
|
|
|
|
|
self::assertValidPrimaryKey($questionId);
|
|
|
|
|
if (!count($answersId)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
foreach ($answersId as $answerId) {
|
|
|
|
|
self::assertValidPrimaryKey($answerId);
|
|
|
|
|
}
|
|
|
|
|
static::setUpApiInterface(); // set apiInterface
|
|
|
|
|
$request = Utils::getResquest(
|
|
|
|
|
'apiV2.customers.answers.store',
|
|
|
|
|
[
|
|
|
|
|
'customer' => $hashcode
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'question' => $questionId,
|
|
|
|
|
'answers' => $answersId
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = static::$apiInterface->call($request);
|
|
|
|
|
return $response->status->equals(ApiStatus::OK());
|
|
|
|
|
}
|
2021-08-16 13:22:34 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Serialize this object to an array
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function jsonSerialize()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'id' => $this->id,
|
|
|
|
|
'answer' => $this->answer
|
|
|
|
|
];
|
|
|
|
|
}
|
2021-08-11 17:26:30 +02:00
|
|
|
}
|