コード例 #1
0
def main(argv):
    # TODO: test for new_limesurvey_id existence
    questionnaire_id, new_limesurvey_id = parse_options(argv)
    questionnaire = Questionnaire.objects.get(id=questionnaire_id)
    survey = questionnaire.survey
    old_limesurvey_id = survey.lime_survey_id
    survey.lime_survey_id = new_limesurvey_id
    # TODO:
    # if old_limesurvey_id == new_limesurvey_id do nothing, exit with message
    survey.save()

    limesurvey_surveys = Questionnaires()
    print('Getting old tokens ...')
    old_tokens = \
        limesurvey_surveys.find_tokens_by_questionnaire(old_limesurvey_id)
    print('Getting new tokens ...')
    new_tokens = \
        limesurvey_surveys.find_tokens_by_questionnaire(survey.lime_survey_id)

    print('Updating experiment questionnaire responses ...')
    for q_experiment_response in EQuestionnaireResponse.objects.filter(
            data_configuration_tree__component_configuration__component=
            questionnaire.id):
        update_questionnaire_response(old_tokens, new_tokens,
                                      q_experiment_response)

    print('Updating patients questionnaire responses ...')
    for q_patient_response in PQuestionnaireResponse.objects.filter(
            survey=survey):
        update_questionnaire_response(old_tokens, new_tokens,
                                      q_patient_response)
コード例 #2
0
def survey_list(request, template_name='survey/survey_list.html'):

    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    language_code = request.LANGUAGE_CODE

    for survey in Survey.objects.all():
        language = get_questionnaire_language(surveys, survey.lime_survey_id,
                                              language_code)
        questionnaires_list.append({
            'id':
            survey.id,
            'lime_survey_id':
            survey.lime_survey_id,
            'title':
            surveys.get_survey_title(survey.lime_survey_id, language),
            'is_initial_evaluation':
            survey.is_initial_evaluation
        })

    surveys.release_session_key()

    questionnaires_list = sorted(questionnaires_list, key=itemgetter('title'))

    context = {
        'questionnaires_list': questionnaires_list,
        'limesurvey_available': limesurvey_available
    }

    return render(request, template_name, context)
コード例 #3
0
def update_questionnaire_list(questionnaire_list, heading_type, current_language="pt-BR"):

    questionnaire_list_updated = []

    if heading_type == 'code':
        return questionnaire_list

    questionnaire_lime_survey = Questionnaires()

    for questionnaire in questionnaire_list:

        # position 0: id, postion 1: title

        questionnaire_id = questionnaire[0]

        # position 2: output_list (field, header)
        fields, headers = zip(*questionnaire[2])

        questionnaire_field_header = get_questionnaire_header(questionnaire_lime_survey, questionnaire_id,
                                                              fields, heading_type, current_language)

        questionnaire_list_updated.append([questionnaire_id, questionnaire[1], questionnaire_field_header])

    questionnaire_lime_survey.release_session_key()

    return questionnaire_list_updated
コード例 #4
0
ファイル: test_views.py プロジェクト: neuromat/nes
    def test_surveys_list_get_updated(self):
        lime_survey = Questionnaires()
        sid = None

        # Create a new survey at LimeSurvey with pt title
        try:
            pt_title_survey = 'Questionário Teste'
            sid = lime_survey.add_survey(9999, pt_title_survey, 'pt-BR', 'G')

            # Update the infos when viewed
            survey = Survey.objects.create(lime_survey_id=sid)

            # Check that pt_title is null
            self.assertIsNone(Survey.objects.last().pt_title)

            self.client.get(reverse('survey_list'))

            # Check that pt_title is updated
            self.assertEqual(Survey.objects.last().pt_title, pt_title_survey)

            # Simulate a discrepancy between the survey informations at limesurvey and NES
            survey.pt_title = "Título discrepante"
            survey.save()

            self.assertNotEqual(Survey.objects.last().pt_title, pt_title_survey)

            # Simulate clicking to update the list with new limesurvey information
            self.client.post(reverse('survey_list'), {'action': 'update'}, follow=True)

            # Check if the pt_title was updated properly
            self.assertEqual(Survey.objects.last().pt_title, pt_title_survey)

        finally:
            # Deletes the survey created at Lime Survey
            self.assertEqual(lime_survey.delete_survey(sid), 'OK')
コード例 #5
0
def survey_update(request, survey_id, template_name="survey/survey_register.html"):
    survey = get_object_or_404(Survey, pk=survey_id)

    surveys = Questionnaires()
    language = get_questionnaire_language(surveys, survey.lime_survey_id, request.LANGUAGE_CODE)
    survey_title = surveys.get_survey_title(survey.lime_survey_id, language)
    limesurvey_available = check_limesurvey_access(request, surveys)

    surveys.release_session_key()

    survey_form = SurveyForm(request.POST or None, instance=survey,
                             initial={'title': str(survey.lime_survey_id) + ' - ' + survey_title})

    if request.method == "POST":
        if request.POST['action'] == "save":
            if survey_form.is_valid():
                if survey_form.has_changed():
                    survey_form.save()
                    messages.success(request, _('Questionnaire updated successfully.'))
                else:
                    messages.success(request, _('There are no changes to save.'))

                redirect_url = reverse("survey_view", args=(survey.id,))
                return HttpResponseRedirect(redirect_url)

    context = {
        "limesurvey_available": limesurvey_available,
        "survey": survey,
        "survey_form": survey_form,
        "survey_title": survey_title,
        "editing": True,
        "creating": False}

    return render(request, template_name, context)
コード例 #6
0
ファイル: views.py プロジェクト: INCF/nes
def survey_update(request, survey_id, template_name="survey/survey_register.html"):
    survey = get_object_or_404(Survey, pk=survey_id)

    surveys = Questionnaires()
    survey_title = surveys.get_survey_title(survey.lime_survey_id)
    limesurvey_available = check_limesurvey_access(request, surveys)

    surveys.release_session_key()

    survey_form = SurveyForm(request.POST or None, instance=survey,
                             initial={'title': str(survey.lime_survey_id) + ' - ' + survey_title})

    if request.method == "POST":
        if request.POST['action'] == "save":
            if survey_form.is_valid():
                if survey_form.has_changed():
                    survey_form.save()
                    messages.success(request, _('Questionnaire updated successfully.'))
                else:
                    messages.success(request, _('There are no changes to save.'))

                redirect_url = reverse("survey_view", args=(survey.id,))
                return HttpResponseRedirect(redirect_url)

    context = {
        "limesurvey_available": limesurvey_available,
        "survey": survey,
        "survey_form": survey_form,
        "survey_title": survey_title,
        "editing": True,
        "creating": False}

    return render(request, template_name, context)
コード例 #7
0
ファイル: views.py プロジェクト: INCF/nes
def survey_list(request, template_name='survey/survey_list.html'):

    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    for survey in Survey.objects.all():
        questionnaires_list.append(
            {
                'id': survey.id,
                'lime_survey_id': survey.lime_survey_id,
                'title': surveys.get_survey_title(survey.lime_survey_id),
                'is_initial_evaluation': survey.is_initial_evaluation
            }
        )

    surveys.release_session_key()

    questionnaires_list = sorted(questionnaires_list, key=itemgetter('title'))

    context = {
        'questionnaires_list': questionnaires_list,
        'limesurvey_available': limesurvey_available
    }

    return render(request, template_name, context)
コード例 #8
0
ファイル: test_views.py プロジェクト: neuromat/nes
    def test_add_and_delete_survey_methods(self):
        questionnaires = Questionnaires()
        sid = questionnaires.add_survey('9999', 'Questionario de Teste', 'en', 'G')
        self.assertGreaterEqual(sid, 0)

        status = questionnaires.delete_survey(sid)
        self.assertEqual(status, 'OK')
コード例 #9
0
ファイル: test_views.py プロジェクト: fpuna-nes/nes
    def test_survey_without_pt_or_en_title_returns_default_language_title_to_be_listed_but_does_not_save(
            self, mockServer):
        mockServer.return_value.get_session_key.return_value = \
            'vz224sb7jzkvh8i4kpx8fxbcxd67meht'
        mockServer.return_value.add_survey.return_value = 21212150
        mockServer.return_value.get_language_properties.side_effect = [{
            'surveyls_title':
            None
        }, {
            'surveyls_title':
            None
        }, {
            'surveyls_title':
            'Test Questionnaire in French'
        }]
        mockServer.return_value.get_survey_properties.return_value = {
            'active': 'N'
        }

        lime_survey = Questionnaires()

        # Create a new survey at LimeSurvey withou titles in pt or en languages
        fr_title_survey = 'Test Questionnaire in French'
        sid = lime_survey.add_survey(9999, fr_title_survey, 'fr', 'G')

        Survey.objects.create(lime_survey_id=sid)
        response = self.client.get(reverse('survey_list'))

        # Check if the page renders the fr title of the survey
        self.assertContains(response, fr_title_survey)

        # Check that pt_title and en_title remain null
        self.assertIsNone(Survey.objects.last().en_title)
        self.assertIsNone(Survey.objects.last().pt_title)
コード例 #10
0
    def _export_surveys(self):
        """Export experiment surveys archives using LimeSurvey RPC API.
        :return: list of survey archive paths
        """
        questionnaire_ids = Questionnaire.objects.filter(
            experiment=self.experiment).values_list('survey_id', flat=True)
        surveys = Survey.objects.filter(id__in=questionnaire_ids)
        ls_interface = Questionnaires(settings.LIMESURVEY['URL_API'] +
                                      '/index.php/plugins/unsecure?plugin=extendRemoteControl&function=action')
        if ls_interface.session_key is None:
            return self.LIMESURVEY_ERROR, _('Could not export LimeSurvey data. Please try again. If problem persists '
                                            'please contact the system administator')
        archive_paths = []
        for survey in surveys:
            result = ls_interface.export_survey(survey.lime_survey_id)
            if result is None:
                return self.LIMESURVEY_ERROR, _(
                    'Could not export LimeSurvey data. Please try again. If problem persists '
                    'please contact the system administator')
            decoded_archive = b64decode(result)
            lsa_archive_path = os.path.join(self.temp_dir, str(survey.lime_survey_id) + '.lsa')
            lsa_archive = open(lsa_archive_path, 'wb')
            lsa_archive.write(decoded_archive)
            archive_paths.append(lsa_archive_path)

        ls_interface.release_session_key()

        return archive_paths if archive_paths else []  # TODO (NES_956): return empty list?
コード例 #11
0
ファイル: test_views.py プロジェクト: liyifan5923013/nes
    def test_add_and_delete_survey_methods(self):
        questionnaires = Questionnaires()
        sid = questionnaires.add_survey('9999', 'Questionario de Teste', 'en',
                                        'G')
        self.assertGreaterEqual(sid, 0)

        status = questionnaires.delete_survey(sid)
        self.assertEqual(status, 'OK')
コード例 #12
0
ファイル: test_views.py プロジェクト: liyifan5923013/nes
 def test_find_questionnaire_by_id_method_found_survey(self):
     questionnaires = Questionnaires()
     list_survey = self.server.list_surveys(self.session_key, None)
     self.server.release_session_key(self.session_key)
     self.assertEqual(
         questionnaires.find_questionnaire_by_id(list_survey[3]['sid']),
         list_survey[3])
     questionnaires.release_session_key()
