def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] try: exam = Exam.objects.get(course = course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 record = ExamRecord(course=course, exam=exam, student=request.user, json_data=request.POST['json_data']) record.save() return HttpResponse("Submission has been saved.")
def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] try: exam = Exam.objects.get(course = course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 postdata = request.POST['json_data'] #will return an error code to the user if either of these fail (throws 500) json.loads(postdata) record = ExamRecord(course=course, exam=exam, student=request.user, json_data=request.POST['json_data']) record.save() return HttpResponse("Submission has been saved.")
def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] try: exam = Exam.objects.get(course=course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 postdata = request.POST[ 'json_data'] #will return an error code to the user if either of these fail (throws 500) json.loads(postdata) record = ExamRecord(course=course, exam=exam, student=request.user, json_data=request.POST['json_data']) record.save() return HttpResponse("Submission has been saved.")
def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] try: exam = Exam.objects.get(course = course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 postdata = request.POST['json_data'] #will return an error code to the user if either of these fail (throws 500) json_obj=json.loads(postdata) record = ExamRecord(course=course, exam=exam, student=request.user, json_data=postdata) record.save() if exam.autograde: try: autograder = AutoGrader(exam.xml_metadata) except Exception as e: #Pass back all the exceptions so user can see return HttpResponseBadRequest(unicode(e)) feedback = {} for prob,v in json_obj.iteritems(): try: if isinstance(v,list): submission = map(lambda li: li['value'], v) print(submission) feedback[prob] = autograder.grade(prob, submission) else: submission = float(v['value']) print(submission) feedback[prob] = autograder.grade(prob, submission) except ValueError: feedback[prob] = False except AutoGraderGradingException: pass return HttpResponse(json.dumps(feedback)) else: return HttpResponse("Submission has been saved.")
def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] user = request.user try: exam = Exam.objects.get(course = course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 postdata = request.POST['json_data'] #will return an error code to the user if either of these fail (throws 500) json_obj=json.loads(postdata) if exam.mode == "ready" and exam.past_all_deadlines(): return HttpResponseBadRequest("Sorry! This submission is past the last deadline of %s" % \ datetime.datetime.strftime(exam.partial_credit_deadline, "%m/%d/%Y %H:%M PST")); attempt_number = exam.num_of_student_records(user)+1 onpage = request.POST.get('onpage','') record = ExamRecord(course=course, exam=exam, student=user, json_data=postdata, onpage=onpage, attempt_number=attempt_number, late=exam.past_due()) record.save() autograder = None if exam.exam_type == "survey": autograder = AutoGrader("<null></null>", default_return=True) #create a null autograder that always returns the "True" object elif exam.autograde: try: autograder = AutoGrader(exam.xml_metadata) except Exception as e: #Pass back all the exceptions so user can see return HttpResponseBadRequest(unicode(e)) if autograder: record_score = ExamRecordScore(record = record) record_score.save() feedback = {} total_score = 0 for prob,v in json_obj.iteritems(): #prob is the "input" id, v is the associated value, #which can be an object (input box) or a list of objects (multiple-choice) try: if isinstance(v,list): #multiple choice case submission = map(lambda li: li['value'], v) feedback[prob] = autograder.grade(prob, submission) field_obj = ExamRecordScoreField(parent=record_score, field_name = prob, human_name=v[0].get('questionreport', "") if len(v)>0 else "", subscore = feedback[prob]['score'], value = map(lambda li:li.encode('utf-8'),submission), correct = feedback[prob]['correct'], comments="", associated_text = v[0].get('associatedText', "") if len(v)>0 else "", ) field_obj.save() for li in v: if 'correct_choices' not in feedback[prob]: is_correct = None else: is_correct = li['value'] in feedback[prob]['correct_choices'] fc = ExamRecordScoreFieldChoice(parent=field_obj, choice_value=li['value'], correct=is_correct, human_name=li.get('report',""), associated_text=li.get('associatedText',"")) fc.save() else: #single answer submission = v['value'] feedback[prob] = autograder.grade(prob, submission) field_obj = ExamRecordScoreField(parent=record_score, field_name = prob, human_name=v.get('report', ""), subscore = feedback[prob]['score'], value = submission, correct = feedback[prob]['correct'], comments="", associated_text = v.get('associatedText', "")) field_obj.save() except AutoGraderGradingException as e: feedback[prob]={'correct':False, 'score':0} field_obj = ExamRecordScoreField(parent=record_score, field_name = prob, human_name=v.get('report', ""), subscore = 0, correct = feedback[prob]['correct'], comments = unicode(e), associated_text = v.get('associatedText', "")) field_obj.save() #This is when using code indents to denote blocks is a bit hairy #supposed to be at the same level as try...except. Run once per prob,v total_score += feedback[prob]['score'] #Set raw score for ExamRecordScore record_score.raw_score = total_score record_score.save() #Set penalty inclusive score for ExamRecord record.json_score_data = json.dumps(feedback) #apply resubmission penalty resubmission_penalty_percent = pow(((100 - exam.resubmission_penalty)/100), (attempt_number -1)) total_score = max(total_score * resubmission_penalty_percent, 0) #apply the late penalty if exam.grace_period and exam.late_penalty > 0 and datetime.datetime.now() > exam.grace_period: total_score = max(total_score * ((100 - exam.late_penalty)/100), 0) record.score = total_score record.save() #Set ExamScore.score to max of ExamRecord.score for that student, exam. exam_score, created = ExamScore.objects.get_or_create(course=course, exam=exam, student=user) exam_score.setScore() return HttpResponse(reverse(exam.record_view, args=[course.prefix, course.suffix, exam.slug, record.id])) else: return HttpResponse("Submission has been saved.")
def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] try: exam = Exam.objects.get(course = course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 postdata = request.POST['json_data'] #will return an error code to the user if either of these fail (throws 500) json_obj=json.loads(postdata) record = ExamRecord(course=course, exam=exam, student=request.user, json_data=postdata) record.save() autograder = None if exam.exam_type == "survey": autograder = AutoGrader("<null></null>", default_return=True) #create a null autograder that always returns the "True" object elif exam.autograde: try: autograder = AutoGrader(exam.xml_metadata) except Exception as e: #Pass back all the exceptions so user can see return HttpResponseBadRequest(unicode(e)) if autograder: record_score = ExamRecordScore(record = record) record_score.save() feedback = {} total_score = 0 for prob,v in json_obj.iteritems(): try: if isinstance(v,list): #multiple choice case submission = map(lambda li: li['value'], v) feedback[prob] = autograder.grade(prob, submission) field_obj = ExamRecordScoreField(parent=record_score, field_name = prob, human_name=v[0].get('questiontag4humans', "") if len(v)>0 else "", subscore = feedback[prob]['score'], value = submission, correct = feedback[prob]['correct'], comments="", associated_text = v[0].get('associatedText', "") if len(v)>0 else "", ) field_obj.save() for li in v: fc = ExamRecordScoreFieldChoice(parent=field_obj, choice_value=li['value'], human_name=li.get('tag4humans',""), associated_text=li.get('associatedText',"")) fc.save() else: #single answer submission = v['value'] feedback[prob] = autograder.grade(prob, submission) field_obj = ExamRecordScoreField(parent=record_score, field_name = prob, human_name=v.get('questiontag4humans', ""), subscore = feedback[prob]['score'], value = submission, correct = feedback[prob]['correct'], comments="", associated_text = v.get('associatedText', "")) field_obj.save() except AutoGraderGradingException as e: feedback[prob]={'correct':False, 'score':0} field_obj = ExamRecordScoreField(parent=record_score, field_name = prob, human_name=v.get('questiontag4humans', ""), subscore = 0, correct = feedback[prob]['correct'], comments = unicode(e), associated_text = v.get('associatedText', "")) field_obj.save() #This is when using code indents to denote blocks is a bit hairy #supposed to be at the same level as try...except. Run once per prob,v total_score += feedback[prob]['score'] record_score.score = total_score record_score.save() record_score.copyToExamScore() #Make this score the current ExamScore record.json_score_data = json.dumps(feedback) record.score = total_score record.save() return HttpResponse(json.dumps(feedback)) else: return HttpResponse("Submission has been saved.")
def collect_data(request, course_prefix, course_suffix, exam_slug): course = request.common_page_data['course'] user = request.user try: exam = Exam.objects.get(course=course, is_deleted=0, slug=exam_slug) except Exam.DoesNotExist: raise Http404 postdata = request.POST[ 'json_data'] #will return an error code to the user if either of these fail (throws 500) json_obj = json.loads(postdata) if exam.past_all_deadlines(): return HttpResponseBadRequest("Sorry! This submission is past the last deadline of %s" % \ datetime.datetime.strftime(exam.partial_credit_deadline, "%m/%d/%Y %H:%M PST")) attempt_number = exam.num_of_student_records(user) + 1 onpage = request.POST.get('onpage', '') record = ExamRecord(course=course, exam=exam, student=user, json_data=postdata, onpage=onpage, attempt_number=attempt_number, late=exam.past_due()) record.save() autograder = None if exam.exam_type == "survey": autograder = AutoGrader( "<null></null>", default_return=True ) #create a null autograder that always returns the "True" object elif exam.autograde: try: autograder = AutoGrader(exam.xml_metadata) except Exception as e: #Pass back all the exceptions so user can see return HttpResponseBadRequest(unicode(e)) if autograder: record_score = ExamRecordScore(record=record) record_score.save() feedback = {} total_score = 0 for prob, v in json_obj.iteritems( ): #prob is the "input" id, v is the associated value, #which can be an object (input box) or a list of objects (multiple-choice) try: if isinstance(v, list): #multiple choice case submission = map(lambda li: li['value'], v) feedback[prob] = autograder.grade(prob, submission) field_obj = ExamRecordScoreField( parent=record_score, field_name=prob, human_name=v[0].get('questionreport', "") if len(v) > 0 else "", subscore=feedback[prob]['score'], value=submission, correct=feedback[prob]['correct'], comments="", associated_text=v[0].get('associatedText', "") if len(v) > 0 else "", ) field_obj.save() for li in v: if 'correct_choices' not in feedback[prob]: is_correct = None else: is_correct = li['value'] in feedback[prob][ 'correct_choices'] fc = ExamRecordScoreFieldChoice( parent=field_obj, choice_value=li['value'], correct=is_correct, human_name=li.get('report', ""), associated_text=li.get('associatedText', "")) fc.save() else: #single answer submission = v['value'] feedback[prob] = autograder.grade(prob, submission) field_obj = ExamRecordScoreField( parent=record_score, field_name=prob, human_name=v.get('report', ""), subscore=feedback[prob]['score'], value=submission, correct=feedback[prob]['correct'], comments="", associated_text=v.get('associatedText', "")) field_obj.save() except AutoGraderGradingException as e: feedback[prob] = {'correct': False, 'score': 0} field_obj = ExamRecordScoreField( parent=record_score, field_name=prob, human_name=v.get('report', ""), subscore=0, correct=feedback[prob]['correct'], comments=unicode(e), associated_text=v.get('associatedText', "")) field_obj.save() #This is when using code indents to denote blocks is a bit hairy #supposed to be at the same level as try...except. Run once per prob,v total_score += feedback[prob]['score'] #Set raw score for ExamRecordScore record_score.raw_score = total_score record_score.save() #Set penalty inclusive score for ExamRecord record.json_score_data = json.dumps(feedback) #apply resubmission penalty resubmission_penalty_percent = pow( ((100 - exam.resubmission_penalty) / 100), (attempt_number - 1)) total_score = max(total_score * resubmission_penalty_percent, 0) #apply the late penalty if exam.grace_period and exam.late_penalty > 0 and datetime.datetime.now( ) > exam.grace_period: total_score = max(total_score * ((100 - exam.late_penalty) / 100), 0) record.score = total_score record.save() #Set ExamScore.score to max of ExamRecord.score for that student, exam. exam_score, created = ExamScore.objects.get_or_create(course=course, exam=exam, student=user) exam_score.setScore() return HttpResponse( reverse(exam.record_view, args=[course.prefix, course.suffix, exam.slug, record.id])) else: return HttpResponse("Submission has been saved.")