def previwe_paper(): print_log('preview paper', request.method) exam_id = request.args.get('exam_id') if session.get('user_id').upper() == common_helper.admin_type: teacher_id = request.args.get('user_id') else: teacher_id = session.get('user_id') sql = 'SELECT paper_path, paper_title, paper_time, paper_date FROM ' + \ exam_paper_table + ' INNER JOIN ' + user_table + ' ON exam_paper.paper_userid=user.user_id ' + \ 'WHERE paper_id=%s and paper_userid=%s' cursor.execute(sql, (exam_id, teacher_id)) data = cursor.fetchone() exam_dict = { 'title': data.get('paper_title'), 'exam_id': exam_id, 'duration': data.get('paper_time'), 'date': data.get('paper_date') } if session.get('user_id').upper() == 'ADMIN': return render_template('admin-preview-paper.html', question=common_helper.parse_paper( data.get('paper_path')), exam=exam_dict) else: return render_template('preview-paper.html', question=common_helper.parse_paper( data.get('paper_path')), exam=exam_dict)
def admin_add_questions_by_file(): import global_helper global_helper.cmd_exec('rm -r \"' + path.join(file_dest, question_file_folder) + '\"') question_file = request.files['upload_questions_file'] # 数据库中的type,表示科目 q_dbtype = request.form.get('input-q-type') file_name = 'Admin-Add-Questions.xls' question_set.save(storage=question_file, folder=path.join(file_dest, question_file_folder), name=file_name) abs_path = path.join(file_dest, question_file_folder, file_name) questions_list = common_helper.parse_paper(abs_path) print('[admin add ques by file]', len(questions_list)) print('[admin add ques by file]', q_dbtype) # current_year = datetime.now().year current_year = 1970 choice_sql = 'INSERT INTO ' + choice_question_table + \ '(q_description, q_value, q_answer, q_A, q_B, q_C, q_D, q_counter, q_difficulty, q_year, q_type) ' + \ 'VALUES (%s, %s, %s, %s, %s, %s, %s, 0, %s, %s, %s)' judge_subjective_sql = 'INSERT INTO {} ' + \ '(q_description, q_value, q_answer, q_counter, q_difficulty, q_year, q_type)' + \ 'VALUES (%s, %s, %s, 0, %s, %s, %s)' for x in questions_list: q_type = x.get('q_type') q_desc = x.get('q_text') q_val = x.get('value') q_ans = x.get('answer') diff = random.randint(1, 3) if q_type == 'radio' or q_type == 'checkbox': try: cursor.execute( choice_sql, (q_desc, q_val, q_ans, x.get('A'), x.get('B'), x.get('C'), x.get('D'), diff, current_year, q_dbtype)) db_connector.commit() except: db_connector.rollback() elif q_type == 'decide': q_ans = 1 if str(q_ans).upper()[0] == 'T' else 0 try: cursor.execute( judge_subjective_sql.format(judge_question_table), (q_desc, q_val, q_ans, diff, current_year, q_dbtype)) db_connector.commit() except: db_connector.rollback() elif q_type == 'textarea': try: cursor.execute( judge_subjective_sql.format(subjective_question_table), (q_desc, q_val, q_ans, diff, current_year, q_dbtype)) db_connector.commit() except: db_connector.rollback() return render_template('admin-add-questions.html')
def teacher_check_paper(): # 这个函数跟学生的 show_answers() 逻辑是一样的 print_log('teacher check paper', request.method) # 提取GET方法的参数 paper_id = request.args.get('paper_id') student_id = request.args.get('student_id') student_name = request.args.get('student_name') # 找到试卷文件,并提取出试题和正确答案 sql = 'SELECT paper_path FROM ' + exam_paper_table + 'WHERE paper_id=%s' cursor.execute(sql, paper_id) path = cursor.fetchone().get('paper_path') std_ans = common_helper.get_std_answers(path) question = common_helper.parse_paper(path) # 找到学生选择的试卷的答案 sql = 'SELECT answer_json FROM ' + student_exam_log_table + \ 'WHERE student_id=%s and paper_id=%s' cursor.execute(sql, (student_id, paper_id)) answers = json.loads(cursor.fetchone().get('answer_json')) for i in range(0, len(question)): question[i]['std_ans'] = std_ans[str(i)] if answers.get(str(i)) is None: continue if question[i].get('q_type') == 'checkbox': is_correct = set(answers[str(i)]) == set(std_ans[str(i)]) else: is_correct = answers[str(i)] == std_ans[str(i)] question[i]['is_correct'] = is_correct question[i]['selected'] = answers[str(i)] # 找到试卷相关信息 sql = 'SELECT * FROM ' + \ exam_paper_table + 'INNER JOIN' + user_table + 'ON user.user_id=exam_paper.paper_userid ' + \ 'WHERE paper_id=%s' cursor.execute(sql, paper_id) data = cursor.fetchone() exam = { 'title': data.get('paper_title'), 'exam_id': paper_id, 'teacher': data.get('user_name'), 'duration': data.get('paper_time') } return render_template('teacher-check-paper.html', question=question, exam=exam, student=dict(student_name=student_name, student_id=student_id))
def start_exam(): print_log('start-exam', request.method + request.args.get('exam_id')) exam_id = request.args.get('exam_id') sql = 'SELECT * FROM ' + exam_paper_table + ' WHERE paper_id=%s' cursor.execute(sql, str(exam_id)) data = cursor.fetchone() sql2 = 'SELECT user_name FROM ' + user_table + ' WHERE user_id=%s' cursor.execute(sql2, data.get('paper_userid')) data2 = cursor.fetchone() question = common_helper.parse_paper(data.get('paper_path')) exam = { 'exam_id': exam_id, 'title': data.get('paper_title'), 'duration': data.get('paper_time'), 'teacher': data2.get('user_name') } return render_template('exam.html', question=question, exam=exam)