def _remove_limesurvey_participants(self): """Must be called after updating Limesurvey surveys references""" result = 0, '' indexes = self._get_indexes('experiment', 'questionnaire') ls_interface = Questionnaires() # As there can be same survey in more than one questionnaire component, # create a dictionaire to nao questionnaire compontents by limesurvey # surveys. token_ids_survey = dict() for index in indexes: questionnaire = Questionnaire.objects.get(id=self.data[index]['pk']) limesurvey_id = questionnaire.survey.lime_survey_id # Initialize dict if first time of limesurvey_id if limesurvey_id not in token_ids_survey: token_ids_survey[limesurvey_id] = [] token_ids = list(QuestionnaireResponse.objects.filter( data_configuration_tree__component_configuration__component= questionnaire.id).values_list('token_id', flat=True)) token_ids_survey[limesurvey_id] += token_ids for limesurvey_id, token_ids in token_ids_survey.items(): all_participants = ls_interface.find_tokens_by_questionnaire(limesurvey_id) if all_participants is None: result = self.LIMESURVEY_ERROR, _('Could not clear all extra survey participants data.') continue # TODO (NES-956): don't remove participants of other experiment of this NES. for participant in all_participants: if participant['tid'] not in token_ids: status_delete = ls_interface.delete_participants(limesurvey_id, [participant['tid']]) if status_delete is None: result = self.LIMESURVEY_ERROR, _('Could not clear all extra survey participants data.') continue responses = ls_interface.get_responses_by_token(sid=limesurvey_id, token=participant['token']) if responses is None: result = self.LIMESURVEY_ERROR, _('Could not clear all extra survey participants data.') continue responses = QuestionnaireUtils.responses_to_csv(responses) del(responses[0]) # First line is the header line response_ids = [] for response in responses: response_ids.append(int(response[0])) ls_interface = Questionnaires( settings.LIMESURVEY['URL_API'] + '/index.php/plugins/unsecure?plugin=extendRemoteControl&function=action') status = ls_interface.delete_responses(limesurvey_id, response_ids) if status is None: result = self.LIMESURVEY_ERROR, _('Could not clear all extra survey participants data.') ls_interface = Questionnaires() # Return to access core RPC ls_interface.release_session_key() return result
def test_delete_participant_to_a_survey(self): """ Remove survey participant test testa a insercao de participante em um questionario """ surveys = Questionnaires() list_active_surveys = surveys.find_all_active_questionnaires() self.assertNotEqual(list_active_surveys, None) survey = list_active_surveys[0] sid = int(survey['sid']) # list_participants = self.server.list_participants(self.session_key, sid) # participant_data = {'email': '*****@*****.**', 'lastname': 'junqueira', 'firstname': 'juca'} participant_data_result = surveys.add_participant(sid) # verificar se info retornada eh a mesma # self.assertEqual(participant_data_result[0]['email'], participant_data['email']) # self.assertEqual(participant_data_result[0]['lastname'], participant_data['lastname']) # self.assertEqual(participant_data_result[0]['firstname'], participant_data['firstname']) self.assertNotEqual(participant_data_result, None) # list_participants_new = self.server.list_participants(self.session_key, sid) # self.assertEqual(len(list_participants_new), len(list_participants) + 1) # token_id = participant_data_result[0]['tid'] token_id = participant_data_result['tid'] # tokens_to_delete = [token_id] # remover participante do questionario result = surveys.delete_participants(sid, [token_id]) self.assertEqual(result[str(token_id)], 'Deleted') surveys.release_session_key()