def make_example_page(id): #获取全考题列表 problem_list_exam = ExamService.get_prolbem_list_by_id(id) #获取考题配置 problem_condition_dict = ExamService.get_problem_condition_dict_byid(id) choice = [] blank = [] short_answer = [] #生成样例试卷 for problem in problem_list_exam: problem_detail = ProLibService.get_problem_detailed_info(problem) if problem_detail.type == Config.PROBLEM_TYPE['choice']: choice.append(problem_detail.pid) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank.append(problem_detail.pid) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer.append(problem_detail.pid) choice_r = random.sample(choice,problem_condition_dict['choice']['num']) blank_r = random.sample(blank,problem_condition_dict['blank']['num']) short_answer_r = random.sample(short_answer,problem_condition_dict['short_answer']['num']) List = choice_r + blank_r + short_answer_r ExamService.update_problem_list_example(List,id) return redirect(url_for('admin.exam_problem',id=id))
def exam_add_member(id,page=1,values=''): form = ExamStudentSearchForm() apply_num = ExamService.get_apply_num_by_id(id) if request.method == 'GET': values_json = {} if values != '': values_json = json.loads(base64.b64decode(values)) pagination = UserService.search_userinfo(values_json,page) return render_template('web/exam_manage/exam_add_member.html', id = id, values = values, form = form, pagination = pagination, user_list = pagination.items, apply_num = apply_num, level = Config.USER_LEVEL, status = Config.STATUS, active = Config.ADMIN_PAGE_ACTIVE['exam']) if form.validate_on_submit(): if form.submit.data: values = base64.b64encode(json.dumps(form.to_dict())) return redirect(url_for('web.exam_add_member', id=id, page=1, values=values)) elif form.submit_add.data: user_filter_list = UserService.search_userinfo_nopage(form.to_dict()) #按条件批量获取用户id,并添加 ExamService.add_exam_member_by_list(id,user_filter_list) return redirect(url_for('web.exam_add_member', id=id, page=page)) else : return redirect(url_for('web.exam_add_member',id=id,page=page))
def del_exam_member_single(id,user_id): try: ExamService.del_exam_member(id,user_id) flash('success') except: flash('failed') return redirect(url_for('web.exam_member',id=id))
def exam_add_problem(id,page=1,values=''): form = ExamProblemSearchForm() problem_list_exam = ExamService.get_prolbem_list_by_id(id) if request.method == 'GET': values_json = {} if values != '': values_json = json.loads(base64.b64decode(values)) pagination = ProLibService.search_problem_info(values_json,page) return render_template('web/exam_manage/exam_add_problem.html', id = id, values = values, form = form, pagination = pagination, problem_list = pagination.items, problem_list_exam = problem_list_exam, type = Config.PROBLEM_TYPE_HTML, status=Config.STATUS, active = Config.ADMIN_PAGE_ACTIVE['exam']) if form.validate_on_submit(): if form.submit.data: values = base64.b64encode(json.dumps(form.to_dict())) return redirect(url_for('web.exam_add_problem', id=id, page=1, values=values)) elif form.submit_add.data: #按条件批量添加题目 problem_filter_list = ProLibService.search_problem_info_nopage(form.to_dict()) ExamService.add_exam_problem_by_list(id,problem_filter_list) return redirect(url_for('web.exam_add_problem', id=id, page=page)) else: return redirect(url_for('web.exam_add_problem', id=id, page=page))
def del_exam(id): try : ExamService.del_exam(id) flash("Del exam success") except: flash("Del exam failed") return redirect(url_for('web.exam_manage'))
def exam_update(id): form = ExamUpdateForm() if request.method == 'GET': exam_info = ExamService.get_exam_detail_info(id) form.describe.data = exam_info.describe form.name.data = exam_info.name form.creater.data = exam_info.creater form.count.data = exam_info.count form.during_time.data = exam_info.during_time form.start_time.data = exam_info.start_time form.end_time.data = exam_info.end_time form.reg_start_time.data = exam_info.reg_start_time form.reg_end_time.data = exam_info.reg_end_time form.is_register.data = str(exam_info.is_register) form.is_hidden.data = str(exam_info.is_hidden) form.is_show_result.data = str(exam_info.is_show_result) return render_template('web/exam_manage/exam_update.html', id = id, form = form, active = Config.ADMIN_PAGE_ACTIVE['exam']) if form.validate_on_submit(): try: ExamService.update_exam(form,id) flash("Update exam success") return redirect(url_for('web.exam_update',id=id)) except: flash("Update exam failed") return redirect(url_for('web.exam_update', id=id))
def del_exam_problem_single(id,problem_id): try: ExamService.del_exam_problem(id,problem_id) flash('success') except: flash('failed') return redirect(url_for('web.exam_problem',id=id))
def add_exam_problem_single(id,problem_id,page=1,values=''): try: if ProLibService.get_problem_detailed_info(problem_id).status: ExamService.add_exam_problem_single(id,problem_id) flash('success') else: flash('failed') except: flash('failed') if values == '': return redirect(url_for('web.exam_add_problem',id=id,page=page)) return redirect(url_for('web.exam_add_problem', id=id, page=page, values=values))
def add_exam_member_single(id,user_id,page=1,values=''): try: if UserService.get_userinfo_by_id(user_id).status: ExamService.add_exam_member_single(id,user_id) ExamConditionService.add_user_exam_condition(id, user_id) flash('success') else: flash('failed') except: flash('failed') if values == '': return redirect(url_for('web.exam_add_member', id=id, page=page)) return redirect(url_for('web.exam_add_member',id=id,page=page,values=values))
def function(*args, **kwargs): failed = 'You do not have permission to enter' url = url_for('web.exam_list') path = request.path.split('/') id = path[-2] count = int(path[-1]) now_date = datetime.now() exam_page = ExamConditionService.get_user_exam_page( id, current_user.user_id, count) exam_end_time = exam_page['end_time'] #考试的设定信息 exam_info = ExamService.get_exam_detail_info(id) #已经考的场数 exam_count = ExamConditionService.get_user_exam_count( id, current_user.user_id) #中途加入的考试必须为最新的一场即下标为exam_count-1 if count != exam_count - 1: return render_template("web/failed.html", failed=failed, url=url) #验证考试时间 if now_date < exam_info.start_time or now_date > exam_info.end_time: return render_template("web/failed.html", failed=failed, url=url) #获取当前时间 now_time = time.time() # +5s 延迟,防止js强制结束考试时无法提交 if now_time > exam_end_time + 5: return render_template("web/failed.html", failed=failed, url=url) return f(*args, **kwargs)
def exam_history(page=1): if request.method == 'GET': pagination = ExamService.get_user_exam_history(current_user, page) exam_count = ExamConditionService.get_user_exam_count_all( current_user.user_id) return render_template('web/exam/own_exam.html', pagination=pagination, exam_count=exam_count, exam_list=pagination.items, current_user=current_user)
def exam_add(): form = ExamUpdateForm() if request.method == 'GET': form.creater.data = current_user.user_name return render_template('web/exam_manage/exam_add.html', form = form, active = Config.ADMIN_PAGE_ACTIVE['exam']) if form.validate_on_submit(): try: ExamService.add_exam(form) flash("Add exam success","success") return redirect(url_for('web.exam_manage')) except: flash("Add exam field","danger") return render_template('web/exam_manage/exam_add.html', form = form, active = Config.ADMIN_PAGE_ACTIVE['exam'])
def exam_list(page=1): if request.method == 'GET': pagination = ExamService.get_user_exam_during(current_user, page) exam_count = ExamConditionService.get_user_exam_count_all( current_user.user_id) now_time = int(time.time()) return render_template('web/exam/exam_list.html', pagination=pagination, exam_count=exam_count, nowtime=now_time, exam_list=pagination.items, current_user=current_user)
def exam_member(id): form = ExamStudentSearchForm() apply_num = ExamService.get_apply_num_by_id(id) member_list = [] for student in apply_num: member_list.append(UserService.get_userinfo_by_id(student)) if request.method == 'GET': return render_template('admin/exam/exam_member.html', id = id, form = form, member_list = member_list, status=Config.STATUS, active=Config.ADMIN_PAGE_ACTIVE['exam']) #对当前页面中用户进行筛选 filter = [] if form.validate_on_submit(): if form.username.data: username = re.compile('.*'+form.username.data+'.*') if form.nickname.data: nickname = re.compile('.*'+form.nickname.data+'.*') if form.college.data: college = re.compile('.*'+form.college.data+'.*') if form.major.data: major = re.compile('.*'+form.major.data+'.*') for item in member_list: if username and username.search(item.user_name): filter.append(item) continue if nickname and nickname.search(item.nick_name): filter.append(item) continue if college and college.search(item.college_name): filter.append(item) continue if major and major.search(item.major_name): filter.append(item) continue if item.grade == form.grade.data or item.classnum == form.classnum.data: filter.append(item) continue return render_template('admin/exam/exam_member.html', id=id, form=form, member_list=filter, active=Config.ADMIN_PAGE_ACTIVE['exam'])
def exam_finish(id,count): exam_answer = request.json ExamConditionService.save_page(id,current_user.user_id,None,exam_answer,count) print exam_answer logout_ip = request.remote_addr ExamConditionService.update_exam_condition(id,current_user.user_id,logout_ip,count) grade = 0 problem_condition_dict = ExamService.get_problem_condition_dict_byid(id) for pid, answer in exam_answer['choice'].items(): problem = ProLibService.get_problem_detailed_info(pid) if answer == problem.answer: grade += problem_condition_dict['choice']['score'] ExamConditionService.update_user_exam_grade(id, current_user.user_id, grade, count) return json.dumps({'key':'success'})
def function(*args, **kwargs): failed = 'You do not have permission to enter' url = url_for('web.exam_list') path = request.path.split('/') id = path[-2] count = int(path[-1]) now_date = datetime.now() now_time = int(time.time()) #考试设定信息 exam_info = ExamService.get_exam_detail_info(id) #已经考的场数 exam_count = ExamConditionService.get_user_exam_count( id, current_user.user_id) # 如果考试不是无限次那么已经考试次数>=设定次数就不能参加考试 if exam_info.count != -1 and exam_count >= exam_info.count: return render_template("web/failed.html", failed=failed, url=url) #由于是新建考试,那么当前my_answer列表长度即新一场考试在列表中的下标,数值完全匹配才能够新建考试 #(0号下标依然成立) if count != exam_count: return render_template("web/failed.html", failed=failed, url=url) if count != 0: # 获得上一场考试的情况(用于验证上一场考试是否结束) exam_page = ExamConditionService.get_user_exam_page( id, current_user.user_id, count - 1) #如果要新建的考试不是第一场次考试, 那么检查上一场的考试 if exam_page['now_time'] < exam_page['end_time']: if now_time < exam_page['end_time']: return render_template("web/failed.html", failed=failed, url=url) #验证考试时间 if now_date < exam_info.start_time or now_date > exam_info.end_time: return render_template("web/failed.html", failed=failed, url=url) return f(*args, **kwargs)
def exam_manage(page=1,values=''): form = ExamSearchForm() if request.method == 'GET': values_json = {} if values != '': values_json = json.loads(base64.b64decode(values)) pagination = ExamService.search_exam_info(values_json,page) return render_template('web/exam_manage/exam_manage.html', form = form, values = values, pagination = pagination, exam_list = pagination.items, is_register = Config.IS_REGISTER, status = Config.IS_HIDDEN, active = Config.ADMIN_PAGE_ACTIVE['exam']) if form.validate_on_submit(): values = base64.b64encode(json.dumps(form.to_dict())) return redirect(url_for('web.exam_manage', page=1, values=values)) return redirect(url_for('web.exam_manage', page=page))
def create_page(id): examAllInfo = ExamService.get_exam_detail_info(id) choice = [] blank = [] short_answer = [] problem_list = [] if examAllInfo.is_random == 1: problem_list = examAllInfo.problem_list elif examAllInfo.is_random == 0: problem_list = examAllInfo.problem_list_example for problem in problem_list: problem_detail = ProLibService.get_problem_detailed_info(problem) if problem_detail.type == Config.PROBLEM_TYPE['choice']: # 选项乱序 list = problem_detail.desc_other.split('#$') problem_detail.desc_other = random.sample(list, len(list)) choice.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer.append(problem_detail) # 题目乱序 choice_r = random.sample(choice, examAllInfo.choice['num']) blank_r = random.sample(blank, examAllInfo.blank['num']) short_answer_r = random.sample(short_answer, examAllInfo.answer['num']) # 生成json exam_page = {} exam_page['name'] = examAllInfo.name exam_page['choice'] = [] exam_page['blank'] = [] exam_page['short_answer'] = [] exam_page['times'] = examAllInfo.during_time #记录时长 exam_page['now_time'] = int(time.time()) #当前时间(时间戳,在render前赋值保证精确) exam_page['end_time'] = exam_page['now_time'] + exam_page['times'] #结束时间(时间戳,在render前赋值保证精确) exam_page['choice_score'] = examAllInfo.choice['score'] exam_page['blank_score'] = examAllInfo.blank['score'] exam_page['short_answer_score'] = examAllInfo.answer['score'] #count为当前考试在my_answer列表中的下标值 exam_page['count'] = ExamConditionService.get_user_exam_count(id, current_user.user_id) ''' { 'choice':[ {'pid':'123', 'describe_main':'test1', 'describe_other':['a','b','c','d'], }, {'pid':'1234', 'describe_main':'test2', 'describe_other':['a','b','c','d','e'], } ], 'blank':[ {'pid':'1111', 'describe_main':'test3', 'describe_other':'', 'answer':'' }] 'short_answer':[], 'name':'Test', 'times':'120', 'now_time':'' 'end_time':'' 'choice_score':10, 'blank_score':0, 'short_answer_score':0 'count':0 } ''' for problem in choice_r: problem_dict = {'pid': problem.pid, 'desc_main': problem.desc_main, 'desc_other': problem.desc_other, 'answer': ''} exam_page['choice'].append(problem_dict) for problem in blank_r: problem_dict = {'pid': problem.pid, 'desc_main': problem.desc_main, 'desc_other': problem.desc_other, 'answer': ''} exam_page['blank'].append(problem_dict) for problem in short_answer_r: problem_dict = {'pid': problem.pid, 'desc_main': problem.desc_main, 'desc_other': problem.desc_other, 'answer': ''} exam_page['short_answer'].append(problem_dict) return exam_page
def exam_problem(id): form = ExamProblemNumForm() examinfo = ExamService.get_exam_detail_info(id) problem_list_exam = examinfo.problem_list #考试题库题目id列表 problem_list_example = examinfo.problem_list_example #考试样例试卷题目id列表 problem_condition_dict = ExamService.get_problem_condition_dict_byid(id) #考题情况字典 choice = [] blank = [] short_answer = [] choice_ex = [] blank_ex = [] short_answer_ex = [] # 考试题目列表仅仅有id,通过循环获取 # 题目相关信息,下一循环同理 # 区别在于题库和样例试卷的题目不同 for problem in problem_list_exam: problem_detail = ProLibService.get_problem_detailed_info(problem) problem_detail.belong_to_book = BookService.get_book_by_id(problem_detail.belong_to_book).book_name problem_detail.belong_to_unit = BookService.get_unit_by_id(problem_detail.belong_to_unit).unit_name problem_detail.belong_to_section = BookService.get_section_by_id(problem_detail.belong_to_section).section_name if problem_detail.type == Config.PROBLEM_TYPE['choice']: choice.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer.append(problem_detail) for problem in problem_list_example: problem_detail = ProLibService.get_problem_detailed_info(problem) problem_detail.belong_to_book = BookService.get_book_by_id(problem_detail.belong_to_book).book_name problem_detail.belong_to_unit = BookService.get_unit_by_id(problem_detail.belong_to_unit).unit_name problem_detail.belong_to_section = BookService.get_section_by_id(problem_detail.belong_to_section).section_name if problem_detail.type == Config.PROBLEM_TYPE['choice']: choice_ex.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank_ex.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer_ex.append(problem_detail) problem_list_dict = dict((('choice',choice),('blank',blank),('short_answer',short_answer))) problem_list_example_dict = dict((('choice', choice_ex), ('blank', blank_ex), ('short_answer', short_answer_ex))) if request.method == 'GET': form.choice_num.choices = [(i, i) for i in range(len(choice) + 1)] form.blank_num.choices = [(i, i) for i in range(len(blank) + 1)] form.short_answer_num.choices = [(i, i) for i in range(len(short_answer) + 1)] form.choice_num.data = str(problem_condition_dict['choice']['num']) form.choice_score.data = str(problem_condition_dict['choice']['score']) form.blank_num.data = str(problem_condition_dict['blank']['num']) form.blank_score.data = str(problem_condition_dict['blank']['score']) form.short_answer_num.data = str(problem_condition_dict['short_answer']['num']) form.short_answer_score.data = str(problem_condition_dict['short_answer']['num']) form.is_random.data = str(problem_condition_dict['is_random']) return render_template('web/exam_manage/exam_problem.html', form = form, id = id, is_random = problem_condition_dict['is_random'], problem_num_dict = problem_condition_dict, problem_list_dict = problem_list_dict, problem_list_example = problem_list_example_dict, status=Config.STATUS, active=Config.ADMIN_PAGE_ACTIVE['exam']) if request.method == 'POST': ExamService.update_problem_number(form,id) return redirect(url_for('web.exam_problem',id=id))