Evolving Survey Logic: Handling Multi-Choice Responses and Dynamic Rules in PHP
Enhancing the Reimpact REP Survey for Richer Insights
The Reimpact platform recently saw an important update to its REP survey. To better understand user preferences and collect more comprehensive data, a key survey question was transformed from a single-choice selection into a multi-choice format, enabling users to select multiple relevant options. This change required a thoughtful approach to both frontend presentation and backend logic, particularly in how survey outcomes are determined.
Adapting to Multi-Choice Input
The core of this update involved converting a product type question (Q1) from a simple radio button selection to a set of checkboxes. This allows users to indicate all applicable product types, such as "textiles" which was also added as a new option. While straightforward on the UI, the backend processing needed to shift from expecting a single scalar value to an array of selected options.
Updating Question Labels and Options
Beyond the multi-choice conversion, all four survey questions received revised labels and options for clarity and improved user experience. For instance, volume options were simplified to a container-based scale, making it easier for users to provide accurate input without ambiguity.
Implementing Dynamic Outcome Rules for Array Data
Perhaps the most significant technical challenge was re-architecting the outcome rules. With product_type now returning an array of choices, the existing single-value comparison logic was no longer sufficient. We needed to introduce operators like contains and not_contains to evaluate if specific values exist within the submitted array.
Consider a simplified example of how such rules might be implemented in PHP:
<?php
class SurveyProcessor
{
public function processResponse(array $responses): string
{
$productTypes = $responses['product_type'] ?? [];
$volumeScale = $responses['volume_scale'] ?? null;
// Example: Rule for specific product types
if (in_array('textiles', $productTypes) && in_array('plastic', $productTypes)) {
return 'complex_recycling_guidance';
}
// Example: Rule based on presence of 'textiles' and volume
if (in_array('textiles', $productTypes) && $volumeScale === 'large_container') {
return 'textile_partner_referral';
}
// Example: Rule based on absence of a specific type
if (! in_array('hazardous_waste', $productTypes)) {
return 'general_recycling_info';
}
return 'default_guidance';
}
}
// Example usage:
$userResponses = [
'product_type' => ['textiles', 'paper'],
'volume_scale' => 'small_container',
// ... other questions
];
$processor = new SurveyProcessor();
echo $processor->processResponse($userResponses);
?>
This approach leverages PHP's in_array() function to efficiently check for the presence or absence of specific values within the submitted array of choices, enabling flexible and powerful conditional logic for survey outcomes.
Results and Future Enhancements
This update provides the Reimpact platform with a much more flexible and insightful REP survey. By capturing multi-choice preferences, the system can now offer more tailored recommendations and gather richer data. The refined outcome rules ensure that users receive accurate guidance based on their specific, multifaceted needs.
The next steps could involve adding more complex rule chaining or implementing a configurable rules engine to allow non-developers to define and modify survey outcomes dynamically.