Esempio n. 1
0
 def process_yesno():
     if side_effects:
         question.set_widget(get_widget(type='radioGroup'))
         question.add_enumeration(
             structures.DescriptorObject(
                 id="yes",
                 text=localized_string_object(self.localization, "Yes"),
             ))
         question.add_enumeration(
             structures.DescriptorObject(
                 id="no",
                 text=localized_string_object(self.localization, "No"),
             ))
     type_object = structures.TypeObject(base='enumeration', )
     type_object.add_enumeration('yes', description='Yes')
     type_object.add_enumeration('no', description='No')
     return type_object
Esempio n. 2
0
    def question_and_field_processor(self, page, row, question, field):
        """
        Processes questions and field types.

        Question object are fully configured, and the field type is generated
        for the instrument definition fields corresponding to the question.
        """

        # Use question object in question['options']
        question_obj = question['options']

        # Process choices before questions, so choices are available to
        # question processing
        if row['enumeration_type'] in (
                'enumeration',
                'enumerationSet',
        ):
            # data_type might be a JSON string of a dict which contains
            # 'Choices' or 'choices', an array of single key dicts.
            try:
                data_type = json.loads(row['data_type'])
                choices = (data_type.get('Choices', False)
                           or data_type.get('choices', False) or None)
            except:
                error = RedcapFormatError(
                    "Unable to parse \"data_type\" field:",
                    "Expected valid JSON formatted text")
                raise error

            # Sort the array on key order not array order
            if choices:
                field['type'] = structures.TypeObject(
                    base=row['enumeration_type'], )

                choices = [{
                    self.reader.get_name(k): v
                } for c in choices for k, v in c.items()]

                choices.sort()

                for choice in choices:
                    field['type'].add_enumeration(choice.keys()[0])
                    name, description = choice.items()[0]
                    question_obj.add_enumeration(
                        structures.DescriptorObject(
                            id=self.reader.get_name(name),
                            text=localized_string_object(
                                self.localization, description),
                        ))

                question_obj.set_widget(
                    structures.WidgetConfigurationObject(
                        type='checkGroup' if row['enumeration_type'] ==
                        'enumerationSet' else 'radioGroup'))

        else:
            field['type'] = row['data_type']
Esempio n. 3
0
 def process_truefalse():
     if side_effects:
         question.set_widget(get_widget(type='radioGroup'))
         question.add_enumeration(
             structures.DescriptorObject(
                 id="true",
                 text=localized_string_object(self.localization,
                                              "True"),
             ))
         question.add_enumeration(
             structures.DescriptorObject(
                 id="false",
                 text=localized_string_object(self.localization,
                                              "False"),
             ))
     type_object = structures.TypeObject(base='enumeration', )
     type_object.add_enumeration('true', description='True')
     type_object.add_enumeration('false', description='False')
     return type_object
Esempio n. 4
0
    def get_choices_form(self, row):
        """
        Returns array of DescriptorObject

        Expecting: choices_or_calculations to be pipe separated list
        of (comma delimited) tuples: internal, external
        """
        return [
            structures.DescriptorObject(
                id=self.reader.get_name(x.strip().split(',')[0]),
                text=localized_string_object(
                    self.localization,
                    ','.join(x.strip().split(',')[1:]).strip()),
            ) for x in row['choices_or_calculations'].split('|')
        ]
Esempio n. 5
0
    def question_field_processor(self, question_data, question):
        """ Processe questions and fields """
        question_type = question_data['QuestionType']
        question_text = localized_string_object(
            self.localization,
            self.clean_question(question_data['QuestionText'])
        )
        if question_type == 'DB':
            # Question is only display text
            question['type'] = 'text'
            question['options'] = {'text': question_text}
        else:
            # Question is an interactive form element
            question['type'] = 'question'
            question['options'] = structures.QuestionObject(
                fieldId=question_data['DataExportTag'].lower(),
                text=localized_string_object(
                    self.localization,
                    self.clean_question(
                        question_data['QuestionText']
                    )
                ),
            )

            # Choices are generated, where "choices" is an array of
            # tuples: (id, choice)
            self._choices = question_data.get('Choices', [])
            order = question_data.get('ChoiceOrder', [])
            if self._choices:
                if isinstance(self._choices, dict):
                    if not order:
                        keys = self._choices.keys()
                        if all([k.isdigit() for k in keys]):
                            keys = [int(k) for k in keys]
                        order = sorted(keys)
                    self._choices = [(x, self._choices[str(x)]) for x in order]
                elif isinstance(self._choices, list):
                    self._choices = [i for i in enumerate(self._choices)]
                else:
                    error = ConversionValueError(
                        "Choices are not formatted correctly. Got choices:",
                        str(self._choices)
                    )
                    error.wrap("With question data:", str(question))
                    raise error
                self._choices = [
                    (str(i).lower(), c['Display'])
                    for i, c in self._choices
                ]
                # Process question object and field type object
                question_obj = question['options']
                field_type = structures.TypeObject(base='enumeration', )
                for _id, choice in self._choices:
                    question_obj.add_enumeration(
                        structures.DescriptorObject(
                            id=_id,
                            text=localized_string_object(
                                self.localization,
                                choice
                            ),
                        )
                    )
                    field_type.add_enumeration(str(_id))
            else:
                field_type = 'text'

            # Consruct field for instrument definition
            field = structures.FieldObject(
                id=question_data['DataExportTag'].lower(),
                description=question_data['QuestionDescription'],
                type=field_type,
                required=False,
                identifiable=False,
            )
            self._field_storage.append(field)
