Merge pull request #265 from jotoeri/enh/mandatory

Enh/mandatory-questions
This commit is contained in:
Jonas 2020-04-07 14:56:52 +02:00 committed by GitHub
commit ad3ae139d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 9 deletions

View file

@ -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'],

View file

@ -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
*/

View file

@ -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);
}
}