<s> from django . shortcuts import render <EOL> from django . core import serializers <EOL> from django . http import HttpResponse <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . conf import settings <EOL> import json <EOL> import datetime <EOL> from registrar . models import PeerReview <EOL> from registrar . models import Course <EOL> from registrar . models import Teacher <EOL> from registrar . models import Student <EOL> from registrar . models import Assignment <EOL> from registrar . models import AssignmentSubmission <EOL> from registrar . models import EssayQuestion <EOL> from registrar . models import EssaySubmission <EOL> from registrar . models import MultipleChoiceQuestion <EOL> from registrar . models import MultipleChoiceSubmission <EOL> from registrar . models import ResponseQuestion <EOL> from registrar . models import ResponseSubmission <EOL> from registrar . models import TrueFalseQuestion <EOL> from registrar . models import TrueFalseSubmission <EOL> from student . forms import PeerReviewForm <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def peer_reviews_page ( request , course_id ) : <EOL> course = Course . objects . get ( id = course_id ) <EOL> try : <EOL> assignments = Assignment . objects . filter ( course = course ) . order_by ( '<STR_LIT>' ) <EOL> except Assignment . DoesNotExist : <EOL> assignments = None <EOL> try : <EOL> submissions = AssignmentSubmission . objects . filter ( assignment__course = course ) <EOL> except AssignmentSubmission . DoesNotExist : <EOL> submissions = None <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : course , <EOL> '<STR_LIT>' : assignments , <EOL> '<STR_LIT>' : submissions , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : settings . APPLICATION_HAS_ADVERTISMENT , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_CSS_LIBRARY_URLS , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_JS_LIBRARY_URLS , <EOL> } ) <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def assignment_page ( request , course_id , assignment_id ) : <EOL> course = Course . objects . get ( id = course_id ) <EOL> try : <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> try : <EOL> e_submissions = EssaySubmission . objects . filter ( question__assignment = assignment ) <EOL> except EssaySubmission . DoesNotExist : <EOL> e_submissions = None <EOL> try : <EOL> r_submissions = ResponseSubmission . objects . filter ( question__assignment = assignment ) <EOL> except ResponseSubmission . DoesNotExist : <EOL> r_submissions = None <EOL> except Assignment . DoesNotExist : <EOL> assignment = None <EOL> e_submissions = None <EOL> r_submissions = None <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : course , <EOL> '<STR_LIT>' : assignment , <EOL> '<STR_LIT>' : e_submissions , <EOL> '<STR_LIT>' : r_submissions , <EOL> '<STR_LIT>' : settings . ESSAY_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . MULTIPLECHOICE_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . TRUEFALSE_QUESTION_TYPE , <EOL> '<STR_LIT>' : settings . RESPONSE_QUESTION_TYPE , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : settings . APPLICATION_HAS_ADVERTISMENT , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_CSS_LIBRARY_URLS , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_JS_LIBRARY_URLS , <EOL> } ) <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def peer_review_modal ( request , course_id , assignment_id ) : <EOL> response_data = { } <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = PeerReviewForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : request . POST [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : request . POST [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : request . POST [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_CSS_LIBRARY_URLS , <EOL> '<STR_LIT>' : settings . SB_ADMIN_2_JS_LIBRARY_URLS , <EOL> } ) <EOL> @ login_required ( ) <EOL> def save_peer_review ( request , course_id , assignment_id ) : <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> question_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> question_type = int ( request . POST [ '<STR_LIT>' ] ) <EOL> submission_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> course = Course . objects . get ( id = course_id ) <EOL> try : <EOL> assignment = Assignment . objects . get ( assignment_id = assignment_id ) <EOL> except Assignment . DoesNotExist : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> question = None <EOL> submission = None <EOL> if question_type == settings . RESPONSE_QUESTION_TYPE : <EOL> question = ResponseQuestion . objects . get ( <EOL> assignment = assignment , <EOL> question_id = question_id , <EOL> ) <EOL> submission = ResponseSubmission . objects . get ( <EOL> submission_id = submission_id <EOL> ) <EOL> elif question_type == settings . ESSAY_QUESTION_TYPE : <EOL> question = EssayQuestion . objects . get ( <EOL> assignment = assignment , <EOL> question_id = question_id <EOL> ) <EOL> submission = EssaySubmission . objects . get ( <EOL> submission_id = submission_id <EOL> ) <EOL> if question is None : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' + question_id } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> if submission is None : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> form = PeerReviewForm ( request . POST , request . FILES ) ; <EOL> if form . is_valid ( ) : <EOL> form . instance . user = request . user <EOL> form . save ( ) <EOL> submission . reviews . add ( form . instance ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> else : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : json . dumps ( form . errors ) } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def delete_peer_review ( request , course_id , assignment_id ) : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> review_id = request . POST [ '<STR_LIT>' ] <EOL> try : <EOL> peer_review = PeerReview . objects . get ( review_id = review_id ) <EOL> if peer_review . user == request . user : <EOL> peer_review . delete ( ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> else : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> except PeerReview . DoesNotExist : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required ( ) <EOL> def update_assignment_marks ( request , course_id , submission_id ) : <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> if request . is_ajax ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> course = Course . objects . get ( id = course_id ) <EOL> a_submission = AssignmentSubmission . objects . get ( submission_id = submission_id ) <EOL> e_submissions = EssaySubmission . objects . filter ( <EOL> question__assignment = a_submission . assignment , <EOL> student = a_submission . student , <EOL> ) <EOL> for e_submission in e_submissions : <EOL> process_submission_question ( a_submission , e_submission ) <EOL> r_submissions = ResponseSubmission . objects . filter ( <EOL> question__assignment = a_submission . assignment , <EOL> student = a_submission . student , <EOL> ) <EOL> for r_submission in r_submissions : <EOL> process_submission_question ( a_submission , r_submission ) <EOL> process_submission_assignment ( a_submission ) <EOL> response_data = { '<STR_LIT:status>' : '<STR_LIT:success>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( response_data ) , content_type = "<STR_LIT:application/json>" ) <EOL> def process_submission_question ( a_submission , q_submission ) : <EOL> total_reviews = q_submission . reviews . count ( ) <EOL> if total_reviews == <NUM_LIT:0> : <EOL> q_submission . marks = <NUM_LIT:0> <EOL> q_submission . save ( ) <EOL> return <EOL> marks = <NUM_LIT:0> <EOL> reviewer_weight = <NUM_LIT:1> / total_reviews <EOL> for peer_review in q_submission . reviews . all ( ) : <EOL> weight = peer_review . marks / <NUM_LIT:5> <EOL> reviewer_mark = weight * q_submission . question . marks <EOL> distributed_mark = reviewer_mark * reviewer_weight <EOL> marks += distributed_mark <EOL> q_submission . marks = marks <EOL> q_submission . save ( ) <EOL> def process_submission_assignment ( submission ) : <EOL> assignment = submission . assignment <EOL> student = submission . student <EOL> submission . total_marks = <NUM_LIT:0> <EOL> submission . earned_marks = <NUM_LIT:0> <EOL> e_submissions = EssaySubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for e_submission in e_submissions : <EOL> submission . total_marks += e_submission . question . marks <EOL> submission . earned_marks += e_submission . marks <EOL> mc_submissions = MultipleChoiceSubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for mc_submission in mc_submissions : <EOL> submission . total_marks += mc_submission . question . marks <EOL> submission . earned_marks += mc_submission . marks <EOL> tf_submissions = TrueFalseSubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for tf_submission in tf_submissions : <EOL> submission . total_marks += tf_submission . question . marks <EOL> submission . earned_marks += tf_submission . marks <EOL> r_submissions = ResponseSubmission . objects . filter ( <EOL> student = student , <EOL> question__assignment = assignment , <EOL> ) <EOL> for r_submission in r_submissions : <EOL> submission . total_marks += r_submission . question . marks <EOL> submission . earned_marks += r_submission . marks <EOL> submission . percent = round ( ( submission . earned_marks / submission . total_marks ) * <NUM_LIT:100> ) <EOL> submission . save ( ) </s>
