From eb2d52d74b9e154a5622a2497d0d3982e7258b92 Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Sat, 4 Apr 2020 00:00:04 +0200 Subject: [PATCH] Add mandatory-option on questions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- appinfo/routes.php | 1 + lib/Controller/ApiController.php | 32 +++++++++++++++++++++++++++++++ lib/Controller/PageController.php | 21 +++++++++++--------- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 5adbf4d..fae4406 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -46,6 +46,7 @@ return [ ['name' => 'api#newForm', 'url' => 'api/v1/form', 'verb' => 'POST'], ['name' => 'api#deleteForm', 'url' => 'api/v1/form/{id}', 'verb' => 'DELETE'], + ['name' => 'api#updateQuestion', 'url' => 'api/v1/question/update/', 'verb' => 'POST'], ['name' => 'api#newQuestion', 'url' => 'api/v1/question/', 'verb' => 'POST'], ['name' => 'api#deleteQuestion', 'url' => 'api/v1/question/{id}', 'verb' => 'DELETE'], ['name' => 'api#newOption', 'url' => 'api/v1/option/', 'verb' => 'POST'], diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 20f4721..60b6170 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -529,6 +529,38 @@ class ApiController extends Controller { return new Http\JSONResponse($question->getId()); } + /** + * @NoAdminRequired + * @param int $id QuestionId of question to update + * @param array $keyvalues Array of key=>value pairs to update. + */ + public function updateQuestion(int $id, array $keyvalues): Http\JSONResponse { + $this->logger->debug('Updating question: questionId: {id}, values: {keyvalues}', [ + 'id' => $id, + 'keyvalues' => $keyvalues + ]); + + try { + $question = $this->questionMapper->findById($id); + $form = $this->formMapper->find($question->getFormId()); + } catch (IMapperException $e) { + $this->logger->debug('Could not find question or form'); + return new Http\JSONResponse([], Http::STATUS_BAD_REQUEST); + } + + if ($form->getOwnerId() !== $this->userId) { + $this->logger->debug('This form is not owned by the current user'); + return new Http\JSONResponse([], Http::STATUS_FORBIDDEN); + } + + $question = Question::fromParams($keyvalues); + $question->setId($id); + + $this->questionMapper->update($question); + + return new Http\JSONResponse($question->getId()); + } + /** * @NoAdminRequired */ diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 2994205..77a6b49 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -277,20 +277,23 @@ class PageController extends Controller { //Insert Answers foreach($questions as $question) { - if($question['type'] === "checkbox"){ - foreach(($answers[$question['text']]) as $ansText) { + // If question is answered, the questionText exists as key in $answers. Does not exist, when a (non-mandatory) question was not answered. + if (array_key_exists($question['text'], $answers)) { + if($question['type'] === "checkbox"){ + foreach(($answers[$question['text']]) as $ansText) { + $answer = new Answer(); + $answer->setSubmissionId($submissionId); + $answer->setQuestionId($question['id']); + $answer->setText($ansText); + $this->answerMapper->insert($answer); + } + } else { $answer = new Answer(); $answer->setSubmissionId($submissionId); $answer->setQuestionId($question['id']); - $answer->setText($ansText); + $answer->setText($answers[$question['text']]); $this->answerMapper->insert($answer); } - } else { - $answer = new Answer(); - $answer->setSubmissionId($submissionId); - $answer->setQuestionId($question['id']); - $answer->setText($answers[$question['text']]); - $this->answerMapper->insert($answer); } }