Skip to content

Commit 99b010f

Browse files
committed
update question
Signed-off-by: Christian Hartmann <[email protected]>
1 parent 618c0ee commit 99b010f

File tree

4 files changed

+89
-20
lines changed

4 files changed

+89
-20
lines changed

appinfo/routes.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
// Questions
7070
['name' => 'api#getQuestions', 'url' => '/api/{apiVersion}/forms/{id}/questions', 'verb' => 'GET', 'requirements' => $requirements_v3],
7171
['name' => 'api#newQuestion', 'url' => '/api/{apiVersion}/forms/{id}/questions', 'verb' => 'POST', 'requirements' => $requirements_v3],
72-
# ['name' => 'api#getQuestion', 'url' => '/api/{apiVersion}/questions', 'verb' => 'GET', 'requirements' => $requirements_v3],
72+
# ['name' => 'api#getQuestion', 'url' => '/api/{apiVersion}/forms/{id}/questions/{questionId}', 'verb' => 'GET', 'requirements' => $requirements_v3],
73+
['name' => 'api#updateQuestion', 'url' => '/api/{apiVersion}/forms/{id}/questions/{questionId}', 'verb' => 'PATCH', 'requirements' => $requirements_v3],
7374

7475
// Legacy v2 routes (TODO: remove with Forms v5)
7576
// Forms
@@ -120,21 +121,13 @@
120121
]
121122
],
122123
// TODO: Remove POST in next API release
123-
[
124-
'name' => 'api#updateQuestion',
125-
'url' => '/api/{apiVersion}/question/update',
126-
'verb' => 'POST',
127-
'requirements' => [
128-
'apiVersion' => 'v2(\.[1-4])?'
129-
]
124+
['name' => 'api#updateQuestionLegacy', 'url' => '/api/{apiVersion}/question/update', 'verb' => 'POST', 'requirements' => [
125+
'apiVersion' => 'v2(\.[1-4])?'
126+
]
130127
],
131-
[
132-
'name' => 'api#updateQuestion',
133-
'url' => '/api/{apiVersion}/question/update',
134-
'verb' => 'PATCH',
135-
'requirements' => [
136-
'apiVersion' => 'v2\.[2-4]'
137-
]
128+
['name' => 'api#updateQuestionLegacy', 'url' => '/api/{apiVersion}/question/update', 'verb' => 'PATCH', 'requirements' => [
129+
'apiVersion' => 'v2\.[2-4]'
130+
]
138131
],
139132
// TODO: Remove POST in next API release
140133
[

lib/Controller/ApiController.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,78 @@ public function newQuestion(int $id, ?string $type = null, string $text = '', ?i
494494
return new DataResponse($response);
495495
}
496496

497+
/**
498+
* @CORS
499+
* @NoAdminRequired
500+
*
501+
* Writes the given key-value pairs into Database.
502+
* Key 'order' should only be changed by reorderQuestions() and is not allowed here.
503+
*
504+
* @param int $id the form id
505+
* @param int $questionId id of question to update
506+
* @param array $keyValuePairs Array of key=>value pairs to update.
507+
* @return DataResponse
508+
* @throws OCSBadRequestException
509+
* @throws OCSForbiddenException
510+
*/
511+
public function updateQuestion(int $id, int $questionId, array $keyValuePairs): DataResponse {
512+
$this->logger->debug('Updating question: formId: {id}, questionId: {id}, values: {keyValuePairs}', [
513+
'id' => $id,
514+
'questionId' => $questionId,
515+
'keyValuePairs' => $keyValuePairs
516+
]);
517+
518+
try {
519+
$question = $this->questionMapper->findById($questionId);
520+
} catch (IMapperException $e) {
521+
$this->logger->debug('Could not find question');
522+
throw new OCSBadRequestException('Could not find question');
523+
}
524+
525+
if ($question->getFormId() !== $id) {
526+
throw new OCSBadRequestException('Question doesn\'t belong to given Form');
527+
}
528+
529+
$form = $this->getFormIfAllowed($id);
530+
if ($this->formsService->isFormArchived($form)) {
531+
$this->logger->debug('This form is archived and can not be modified');
532+
throw new OCSForbiddenException();
533+
}
534+
535+
// Don't allow empty array
536+
if (sizeof($keyValuePairs) === 0) {
537+
$this->logger->info('Empty keyValuePairs, will not update.');
538+
throw new OCSForbiddenException();
539+
}
540+
541+
//Don't allow to change id or formId
542+
if (key_exists('id', $keyValuePairs) || key_exists('formId', $keyValuePairs)) {
543+
$this->logger->debug('Not allowed to update id or formId');
544+
throw new OCSForbiddenException();
545+
}
546+
547+
// Don't allow to reorder here
548+
if (key_exists('order', $keyValuePairs)) {
549+
$this->logger->debug('Key \'order\' is not allowed on updateQuestion. Please use reorderQuestions() to change order.');
550+
throw new OCSForbiddenException('Please use reorderQuestions() to change order');
551+
}
552+
553+
if (key_exists('extraSettings', $keyValuePairs) && !$this->formsService->areExtraSettingsValid($keyValuePairs['extraSettings'], $question->getType())) {
554+
throw new OCSBadRequestException('Invalid extraSettings, will not update.');
555+
}
556+
557+
// Create QuestionEntity with given Params & Id.
558+
$question = Question::fromParams($keyValuePairs);
559+
$question->setId($questionId);
560+
561+
// Update changed Columns in Db.
562+
$this->questionMapper->update($question);
563+
564+
$this->formsService->setLastUpdatedTimestamp($id);
565+
566+
return new DataResponse($question->getId());
567+
}
568+
497569
/*
498570
/* Legacy API v2 methods (TODO: remove with Forms v5)
499571
*/
@@ -959,7 +1031,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
9591031
* @throws OCSBadRequestException
9601032
* @throws OCSForbiddenException
9611033
*/
962-
public function updateQuestion(int $id, array $keyValuePairs): DataResponse {
1034+
public function updateQuestionLegacy(int $id, array $keyValuePairs): DataResponse {
9631035
$this->logger->debug('Updating question: questionId: {id}, values: {keyValuePairs}', [
9641036
'id' => $id,
9651037
'keyValuePairs' => $keyValuePairs

src/mixins/QuestionMixin.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,14 @@ export default {
363363
try {
364364
// TODO: add loading status feedback ?
365365
await axios.patch(
366-
generateOcsUrl('apps/forms/api/v2.4/question/update'),
366+
generateOcsUrl(
367+
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
368+
{
369+
id: this.formId,
370+
questionId: this.id,
371+
},
372+
),
367373
{
368-
id: this.id,
369374
keyValuePairs: {
370375
[key]: value,
371376
},

tests/Integration/Api/ApiV3Test.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,9 +787,8 @@ public function dataUpdateQuestionProperties() {
787787
* @param array $fullFormExpected
788788
*/
789789
public function testUpdateQuestionProperties(array $fullFormExpected): void {
790-
$resp = $this->http->request('PATCH', 'api/v2.4/question/update', [
790+
$resp = $this->http->request('PATCH', "api/v3/forms/{$this->testForms[0]['id']}/questions/{$this->testForms[0]['questions'][0]['id']}", [
791791
'json' => [
792-
'id' => $this->testForms[0]['questions'][0]['id'],
793792
'keyValuePairs' => [
794793
'isRequired' => false,
795794
'text' => 'Still first Question!'

0 commit comments

Comments
 (0)