Ejemplo n.º 1
0
def get_valid_form_groups_or_render_vote_page(request,
                                              course,
                                              preview,
                                              for_rendering_in_modal=False):
    contributions_to_vote_on = course.contributions.all()
    # prevent a user from voting on themselves
    if not preview:
        contributions_to_vote_on = contributions_to_vote_on.exclude(
            contributor=request.user)

    form_groups = OrderedDict()
    for contribution in contributions_to_vote_on:
        questionnaires = contribution.questionnaires.all()
        if not questionnaires.exists():
            continue
        form_groups[contribution] = [
            QuestionnaireVotingForm(request.POST or None,
                                    contribution=contribution,
                                    questionnaire=questionnaire)
            for questionnaire in questionnaires
        ]

    if all(
            all(form.is_valid() for form in form_group)
            for form_group in form_groups.values()):
        assert not preview
        return form_groups, None

    course_form_group = form_groups.pop(course.general_contribution)

    template_data = dict(
        errors_exist=any(
            any(form.errors for form in form_group)
            for form_group in form_groups.values()),
        course_form_group_top=[
            questions_form for questions_form in course_form_group
            if questions_form.questionnaire.is_above_contributors
        ],
        course_form_group_bottom=[
            questions_form for questions_form in course_form_group
            if questions_form.questionnaire.is_below_contributors
        ],
        contributor_form_groups=[
            (contribution.contributor, contribution.label, form_group,
             any(form.errors for form in form_group))
            for contribution, form_group in form_groups.items()
        ],
        course=course,
        participants_warning=course.num_participants <= 5,
        preview=preview,
        vote_end_datetime=course.vote_end_datetime,
        hours_left_for_evaluation=course.time_left_for_evaluation.seconds //
        3600,
        minutes_left_for_evaluation=(course.time_left_for_evaluation.seconds //
                                     60) % 60,
        success_magic_string=SUCCESS_MAGIC_STRING,
        success_redirect_url=reverse('student:index'),
        evaluation_ends_soon=course.evaluation_ends_soon(),
        for_rendering_in_modal=for_rendering_in_modal)
    return None, render(request, "student_vote.html", template_data)
Ejemplo n.º 2
0
def get_vote_page_form_groups(request, evaluation, preview):
    contributions_to_vote_on = evaluation.contributions.all()
    # prevent a user from voting on themselves
    if not preview:
        contributions_to_vote_on = contributions_to_vote_on.exclude(
            contributor=request.user)

    form_groups = OrderedDict()
    for contribution in contributions_to_vote_on:
        questionnaires = contribution.questionnaires.all()
        if not questionnaires.exists():
            continue
        form_groups[contribution] = [
            QuestionnaireVotingForm(request.POST or None,
                                    contribution=contribution,
                                    questionnaire=questionnaire)
            for questionnaire in questionnaires
        ]
    return form_groups
Ejemplo n.º 3
0
def get_valid_form_groups_or_render_vote_page(request, evaluation, preview, for_rendering_in_modal=False):
    contributions_to_vote_on = evaluation.contributions.all()
    # prevent a user from voting on themselves
    if not preview:
        contributions_to_vote_on = contributions_to_vote_on.exclude(contributor=request.user)

    form_groups = OrderedDict()
    for contribution in contributions_to_vote_on:
        questionnaires = contribution.questionnaires.all()
        if not questionnaires.exists():
            continue
        form_groups[contribution] = [QuestionnaireVotingForm(request.POST or None, contribution=contribution, questionnaire=questionnaire) for questionnaire in questionnaires]

    if all(all(form.is_valid() for form in form_group) for form_group in form_groups.values()):
        assert not preview
        return form_groups, None

    evaluation_form_group = form_groups.pop(evaluation.general_contribution)

    contributor_form_groups = [(contribution.contributor, contribution.label, form_group, any(form.errors for form in form_group), textanswers_visible_to(contribution)) for contribution, form_group in form_groups.items()]
    evaluation_form_group_top = [questions_form for questions_form in evaluation_form_group if questions_form.questionnaire.is_above_contributors]
    evaluation_form_group_bottom = [questions_form for questions_form in evaluation_form_group if questions_form.questionnaire.is_below_contributors]
    if not contributor_form_groups:
        evaluation_form_group_top += evaluation_form_group_bottom
        evaluation_form_group_bottom = []

    template_data = dict(
        errors_exist=any(any(form.errors for form in form_group) for form_group in [*(form_groups.values()), evaluation_form_group_top, evaluation_form_group_bottom]),
        evaluation_form_group_top=evaluation_form_group_top,
        evaluation_form_group_bottom=evaluation_form_group_bottom,
        contributor_form_groups=contributor_form_groups,
        evaluation=evaluation,
        small_evaluation_size_warning=evaluation.num_participants <= settings.SMALL_COURSE_SIZE,
        preview=preview,
        success_magic_string=SUCCESS_MAGIC_STRING,
        success_redirect_url=reverse('student:index'),
        for_rendering_in_modal=for_rendering_in_modal,
        general_contribution_textanswers_visible_to=textanswers_visible_to(evaluation.general_contribution),
        text_answer_warnings=TextAnswerWarning.objects.all(),
    )
    return None, render(request, "student_vote.html", template_data)