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 problem_update(id): form = ProblemUpdateForms() proIsExist = ProLibService.get_problem_detailed_info(id) if proIsExist is None: return redirect(url_for('admin.problem_manage')) if request.method == 'GET': problem = ProLibService.get_problem_detailed_info(id) book = BookService.get_book_by_id(problem.belong_to_book) unit = BookService.get_unit_by_id(problem.belong_to_unit) section = BookService.get_section_by_id(problem.belong_to_section) form.type.data = problem.type form.desc_main.data = problem.desc_main form.desc_other.data = problem.desc_other form.author.data = problem.author form.answer.data = problem.answer form.tips.data = problem.tips form.tags.data = problem.tags form.subject.data = problem.subject form.level.data = str(problem.level) form.status.data = str(problem.status) if book: form.b_book_id.data = book.book_id form.b_book.data = book.book_name if unit: form.b_unit_id.data = unit.unit_id form.b_unit.data = unit.unit_name if section: form.b_section_id.data = section.section_id form.b_section.data = section.section_name return render_template('admin/problem/problem_update.html', form=form, active=Config.ADMIN_PAGE_ACTIVE['problem']) if form.book.data == '0': form.book.data = form.b_book_id.data form.unit.data = form.b_unit_id.data form.section.data = form.b_section_id.data #存在js级联查询,不能使用表单验证,如果使用必然不通过 try: ProLibService.update_problem(form, id) flash("Update problem information success", "success") except: flash("Update problem infomation failed", "danger") return redirect(url_for('admin.problem_update', id=id))
def problem_add(): form = ProblemAddForms() if request.method == 'GET': form.author.data = current_user.user_name return render_template('admin/problem/problem_add.html', form=form, active=Config.ADMIN_PAGE_ACTIVE['problem']) try: ProLibService.add_problem(form) flash("Add problem success", "success") except: flash("Add problem field", "danger") return redirect(url_for('admin.problem_manage'))
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 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 get_page_result(id,count): page,answer = ExamConditionService.get_page(id,current_user.user_id,count) page = json.loads(page) true_answer = {} for item in page['choice']+page['blank']+page['short_answer']: pid = item['pid'] true_answer[pid]=ProLibService.get_problem_detailed_info(item['pid']).answer page['now_time'] = int(time.time()) page_dict = {'page':page,'answer':answer,'true_answer':true_answer} print true_answer page_json = json.dumps(page_dict) return page_json
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 problem_manage(page=1, values=''): form = ProblemSearchForms() if request.method == 'GET': values_json = {} print values if values != '': values_json = json.loads(base64.b64decode(values)) pagination = ProLibService.search_problem_info(values_json, page) return render_template('admin/problem/problem_manage.html', form=form, values=values, pagination=pagination, problem_list=pagination.items, type=Config.PROBLEM_TYPE_HTML, status=Config.STATUS, active=Config.ADMIN_PAGE_ACTIVE['problem']) values = base64.b64encode(json.dumps(form.to_dict())) return redirect(url_for('admin.problem_manage', page=1, values=values))
def problem_delete(id): ProLibService.del_problem(id) return redirect(url_for('admin.problem_manage'))
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))