コード例 #13
0
def survey_view(request,
                survey_id,
                template_name="survey/survey_register.html"):
    survey = get_object_or_404(Survey, pk=survey_id)

    surveys = Questionnaires()

    limesurvey_available = check_limesurvey_access(request, surveys)
    language = get_questionnaire_language(surveys, survey.lime_survey_id,
                                          request.LANGUAGE_CODE)
    survey_title = surveys.get_survey_title(survey.lime_survey_id, language)

    # There is no need to use "request.POST or None" because the data will never be changed here. In fact we have to
    # use "None" only, because request.POST does not contain any value because the fields are disabled, and this results
    # in the form being created without considering the initial value.
    survey_form = SurveyForm(
        None,
        instance=survey,
        initial={'title': str(survey.lime_survey_id) + ' - ' + survey_title})

    for field in survey_form.fields:
        survey_form.fields[field].widget.attrs['disabled'] = True

    if request.method == "POST":
        if request.POST['action'] == "remove":
            try:
                survey.delete()
                messages.success(request,
                                 _('Questionnaire deleted successfully.'))
                return redirect('survey_list')
            except ProtectedError:
                messages.error(
                    request,
                    _("It was not possible to delete questionnaire, because there are experimental "
                      "answers or steps associated."))

    patients_questionnaire_data_list = create_patients_questionnaire_data_list(
        survey, surveys)

    if request.user.has_perm("experiment.view_researchproject"):
        experiments_questionnaire_data_list = create_experiments_questionnaire_data_list(
            survey, surveys)
    else:
        experiments_questionnaire_data_list = []

    context = {
        "limesurvey_available": limesurvey_available,
        "patients_questionnaire_data_list": patients_questionnaire_data_list,
        "experiments_questionnaire_data_list":
        experiments_questionnaire_data_list,
        "survey": survey,
        "survey_form": survey_form,
        "survey_title": survey_title,
    }

    return render(request, template_name, context)
コード例 #14
0
ファイル: test_views.py プロジェクト: neuromat/nes
 def test_list_active_questionnaires(self):
     questionnaires = Questionnaires()
     list_survey = self.server.list_surveys(self.session_key, None)
     self.server.release_session_key(self.session_key)
     list_active_survey = []
     for survey in list_survey:
         survey_has_token = questionnaires.survey_has_token_table(survey['sid'])
         if survey['active'] == "Y" and survey_has_token is True:
             list_active_survey.append(survey)
     self.assertEqual(questionnaires.find_all_active_questionnaires(), list_active_survey)
     questionnaires.release_session_key()
コード例 #15
0
ファイル: tests.py プロジェクト: fpuna-nes/nes
    def test_acquisitiondate_field_is_not_hidden(self):
        ls = Questionnaires()
        groups = ls.list_groups(LS_ID)
        identification_group = next(item for item in groups
                                    if item['group_name'] == 'Identification')
        questions = ls.list_questions(LS_ID, identification_group['gid'])
        acquisitiondate_question = next(item for item in questions
                                        if item['title'] == 'acquisitiondate')
        question_properties = ls.get_question_properties(
            acquisitiondate_question['qid'], 'en')

        self.assertEqual(question_properties['attributes']['hidden'], '0')
コード例 #16
0
def survey_create(request, template_name="survey/survey_register.html"):
    survey_form = SurveyForm(request.POST or None,
                             initial={
                                 'title': 'title',
                                 'is_initial_evaluation': False
                             })

    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    if limesurvey_available:
        questionnaires_list = surveys.find_all_active_questionnaires()

    surveys.release_session_key()

    if questionnaires_list:
        # removing surveys already registered
        used_surveys = Survey.objects.all()
        for used_survey in used_surveys:
            for questionnaire in questionnaires_list:
                if used_survey.lime_survey_id == questionnaire['sid']:
                    questionnaires_list.remove(questionnaire)
                    break
    else:
        messages.warning(request, _('No questionnaire found.'))

    if request.method == "POST":
        if request.POST['action'] == "save":
            if survey_form.is_valid():

                survey_added = survey_form.save(commit=False)

                survey, created = Survey.objects.get_or_create(
                    lime_survey_id=request.POST['questionnaire_selected'],
                    is_initial_evaluation=survey_added.is_initial_evaluation)

                if created:
                    messages.success(request,
                                     _('Questionnaire created successfully.'))
                    redirect_url = reverse("survey_list")
                    return HttpResponseRedirect(redirect_url)

    context = {
        "survey_form": survey_form,
        "creating": True,
        "editing": True,
        "questionnaires_list": questionnaires_list,
        'limesurvey_available': limesurvey_available
    }

    return render(request, template_name, context)
コード例 #17
0
def survey_list(request, template_name='survey/survey_list.html'):
    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    language_code = request.LANGUAGE_CODE

    update = False
    if request.method == "POST":
        if request.POST['action'] == "update":
            update = True

    for survey in Survey.objects.all():
        survey_title = get_survey_title_based_on_the_user_language(survey, language_code, update)

        # Get the status of the survey
        # If there's any inactive survey, search LimeSurvey to see if there's any change in that matter
        # and update the fields in the database
        is_active = survey.is_active

        if not is_active or update:
            status = surveys.get_survey_properties(survey.lime_survey_id, 'active')
            if status == 'Y':
                survey.is_active = True
            else:
                survey.is_active = False

            survey.save()

        questionnaires_list.append(
            {
                'id': survey.id,
                'lime_survey_id': survey.lime_survey_id,
                'title': survey_title,
                'is_initial_evaluation': survey.is_initial_evaluation,
                'is_active': survey.is_active,
            }
        )

    surveys.release_session_key()

    questionnaires_list = sorted(questionnaires_list, key=itemgetter('title'))

    context = {
        'questionnaires_list': questionnaires_list,
        'limesurvey_available': limesurvey_available,
    }

    return render(request, template_name, context)
コード例 #18
0
ファイル: views.py プロジェクト: neuromat/nes
def survey_list(request, template_name='survey/survey_list.html'):
    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    language_code = request.LANGUAGE_CODE

    update = False
    if request.method == "POST":
        if request.POST['action'] == "update":
            update = True

    for survey in Survey.objects.all():
        survey_title = get_survey_title_based_on_the_user_language(survey, language_code, update)

        # Get the status of the survey
        # If there's any inactive survey, search LimeSurvey to see if there's any change in that matter
        # and update the fields in the database
        is_active = survey.is_active

        if not is_active or update:
            status = surveys.get_survey_properties(survey.lime_survey_id, 'active')
            if status == 'Y':
                survey.is_active = True
            else:
                survey.is_active = False

            survey.save()

        questionnaires_list.append(
            {
                'id': survey.id,
                'lime_survey_id': survey.lime_survey_id,
                'title': survey_title,
                'is_initial_evaluation': survey.is_initial_evaluation,
                'is_active': survey.is_active,
            }
        )

    surveys.release_session_key()

    questionnaires_list = sorted(questionnaires_list, key=itemgetter('title'))

    context = {
        'questionnaires_list': questionnaires_list,
        'limesurvey_available': limesurvey_available,
    }

    return render(request, template_name, context)
コード例 #19
0
ファイル: test_limesurvey.py プロジェクト: fpuna-nes/nes
    def test_get_question_properties(self, mockServer):
        lime_survey = Questionnaires()
        lime_survey.get_question_properties(1, 'en')

        question_properties = {
            'gid', 'question', 'question_order', 'subquestions',
            'answeroptions', 'title', 'type', 'attributes_lang', 'attributes',
            'other'
        }
        (session_key, question_id, properties, language), kwargs = \
            mockServer.return_value.get_question_properties.call_args
        self.assertTrue(
            set(question_properties).issubset(properties),
            str(set(question_properties)) + ' is not a subset of ' +
            str(set(properties)))
コード例 #20
0
ファイル: test_limesurvey.py プロジェクト: neuromat/nes
    def test_get_question_properties(self, mockServerClass):
        lime_survey = Questionnaires()
        lime_survey.get_question_properties(1, 'en')

        question_properties = {
            'gid', 'question', 'question_order', 'subquestions',
            'answeroptions', 'title', 'type', 'attributes_lang',
            'attributes', 'other'
        }
        (session_key, question_id, properties, language), kwargs = \
            mockServerClass.return_value.get_question_properties.call_args
        self.assertTrue(
            set(question_properties).issubset(properties),
            str(set(question_properties)) + ' is not a subset of ' +
            str(set(properties))
        )
コード例 #21
0
def find_questionnaire_name(survey, language_code):
    language_code = language_code.lower()
    titles = {'pt-br': survey.pt_title, 'en': survey.en_title}
    fallback_language = 'en' if language_code == 'pt-br' else 'pt-br'

    if titles[language_code] is not None and titles[language_code] != '':
        title = titles[language_code]
    elif titles[fallback_language] is not None \
            and titles[fallback_language] != '':
        title = titles[fallback_language]
    else:
        surveys = Questionnaires()
        title = surveys.get_survey_title(survey.lime_survey_id)
        surveys.release_session_key()

    return {'sid': survey.lime_survey_id, 'name': title}
コード例 #22
0
ファイル: views.py プロジェクト: neuromat/nes
def survey_view(request, survey_id, template_name="survey/survey_register.html"):
    survey = get_object_or_404(Survey, pk=survey_id)

    surveys = Questionnaires()

    limesurvey_available = check_limesurvey_access(request, surveys)
    language = get_questionnaire_language(surveys, survey.lime_survey_id, request.LANGUAGE_CODE)
    survey_title = surveys.get_survey_title(survey.lime_survey_id, language)

    # There is no need to use "request.POST or None" because the data will never be changed here. In fact we have to
    # use "None" only, because request.POST does not contain any value because the fields are disabled, and this results
    # in the form being created without considering the initial value.
    survey_form = SurveyForm(None,
                             instance=survey,
                             initial={'title': str(survey.lime_survey_id) + ' - ' + survey_title})

    for field in survey_form.fields:
        survey_form.fields[field].widget.attrs['disabled'] = True

    if request.method == "POST":
        if request.POST['action'] == "remove":
            try:
                survey.delete()
                messages.success(request, _('Questionnaire deleted successfully.'))
                return redirect('survey_list')
            except ProtectedError:
                messages.error(request, _("It was not possible to delete questionnaire, because there are experimental "
                                          "answers or steps associated."))

    patients_questionnaire_data_list = create_patients_questionnaire_data_list(survey, surveys)

    if request.user.has_perm("experiment.view_researchproject"):
        experiments_questionnaire_data_list = create_experiments_questionnaire_data_list(survey, surveys)
    else:
        experiments_questionnaire_data_list = []

    context = {
        "limesurvey_available": limesurvey_available,
        "patients_questionnaire_data_list": patients_questionnaire_data_list,
        "experiments_questionnaire_data_list": experiments_questionnaire_data_list,
        "survey": survey,
        "survey_form": survey_form,
        "survey_title": survey_title,
    }

    return render(request, template_name, context)
