-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QE-429 op handler subquestion update (#3492)
* OpHandlerSubquestionUpdate.php first steps * OpHandlerSubquestionUpdate.php with basic unit tests * re-added deleted function * exception when no question found * OpHandlerSubQuestion.php can now handle create and update * OpHandlerSubQuestion.php fixed a test * QE-429 dev: throw exception when prepared data is empty array * adjustments after change of how subquestions are saved * OpHandlerSubQuestion.php can now handle create and update after changes in SubQuestionService * fix psalm issue --------- Co-authored-by: pstelling <patricia.stelling@limesurvey.org>
- Loading branch information
Showing
9 changed files
with
404 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
application/libraries/Api/Command/V1/SurveyPatch/OpHandlerSubQuestion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
|
||
namespace LimeSurvey\Api\Command\V1\SurveyPatch; | ||
|
||
use LimeSurvey\Api\Command\V1\Transformer\Input\{ | ||
TransformerInputQuestion, | ||
TransformerInputQuestionL10ns | ||
}; | ||
use LimeSurvey\Models\Services\{ | ||
Exception\NotFoundException, | ||
Exception\PermissionDeniedException, | ||
Exception\PersistErrorException, | ||
QuestionAggregateService, | ||
QuestionAggregateService\QuestionService, | ||
QuestionAggregateService\SubQuestionsService | ||
}; | ||
use LimeSurvey\ObjectPatch\{Op\OpInterface, | ||
OpHandler\OpHandlerException, | ||
OpHandler\OpHandlerInterface, | ||
OpType\OpTypeCreate, | ||
OpType\OpTypeUpdate | ||
}; | ||
|
||
/** | ||
* Class OpHandlerSubQuestion can handle create and update | ||
* of subquestions which belong to a single question. | ||
*/ | ||
class OpHandlerSubQuestion implements OpHandlerInterface | ||
{ | ||
use OpHandlerSurveyTrait; | ||
use OpHandlerQuestionTrait; | ||
|
||
protected QuestionAggregateService $questionAggregateService; | ||
protected SubQuestionsService $subQuestionsService; | ||
protected QuestionService $questionService; | ||
protected TransformerInputQuestion $transformer; | ||
protected TransformerInputQuestionL10ns $transformerL10ns; | ||
|
||
public function __construct( | ||
QuestionAggregateService $questionAggregateService, | ||
SubQuestionsService $subQuestionsService, | ||
QuestionService $questionService, | ||
TransformerInputQuestionL10ns $transformerL10n, | ||
TransformerInputQuestion $transformer | ||
) { | ||
$this->questionAggregateService = $questionAggregateService; | ||
$this->subQuestionsService = $subQuestionsService; | ||
$this->questionService = $questionService; | ||
$this->transformer = $transformer; | ||
$this->transformerL10ns = $transformerL10n; | ||
} | ||
|
||
/** | ||
* Checks if the operation is applicable for the given entity. | ||
* | ||
* @param OpInterface $op | ||
* @return bool | ||
*/ | ||
public function canHandle(OpInterface $op): bool | ||
{ | ||
return ( | ||
$op->getType()->getId() === OpTypeUpdate::ID | ||
|| $op->getType()->getId() === OpTypeCreate::ID | ||
) | ||
&& $op->getEntityType() === 'subquestion'; | ||
} | ||
|
||
/** | ||
* Handle subquestion create or update operation. | ||
* Attention: subquestions not present in the patch will be deleted. | ||
* Expects a patch structure like this for update: | ||
* { | ||
* "patch": [{ | ||
* "entity": "subquestion", | ||
* "op": "update", | ||
* "id": 722, //parent qid | ||
* "props": { | ||
* "0": { | ||
* "qid": 728, | ||
* "title": "SQ001new", | ||
* "l10ns": { | ||
* "de": { | ||
* "question": "subger1updated", | ||
* "language": "de" | ||
* }, | ||
* "en": { | ||
* "question": "sub1updated", | ||
* "language": "en" | ||
* } | ||
* } | ||
* }, | ||
* "1": { | ||
* "qid": 729, | ||
* "title": "SQ002new", | ||
* "l10ns": { | ||
* "de": { | ||
* "question": "subger2updated", | ||
* "language": "de" | ||
* }, | ||
* "en": { | ||
* "question": "sub2updated", | ||
* "language": "en" | ||
* } | ||
* } | ||
* } | ||
* } | ||
* } | ||
* ] | ||
* } | ||
* | ||
* Expects a patch structure like this for update: | ||
* { | ||
* "patch": [{ | ||
* "entity": "subquestion", | ||
* "op": "create", | ||
* "id": 722, | ||
* "props": { | ||
* "0": { | ||
* "title": "SQ011", | ||
* "l10ns": { | ||
* "de": { | ||
* "question": "germanized1", | ||
* "language": "de" | ||
* }, | ||
* "en": { | ||
* "question": "englishized", | ||
* "language": "en" | ||
* } | ||
* } | ||
* }, | ||
* "1": { | ||
* "title": "SQ012", | ||
* "l10ns": { | ||
* "de": { | ||
* "question": "germanized2", | ||
* "language": "de" | ||
* }, | ||
* "en": { | ||
* "question": "englishized2", | ||
* "language": "en" | ||
* } | ||
* } | ||
* } | ||
* } | ||
* } | ||
* ] | ||
* } | ||
* | ||
* @param OpInterface $op | ||
* @throws OpHandlerException | ||
* @throws NotFoundException | ||
* @throws PermissionDeniedException | ||
* @throws PersistErrorException | ||
*/ | ||
public function handle(OpInterface $op): void | ||
{ | ||
$surveyId = $this->getSurveyIdFromContext($op); | ||
$this->questionAggregateService->checkUpdatePermission($surveyId); | ||
$preparedData = $this->prepareSubQuestions( | ||
$op, | ||
$this->transformer, | ||
$this->transformerL10ns, | ||
$op->getProps(), | ||
['subquestions'] | ||
); | ||
//be careful here! if for any reason the incoming data is not prepared | ||
//as it should, all existing subquestions will be deleted! | ||
if (count($preparedData) === 0) { | ||
throw new OpHandlerException('No data to create or update a subquestion'); | ||
} | ||
$questionId = $op->getEntityId(); | ||
$this->subQuestionsService->save( | ||
$this->questionService->getQuestionBySidAndQid( | ||
$surveyId, | ||
$questionId | ||
), | ||
$preparedData | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.