Example #1
0
def get_state_answers(exploration_id, exploration_version, state_name):
    """Returns a StateAnswers object containing all answers associated with the
    specified exploration state, or None if no such answers have yet been
    submitted.

    Args:
        exploration_id: str. The exploration ID.
        exploration_version: int. The version of the exploration to fetch
            answers for.
        state_name: str. The name of the state to fetch answers for.

    Returns:
        StateAnswers or None. A StateAnswers object containing all answers
        associated with the state, or None if no such answers exist.
    """
    state_answers_models = stats_models.StateAnswersModel.get_all_models(
        exploration_id, exploration_version, state_name)
    if state_answers_models:
        main_state_answers_model = state_answers_models[0]
        submitted_answer_dict_list = itertools.chain.from_iterable([
            state_answers_model.submitted_answer_list
            for state_answers_model in state_answers_models
        ])
        return stats_domain.StateAnswers(
            exploration_id,
            exploration_version,
            state_name,
            main_state_answers_model.interaction_id, [
                stats_domain.SubmittedAnswer.from_dict(submitted_answer_dict)
                for submitted_answer_dict in submitted_answer_dict_list
            ],
            schema_version=main_state_answers_model.schema_version)
    else:
        return None
Example #2
0
    def setUp(self):
        super(StateAnswersValidationTests, self).setUp()
        self.state_answers = stats_domain.StateAnswers('exp_id', 1,
                                                       'initial_state',
                                                       'TextInput', [])

        # The canonical object should have no validation problems.
        self.state_answers.validate()
Example #3
0
def record_answers(exploration_id, exploration_version, state_name,
                   interaction_id, submitted_answer_list):
    """Optimally record a group of answers using an already loaded exploration..
    The submitted_answer_list is a list of SubmittedAnswer domain objects.
    """
    state_answers = stats_domain.StateAnswers(exploration_id,
                                              exploration_version, state_name,
                                              interaction_id,
                                              submitted_answer_list)
    for submitted_answer in submitted_answer_list:
        submitted_answer.validate()

    stats_models.StateAnswersModel.insert_submitted_answers(
        state_answers.exploration_id, state_answers.exploration_version,
        state_answers.state_name, state_answers.interaction_id,
        state_answers.get_submitted_answer_dict_list())
Example #4
0
def record_answers(exploration_id, exploration_version, state_name,
                   interaction_id, submitted_answer_list):
    """Optimally record a group of answers using an already loaded exploration..
    The submitted_answer_list is a list of SubmittedAnswer domain objects.

    Args:
        exploration_id: str. The exploration ID.
        exploration_version: int. The version of the exploration.
        state_name: str. The name of the state.
        interaction_id: str. The ID of the interaction.
        submitted_answer_list: list(SubmittedAnswer). The list of answers to be
            recorded.
    """
    state_answers = stats_domain.StateAnswers(exploration_id,
                                              exploration_version, state_name,
                                              interaction_id,
                                              submitted_answer_list)
    for submitted_answer in submitted_answer_list:
        submitted_answer.validate()

    stats_models.StateAnswersModel.insert_submitted_answers(
        state_answers.exploration_id, state_answers.exploration_version,
        state_answers.state_name, state_answers.interaction_id,
        state_answers.get_submitted_answer_dict_list())
Example #5
0
 def test_can_retrieve_properly_constructed_submitted_answer_dict_list(
         self):
     state_answers = stats_domain.StateAnswers(
         'exp_id', 1, 'initial_state', 'TextInput', [
             stats_domain.SubmittedAnswer(
                 'Text',
                 'TextInput',
                 0,
                 1,
                 exp_domain.EXPLICIT_CLASSIFICATION, {},
                 'sess',
                 10.5,
                 rule_spec_str='rule spec str1',
                 answer_str='answer str1'),
             stats_domain.SubmittedAnswer(
                 'Other text',
                 'TextInput',
                 1,
                 0,
                 exp_domain.DEFAULT_OUTCOME_CLASSIFICATION, {},
                 'sess',
                 7.5,
                 rule_spec_str='rule spec str2',
                 answer_str='answer str2')
         ])
     submitted_answer_dict_list = (
         state_answers.get_submitted_answer_dict_list())
     self.assertEqual(submitted_answer_dict_list,
                      [{
                          'answer': 'Text',
                          'interaction_id': 'TextInput',
                          'answer_group_index': 0,
                          'rule_spec_index': 1,
                          'classification_categorization':
                          exp_domain.EXPLICIT_CLASSIFICATION,
                          'params': {},
                          'session_id': 'sess',
                          'time_spent_in_sec': 10.5,
                          'rule_spec_str': 'rule spec str1',
                          'answer_str': 'answer str1'
                      }, {
                          'answer':
                          'Other text',
                          'interaction_id':
                          'TextInput',
                          'answer_group_index':
                          1,
                          'rule_spec_index':
                          0,
                          'classification_categorization':
                          (exp_domain.DEFAULT_OUTCOME_CLASSIFICATION),
                          'params': {},
                          'session_id':
                          'sess',
                          'time_spent_in_sec':
                          7.5,
                          'rule_spec_str':
                          'rule spec str2',
                          'answer_str':
                          'answer str2'
                      }])