コード例 #23
0
ファイル: test_limesurvey.py プロジェクト: fpuna-nes/nes
    def test_create_questionnaire_explanation_fields_has_question_order(
            self, mockServerClass):
        # There are fields that
        # create_questionnaire_explanation_fields method get filled we must
        # to define before calling it (fake values)
        survey_utils = QuestionnaireUtils()
        questionnaire_id = '999999'
        fields = ['question']
        survey_utils.questionnaires_experiment_data[questionnaire_id] = {}
        survey_utils.questionnaires_experiment_data[questionnaire_id][
            'fields'] = fields
        survey_utils.questionnaires_experiment_data[questionnaire_id][
            'header'] = 'dummie_header'
        # mock needed LimeSurvey RPC API methods
        mockServerClass.return_value.get_language_properties.return_value = \
            {'surveyls_title': 'Ein wunderbar Titel'}
        mockServerClass.return_value.list_questions.return_value = [{
            'id': {
                'qid': 1
            }
        }]
        # mock get_question_properties LimeSurvey API method using
        # ABCSearchEngine.QUESTION_PROPERTIES constant list with fake values
        question_order = 21
        group_id = 981
        question_properties = dict(
            zip(ABCSearchEngine.QUESTION_PROPERTIES, [
                group_id, 'Question Title', question_order,
                'No available answers', 'No available answer options',
                'question', 'N', 'No available attributes', {'hidden', '1'
                                                             }, 'N'
            ]))
        mockServerClass.return_value.get_question_properties.return_value = \
            question_properties
        # mock list_groups LimeSurvey API method (fake values)
        language = 'en'
        entrance_survey = False
        mockServerClass.return_value.list_groups.return_value = \
            [{'randomization_group': '',
              'id': {'gid': group_id, 'language': language},
              'group_name': 'Grupo 1', 'description': '', 'group_order': 1,
              'sid': 376459, 'gid': group_id,
              'language': language, 'grelevance': ''}]

        lime_survey = Questionnaires()
        error, questionnaire_fields = survey_utils.create_questionnaire_explanation_fields(
            questionnaire_id, language, lime_survey, fields, entrance_survey)

        # First line contains metadata column headers, subsequent lines
        # contains the metadata column values.
        # Assert for correct length for metadata headers and values
        self.assertTrue(len(questionnaire_fields[0]),
                        len(questionnaire_fields[1]))

        # asserts for question_order field
        self.assertTrue('question_order' in questionnaire_fields[0])
        index_ = questionnaire_fields[0].index('question_order')
        self.assertEqual(questionnaire_fields[1][index_], str(question_order))
コード例 #24
0
ファイル: views.py プロジェクト: INCF/nes
def survey_create(request, template_name="survey/survey_register.html"):
    survey_form = SurveyForm(request.POST or None, initial={'title': 'title', 'is_initial_evaluation': False})

    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    if limesurvey_available:
        questionnaires_list = surveys.find_all_active_questionnaires()

    surveys.release_session_key()

    # removing surveys already registered
    used_surveys = Survey.objects.all()
    for used_survey in used_surveys:
        for questionnaire in questionnaires_list:
            if used_survey.lime_survey_id == questionnaire['sid']:
                questionnaires_list.remove(questionnaire)
                break

    if request.method == "POST":
        if request.POST['action'] == "save":
            if survey_form.is_valid():

                survey_added = survey_form.save(commit=False)

                survey, created = Survey.objects.get_or_create(
                    lime_survey_id=request.POST['questionnaire_selected'],
                    is_initial_evaluation=survey_added.is_initial_evaluation)

                if created:
                    messages.success(request, _('Questionnaire created successfully.'))
                    redirect_url = reverse("survey_list")
                    return HttpResponseRedirect(redirect_url)

    context = {
        "survey_form": survey_form,
        "creating": True,
        "editing": True,
        "questionnaires_list": questionnaires_list,
        'limesurvey_available': limesurvey_available
    }

    return render(request, template_name, context)
コード例 #25
0
    def _import_limesurvey_surveys(self):
        """Import surveys to Limesurvey server
        :return: list of limsurvey surveys imported
        """
        result = 0, ''
        ls_interface = Questionnaires()
        if ls_interface.session_key is None:
            result = self.LIMESURVEY_ERROR, _('Could not import survey(s) to LimeSurvey. Only Experiment data was '
                                              'imported. You can remove experiment imported and try again. If problem '
                                              'persists please contact system administrator')
            return result
        limesurvey_ids = []
        # Does not add try/exception trying to open zipfile here because it
        # was done in import_all method
        with zipfile.ZipFile(self.file_path) as zip_file:
            for old_ls_id, dummy_ls_id in self.limesurvey_relations.items():
                survey_archivename = str(old_ls_id) + '.lsa'
                if survey_archivename in zip_file.namelist():
                    survey_archive = zip_file.extract(survey_archivename, self.temp_dir)
                    with open(survey_archive, 'rb') as file:
                        encoded_string = b64encode(file.read())
                        encoded_string = encoded_string.decode('utf-8')
                        new_ls_id = ls_interface.import_survey(encoded_string)
                        if new_ls_id is None:
                            result = self.LIMESURVEY_ERROR, _('Could not import survey(s) to LimeSurvey. Only '
                                                              'Experiment data was imported. You can remove experiment '
                                                              'imported and try again. If problem persists please '
                                                              'contact system administrator')
                            return result
                        survey = Survey.objects.get(lime_survey_id=dummy_ls_id)
                        survey.lime_survey_id = new_ls_id
                        survey.save()
                        limesurvey_ids.append(new_ls_id)
                else:
                    # TODO (NES-956): add information that was not a survey archive
                    #  to this survey
                    continue

        if limesurvey_ids:
            result = self._remove_limesurvey_participants()
            if not result[0]:
                return self._update_limesurvey_identification_questions()

        return result
コード例 #26
0
ファイル: input_export.py プロジェクト: umarbrowser/nes
    def build_questionnaire(self, questionnaire_list, language=DEFAULT_LANGUAGE):
        print("questionnaire")

        self.data["questionnaires"] = []

        questionnaire_lime_survey = Questionnaires()

        for sid, title, field_header_list in questionnaire_list:
            language = get_questionnaire_language(questionnaire_lime_survey, sid, language)

            self.data["questionnaires"].append({"id": sid, "language": language,
                                                "prefix_filename_fields": PREFIX_FILENAME_FIELDS,
                                                "questionnaire_name": title,
                                                "prefix_filename_responses": PREFIX_FILENAME_RESPONSES,
                                                "output_list": []})
            for header, field in field_header_list:
                output_data = {"header": header, "field": field}
                self.data["questionnaires"][-1]["output_list"].append(output_data)
                # ["header"] = header
                # self.data["questionnaires"][0]["output_list"]["field"] = field

        questionnaire_lime_survey.release_session_key()
コード例 #27
0
ファイル: test_views.py プロジェクト: neuromat/nes
    def test_survey_without_pt_or_en_title_in_ls_returns_default_language_title_to_be_listed_but_dont_save(self):
        lime_survey = Questionnaires()
        sid = None

        # Create a new survey at LimeSurvey withou titles in pt or en languages
        try:
            fr_title_survey = 'Test Questionnaire in French'
            sid = lime_survey.add_survey(9999, fr_title_survey, 'fr', 'G')

            Survey.objects.create(lime_survey_id=sid)
            response = self.client.get(reverse('survey_list'))

            # Check if the page renders the fr title of the survey
            self.assertContains(response, fr_title_survey)

            # Check that pt_title and en_title remain null
            self.assertIsNone(Survey.objects.last().en_title)
            self.assertIsNone(Survey.objects.last().pt_title)

        finally:
            # Deletes the survey created at Lime Survey
            self.assertEqual(lime_survey.delete_survey(sid), 'OK')
コード例 #28
0
ファイル: views.py プロジェクト: neuromat/nes
def get_survey_title_based_on_the_user_language(survey, language_code, update=False):

    if not update:
        # Get the titles in both languages and return the one compatible with the user's language,
        # If the title is blank in that language, return it in the other language, and
        # if that one is also blank, try to update the titles in each language
        # if any of them updates isn't blank, recursively calls this function
        # otherwise, try to get the title for this survey at LimeSurvey and return it
        titles = {'pt-br': survey.pt_title, 'en': survey.en_title}
        fallback_language = 'en' if language_code == 'pt-br' else 'pt-br'

        if titles[language_code]:
            return titles[language_code]
        elif titles[fallback_language]:
            return titles[fallback_language]

    surveys = Questionnaires()
    pt_title = surveys.get_survey_title(survey.lime_survey_id, 'pt-BR')
    en_title = surveys.get_survey_title(survey.lime_survey_id, 'en')

    if language_code == 'pt-br' and pt_title:
        surveys.release_session_key()
        survey.pt_title = pt_title
        survey.save()
        return get_survey_title_based_on_the_user_language(survey, language_code)
    elif language_code == 'en' and en_title:
        surveys.release_session_key()
        survey.en_title = en_title
        survey.save()
        return get_survey_title_based_on_the_user_language(survey, language_code)
    else:
        title = surveys.get_survey_title(survey.lime_survey_id)
        surveys.release_session_key()

        if title:
            return title
        else:
            return survey.lime_survey_id
コード例 #29
0
    def test_survey_without_pt_or_en_title_in_ls_returns_default_language_title_to_be_listed_but_dont_save(
            self):
        lime_survey = Questionnaires()
        sid = None

        # Create a new survey at LimeSurvey withou titles in pt or en languages
        try:
            fr_title_survey = 'Test Questionnaire in French'
            sid = lime_survey.add_survey(9999, fr_title_survey, 'fr', 'G')

            Survey.objects.create(lime_survey_id=sid)
            response = self.client.get(reverse('survey_list'))

            # Check if the page renders the fr title of the survey
            self.assertContains(response, fr_title_survey)

            # Check that pt_title and en_title remain null
            self.assertIsNone(Survey.objects.last().en_title)
            self.assertIsNone(Survey.objects.last().pt_title)

        finally:
            # Deletes the survey created at Lime Survey
            self.assertEqual(lime_survey.delete_survey(sid), 'OK')
コード例 #30
0
    def test_surveys_list_get_updated(self):
        lime_survey = Questionnaires()
        sid = None

        # Create a new survey at LimeSurvey with pt title
        try:
            pt_title_survey = 'Questionário Teste'
            sid = lime_survey.add_survey(9999, pt_title_survey, 'pt-BR', 'G')

            # Update the infos when viewed
            survey = Survey.objects.create(lime_survey_id=sid)

            # Check that pt_title is null
            self.assertIsNone(Survey.objects.last().pt_title)

            self.client.get(reverse('survey_list'))

            # Check that pt_title is updated
            self.assertEqual(Survey.objects.last().pt_title, pt_title_survey)

            # Simulate a discrepancy between the survey informations at limesurvey and NES
            survey.pt_title = "Título discrepante"
            survey.save()

            self.assertNotEqual(Survey.objects.last().pt_title,
                                pt_title_survey)

            # Simulate clicking to update the list with new limesurvey information
            self.client.post(reverse('survey_list'), {'action': 'update'},
                             follow=True)

            # Check if the pt_title was updated properly
            self.assertEqual(Survey.objects.last().pt_title, pt_title_survey)

        finally:
            # Deletes the survey created at Lime Survey
            self.assertEqual(lime_survey.delete_survey(sid), 'OK')
