# Path: pinax/lms/activities/forms.py
# class SurveyForm(forms.Form):
# 
#     def __init__(self, *args, **kwargs):
# 
#         self.questions = kwargs.pop("questions")
#         super(SurveyForm, self).__init__(*args, **kwargs)
# 
#         for question in self.questions:
#             field_class = question["field_class"]
#             kwargs = {
#                 "label": question["label"],
#             }
# 
#             if question.get("help_text"):
#                 kwargs.update({"help_text": question["help_text"]})
# 
#             if question.get("required") is not None:
#                 kwargs.update({"required": question["required"]})
# 
#             kwargs.update(question.get("extra_args", {}))
#             self.fields[question["name"]] = field_class(**kwargs)
# 
# Path: pinax/lms/activities/signals.py



from django.shortcuts import redirect, render
from .forms import SurveyForm
from .signals import activity_completed
    def handle_get_request(self, request):
        if self.data["page"] == len(self.pages):
            self.already_completed_message(request)
            return redirect(self.completed_url)

        questions = self.get_questions()
        form = SurveyForm(questions=questions)
        return self.render(request, form=form)

    def handle_post_request(self, request):
        if self.data["page"] == len(self.pages):
            self.already_completed_message(request)
            return redirect(self.completed_url)

        questions = self.get_questions()
        form = SurveyForm(request.POST, questions=questions)

        if form.is_valid():
            self.session_state.data.update({"answers_%d" % self.data["page"]: form.cleaned_data})
            self.session_state.data.update({"page": self.data["page"] + 1})

            if self.data["page"] == len(self.pages):
                self.session_state.mark_completed()
                self.success_message(request)
                return redirect(self.completed_url)
            else:
                self.session_state.save()

                return redirect(self.activity_url)


# Path: pinax/lms/activities/forms.py
# class SurveyForm(forms.Form):
# 
#     def __init__(self, *args, **kwargs):
# 
#         self.questions = kwargs.pop("questions")
#         super(SurveyForm, self).__init__(*args, **kwargs)
# 
#         for question in self.questions:
#             field_class = question["field_class"]
#             kwargs = {
#                 "label": question["label"],
#             }
# 
#             if question.get("help_text"):
#                 kwargs.update({"help_text": question["help_text"]})
# 
#             if question.get("required") is not None:
#                 kwargs.update({"required": question["required"]})
# 
#             kwargs.update(question.get("extra_args", {}))
#             self.fields[question["name"]] = field_class(**kwargs)
# 
# Path: pinax/lms/activities/signals.py



# Path: pinax/lms/activities/base.py
from django.shortcuts import redirect, render
from .forms import SurveyForm
from .signals import activity_completed

    def handle_get_request(self, request):
        if self.data["page"] == len(self.pages):
            self.already_completed_message(request)
            return redirect(self.completed_url)

        questions = self.get_questions()
        form = SurveyForm(questions=questions)
        return self.render(request, form=form)

    def handle_post_request(self, request):
        if self.data["page"] == len(self.pages):
            self.already_completed_message(request)
            return redirect(self.completed_url)

        questions = self.get_questions()
        form = SurveyForm(request.POST, questions=questions)

        if form.is_valid():
            self.session_state.data.update({"answers_%d" % self.data["page"]: form.cleaned_data})
            self.session_state.data.update({"page": self.data["page"] + 1})

            if self.data["page"] == len(self.pages):
                self.session_state.mark_completed()
                self.success_message(request)
                return redirect(self.completed_url)
            else:
                self.session_state.save()

                return redirect(self.activity_url)


        return self.render(request, form=form)