def select_interaction(self): """ select interaction that selects an item/challenge Identifies the variables that don't have a value i.e. a user hasn't provided a value or default doesn't exist It then queries the server to get the questions with the missing variables so that the user can provide the correct answer """ if self.CHALLENGE_PARAM_KEY in self.request.GET: challenge_id = self.request.GET[self.CHALLENGE_PARAM_KEY] (success, challenge_id) = self.__parse_int(challenge_id) if success: # save the old_challenge_id so that we can save it in interaction log old_challenge_id = self.user.selected_challenge_id self.response = SlapResponse(self.user) # a. save the selected transaction in selectedChallenge self.user.select_challenge(challenge_id) # b. query the database to get the missing variables in the challenge (item) # Note: we are sending business facet to '*' because we need to select the challenge without the facet items = SolrQuery(query_type = SolrQuery.CHALLENGE_QUERY, query_params = {'rows' : 1, 'id' : str(challenge_id), 'businessmodel' : '*'}).query() answered_variables = self.user.get_answered_variables() ItemTransformer(items = items).transform(answered_variables = answered_variables) self.response.set_items(items) # c & d. query the database for question with the missing variables # query the database for question with default variables # since we are selecting only one item we can select the variables with missing and default value for it variables_lists = [items[0]['missingVariables'], items[0]['defaultsOnly']] questions = [] for missing_vars in variables_lists: query_params = {} if len(missing_vars) > 0: for var in missing_vars: var = '&' + var query_params['variables'] = Filter(missing_vars) questions.extend(SolrQuery(query_params = query_params).query()) # e. Return response Transformer.convert_answers_to_proper_format(questions) self.response.set_questions(questions) # Log the current interaction self._push_interaction(self.Interactions.SELECT, {self.CHALLENGE_PARAM_KEY : old_challenge_id}) else: self.has_error = True self.response = Error('Invalid challenge was selected')
def default_interaction(self): """ default interaction """ if self.user.selected_challenge_id != None: #Challenge is selected so we need to switch to challenge interaction self.select_interaction() else: #check if facets were previously submitted #if they have been we select based on the facet #else get a single query response self.response = SlapResponse(self.user) questions = SolrQuery(query_params = {'rows' : 1}).query() Transformer.convert_answers_to_proper_format(questions) self.response.set_questions(questions) items = SolrQuery(query_type = SolrQuery.CHALLENGE_QUERY, query_params = {'rows' : 1}).query() Transformer.convert_items_to_proper_format(items) self.response.set_items(items)
def submit_interaction(self): """ Submit interaction stores the given submission provided by the user. Submission can be answer to a particular variable or selection of a facet. """ getRequest = self.request.GET if self.QUESTION_PARAM_KEY in getRequest: question_id = getRequest[self.QUESTION_PARAM_KEY] variables = {} facets = {} has_submission = False # Old submission will be stored incase we need to undo old_submission = Question.get_submissions_for(self.user.visitor_id, question_id) old_submission = old_submission[0].to_json() if len(old_submission) == 1 else None if self.VARIABLE_PARAM_KEY in getRequest: variables = self.__get_name_value_from_string(getRequest[self.VARIABLE_PARAM_KEY]) has_submission = True # a user could have submitted either a variable's value or facets if self.FACETS_PARAM_KEY in getRequest: facets = self.__get_name_value_from_string(self.request.GET[self.FACETS_PARAM_KEY]) has_submission = True # there is no point saving if nothing has been submitted or selected if has_submission: self.user.save_submission(question_id, variables, facets) self._push_interaction(self.Interactions.SUBMIT, {self.QUESTION_PARAM_KEY: question_id, 'submission': old_submission}) answered_variables = self.user.get_answered_variables() selected_facets = self.user.get_selected_facets() # TODO: Code Refactor, too many duplicates, move code Question and Item Query block to a common method self.response = SlapResponse(visitor = self.user) item_id = None if self.CHALLENGE_PARAM_KEY in getRequest: challenge_id = getRequest[self.CHALLENGE_PARAM_KEY] (success, challenge_id) = self.__parse_int(challenge_id) if success: item_id = challenge_id if item_id is not None: items = SolrQuery(query_type = SolrQuery.CHALLENGE_QUERY, query_params = {'rows' : 1, 'id' : str(item_id), 'businessmodel' : '*'}).query() answered_variables = self.user.get_answered_variables() ItemTransformer(items = items).transform(answered_variables = answered_variables) self.response.set_items(items) # c & d. query the database for question with the missing variables # query the database for question with default variables # since we are selecting only one item we can select the variables with missing and default value for it variables_lists = [items[0]['missingVariables'], items[0]['defaultsOnly']] questions = [] for missing_vars in variables_lists: query_params = {} if len(missing_vars) > 0: for var in missing_vars: var = '&' + var query_params['variables'] = Filter(missing_vars) questions.extend(SolrQuery(query_params = query_params).query()) if len(questions) > 1: questions = questions[:1] # select only one question when item id is provided break Transformer.convert_answers_to_proper_format(questions) self.response.set_questions(questions) else: items = SolrQuery(query_type = SolrQuery.CHALLENGE_QUERY, query_params = selected_facets).query() ItemTransformer(items = items).transform(answered_variables = answered_variables) self.response.set_items(items) questions = SolrQuery().query() # fetch default questions Transformer.convert_answers_to_proper_format(questions) self.response.set_questions(questions)