Esempio n. 6
0
    def question_and_field_processor(self, page, row, question):
        """
        Processes questions.

        Return True if a question should be added as a new element to the page,
        and if a form field should be added to the instrument definition. New
        matrix questions and regular questions are added to both definitions.
        However, existing matrix questions are not added.

        A pointer reference is store to allow for:
            1) modifying existing matrix questions
            2) storing the instrument field by the parent function scope
        """

        add_question_and_store_field = True

        # Use question object in question['options']
        question_obj = question['options']

        # If row involves branching logic, add to question
        if row['branching_logic']:
            question_obj.add_event(
                structures.EventObject(
                    trigger=self.convert_trigger(row['branching_logic']),
                    action='disable',
                ))

        # Check for a matrix question
        matrix_group_name = self.reader.get_name(
            row.get('matrix_group_name', ''))

        if matrix_group_name:
            # Question is a matrix question
            #
            # assessment[m][r][c]
            # m = matrix_group_name
            # r = variable_field_name
            # c = field_type

            # Ranme for code clarity
            matrix = question_obj

            if self._current_matrix_group_name != matrix_group_name:
                # New matrix question

                self._current_matrix_group_name = matrix_group_name

                # Start a new matrix question field for the form
                matrix['fieldId'] = matrix_group_name

                # Create a new matrix question field for the instrument
                field = structures.FieldObject()
                field['id'] = self.reader.get_name(row['matrix_group_name'])
                field['description'] = row.get('section_header', '')
                field['type'] = structures.TypeObject(base='matrix', )

                field_type = field['type']

                # Process matrix
                # Append the only column(to instrument).
                # Use the field_type (checkbox or radiobutton) as the id.
                field_type.add_column(
                    structures.ColumnObject(
                        id=self.reader.get_name(row['field_type']),
                        description=row['field_type'],
                        type=self.get_type(matrix, row, side_effects=False),
                        required=bool(row['required_field']),
                        identifiable=bool(row['identifier']),
                    ))
                # add the column to the form
                matrix.add_question(
                    structures.QuestionObject(
                        fieldId=self.reader.get_name(row['field_type']),
                        text=localized_string_object(self.localization,
                                                     row['field_label']),
                        enumerations=self.get_choices_form(row),
                    ))
                # Append the first row (to instrument).
                field_type.add_row(
                    structures.RowObject(
                        id=self.reader.get_name(row['variable_field_name']),
                        description=row['field_label'],
                        required=bool(row['required_field']),
                    ))
                # add the row to the form.
                matrix.add_row(
                    structures.DescriptorObject(
                        id=self.reader.get_name(row['variable_field_name']),
                        text=localized_string_object(self.localization,
                                                     row['field_label']),
                    ))

                # Assign pointers to matrix construction objects, so
                # matrix questions have modifiable instrument and form
                # definitions if next question is  an existing matrix
                # question
                self._matrix = matrix
                self._field = field
                self._field_type = field_type

            else:
                # Current matrix question
                # Modify existing matrix question for instrument definition
                self._field_type.add_row(
                    structures.RowObject(
                        id=self.reader.get_name(row['variable_field_name']),
                        description=row['field_label'],
                        required=bool(row['required_field']),
                    ))

                # Field already exists, so prevent adding it to form
                self._field = structures.FieldObject()

                # Modify existing matrix question for form definition
                self._matrix.add_row(
                    structures.DescriptorObject(
                        id=self.reader.get_name(row['variable_field_name']),
                        text=localized_string_object(self.localization,
                                                     row['field_label']),
                    ))
                self._field = None
                add_question_and_store_field = False
        else:
            # A non-matrix, regular question
            self._current_matrix_group_name = None
            self._matrix = None
            self._field_type = None

            # Make instrument field
            field = structures.FieldObject()
            field_type = self.get_type(question_obj, row)
            if field_type:
                field_name = self.reader.get_name(row['variable_field_name'])
                if row['section_header']:
                    field_name = '%s_%s' % (
                        field_name,
                        self.reader.get_name(row['section_header']),
                    )
                field['id'] = field_name
                field['description'] = row['field_label']
                field['type'] = field_type
                field['required'] = bool(row['required_field'])
                field['identifiable'] = bool(row['identifier'])

            self._field = field

        return add_question_and_store_field