care.emr.resources.questionnaire.utils module

create_responses_mapping(results_list)

Creates a mapping of question IDs to their responses.

Parameters:

results_list – List of question results

Returns:

Mapping of question IDs to their responses

Return type:

dict

check_required(questionnaire, questionnaire_ref)

Recursively check if the question is marked as required anywhere in its parents

get_valid_choices(question)

Extracts valid choices from a choice question dictionary.

validate_data(values, value_type, questionnaire_ref)

Validate the type of the value based on the question type. :param values: List of values to validate :param value_type: Type of the question (from QuestionType enum)

Returns:

List of validation errors, empty if validation succeeds

Return type:

list

normalize_boolean_value(value)

Convert BOOLEAN_TRUE_STRING and BOOLEAN_FALSE_STRING to proper boolean values.

This helps when comparing condition values in enable_when, where answers may come as strings but actually mean True or False.

If the value doesn’t look like a boolean, return it as-is.

is_question_enabled(question, responses, questionnaire_obj)

Check if a question should be enabled based on its enable_when conditions. Returns True if the question is enabled, False otherwise.

validate_question_result(questionnaire, responses, errors, parent, questionnaire_mapping)
create_observation_spec(questionnaire, response, parent_id=None)
create_components(questionnaire, responses)
convert_to_observation_spec(questionnaire, responses, parent_id=None, is_component=False)
collect_and_validate_enable_when_questions(questions, responses, questionnaire_obj, errors, parent=None)

Filter questions by enable_when rules and record related errors.

Algorithm:

  • If a question’s enable_when evaluates to False and the question (or any of its descendant questions) has answers, an enable_when_failed error is appended to errors.

  • Enabled groups are traversed recursively and pruned so that only their enabled descendants remain.

Returns:

The filtered list of enabled questions (with nested structure preserved for groups that remain enabled).

Return type:

list

Recursively extract a map of link_id to question_id from all questions, including nested ones.

handle_response(questionnaire_obj: Questionnaire, results, user)

Generate observations and questionnaire responses after validation