Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Constants {
public const ANSWER_TYPE_MULTIPLE = 'multiple';
public const ANSWER_TYPE_MULTIPLEUNIQUE = 'multiple_unique';
public const ANSWER_TYPE_RANKING = 'ranking';
public const ANSWER_TYPE_RATING = 'rating';
public const ANSWER_TYPE_SHORT = 'short';
public const ANSWER_TYPE_TIME = 'time';

Expand All @@ -131,9 +132,17 @@ class Constants {
self::ANSWER_TYPE_MULTIPLE,
self::ANSWER_TYPE_MULTIPLEUNIQUE,
self::ANSWER_TYPE_RANKING,
self::ANSWER_TYPE_RATING,
self::ANSWER_TYPE_SHORT,
self::ANSWER_TYPE_TIME,
];
Comment thread
Chartman123 marked this conversation as resolved.
Resolved

// AnswerTypes for date/time questions
public const ANSWER_TYPES_DATETIME = [
self::ANSWER_TYPE_DATE,
self::ANSWER_TYPE_DATETIME,
self::ANSWER_TYPE_TIME,
];

// AnswerTypes, that need/have predefined Options
public const ANSWER_TYPES_PREDEFINED = [
Expand All @@ -145,18 +154,17 @@ class Constants {
self::ANSWER_TYPE_RANKING,
];

// AnswerTypes for date/time questions
public const ANSWER_TYPES_DATETIME = [
self::ANSWER_TYPE_DATE,
self::ANSWER_TYPE_DATETIME,
self::ANSWER_TYPE_TIME
// AnswerTypes for scale questions
// TODO: add linearscale
public const ANSWER_TYPES_SCALE = [
self::ANSWER_TYPE_RANKING,
];

// Formats for AnswerTypes date/datetime/time
public const ANSWER_PHPDATETIME_FORMAT = [
self::ANSWER_TYPE_DATE => 'Y-m-d',
self::ANSWER_TYPE_DATETIME => 'Y-m-d H:i',
self::ANSWER_TYPE_TIME => 'H:i'
self::ANSWER_TYPE_TIME => 'H:i',
];

/**
Expand All @@ -168,7 +176,7 @@ class Constants {
'phone',
'email',
'regex',
'number'
'number',
];

// This are allowed extra settings
Expand Down Expand Up @@ -229,6 +237,11 @@ class Constants {
'rows' => ['array'],
];

public const EXTRA_SETTINGS_RATING = [
'optionsHighest' => ['integer', 'NULL'],
'ratingIcon' => ['string', 'NULL'],
];

public const EXTRA_SETTINGS_RANKING = [
'shuffleOptions' => ['boolean'],
];
Expand Down
7 changes: 5 additions & 2 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
Constants::ANSWER_TYPE_DATE => Constants::EXTRA_SETTINGS_DATE,
Constants::ANSWER_TYPE_GRID => Constants::EXTRA_SETTINGS_GRID,
Constants::ANSWER_TYPE_RANKING => Constants::EXTRA_SETTINGS_RANKING,
Constants::ANSWER_TYPE_RATING => Constants::EXTRA_SETTINGS_RATING,
Constants::ANSWER_TYPE_TIME => Constants::EXTRA_SETTINGS_TIME,
Constants::ANSWER_TYPE_LINEARSCALE => Constants::EXTRA_SETTINGS_LINEARSCALE,
default => [],
Expand Down Expand Up @@ -1045,8 +1046,10 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
}

// Special handling of linear scale validation
} elseif ($questionType === Constants::ANSWER_TYPE_LINEARSCALE) {
// Ensure limits are sane
} elseif ($questionType === Constants::ANSWER_TYPE_LINEARSCALE
|| $questionType === Constants::ANSWER_TYPE_RATING) {
// Ensure limits are sane. A rating cannot set optionsLowest at all, so for it
// only the top end is checked.
if (isset($extraSettings['optionsLowest']) && ($extraSettings['optionsLowest'] < 0 || $extraSettings['optionsLowest'] > 1)
|| isset($extraSettings['optionsHighest']) && ($extraSettings['optionsHighest'] < 2 || $extraSettings['optionsHighest'] > 10)) {
return false;
Expand Down
37 changes: 30 additions & 7 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,14 @@ public function validateSubmission(array $questions, array $answers, string $for
* Otherwise, validate the single answer for the date/time question
*/
if (in_array($question['type'], Constants::ANSWER_TYPES_DATETIME)) {
$this->validateDateTime($answers[$questionId], Constants::ANSWER_PHPDATETIME_FORMAT[$question['type']], $question['text'] ?? null, $question['extraSettings'] ?? null);
$this->validateDateTimeQuestion($answers[$questionId], Constants::ANSWER_PHPDATETIME_FORMAT[$question['type']], $question['text'] ?? null, $question['extraSettings'] ?? null);
}

// Check if answers to scale questions are within the limits
if (in_array($question['type'], Constants::ANSWER_TYPES_SCALE)) {
foreach ($answers[$questionId] as $answer) {
$this->validateScaleQuestion($question, $answer);
}
}

// Check if all answers are within the possible options
Expand All @@ -701,12 +708,9 @@ public function validateSubmission(array $questions, array $answers, string $for

foreach ($answers[$questionId] as $answer) {
// Handle linear scale questions
// TODO: move to scale question check
if ($question['type'] === Constants::ANSWER_TYPE_LINEARSCALE) {
$optionsLowest = $question['extraSettings']['optionsLowest'] ?? 1;
$optionsHighest = $question['extraSettings']['optionsHighest'] ?? 5;
if (!is_scalar($answer) || is_bool($answer) || !ctype_digit((string)$answer) || intval($answer) < $optionsLowest || intval($answer) > $optionsHighest) {
throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be an integer between %d and %d.', $question['text'], $optionsLowest, $optionsHighest));
}
$this->validateScaleQuestion($question, $answer);
}
// Check if all grid rows, columns and values match the configured grid subtype
elseif ($question['type'] === Constants::ANSWER_TYPE_GRID) {
Expand Down Expand Up @@ -794,14 +798,33 @@ public function validateSubmission(array $questions, array $answers, string $for
}
}

/**
* Check one answer to a question answered on a numbered scale.
*
* Shared by the linear scale and the rating, which differ only in how the scale is
* drawn. The bounds and their defaults are the linear scale's; a rating does not
* accept optionsLowest, so its lowest end always falls back to 1.
*
* @param array $question the question being answered
* @param mixed $answer one submitted value
* @throws \InvalidArgumentException if the answer is not a whole number within range
*/
private function validateScaleQuestion(array $question, mixed $answer): void {
$optionsLowest = $question['extraSettings']['optionsLowest'] ?? 1;
$optionsHighest = $question['extraSettings']['optionsHighest'] ?? 5;
if (!ctype_digit((string)$answer) || intval($answer) < $optionsLowest || intval($answer) > $optionsHighest) {
throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be an integer between %d and %d.', $question['text'], $optionsLowest, $optionsHighest));
}
}

/**
* Validate correct date/time formats
* @param array $answers Array with date from answer
* @param string $format String with the format to validate
* @param string|null $text String with the title of the question
* @param array|null $extraSettings Array with extra settings for validation
*/
private function validateDateTime(array $answers, string $format, ?string $text = null, ?array $extraSettings = null): void {
private function validateDateTimeQuestion(array $answers, string $format, ?string $text = null, ?array $extraSettings = null): void {
$previousDate = null;

foreach ($answers as $dateStr) {
Expand Down
Loading
Loading