コード例 #31
0
ファイル: test_views.py プロジェクト: neuromat/nes
    def test_add_participant_to_a_survey(self):
        """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]['firsStname'], 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 = self.server.delete_participants(self.session_key, sid, [token_id])

        self.assertEqual(result[str(token_id)], 'Deleted')

        surveys.release_session_key()
コード例 #32
0
ファイル: test_views.py プロジェクト: liyifan5923013/nes
 def test_list_active_questionnaires(self):
     questionnaires = Questionnaires()
     list_survey = self.server.list_surveys(self.session_key, None)
     self.server.release_session_key(self.session_key)
     list_active_survey = []
     for survey in list_survey:
         survey_has_token = questionnaires.survey_has_token_table(
             survey['sid'])
         if survey['active'] == "Y" and survey_has_token is True:
             list_active_survey.append(survey)
     self.assertEqual(questionnaires.find_all_active_questionnaires(),
                      list_active_survey)
     questionnaires.release_session_key()
コード例 #33
0
    def _update_limesurvey_identification_questions(self):
        """Must be called after updating Limesurvey surveys references
        """
        result = 0, ''
        indexes = self._get_indexes('experiment', 'questionnaire')
        ls_interface = Questionnaires(
            settings.LIMESURVEY['URL_API'] + '/index.php/plugins/unsecure?plugin=extendRemoteControl&function=action')
        questionnaire_utils = QuestionnaireUtils()
        for index in indexes:
            questionnaire = Questionnaire.objects.get(id=self.data[index]['pk'])
            questionnaire_responses = QuestionnaireResponse.objects.filter(
                data_configuration_tree__component_configuration__component=questionnaire.id)
            limesurvey_id = questionnaire.survey.lime_survey_id
            for response in questionnaire_responses:
                responsible_id = response.questionnaire_responsible_id
                subject_id = response.subject_of_group.subject_id
                token_id = response.token_id
                token = ls_interface.get_participant_properties(limesurvey_id, token_id, 'token')
                if token is None:
                    result = self.LIMESURVEY_ERROR, _('Could not update identification questions for all responses.')
                    continue
                # TODO (NES-956): get the language. By now put 'en' to test
                ls_subject_id_column_name = \
                    questionnaire_utils.get_response_column_name_for_identification_group_questions(
                        ls_interface, limesurvey_id, 'subjectid', 'en')
                if isinstance(ls_subject_id_column_name, tuple):  # Returned error
                    result = ls_subject_id_column_name[0], _('Could not update identification questions for all '
                                                             'responses.')
                    continue
                ls_responsible_id_column_name = \
                    questionnaire_utils.get_response_column_name_for_identification_group_questions(
                        ls_interface, limesurvey_id, 'responsibleid', 'en')
                if isinstance(ls_responsible_id_column_name, tuple):  # Returned error
                    result = ls_responsible_id_column_name[0], _('Could not update identification questions for all '
                                                                 'responses.')
                    continue
                result_update = ls_interface.update_response(limesurvey_id, {
                        'token': token,
                        ls_subject_id_column_name: subject_id,
                        ls_responsible_id_column_name: responsible_id
                })
                if not result_update:
                    return self.LIMESURVEY_ERROR, _('Could not update all responses.')

        ls_interface.release_session_key()

        return result
コード例 #34
0
ファイル: test_views.py プロジェクト: liyifan5923013/nes
    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_participant(sid, token_id)

        self.assertEqual(result[str(token_id)], 'Deleted')

        surveys.release_session_key()
コード例 #35
0
ファイル: views.py プロジェクト: INCF/nes
def get_questionnaire_responses(language_code, lime_survey_id, token_id, request):

    questionnaire_responses = []
    surveys = Questionnaires()
    token = surveys.get_participant_properties(lime_survey_id, token_id, "token")
    question_properties = []
    groups = surveys.list_groups(lime_survey_id)

    survey_title = surveys.get_survey_title(lime_survey_id)

    if not isinstance(groups, dict):

        # defining language to be showed
        languages = surveys.get_survey_languages(lime_survey_id)

        # language to be showed can be the base language, or...
        language = languages['language']

        # ...can be one of the additional languages
        if language.lower() != language_code.lower() and languages['additional_languages']:

            # search for the right language in addional languages,
            # considering that the LimeSurvey uses upper case in the two-letter language code, like en-US and pt-BR.
            additional_languages_list = languages['additional_languages'].split(' ')
            additional_languages_list_lower = [item.lower() for item in additional_languages_list]
            if language_code.lower() in additional_languages_list_lower:
                index = additional_languages_list_lower.index(language_code.lower())
                language = additional_languages_list[index]

        for group in groups:
            if 'id' in group and group['id']['language'] == language:

                question_list = surveys.list_questions(lime_survey_id, group['id'])
                question_list = sorted(question_list)

                for question in question_list:

                    properties = surveys.get_question_properties(question, group['id']['language'])

                    # cleaning the question field
                    properties['question'] = re.sub('{.*?}', '', re.sub('<.*?>', '', properties['question']))
                    properties['question'] = properties['question'].replace('&nbsp;', '').strip()

                    is_purely_formula = (properties['type'] == '*') and (properties['question'] == '')

                    if not is_purely_formula and properties['question'] != '':

                        if isinstance(properties['subquestions'], dict):
                            question_properties.append({
                                'question': properties['question'],
                                'question_id': properties['title'],
                                'answer_options': 'super_question',
                                'type': properties['type'],
                                'attributes_lang': properties['attributes_lang'],
                                'hidden': 'hidden' in properties['attributes'] and
                                          properties['attributes']['hidden'] == '1'
                            })
                            for key, value in sorted(properties['subquestions'].items()):
                                question_properties.append({
                                    'question': value['question'],
                                    'question_id': properties['title'] + '[' + value['title'] + ']',
                                    'answer_options': properties['answeroptions'],
                                    'type': properties['type'],
                                    'attributes_lang': properties['attributes_lang'],
                                    'hidden': 'hidden' in properties['attributes'] and
                                              properties['attributes']['hidden'] == '1'
                                })
                        else:
                            question_properties.append({
                                'question': properties['question'],
                                'question_id': properties['title'],
                                'answer_options': properties['answeroptions'],
                                'type': properties['type'],
                                'attributes_lang': properties['attributes_lang'],
                                'hidden': 'hidden' in properties['attributes'] and
                                          properties['attributes']['hidden'] == '1'
                            })

        # Reading from Limesurvey and...
        responses_string = surveys.get_responses_by_token(lime_survey_id, token, language)

        # ... transforming to a list:
        # response_list[0] has the questions
        #   response_list[1] has the answers
        responses_list = []

        if isinstance(responses_string, bytes):
            reader = csv.reader(StringIO(responses_string.decode()), delimiter=',')

            for row in reader:
                responses_list.append(row)

            for question in question_properties:

                if not question['hidden']:

                    if isinstance(question, str) and question['answer_options'] == "super_question":

                        if question['question'] != '':
                            questionnaire_responses.append({
                                'question': question['question'],
                                'answer': '',
                                'type': question['type']
                            })
                    else:

                        answer = ''

                        if question['type'] == '1':

                            answer_list = []

                            if question['question_id'] + "[1]" in responses_list[0]:
                                index = responses_list[0].index(question['question_id'] + "[1]")
                                answer_options = question['answer_options']

                                # if 'dualscale_headerA' in question['attributes_lang']:

                                answer = question['attributes_lang']['dualscale_headerA'] + ": "
                                if responses_list[1][index] in answer_options:
                                    answer_option = answer_options[responses_list[1][index]]
                                    answer += answer_option['answer']
                                else:
                                    answer += 'Sem resposta'
                                # else:
                                #     answer += 'Sem resposta'

                            answer_list.append(answer)

                            if question['question_id'] + "[2]" in responses_list[0]:
                                index = responses_list[0].index(question['question_id'] + "[2]")
                                answer_options = question['answer_options']

                                # if 'dualscale_headerB' in question['attributes_lang']:

                                answer = question['attributes_lang']['dualscale_headerB'] + ": "
                                if responses_list[1][index] in answer_options:
                                    answer_option = answer_options[responses_list[1][index]]
                                    answer += answer_option['answer']
                                else:
                                    answer += 'Sem resposta'
                                # else:
                                #     answer += 'Sem resposta'

                            answer_list.append(answer)

                            questionnaire_responses.append({
                                'question': question['question'],
                                'answer': answer_list,
                                'type': question['type']
                            })
                        else:

                            if question['question_id'] in responses_list[0]:

                                index = responses_list[0].index(question['question_id'])

                                answer_options = question['answer_options']

                                if isinstance(answer_options, dict):

                                    if responses_list[1][index] in answer_options:
                                        answer_option = answer_options[responses_list[1][index]]
                                        answer = answer_option['answer']
                                    else:
                                        answer = 'Sem resposta'
                                else:
                                    if question['type'] == 'D':
                                        if responses_list[1][index]:
                                            answer = datetime.datetime.strptime(responses_list[1][index],
                                                                                '%Y-%m-%d %H:%M:%S')
                                        else:
                                            answer = ''
                                    else:
                                        answer = responses_list[1][index]

                            questionnaire_responses.append({
                                'question': question['question'],
                                'answer': answer,
                                'type': question['type']
                            })
        else:
            messages.error(request, _("LimeSurvey did not find fill data for this questionnaire."))

    surveys.release_session_key()

    return survey_title, questionnaire_responses
コード例 #36
0
ファイル: test_views.py プロジェクト: neuromat/nes
    def test_complete_survey(self):
        lime_survey = Questionnaires()
        sid = None

        try:
            # Cria uma nova survey no lime survey
            title_survey = 'Questionario de teste'
            sid = lime_survey.add_survey(9999, title_survey, 'en', 'G')

            # Obtenho o titulo da survey
            survey_title = lime_survey.get_survey_title(sid)
            self.assertEqual(survey_title, title_survey)

            # Verifica se esta ativa
            survey_active = lime_survey.get_survey_properties(sid, 'active')
            self.assertEqual(survey_active, 'N')

            # Obtem uma propriedade - Administrador da Survey
            survey_admin = lime_survey.get_survey_properties(sid, 'admin')
            self.assertEqual(survey_admin, None)

            # Importar grupo de questoes
            handle_file_import = \
                open('quiz/static/quiz/tests/limesurvey_groups.lsg', 'r')
            questions_data = handle_file_import.read()
            questions_id = \
                lime_survey.insert_questions(sid, questions_data, 'lsg')
            self.assertGreaterEqual(questions_id, 1)

            # Inicia tabela de tokens
            self.assertEqual(lime_survey.activate_tokens(sid), 'OK')

            # Ativar survey
            self.assertEqual(lime_survey.activate_survey(sid), 'OK')

            # Verifica se esta ativa
            survey_active = lime_survey.get_survey_properties(sid, 'active')
            self.assertEqual(survey_active, 'Y')

            # Adiciona participante e obtem o token
            result_token = lime_survey.add_participant(sid)

            # Verifica se o token está presente na tabela de participantes
            token = lime_survey.get_participant_properties(sid, result_token, "token")
            self.assertEqual(token, result_token['token'])
        finally:
            # Deleta a survey gerada no Lime Survey
            self.assertEqual(lime_survey.delete_survey(sid), 'OK')
コード例 #37
0
ファイル: test_views.py プロジェクト: liyifan5923013/nes
 def test_find_all_questionnaires_method_returns_correct_result(self):
     questionnaires = Questionnaires()
     list_survey = self.server.list_surveys(self.session_key, None)
     self.server.release_session_key(self.session_key)
     self.assertEqual(questionnaires.find_all_questionnaires(), list_survey)
     questionnaires.release_session_key()
コード例 #38
0
def survey_update_sensitive_questions(
        request,
        survey_id,
        template_name="survey/survey_sensitive_fields.html"):
    survey = get_object_or_404(Survey, pk=survey_id)

    surveys = Questionnaires()
    language = get_questionnaire_language(surveys, survey.lime_survey_id,
                                          request.LANGUAGE_CODE)
    survey_title = surveys.get_survey_title(survey.lime_survey_id, language)
    limesurvey_available = check_limesurvey_access(request, surveys)

    current_selected_fields = SensitiveQuestion.objects.filter(survey=survey)

    field_code = get_survey_header(surveys, survey, language, 'code')
    field_text = get_survey_header(surveys, survey, language, 'full')

    surveys.release_session_key()

    counter = 0
    available_fields = []
    while counter < len(field_code):
        if field_code[counter] not in questionnaire_evaluation_fields_excluded:
            available_fields.append({
                'code':
                field_code[counter],
                'text':
                field_text[counter],
                'checked':
                current_selected_fields.filter(
                    code=field_code[counter]).exists()
            })
        counter += 1

    if request.method == "POST":
        if request.POST['action'] == "save":

            changed = False

            # for each available fields
            for field in available_fields:

                field_code = field['code']
                field_text = field['text']

                # field was selected
                if "field_" + str(field_code) in request.POST and request.POST[
                        "field_" + str(field_code)] == 'on':

                    # field not in current configuration
                    if not current_selected_fields.filter(
                            code=str(field_code)).exists():
                        # Add itens selected
                        SensitiveQuestion.objects.create(survey=survey,
                                                         code=field_code,
                                                         question=field_text)
                        changed = True
                else:

                    # field in current configuration
                    if current_selected_fields.filter(
                            code=str(field_code)).exists():
                        current_selected_fields.filter(
                            code=str(field_code)).delete()
                        changed = True

            # Exclude unknown items
            unkown_items = SensitiveQuestion.objects.filter(
                survey=survey).exclude(
                    code__in=[field['code'] for field in available_fields])
            if unkown_items.exists():
                unkown_items.delete()
                changed = True

            if changed:
                messages.success(request,
                                 _('Questionnaire was updated successfully.'))
            else:
                messages.success(request, _('There are no changes to save.'))

            redirect_url = reverse("survey_view", args=(survey.id, ))
            return HttpResponseRedirect(redirect_url)

    context = {
        "available_fields": available_fields,
        "limesurvey_available": limesurvey_available,
        "survey": survey,
        "survey_title": survey_title
    }

    return render(request, template_name, context)
コード例 #39
0
ファイル: input_export.py プロジェクト: liyifan5923013/nes
    def build_questionnaire(self, questionnaire_list, language,
                            entrance_questionnaire):
        questionnaire_lime_survey = Questionnaires()
        if "questionnaire_language" not in self.data:
            self.data["questionnaire_language"] = {}

        if entrance_questionnaire:
            self.data["questionnaires"] = []

            for index, sid, title, field_header_list in questionnaire_list:
                output_language = get_questionnaire_language(
                    questionnaire_lime_survey, sid, language)

                languages = questionnaire_lime_survey.get_survey_languages(sid)
                language_list = [languages['language']]
                additional_language = languages['additional_languages'].split(
                    ' ')
                for item in additional_language:
                    if item != '':
                        language_list.append(item)

                if sid not in self.data["questionnaire_language"]:
                    self.data["questionnaire_language"][sid] = {
                        "language_list": language_list,
                        "output_language": output_language,
                    }

                # if sid not in self.data['questionnaires']:
                self.data["questionnaires"].append({
                    "id":
                    sid,
                    "prefix_filename_fields":
                    PREFIX_FILENAME_FIELDS,
                    "questionnaire_name":
                    title,
                    "prefix_filename_responses":
                    PREFIX_FILENAME_RESPONSES,
                    "output_list": [],
                    "responses_list": []
                })
                for header, field in field_header_list:
                    output_data = {"header": header, "field": field}
                    self.data["questionnaires"][-1]["output_list"].append(
                        output_data)

        else:
            self.data["questionnaires_from_experiments"] = {}

            for index, group_id, sid, title, field_header_list in questionnaire_list:
                output_language = get_questionnaire_language(
                    questionnaire_lime_survey, sid, language)
                languages = questionnaire_lime_survey.get_survey_languages(sid)
                language_list = [languages['language']]
                additional_language = languages['additional_languages'].split(
                    ' ')
                for item in additional_language:
                    if item != '':
                        language_list.append(item)

                if sid not in self.data["questionnaire_language"]:
                    self.data["questionnaire_language"][sid] = {
                        "language_list": language_list,
                        "output_language": output_language,
                    }

                if group_id not in self.data[
                        'questionnaires_from_experiments']:
                    self.data['questionnaires_from_experiments'][group_id] = {}

                if sid not in self.data['questionnaires_from_experiments'][
                        group_id]:
                    self.data['questionnaires_from_experiments'][group_id][
                        sid] = {
                            "prefix_filename_fields": PREFIX_FILENAME_FIELDS,
                            "questionnaire_name": title,
                            "prefix_filename_responses":
                            PREFIX_FILENAME_RESPONSES,
                            "output_list": []
                        }

                for header, field in field_header_list:
                    output_data = {"header": header, "field": field}
                    self.data["questionnaires_from_experiments"][group_id][
                        sid]["output_list"].append(output_data)

                if sid not in self.data["questionnaire_list"]:
                    self.data["questionnaire_list"].append(sid)

        questionnaire_lime_survey.release_session_key()
コード例 #40
0
ファイル: test_views.py プロジェクト: neuromat/nes
 def test_find_all_questionnaires_method_returns_correct_result(self):
     questionnaires = Questionnaires()
     list_survey = self.server.list_surveys(self.session_key, None)
     self.server.release_session_key(self.session_key)
     self.assertEqual(questionnaires.find_all_questionnaires(), list_survey)
     questionnaires.release_session_key()
コード例 #41
0
    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
コード例 #42
0
ファイル: test_views.py プロジェクト: neuromat/nes
 def test_find_questionnaire_by_id_method_not_found_survey_by_out_of_range(self):
     questionnaires = Questionnaires()
     self.assertEqual(None, questionnaires.find_questionnaire_by_id(10000000))
     questionnaires.release_session_key()
コード例 #43
0
ファイル: export.py プロジェクト: INCF/nes
    def process_per_questionnaire(self):

        error_msg = ""
        export_per_questionnaire_directory = ''
        export_metadata_directory = ''
        path_per_questionnaire = ''

        # and save per_participant data
        if self.get_input_data("export_per_questionnaire"):
            per_questionnaire_directory = self.get_input_data("per_questionnaire_directory")
            error_msg, path_per_questionnaire = create_directory(self.get_export_directory(),
                                                                 per_questionnaire_directory)
            if error_msg != "":
                return error_msg

            export_per_questionnaire_directory = path.join(self.get_input_data("base_directory"),
                                                           self.get_input_data("per_questionnaire_directory"))

            export_metadata_directory = path.join(self.get_input_data("base_directory"), metadata_directory)

        questionnaire_lime_survey = Questionnaires()

        for questionnaire in self.get_input_data("questionnaires"):

            questionnaire_id = questionnaire["id"]
            language = questionnaire["language"]

            print(questionnaire_id)

            # per_participant_data is updated by define_questionnaire method
            fields_description = self.define_questionnaire(questionnaire, questionnaire_lime_survey)

            # create directory for questionnaire: <per_questionnaire>/<q_code_title>
            if self.get_input_data("export_per_questionnaire") and (len(fields_description) > 1):
                # path_questionnaire = str(questionnaire_id)

                questionnaire_code = self.get_questionnaire_code_from_id(questionnaire_id)
                questionnaire_title = self.get_title_reduced(questionnaire_id=questionnaire_id)
                path_questionnaire = "%s_%s" % (str(questionnaire_code), questionnaire_title)
                error_msg, export_path = create_directory(path_per_questionnaire, path_questionnaire)
                if error_msg != "":
                    return error_msg

                export_filename = "%s_%s.csv" % (questionnaire["prefix_filename_responses"], str(questionnaire_code))

                export_directory = path.join(export_per_questionnaire_directory, path_questionnaire)

                complete_filename = path.join(export_path, export_filename)

                save_to_csv(complete_filename, fields_description)

                # create questionnaire fields file ("fields.csv")
                fields = self.get_questionnaire_fields(questionnaire_id)

                questionnaire_fields = self.create_questionnaire_explanation_fields_file(questionnaire_id, language,
                                                                                         questionnaire_lime_survey,
                                                                                         fields)

                self.files_to_zip_list.append([complete_filename, export_directory])

                export_filename = "%s_%s.csv" % (questionnaire["prefix_filename_fields"], str(questionnaire_code))

                export_directory = path.join(export_metadata_directory, path_questionnaire)

                complete_filename = path.join(export_path, export_filename)

                save_to_csv(complete_filename, questionnaire_fields)

                self.files_to_zip_list.append([complete_filename, export_directory])

        questionnaire_lime_survey.release_session_key()

        return error_msg
コード例 #44
0
ファイル: views.py プロジェクト: neuromat/nes
def survey_update_sensitive_questions(request, survey_id, template_name="survey/survey_sensitive_fields.html"):
    survey = get_object_or_404(Survey, pk=survey_id)

    surveys = Questionnaires()
    language = get_questionnaire_language(surveys, survey.lime_survey_id, request.LANGUAGE_CODE)
    survey_title = surveys.get_survey_title(survey.lime_survey_id, language)
    limesurvey_available = check_limesurvey_access(request, surveys)

    current_selected_fields = SensitiveQuestion.objects.filter(survey=survey)

    field_code = get_survey_header(surveys, survey, language, 'code')
    field_text = get_survey_header(surveys, survey, language, 'full')

    surveys.release_session_key()

    counter = 0
    available_fields = []
    while counter < len(field_code):
        if field_code[counter] not in questionnaire_evaluation_fields_excluded:
            available_fields.append(
                {'code': field_code[counter],
                 'text': field_text[counter],
                 'checked': current_selected_fields.filter(code=field_code[counter]).exists()})
        counter += 1

    if request.method == "POST":
        if request.POST['action'] == "save":

            changed = False

            # for each available fields
            for field in available_fields:

                field_code = field['code']
                field_text = field['text']

                # field was selected
                if "field_" + str(field_code) in request.POST and request.POST["field_" + str(field_code)] == 'on':

                    # field not in current configuration
                    if not current_selected_fields.filter(code=str(field_code)).exists():
                        # Add itens selected
                        SensitiveQuestion.objects.create(survey=survey, code=field_code, question=field_text)
                        changed = True
                else:

                    # field in current configuration
                    if current_selected_fields.filter(code=str(field_code)).exists():
                        current_selected_fields.filter(code=str(field_code)).delete()
                        changed = True

            # Exclude unknown items
            unkown_items = SensitiveQuestion.objects.filter(survey=survey).exclude(
                code__in=[field['code'] for field in available_fields])
            if unkown_items.exists():
                unkown_items.delete()
                changed = True

            if changed:
                messages.success(request, _('Questionnaire was updated successfully.'))
            else:
                messages.success(request, _('There are no changes to save.'))

            redirect_url = reverse("survey_view", args=(survey.id,))
            return HttpResponseRedirect(redirect_url)

    context = {
        "available_fields": available_fields,
        "limesurvey_available": limesurvey_available,
        "survey": survey,
        "survey_title": survey_title}

    return render(request, template_name, context)
コード例 #45
0
def get_questionnaire_fields(questionnaire_code_list, language_current="pt-BR"):
    """
    :param questionnaire_code_list: list with questionnaire id to be formatted with json file
    :return: 1 list: questionnaires_included - questionnaire_id that was included in the .txt file

    """

    questionnaires_included = []

    questionnaire_lime_survey = Questionnaires()
    for questionnaire in questionnaire_code_list:

        questionnaire_id = questionnaire["sid"]

        language_new = get_questionnaire_language(questionnaire_lime_survey, questionnaire_id, language_current)

        # get a valid token (anyone)
        survey = Survey.objects.filter(lime_survey_id=questionnaire_id).first()
        token_id = QuestionnaireResponse.objects.filter(survey=survey).first().token_id
        token = questionnaire_lime_survey.get_participant_properties(questionnaire_id, token_id, "token")

        responses_string = questionnaire_lime_survey.get_header_response(questionnaire_id, language_new, token)

        questionnaire_title = questionnaire_lime_survey.get_survey_title(questionnaire_id, language_new)

        # print("id: %d " % questionnaire_id)

        if not isinstance(responses_string, dict):

            record_question = {'sid': questionnaire_id, "title": questionnaire_title, "output_list": []}

            questionnaire_questions = perform_csv_response(responses_string)

            responses_full = questionnaire_lime_survey.get_header_response(questionnaire_id,
                                                                           language_new, token, heading_type='full')
            questionnaire_questions_full = perform_csv_response(responses_full)

            index = 0
            # line 0 - header information
            for question in questionnaire_questions[0]:
                if question not in questionnaire_evaluation_fields_excluded:

                    # properties = questionnaire_lime_survey.get_question_properties(question, language)

                    # record_question["output_list"].append({"field": question,
                    #                                        "header": question})

                    description = questionnaire_questions_full[0][index]

                    # if len(description)+3+len(question) > 120:
                    #     length = 120 - (3+len(question))
                    #
                    #     description_part1 = description[:length-30]
                    #     description_part2 = description[-25:]
                    #     description = description_part1 + "..." + description_part2

                    record_question["output_list"].append({"field": question,
                                                           "header": question,
                                                           "description": description
                                                           })

                index += 1

            questionnaires_included.append(record_question)

    questionnaire_lime_survey.release_session_key()

    return questionnaires_included
コード例 #46
0
def get_questionnaire_responses(language_code, lime_survey_id, token_id,
                                request):

    groups_of_questions = []

    surveys = Questionnaires()
    token = surveys.get_participant_properties(lime_survey_id, token_id,
                                               "token")
    question_properties = []
    groups = surveys.list_groups(lime_survey_id)

    # defining language to be showed
    languages = surveys.get_survey_languages(lime_survey_id)

    # language to be showed can be the base language, or...
    language = languages['language']

    # ...can be one of the additional languages
    if language.lower() != language_code.lower(
    ) and languages['additional_languages']:

        # search for the right language in addional languages,
        # considering that the LimeSurvey uses upper case in the two-letter language code, like en-US and pt-BR.
        additional_languages_list = languages['additional_languages'].split(
            ' ')
        additional_languages_list_lower = [
            item.lower() for item in additional_languages_list
        ]
        if language_code.lower() in additional_languages_list_lower:
            index = additional_languages_list_lower.index(
                language_code.lower())
            language = additional_languages_list[index]

    survey_title = surveys.get_survey_title(lime_survey_id, language)

    if not isinstance(groups, dict):

        for group in groups:
            if 'id' in group and group['id']['language'] == language:

                question_list = surveys.list_questions(lime_survey_id,
                                                       group['id'])
                question_list = sorted(question_list)

                for question in question_list:

                    properties = surveys.get_question_properties(
                        question, group['id']['language'])

                    # cleaning the question field
                    properties['question'] = re.sub(
                        '{.*?}', '', re.sub('<.*?>', '',
                                            properties['question']))
                    properties['question'] = properties['question'].replace(
                        '&nbsp;', '').strip()

                    is_purely_formula = (properties['type']
                                         == '*') and (properties['question']
                                                      == '')

                    if not is_purely_formula and properties['question'] != '':

                        if isinstance(properties['subquestions'], dict):
                            question_properties.append({
                                'gid':
                                group['id']['gid'],
                                'group_name':
                                group['group_name'],
                                'qid':
                                question,
                                'question':
                                properties['question'],
                                'question_id':
                                properties['title'],
                                'answer_options':
                                'super_question',
                                'type':
                                'X',  # properties['type'],
                                'other':
                                False,
                                'attributes_lang':
                                properties['attributes_lang'],
                                'hidden':
                                'hidden' in properties['attributes']
                                and properties['attributes']['hidden'] == '1'
                            })
                            for key, value in sorted(
                                    properties['subquestions'].items()):
                                question_properties.append({
                                    'gid':
                                    group['id']['gid'],
                                    'group_name':
                                    group['group_name'],
                                    'qid':
                                    question,
                                    'question':
                                    value['question'],
                                    'question_id':
                                    properties['title'] + '[' +
                                    value['title'] + ']',
                                    'answer_options':
                                    properties['answeroptions'],
                                    'type':
                                    properties['type'],
                                    'other':
                                    False,
                                    'attributes_lang':
                                    properties['attributes_lang'],
                                    'hidden':
                                    'hidden' in properties['attributes'] and
                                    properties['attributes']['hidden'] == '1'
                                })
                            if properties['other'] == 'Y':
                                question_properties.append({
                                    'gid':
                                    group['id']['gid'],
                                    'group_name':
                                    group['group_name'],
                                    'qid':
                                    question,
                                    'question':
                                    _('Other'),
                                    'question_id':
                                    properties['title'] + '[other]',
                                    'answer_options':
                                    properties['answeroptions'],
                                    'type':
                                    properties['type'],
                                    'other':
                                    True,
                                    'attributes_lang':
                                    properties['attributes_lang'],
                                    'hidden':
                                    'hidden' in properties['attributes'] and
                                    properties['attributes']['hidden'] == '1'
                                })

                        else:
                            question_properties.append({
                                'gid':
                                group['id']['gid'],
                                'group_name':
                                group['group_name'],
                                'qid':
                                question,
                                'question':
                                properties['question'],
                                'question_id':
                                properties['title'],
                                'answer_options':
                                properties['answeroptions'],
                                'type':
                                properties['type'],
                                'other':
                                False,
                                'attributes_lang':
                                properties['attributes_lang'],
                                'hidden':
                                'hidden' in properties['attributes']
                                and properties['attributes']['hidden'] == '1'
                            })
                    else:
                        question_properties.append({
                            'gid':
                            group['id']['gid'],
                            'group_name':
                            group['group_name'],
                            'qid':
                            question,
                            'question':
                            _("Formula") + " (" + properties['title'] + ")",
                            'question_id':
                            properties['title'],
                            'answer_options':
                            properties['answeroptions'],
                            'type':
                            properties['type'],
                            'other':
                            False,
                            'attributes_lang':
                            properties['attributes_lang'],
                            'hidden':
                            False
                        })

        # Reading from Limesurvey and...
        responses_string = surveys.get_responses_by_token(
            lime_survey_id, token, language)

        # ... transforming to a list:
        # response_list[0] has the questions
        #   response_list[1] has the answers
        responses_list = []

        if isinstance(responses_string, bytes):
            reader = csv.reader(StringIO(responses_string.decode()),
                                delimiter=',')

            for row in reader:
                responses_list.append(row)

            previous_question = ''
            last_super_question = ''
            last_super_question_index = []

            # for question in question_properties:
            for response in responses_list[0]:

                questions = []
                for question_prop in question_properties:
                    question_id = question_prop['question_id']
                    if question_id in response:
                        if response.split('[')[0] in question_id:
                            questions.append(question_prop)

                for question in questions:
                    if question and (question['question_id'] !=
                                     previous_question):

                        if not question['hidden']:

                            if isinstance(question['answer_options'], str) and \
                                            question['answer_options'] == "super_question":

                                if question['question'] != '' and question[
                                        'question_id'] != last_super_question:
                                    groups_of_questions = add_questionnaire_response_to_group(
                                        groups_of_questions,
                                        question,
                                        '',
                                        None,
                                        no_response_flag=True)
                                    last_super_question = question[
                                        'question_id']
                                    last_super_question_index = [
                                        len(groups_of_questions) - 1,
                                        len(groups_of_questions[-1]
                                            ['questionnaire_responses']) - 1
                                    ]

                            else:
                                previous_question = question['question_id']

                                answer = ''
                                no_response_flag = False

                                # type 'X' means "Text display"
                                if not question['type'] == 'X':

                                    # type "1" means "Array dual scale"
                                    if question['type'] == '1':

                                        answer_list = []

                                        if question[
                                                'question_id'] + "[1]" in responses_list[
                                                    0]:
                                            index = responses_list[0].index(
                                                question['question_id'] +
                                                "[1]")
                                            answer_options = question[
                                                'answer_options']

                                            answer = question[
                                                'attributes_lang'][
                                                    'dualscale_headerA'] + ": "
                                            if responses_list[1][
                                                    index] in answer_options:
                                                answer_option = answer_options[
                                                    responses_list[1][index]]
                                                answer += answer_option[
                                                    'answer']
                                            else:
                                                # Sem resposta
                                                answer += _('No answer')
                                                no_response_flag = True

                                        answer_list.append(answer)

                                        if question[
                                                'question_id'] + "[2]" in responses_list[
                                                    0]:
                                            index = responses_list[0].index(
                                                question['question_id'] +
                                                "[2]")
                                            answer_options = question[
                                                'answer_options']

                                            answer = question[
                                                'attributes_lang'][
                                                    'dualscale_headerB'] + ": "
                                            if responses_list[1][
                                                    index] in answer_options:
                                                answer_option = answer_options[
                                                    responses_list[1][index]]
                                                answer += answer_option[
                                                    'answer']
                                            else:
                                                # Sem resposta
                                                answer += _('No answer')
                                                no_response_flag = True

                                        answer_list.append(answer)

                                        groups_of_questions = add_questionnaire_response_to_group(
                                            groups_of_questions, question,
                                            answer_list, None,
                                            no_response_flag)
                                    else:

                                        link = ''
                                        if question[
                                                'question_id'] in responses_list[
                                                    0]:

                                            index = responses_list[0].index(
                                                question['question_id'])

                                            answer_options = question[
                                                'answer_options']

                                            if isinstance(
                                                    answer_options, dict):

                                                # type "M" means "Multiple choice"
                                                if question['type'] == 'M':
                                                    answer = responses_list[1][
                                                        index]
                                                    if question['other']:
                                                        if answer == '':
                                                            no_response_flag = True
                                                    else:
                                                        if answer != 'Y':
                                                            no_response_flag = True
                                                else:
                                                    if responses_list[1][
                                                            index] in answer_options:
                                                        answer_option = answer_options[
                                                            responses_list[1]
                                                            [index]]
                                                        answer = answer_option[
                                                            'answer']
                                                    else:
                                                        # Sem resposta
                                                        answer = _('No answer')
                                                        no_response_flag = True
                                            else:
                                                # type "D" means "Date/Time"
                                                if question['type'] == 'D':
                                                    if responses_list[1][
                                                            index]:
                                                        answer = datetime.datetime.strptime(
                                                            responses_list[1]
                                                            [index],
                                                            '%Y-%m-%d %H:%M:%S'
                                                        )
                                                    else:
                                                        answer = ''
                                                        no_response_flag = True
                                                else:

                                                    answer = responses_list[1][
                                                        index]

                                                    # type "M" means "Multiple choice"
                                                    if question['type'] == 'M':
                                                        if question['other']:
                                                            if answer == '':
                                                                no_response_flag = True
                                                        else:
                                                            if answer != 'Y':
                                                                no_response_flag = True

                                                    if question[
                                                            'type'] == '|' and answer:
                                                        link = settings.LIMESURVEY['URL_WEB'] + \
                                                               '/index.php/admin/responses/sa/browse/fieldname/' + \
                                                               str(lime_survey_id) + 'X' + \
                                                               str(question['gid']) + 'X' + \
                                                               str(question['qid']) + \
                                                               '/id/' + responses_list[1][0] + \
                                                               '/surveyid/' + str(lime_survey_id) + \
                                                               '/downloadindividualfile/' + \
                                                               json.loads(answer[1:-1])['name']

                                        groups_of_questions = add_questionnaire_response_to_group(
                                            groups_of_questions, question,
                                            answer, link, no_response_flag)

                                        # checking if the super-question should be unmarked
                                        if last_super_question and not no_response_flag \
                                                and question['question_id'].split('[')[0] == last_super_question:
                                            mark_off_super_question(
                                                groups_of_questions,
                                                last_super_question_index)
        else:
            messages.error(
                request,
                _("LimeSurvey did not find fill data for this questionnaire."))

    surveys.release_session_key()

    return survey_title, groups_of_questions
コード例 #47
0
ファイル: input_export.py プロジェクト: neuromat/nes
    def build_questionnaire(self, questionnaire_list, language, entrance_questionnaire):
        questionnaire_lime_survey = Questionnaires()
        if "questionnaire_language" not in self.data:
            self.data["questionnaire_language"] = {}

        if entrance_questionnaire:
            self.data["questionnaires"] = []

            for index, sid, title, field_header_list in questionnaire_list:
                output_language = get_questionnaire_language(questionnaire_lime_survey, sid, language)

                languages = questionnaire_lime_survey.get_survey_languages(sid)
                language_list = [languages['language']]
                if languages['additional_languages']:
                    additional_language = languages['additional_languages'].split(' ')
                else:
                    additional_language = ['']
                for item in additional_language:
                    if item != '':
                        language_list.append(item)

                if sid not in self.data["questionnaire_language"]:
                    self.data["questionnaire_language"][sid] = {
                        "language_list": language_list,
                        "output_language": output_language,
                    }

                # if sid not in self.data['questionnaires']:
                self.data["questionnaires"].append({"id": sid,
                                                    "prefix_filename_fields": PREFIX_FILENAME_FIELDS,
                                                    "questionnaire_name": title,
                                                    "prefix_filename_responses": PREFIX_FILENAME_RESPONSES,
                                                    "output_list": [], "responses_list": []})
                for header, field in field_header_list:
                    output_data = {"header": header, "field": field}
                    self.data["questionnaires"][-1]["output_list"].append(output_data)

        else:
            self.data["questionnaires_from_experiments"] = {}

            for index, group_id, sid, title, field_header_list in questionnaire_list:
                output_language = get_questionnaire_language(questionnaire_lime_survey, sid, language)
                languages = questionnaire_lime_survey.get_survey_languages(sid)
                language_list = [languages['language']]
                if languages['additional_languages']:
                    additional_language = languages['additional_languages'].split(' ')
                else:
                    additional_language = ['']
                for item in additional_language:
                    if item != '':
                        language_list.append(item)

                if sid not in self.data["questionnaire_language"]:
                    self.data["questionnaire_language"][sid] = {
                        "language_list": language_list,
                        "output_language": output_language,
                    }

                if group_id not in self.data['questionnaires_from_experiments']:
                    self.data['questionnaires_from_experiments'][group_id] = {}

                if sid not in self.data['questionnaires_from_experiments'][group_id]:
                    self.data['questionnaires_from_experiments'][group_id][sid] = {
                        "prefix_filename_fields": PREFIX_FILENAME_FIELDS,
                        "questionnaire_name": title,
                        "prefix_filename_responses": PREFIX_FILENAME_RESPONSES,
                        "output_list": []
                    }

                for header, field in field_header_list:
                    output_data = {"header": header, "field": field}
                    self.data["questionnaires_from_experiments"][group_id][sid]["output_list"].append(output_data)

                if sid not in self.data["questionnaire_list"]:
                    self.data["questionnaire_list"].append(sid)

        questionnaire_lime_survey.release_session_key()
コード例 #48
0
ファイル: test_views.py プロジェクト: liyifan5923013/nes
    def test_survey_create_with_fileupload_question(self):
        lime_survey = Questionnaires()
        sid = None

        try:
            # Cria uma nova survey no lime survey
            title_survey = 'Questionario de teste'
            sid = lime_survey.add_survey(9999, title_survey, 'en', 'G')

            # Obtenho o titulo da survey
            survey_title = lime_survey.get_survey_title(sid)
            self.assertEqual(survey_title, title_survey)

            # Verifica se esta ativa
            survey_active = lime_survey.get_survey_properties(sid, 'active')
            self.assertEqual(survey_active, 'N')

            # Obtem uma propriedade - Administrador da Survey
            survey_admin = lime_survey.get_survey_properties(sid, 'admin')
            self.assertEqual(survey_admin, None)

            # Importar grupo de questoes
            handle_file_import = \
                open('quiz/static/quiz/tests/limesurvey_groups_with_fileupload.lsg', 'r')
            questions_data = handle_file_import.read()
            questions_id = \
                lime_survey.insert_questions(sid, questions_data, 'lsg')
            self.assertGreaterEqual(questions_id, 1)

            # Inicia tabela de tokens
            self.assertEqual(lime_survey.activate_tokens(sid), 'OK')

            # Ativar survey
            self.assertEqual(lime_survey.activate_survey(sid), 'OK')

            # Verifica se esta ativa
            survey_active = lime_survey.get_survey_properties(sid, 'active')
            self.assertEqual(survey_active, 'Y')

            # Request the survey register screen
            response = self.client.get(reverse('survey_create'))
            self.assertEqual(response.status_code, 200)

            # Set survey data
            # self.data = {'action': 'save', 'title': 'Survey title'}
            self.data = {'action': 'save', 'questionnaire_selected': sid}

            # Count the number of surveys currently in database
            count_before_insert = Survey.objects.all().count()

            # Add the new survey
            response = self.client.post(reverse('survey_create'), self.data)
            self.assertEqual(response.status_code, 302)

            # Assert if the message is parsed
            self.assertEqual(
                response.wsgi_request._messages._queued_messages[0].message,
                'O NES não importa arquivos carregados pelo Limesurvey através de questões do tipo \'Envio de arquivo\'. Veja \"Best Pratices and Recommendations\" em https://nes.rtfd.io para mais detalhes.'
            )

            # Count the number of surveys currently in database
            count_after_insert = Survey.objects.all().count()

            # Check if it has increased
            self.assertEqual(count_after_insert, count_before_insert + 1)

        finally:
            # Deleta a survey gerada no Lime Survey
            self.assertEqual(lime_survey.delete_survey(sid), 'OK')
コード例 #49
0
def export_view(request, template_name="export/export_data.html"):
    export_form = ExportForm(request.POST or None, initial={'title': 'title',
                                                            'responses': ['short'], 'headings': 'code'})
    # , 'per_participant': False,
    #                                                         'per_questinnaire': False})
    # export_form.per_participant = False
    # export_form.per_questionnaire = True

    # context = {}

    # test with pagination
    # a = [{"b": "2", "c": "3"}, {"d": "7", "e": "8"}]
    # b = [1, 2, 3, 4, 5]
    # c = [7, 9, (4, 3, 2)]
    #
    # contact_list = [a, b, c]
    #
    # paginator = Paginator(contact_list, 1)  # Show 1 info per page
    #
    # page = request.GET.get('page')
    # try:
    #     contacts = paginator.page(page)
    # except PageNotAnInteger:
    #     # If page is not an integer, deliver first page.
    #     page = 1
    #     contacts = paginator.page(1)
    # except EmptyPage:
    #     # If page is out of range (e.g. 9999), deliver last page of results.
    #     page = paginator.num_pages
    #     contacts = paginator.page(paginator.num_pages)
    # page = 1
    #
    # if page == 1:

    selected_ev_quest = []
    selected_participant = []
    selected_diagnosis = []

    if request.method == "POST":

        questionnaires_selected_list = request.POST.getlist('to[]')

        questionnaires_list = []

        # fields = {}

        previous_questionnaire_id = 0
        output_list = []
        for questionnaire in questionnaires_selected_list:
            sid, title, field, header = questionnaire.split("*")

            sid = int(sid)    # transform to integer
            #
            # if sid not in fields:
            #     fields[sid] = []
            # fields[sid].append(field)

            if sid != previous_questionnaire_id:
                if previous_questionnaire_id != 0:
                    output_list = []

                questionnaires_list.append([sid, title, output_list])

                previous_questionnaire_id = sid

            output_list.append((field, header))

        # get participants list
        participant_selected_list = request.POST.getlist('patient_selected')

        participants_list = []

        for participant in participant_selected_list:
            participants_list.append(participant.split("*"))

        # get diagnosis list
        diagnosis_selected_list = request.POST.getlist('diagnosis_selected')

        diagnosis_list = []

        for diagnosis in diagnosis_selected_list:
            diagnosis_list.append(diagnosis.split("*"))

        selected_data_available = (len(questionnaires_selected_list) or
                                   len(participant_selected_list) or len(diagnosis_selected_list))

        if selected_data_available:

            if export_form.is_valid():
                print("valid data")

                per_participant = export_form.cleaned_data['per_participant']
                per_questionnaire = export_form.cleaned_data['per_questionnaire']

                heading_type = export_form.cleaned_data['headings']
                responses_type = export_form.cleaned_data['responses']

                questionnaires_list = update_questionnaire_list(questionnaires_list, heading_type,
                                                                request.LANGUAGE_CODE)

                # insert participation_code
                update_participants_list(participants_list, heading_type)
                update_diagnosis_list(diagnosis_list, heading_type)

                # output_filename =
                # "/Users/sueli/PycharmProjects/nes/patientregistrationsystem/qdc/export/json_export_output2.json"

                # MEDIA_ROOT/export/username_id/export_id

                # input_export_file = create_initial_directory(request.user)

                export_instance = create_export_instance(request.user)

                input_export_file = path.join(EXPORT_DIRECTORY,
                                              path.join(str(request.user.id),
                                                        path.join(str(export_instance.id), str(JSON_FILENAME))))

                # copy data to .../media/export/<user_id>/<export_id>/
                input_filename = path.join(settings.MEDIA_ROOT, input_export_file)
                create_directory(settings.MEDIA_ROOT, path.split(input_export_file)[0])

                build_complete_export_structure(per_participant, per_questionnaire,
                                                participants_list, diagnosis_list,
                                                questionnaires_list, responses_type, heading_type,
                                                input_filename, request.LANGUAGE_CODE)

                complete_filename = export_create(request, export_instance.id, input_filename)

                if complete_filename:

                    messages.success(request, _("Export was finished correctly"))

                    # return file to the user

                    # error_message = "a"
                    # return_response = complete_filename
                    #
                    # redirect_url = reverse("export_result", args=(return_response, error_message))
                    # return HttpResponseRedirect(redirect_url )

                    print("antes do fim: httpResponse")

                    zip_file = open(complete_filename, 'rb')
                    response = HttpResponse(zip_file, content_type='application/zip')
                    response['Content-Disposition'] = 'attachment; filename="export.zip"'
                    response['Content-Length'] = path.getsize(complete_filename)
                    return response
                else:
                    messages.error(request, _("Export data was not generated."))

            else:
                for questionnaire in questionnaires_list:
                    for field in questionnaire[2]:  # get output_list
                        selected_ev_quest.append((questionnaire[0], field[0]))

                for participant in participants_list:
                    selected_participant.append(participant[0])

                for diagnosis in diagnosis_list:
                    selected_diagnosis.append(diagnosis[0])
        else:
            messages.error(request, _("No data was select. Export data was not generated."))

    # else:
    # page 1 - list of questionnaires
    surveys = Questionnaires()
    limesurvey_available = check_limesurvey_access(request, surveys)

    questionnaires_list = []

    if limesurvey_available:
        questionnaires_list = surveys.find_all_active_questionnaires()

    surveys.release_session_key()

    questionnaires_list_final = []

    # removing surveys that are not entrance evaluation
    # entrance_evaluation_questionnaires = QuestionnaireResponse.objects.all()
    entrance_evaluation_questionnaire_ids_list = set(QuestionnaireResponse.objects.values_list('survey',
                                                                                               flat=True))

    # ev_questionnaire_ids_list = entrance_evaluation_questionnaires.values_list("survey")
    surveys_with_ev_list = Survey.objects.filter(id__in=entrance_evaluation_questionnaire_ids_list)

    for survey in surveys_with_ev_list:
        for questionnaire in questionnaires_list:
            if survey.lime_survey_id == questionnaire['sid']:
                questionnaires_list_final.append(questionnaire)
                break

    # page 2 fields

    # entrance evaluation questionnarie fields

    # if len(language_code) > 2:
    #     language_code = "{}-{}".format(language_code[:2],language_code[-2:].upper())

    questionnaires_fields_list = get_questionnaire_fields(questionnaires_list_final, request.LANGUAGE_CODE)

    if len(selected_ev_quest):
        questionnaire_ids, field_id = zip(*selected_ev_quest)
    else:
        questionnaire_ids = ()

    for questionnaire in questionnaires_fields_list:
        questionnaire["selected_counter"] = questionnaire_ids.count(questionnaire["sid"])
        for output_list in questionnaire["output_list"]:
            if (questionnaire["sid"], output_list["field"]) in selected_ev_quest:
                output_list["selected"] = True


    # for field in questionnaires_fields_list:
    #     for questionnaire in questionnaires_list_final:
    #         if field["sid"] == questionnaire['sid']:
    #             field["title"] = questionnaire["surveyls_title"]
    #             break

    context = {

        "limesurvey_available": limesurvey_available,
        "export_form": export_form,
        # "questionnaires_list": questionnaires_list_final,
        # "contacts": contacts,
        "patient_fields": patient_fields,
        "diagnosis_fields": diagnosis_fields,
        "questionnaires_fields_list": questionnaires_fields_list,
        "selected_ev_quest": selected_ev_quest,
        "selected_participant": selected_participant,
        "selected_diagnosis": selected_diagnosis,
    }

    # elif page == 2:

    return render(request, template_name, context)
コード例 #50
0
ファイル: views.py プロジェクト: neuromat/nes
def get_questionnaire_responses(language_code, lime_survey_id, token_id,
                                request):

    groups_of_questions = []

    surveys = Questionnaires()
    token = surveys.get_participant_properties(
        lime_survey_id, token_id, "token"
    )
    question_properties = []
    groups = surveys.list_groups(lime_survey_id)

    # defining language to be showed
    languages = surveys.get_survey_languages(lime_survey_id)

    # language to be showed can be the base language, or...
    language = languages['language']

    # ...can be one of the additional languages
    if language.lower() != language_code.lower() and \
            languages['additional_languages']:

        # search for the right language in addional languages,
        # considering that the LimeSurvey uses upper case in the two-letter
        # language code, like en-US and pt-BR.
        additional_languages_list = languages['additional_languages'].split(' ')
        additional_languages_list_lower = [item.lower() for item in additional_languages_list]
        if language_code.lower() in additional_languages_list_lower:
            index = additional_languages_list_lower.index(language_code.lower())
            language = additional_languages_list[index]

    survey_title = surveys.get_survey_title(lime_survey_id, language)

    if not isinstance(groups, dict):
        for group in groups:
            if 'id' in group and group['id']['language'] == language:

                question_list = surveys.list_questions(
                    lime_survey_id, group['id']['gid']
                )
                question_list = sorted(question_list)

                for question in question_list:

                    properties = surveys.get_question_properties(
                        question, group['id']['language']
                    )

                    # cleaning the question field
                    properties['question'] = re.sub(
                        '{.*?}', '',
                        re.sub('<.*?>', '', properties['question'])
                    )
                    properties['question'] = \
                        properties['question'].replace('&nbsp;', '').strip()

                    is_purely_formula = (properties['type'] == '*') and \
                                        (properties['question'] == '')

                    if not is_purely_formula and properties['question'] != '':
                        if isinstance(properties['subquestions'], dict):
                            question_properties.append({
                                'gid': group['id']['gid'],
                                'group_name': group['group_name'],
                                'qid': question,
                                'question': properties['question'],
                                'question_id': properties['title'],
                                'answer_options': 'super_question',
                                'type': 'X',
                                'other': False,
                                'attributes_lang': properties['attributes_lang'],
                                'hidden': 'hidden' in properties['attributes'] and
                                          properties['attributes']['hidden'] == '1'
                            })
                            for key, value in sorted(
                                    properties['subquestions'].items()
                            ):
                                question_properties.append({
                                    'gid': group['id']['gid'],
                                    'group_name': group['group_name'],
                                    'qid': question,
                                    'question': value['question'],
                                    'question_id':
                                        properties['title'] +
                                        '[' + value['title'] + ']',
                                    'answer_options':
                                        properties['answeroptions'],
                                    'type': properties['type'],
                                    'other': False,
                                    'attributes_lang':
                                        properties['attributes_lang'],
                                    'hidden': 'hidden' in
                                              properties['attributes'] and
                                              properties['attributes']['hidden'] == '1'
                                })
                            if properties['other'] == 'Y':
                                question_properties.append({
                                    'gid': group['id']['gid'],
                                    'group_name': group['group_name'],
                                    'qid': question,
                                    'question': _('Other'),
                                    'question_id':
                                        properties['title'] + '[other]',
                                    'answer_options':
                                        properties['answeroptions'],
                                    'type': properties['type'],
                                    'other': True,
                                    'attributes_lang':
                                        properties['attributes_lang'],
                                    'hidden': 'hidden' in
                                              properties['attributes'] and
                                              properties['attributes']['hidden'] == '1'
                                })

                        else:
                            question_properties.append({
                                'gid': group['id']['gid'],
                                'group_name': group['group_name'],
                                'qid': question,
                                'question': properties['question'],
                                'question_id': properties['title'],
                                'answer_options': properties['answeroptions'],
                                'type': properties['type'],
                                'other': False,
                                'attributes_lang': properties['attributes_lang'],
                                'hidden': 'hidden' in
                                          properties['attributes'] and
                                          properties['attributes']['hidden'] == '1'
                            })
                    else:
                        question_properties.append({
                            'gid': group['id']['gid'],
                            'group_name': group['group_name'],
                            'qid': question,
                            'question':
                                _("Formula") +
                                " (" + properties['title'] + ")",
                            'question_id': properties['title'],
                            'answer_options': properties['answeroptions'],
                            'type': properties['type'],
                            'other': False,
                            'attributes_lang': properties['attributes_lang'],
                            'hidden': False
                        })

        # Reading from Limesurvey and...
        responses_string = surveys.get_responses_by_token(
            lime_survey_id, token, language
        )

        # ... transforming to a list:
        responses_list = []

        if isinstance(responses_string, bytes):
            reader_ = csv.reader(
                StringIO(responses_string.decode()), delimiter=','
            )

            for row in reader_:
                responses_list.append(row)

            previous_question = ''
            last_super_question = ''
            last_super_question_index = []

            # for question in question_properties:
            for response in responses_list[0]:
                questions = []
                for question_prop in question_properties:
                    question_id = question_prop['question_id']
                    if question_id in response:
                        if response.split('[')[0] in question_id:
                            questions.append(question_prop)
                for question in questions:
                    if question and \
                            (question['question_id'] != previous_question):
                        if not question['hidden']:
                            if isinstance(question['answer_options'], str) \
                                    and question['answer_options'] == \
                                    "super_question":
                                if question['question'] != '' and \
                                        question['question_id'] != \
                                        last_super_question:
                                    groups_of_questions = \
                                        add_questionnaire_response_to_group(
                                            groups_of_questions, question, '',
                                            None, no_response_flag=True
                                        )
                                    last_super_question = \
                                        question['question_id']
                                    last_super_question_index = [
                                        len(groups_of_questions) - 1,
                                        len(groups_of_questions[-1]
                                            ['questionnaire_responses']) - 1]

                            else:
                                previous_question = question['question_id']

                                answer = ''
                                no_response_flag = False

                                # type 'X' means "Text display"
                                if not question['type'] == 'X':
                                    # type "1" means "Array dual scale"
                                    if question['type'] == '1':
                                        answer_list = []
                                        if question['question_id'] + "[1]" in \
                                                responses_list[0]:
                                            index = \
                                                responses_list[0].index(
                                                    question['question_id'] + "[1]"
                                                )
                                            answer_options = \
                                                question['answer_options']
                                            answer = \
                                                question['question_id'] + "[1]: "
                                            if responses_list[1][index] in \
                                                    answer_options:
                                                answer_option = \
                                                    answer_options[responses_list[1][index]]
                                                answer += \
                                                    answer_option['answer']
                                            else:
                                                # Sem resposta
                                                answer += _('No answer')
                                                no_response_flag = True

                                        answer_list.append(answer)

                                        if question['question_id'] + "[2]" in \
                                                responses_list[0]:
                                            index = \
                                                responses_list[0].index(
                                                    question['question_id'] + "[2]"
                                                )
                                            answer_options = \
                                                question['answer_options']
                                            answer = \
                                                question['question_id'] + "[2]: "
                                            if responses_list[1][index] in \
                                                    answer_options:
                                                answer_option = \
                                                    answer_options[responses_list[1][index]]
                                                answer += \
                                                    answer_option['answer']
                                            else:
                                                # sem resposta
                                                answer += _('No answer')
                                                no_response_flag = True

                                        answer_list.append(answer)

                                        groups_of_questions =\
                                            add_questionnaire_response_to_group(
                                                groups_of_questions, question,
                                                answer_list, None,
                                                no_response_flag
                                            )
                                    else:
                                        link = ''
                                        if question['question_id'] in \
                                                responses_list[0]:
                                            index = \
                                                responses_list[0].index(question['question_id'])
                                            answer_options = question['answer_options']
                                            if isinstance(answer_options, dict):
                                                # type "M" means "Multiple
                                                # choice"
                                                if question['type'] == 'M':
                                                    answer = responses_list[1][index]
                                                    if question['other']:
                                                        if answer == '':
                                                            no_response_flag = True
                                                    else:
                                                        if answer != 'Y':
                                                            no_response_flag = True
                                                else:
                                                    if responses_list[1][index] in \
                                                            answer_options:
                                                        answer_option = \
                                                            answer_options[responses_list[1][index]]
                                                        answer = \
                                                            answer_option['answer']
                                                    else:
                                                        # sem resposta
                                                        answer = _('No answer')
                                                        no_response_flag = True
                                            else:
                                                # type "D" means "Date/Time"
                                                if question['type'] == 'D':
                                                    if responses_list[1][index]:
                                                        answer = \
                                                            datetime.datetime.strptime(
                                                                responses_list[1][index],
                                                                '%Y-%m-%d %H:%M:%S'
                                                            )
                                                    else:
                                                        answer = ''
                                                        no_response_flag = True
                                                else:

                                                    answer = responses_list[1][index]

                                                    # type "M" means "Multiple choice"
                                                    if question['type'] == 'M':
                                                        if question['other']:
                                                            if answer == '':
                                                                no_response_flag = True
                                                        else:
                                                            if answer != 'Y':
                                                                no_response_flag = True

                                        # not show fileupload questions
                                        if question['type'] != '|':
                                            groups_of_questions = \
                                                add_questionnaire_response_to_group(
                                                    groups_of_questions, question,
                                                    answer, link, no_response_flag
                                                )

                                        # checking if the super-question
                                        # should be unmarked
                                        if last_super_question and not \
                                                no_response_flag and \
                                                question['question_id'].split('[')[0] \
                                                == last_super_question:
                                            mark_off_super_question(
                                                groups_of_questions,
                                                last_super_question_index
                                            )
        else:
            messages.error(
                request,
                _("LimeSurvey did not find fill data for this questionnaire.")
            )

    surveys.release_session_key()

    return survey_title, groups_of_questions
コード例 #51
0
ファイル: test_views.py プロジェクト: neuromat/nes
 def test_find_questionnaire_by_id_method_not_found_survey_by_string(self):
     questionnaires = Questionnaires()
     self.assertEqual(None, questionnaires.find_questionnaire_by_id('three'))
     questionnaires.release_session_key()
コード例 #52
0
ファイル: test_views.py プロジェクト: neuromat/nes
 def test_find_questionnaire_by_id_method_found_survey(self):
     questionnaires = Questionnaires()
     list_survey = self.server.list_surveys(self.session_key, None)
     self.server.release_session_key(self.session_key)
     self.assertEqual(questionnaires.find_questionnaire_by_id(list_survey[3]['sid']), list_survey[3])
     questionnaires.release_session_key()