def get_solutions(student_id,hw_id,student_name=""): # import the appropriate homework hw = import_homework(hw_id) if not hw: print "Status: 404 Not Found\n" sys.exit() # check that due date has not passed curr_time = datetime.now() if curr_time < hw.due_date: print "Status: 401 Unauthorized\n" sys.exit() # set the seed seed = get_seed(student_id) # initialize questions questions = [q(seed) for q in hw.questions] # extract name, body, solutions from each question qs = [{ "name":q.name, "data":q.to_JSON(solution=True), } for q in questions] # return homework name and question data return { "name": hw.name, "text": hw.text, "due_date" : hw.due_date.strftime("%m/%d/%Y %H:%M:%S"), "questions": qs, }
def submit_response(answers,student_id,hw_id,q_id): # import the appropriate homework and question hw = import_homework(hw_id) q = hw.questions[q_id](get_seed(student_id)) # check that deadline has not passed curr_time = datetime.now() if curr_time > hw.due_date: print "Status: 410 Deadline Passed\n" sys.exit() # fetch spreadsheet and worksheet ID's for Google doc sp_id,wk_id = fetch_hw_ids(hw.name,q_id) # query worksheet for all entries with given student_id responses = fetch_responses(student_id,sp_id,wk_id) # check if user has submitted 2 entries in last 6 hours last_times = [resp.content['timestamp'] for resp in responses] if len(last_times) >= q.submits_allowed: last_time = datetime.strptime(last_times[-q.submits_allowed],'%m/%d/%Y %H:%M:%S') if curr_time < last_time + timedelta(hours=q.lockout_period): print "Status: 423 Locked\n" sys.exit() # validate answer by calling "check" method of question try: output = q.check(answers,student_id) except: print "Status: 400 Bad Request\n" sys.exit() scores = output["scores"] comments = output["comments"] # package new response and submit it last_times.append(curr_time.strftime("%m/%d/%Y %H:%M:%S")) new_response = { "id": student_id, "timestamp": last_times[-1] } for i,ans in enumerate(answers): new_response['ans%d' % i] = unicode(ans) new_response['score%d' % i] = str(scores[i]) new_response['comment%d' % i] = unicode(comments[i]) worksheet = fetch_worksheet(sp_id,wk_id) worksheet.AddRecord(new_response) # calculate the total points per question points = { "earned": sum(float(x) for x in scores if x), "graded": sum(q.max_pts[i] for i,x in enumerate(scores) if type(x) in [int,float]), "total": sum(q.max_pts) } # determine if question should be locked is_locked = False if len(last_times) >= q.submits_allowed: last_time = datetime.strptime(last_times[-q.submits_allowed],'%m/%d/%Y %H:%M:%S') if curr_time < last_time + timedelta(hours=q.lockout_period): is_locked = True # return response data return { "last_times" : last_times, "locked" : is_locked, "points" : points, "comments" : comments }
def get_solutions(student_id, hw_id, student_name=""): # import the appropriate homework hw = import_homework(hw_id) if not hw: print "Status: 404 Not Found\n" sys.exit() # check that due date has not passed curr_time = datetime.now() if curr_time < hw.due_date: print "Status: 401 Unauthorized\n" sys.exit() # set the seed seed = get_seed(student_id) # initialize questions questions = [q(seed) for q in hw.questions] # extract name, body, solutions from each question qs = [{ "name": q.name, "data": q.to_JSON(solution=True), } for q in questions] # return homework name and question data return { "name": hw.name, "text": hw.text, "due_date": hw.due_date.strftime("%m/%d/%Y %H:%M:%S"), "questions": qs, }
def get_homework(student_id,hw_id,student_name=""): # import the appropriate homework homework = import_homework(hw_id) if not homework: print "Status: 404 Not Found\n" sys.exit() # set the seed seed = get_seed(student_id) # initialize questions questions = [q(seed) for q in homework.questions] # extract name and body from each question qs = [{ "name":q.name, "data":q.to_JSON(solution=False), } for q in questions] # return homework name and question data return { "name": homework.name, "text": homework.text, "due_date" : homework.due_date.strftime("%m/%d/%Y %H:%M:%S"), "questions": qs }
def get_homework(student_id, hw_id, student_name=""): # import the appropriate homework homework = import_homework(hw_id) if not homework: print "Status: 404 Not Found\n" sys.exit() # set the seed seed = get_seed(student_id) # initialize questions questions = [q(seed) for q in homework.questions] # extract name and body from each question qs = [{ "name": q.name, "data": q.to_JSON(solution=False), } for q in questions] # return homework name and question data return { "name": homework.name, "text": homework.text, "due_date": homework.due_date.strftime("%m/%d/%Y %H:%M:%S"), "questions": qs }
def submit_response(answers, student_id, hw_id, q_id): # import the appropriate homework and question hw = import_homework(hw_id) q = hw.questions[q_id](get_seed(student_id)) # check that deadline has not passed curr_time = datetime.now() if curr_time > hw.due_date: print "Status: 410 Deadline Passed\n" sys.exit() # fetch spreadsheet and worksheet ID's for Google doc sp_id, wk_id = fetch_hw_ids(hw.name, q_id) # query worksheet for all entries with given student_id responses = fetch_responses(student_id, sp_id, wk_id) # check if user has submitted 2 entries in last 6 hours last_times = [resp.content['timestamp'] for resp in responses] if len(last_times) >= q.submits_allowed: last_time = datetime.strptime(last_times[-q.submits_allowed], '%m/%d/%Y %H:%M:%S') if curr_time < last_time + timedelta(hours=q.lockout_period): print "Status: 423 Locked\n" sys.exit() # validate answer by calling "check" method of question try: output = q.check(answers, student_id) except: print "Status: 400 Bad Request\n" sys.exit() scores = output["scores"] comments = output["comments"] # package new response and submit it last_times.append(curr_time.strftime("%m/%d/%Y %H:%M:%S")) new_response = {"id": student_id, "timestamp": last_times[-1]} for i, ans in enumerate(answers): new_response['ans%d' % i] = unicode(ans) new_response['score%d' % i] = str(scores[i]) new_response['comment%d' % i] = unicode(comments[i]) worksheet = fetch_worksheet(sp_id, wk_id) worksheet.AddRecord(new_response) # calculate the total points per question points = { "earned": sum(float(x) for x in scores if x), "graded": sum(q.max_pts[i] for i, x in enumerate(scores) if type(x) in [int, float]), "total": sum(q.max_pts) } # determine if question should be locked is_locked = False if len(last_times) >= q.submits_allowed: last_time = datetime.strptime(last_times[-q.submits_allowed], '%m/%d/%Y %H:%M:%S') if curr_time < last_time + timedelta(hours=q.lockout_period): is_locked = True # return response data return { "last_times": last_times, "locked": is_locked, "points": points, "comments": comments }