Exemplo n.º 1
0
def view_grade_question(request):
    """
    This view lets a teacher grade a question that can not be graded automatically
    """
    if 'user' not in request.session.keys() or 'teacher' not in request.session['user']['roles'] :
        return HTTPFound(location='/')  
    main = get_renderer('templates/master.pt').implementation()
    if "current_test" in request.session.keys():
        request.session.pop('current_test')
    question_id = int(request.GET["id"])
    dbsession = DBSession()
    answer = dbsession.query(TakenAnswer).filter(TakenAnswer.id==question_id).first()
    question = dbsession.query(Question).filter(Question.id==answer.question_id).first()
    taken_test = dbsession.query(TakenTest).filter(TakenTest.id==answer.takentest_id).first()
    test = dbsession.query(Test).filter(Test.id == taken_test.test_id).first()
    section = dbsession.query(Section).filter(Section.id == test.course).first()
    num_questions = len(dbsession.query(Question).filter(Question.test_id == test.id).all())
    post = request.POST
    if 'correct' in post or 'incorrect' in post:
        if not answer.graded:
            answer.graded = True
            taken_test.number_graded_questions += 1
            dbsession.flush()
        if 'correct' in post and not answer.correct:
            answer.correct = True
            taken_test.correct_graded_questions += 1
            dbsession.flush()
        if 'incorrect' in post and answer.correct:
            answer.correct = False
            taken_test.correct_graded_questions -= 1
            dbsession.flush()
        if taken_test.number_graded_questions == num_questions:
            new_grade = int(((taken_test.correct_graded_questions*1.0)/ num_questions)*100)
            #submit grade to school tool
            server = ServerProxy(serverLocation, transport = trans)
            grade = server.check_grade(test.term_id, section.course_id, 
                                taken_test.username, test.schooltool_id)
            if grade == None or grade < new_grade or grade == '':
                server.submit_grade(test.term_id, section.course_id, 
                                taken_test.username, test.schooltool_id, new_grade)
                taken_test.has_ungraded = False
            return HTTPFound(location='/ungraded_tests?id='+str(test.id))
        return HTTPFound(location='/grade_submitted_test?id='+str(taken_test.id))


    messages = []
    messages.append("Question Number "+str(question.question_num))
    messages.append("Question: "+question.question)
    messages.append(taken_test.student_name+ "'s answer: " + answer.user_answer)
    class gradeForm(colander.Schema):
        pass
    schema = gradeForm()
    form = deform.Form(schema, buttons=('correct','incorrect'))
    return {"message": messages, "form": form.render(), 'main': main}
Exemplo n.º 2
0
def view_grade_test(request):
    """
    This view grades tests and reports the grade in the grade page.
    """
    if authenticated_userid(request) == None or 'user' not in request.session.keys():
        return HTTPFound(location='/')
    main = get_renderer('templates/master.pt').implementation()
    
    ###load the test and it's questions froms the database###
    test_id = int(request.GET["id"])
    dbsession = DBSession()
    test = dbsession.query(Test).filter(Test.id==test_id).first()
    section = dbsession.query(Section).filter(Section.id == test.course).first()
    questions = dbsession.query(Question).filter(
                                    Question.test_id==test.id).all()
    num_questions = len(questions)

    ###create the "current_test" in session if it is not already there###
    if "current_test" not in request.session.keys():
        return HTTPFound(location='/test?id='+str(test.id))
    current_test = request.session["current_test"]

    ###grade the test submitted test###
    correct = 0
    num_graded = 0
    taken_tests = dbsession.query(TakenTest).filter(TakenTest.test_id == test.id).all()
    attempts = 1
    for t in taken_tests:
        if t.username == request.session['user']['name']:
            attempts = t.attempts + 1
            taken_test = dbsession.query(TakenTest).filter(TakenTest.id == t.id).first()
            taken_test.attempts = attempts
            dbsession.flush()
            taken_answers = dbsession.query(TakenAnswer).filter(
                                            TakenAnswer.takentest_id == taken_test.id).all()
            for answer in taken_answers:
                dbsession.delete(answer)
                dbsession.flush()
    if attempts == 1:
        taken_test = TakenTest(test.id, request.session['user']['name'], 
                               request.session['user']['name'],  num_graded,
                               correct, False, attempts)

        dbsession.add(taken_test)
        dbsession.flush()
    session = request.session
    question_messages = [] #quesiton_messages will contain reports for the
                           #template about if each quesiton is correct or not
    ungraded_questions = []
    for question in questions:#loop through and grade each question
        if question.graded: num_graded += 1
        q_num = question.question_num
        if str(q_num) in current_test.keys(): #make sure user selected
                                            #an answer to the question
            user_answer=current_test[str(q_num)]
                                            #get users answer for the question
            grade = grade_question(question, dbsession, user_answer)
            if type(user_answer) == type([]):
                for answer in user_answer:
                    takenAnswer = TakenAnswer(taken_test.id, question, answer, question.graded, grade[0])
                    dbsession.add(takenAnswer)
                if len(user_answer) == 0:
                    takenAnswer = TakenAnswer(taken_test.id, question, None, question.graded, grade[0])
            else:
                takenAnswer = TakenAnswer(taken_test.id, question, user_answer, question.graded, grade[0])
                dbsession.add(takenAnswer)
            dbsession.flush()
            if question.graded:
                if grade[0]:
                    correct += grade[1]
                    question_messages.append(str(q_num)+". Correct")
                else: question_messages.append(str(q_num)+". INCORRECT")
            else:
                taken_test.has_ungraded = True
                dbsession.flush()
                question_messages.append(str(q_num)+". Waiting for teacher to grade.")
        else: question_messages.append(str(q_num)+". INCORRECT")

    taken_test.number_graded_questions = num_graded
    taken_test.correct_graded_questions = correct
    dbsession.flush()
    if not taken_test.has_ungraded:
        server = ServerProxy(serverLocation, transport = trans)
        grade = server.check_grade(test.term_id, section.course_id, 
                                   request.session['user']['name'],
                                   test.schooltool_id)
        new_grade = int(((1.0*correct)/num_graded)*100)
        if grade == None or grade < new_grade:
            server.submit_grade(test.term_id, section.course_id, 
                                request.session['user']['name'],
                                test.schooltool_id, new_grade)

    ###create a report to display the result of the test###
    if num_graded > 0:
        message ="You got "+str(correct)+" out of "+str(num_graded)+" correct"
        message =message +"(" + str(int(1000.0*correct/num_graded)/10.0) + "%)."
        message += " This grade only includes automaticly graded questions."
    else:
        message="There were no graded questions."
    session.pop("current_test") #remove "current_test" from sesssion

    return {"test":test, "message":message,"questions":question_messages, 'main': main}