예제 #1
0
def show_all_quiz(request):
    """Generates a list of all the quizzes
    that are currently in the database."""

    user = request.user
    ci = RequestContext(request)
    if not user.is_authenticated() or not is_moderator(user):
        raise Http404("You are not allowed to view this page !")

    if request.method == "POST" and request.POST.get("delete") == "delete":
        data = request.POST.getlist("quiz")
        if data is None:
            quizzes = Quiz.objects.all()
            context = {"papers": [], "quiz": None, "quizzes": quizzes}
            return render_to_response("show_quiz.html", context, context_instance=ci)
        else:
            for i in data:
                quiz = Quiz.objects.get(id=i).delete()
            quizzes = Quiz.objects.all()
            context = {"papers": [], "quiz": None, "quizzes": quizzes}
            return render_to_response("show_quiz.html", context, context_instance=ci)

    elif request.method == "POST" and request.POST.get("edit") == "edit":
        data = request.POST.getlist("quiz")
        forms = []
        for j in data:
            d = Quiz.objects.get(id=j)
            form = QuizForm()
            form.initial["start_date"] = d.start_date_time.date()
            form.initial["start_time"] = d.start_date_time.time()
            form.initial["end_date"] = d.end_date_time.date()
            form.initial["end_time"] = d.end_date_time.time()
            form.initial["duration"] = d.duration
            form.initial["active"] = d.active
            form.initial["description"] = d.description
            forms.append(form)
        return render_to_response("edit_quiz.html", {"forms": forms, "data": data}, context_instance=ci)
    else:
        quizzes = Quiz.objects.all()
        context = {"papers": [], "quiz": None, "quizzes": quizzes}
        return render_to_response("show_quiz.html", context, context_instance=ci)
예제 #2
0
def add_quiz(request, quiz_id=None):
    """To add a new quiz in the database.
    Create a new quiz and store it."""

    user = request.user
    ci = RequestContext(request)
    if not user.is_authenticated() or not is_moderator(user):
        raise Http404("You are not allowed to view this page!")
    if request.method == "POST":
        form = QuizForm(request.POST)
        if form.is_valid():
            if quiz_id is None:
                form.save()
                return redirect("/manage/")
                # to be changed later
                # return redirect("/manage/designquestionpaper")
            else:
                d = Quiz.objects.get(id=quiz_id)
                sd = datetime.datetime.strptime(form["start_date"].data, "%Y-%m-%d").date()
                st = datetime.datetime.strptime(form["start_time"].data, "%H:%M:%S").time()
                ed = datetime.datetime.strptime(form["end_date"].data, "%Y-%m-%d").date()
                et = datetime.datetime.strptime(form["end_time"].data, "%H:%M:%S").time()
                d.start_date_time = datetime.datetime.combine(sd, st)
                d.end_date_time = datetime.datetime.combine(ed, et)
                d.duration = form["duration"].data
                d.active = form["active"].data
                d.description = form["description"].data
                d.pass_criteria = form["pass_criteria"].data
                d.attempts_allowed = form["attempts_allowed"].data
                d.time_between_attempts = form["time_between_attempts"].data
                d.save()
                # quiz = Quiz.objects.get(id=quiz_id)
                return redirect("/manage/")
                # to be changed later
                # return redirect("/manage/showquiz")
        else:
            return render_to_response("add_quiz.html", {"form": form}, context_instance=ci)
    else:
        if quiz_id is None:
            form = QuizForm()
            return render_to_response("add_quiz.html", {"form": form}, context_instance=ci)
        else:
            d = Quiz.objects.get(id=quiz_id)
            form = QuizForm()
            form.initial["start_date"] = d.start_date_time.date()
            form.initial["start_time"] = d.start_date_time.time()
            form.initial["end_date"] = d.end_date_time.date()
            form.initial["end_time"] = d.end_date_time.time()
            form.initial["duration"] = d.duration
            form.initial["description"] = d.description
            form.initial["active"] = d.active
            form.initial["pass_criteria"] = d.pass_criteria
            form.initial["attempts_allowed"] = d.attempts_allowed
            form.initial["time_between_attempts"] = d.time_between_attempts
            return render_to_response("add_quiz.html", {"form": form}, context_instance=ci)