def p_task_class(user, grade_id, paper_ids): """ 教师发送作业班级信息和发送状态 :return: """ units = map(Struct, user.units) if user.units else [] unit_ids = [i.id for i in units] status_map = dict() if user.subject_id == 21: status_map = sx_paper_unit_status(paper_ids, unit_ids, user.id) if user.subject_id == 91: status_map = yy_paper_unit_status(paper_ids, unit_ids, user.id) if user.subject_id == 51: status_map = yw_paper_unit_status(paper_ids, unit_ids, user.id) unit_info = [] for i in units: unit_info.append( dict(status=status_map.get(i.id, 0), unit_name=i.name, unit_class_id=i.id)) data = Struct() data.unit_info = unit_info data.grade_id = grade_id return data
def special_handle_question(question): # 改为json格式,文件url添加前缀 question.subject = json.loads(question.subject) question.subject = Struct(question.subject) if question.subject.get("images"): _images = [] for a in question.subject["images"]: _images.append(format_ziyuan_url(a)) question.subject["images"] = _images question.video_url = format_ziyuan_url(question.video_url) question.video_image = format_ziyuan_url(question.video_image) # 完形填空: 把题干里的<span class="xxx">____</span>替换为<span>(题号)</span><em class="JS-space" ask_id="123"></em> if question.type == 7: question.i = 0 question._no = question.ask_no or 1 def repl_f(m): s = m.group() ask = question.asks[question.i] if question.i < len(question.asks) else None if ask: s = u'<span>(%s)</span>:<em>点击选择</em>' % question._no question.i += 1 question._no += 1 return s question.subject = Struct(question.subject) question.subject.content = re.sub(r'<span.*?</span>', repl_f, question.subject.content) question.subject.title = u"完形填空" if question.subject.content.startswith(u"完形填空"): question.subject.content = question.subject.content.replace(u'完形填空。', '')
def get_reading_pages(book_id, pages): """ 功能说明: 获取课本点读页 """ reading = cache.yy_reading.get((book_id, pages)) if reading: reading = Struct(reading) reading.details = [Struct(r) for r in reading.details] else: page_names = 'P%s' % pages sql = """select r.* from yy_reading r join yy_catalog c on c.id=r.catalog_id where c.book_id=%s and c.name = '%s' and c.status>=0 ORDER BY c.sequence""" % ( book_id, page_names) reading = db.ziyuan_slave.fetchone_dict(sql) if not reading: return sql = """select id,reading_id,audio_text,translation,audio_url,area,duration from yy_reading_detail where reading_id=%s""" % reading.id details = db.ziyuan_slave.fetchall_dict(sql) # 去除翻译中的换行 for d in details: d.translation = d.translation.replace('\n', '') reading.details = details cache.yy_reading.set((book_id, pages), reading) return reading
def yy_paper_list(user, subject_id): """ 英语试卷列表 :param user: :param subject_id: :return: """ user_id = user.id nowt = int(time.time()) # 用户是否设置活动教材 当前使用教材是否是指定教材 mid_book = db.tbkt_active.mid_term_user_book.select('book_id').get( user_id=user_id, subject_id=subject_id) if not mid_book: book = db.default.user_book.select('book_id').get(user_id=user_id, subject_id=91) # 指定教材下的book_id -- 人教版三起点 外研版3起点 湘鲁版三起点 if book.book_id in (44, 300, 641, 642, 126, 305, 647, 648, 177, 322, 998, 999): db.tbkt_active.mid_term_user_book.create( user_id=user_id, book_id=book.book_id, subject_id=91, add_time=nowt, is_work_book=0, ) else: return else: book = mid_book data = {} papers = [] try: if book and book.book_id in english.BOOK_PAPER.keys(): paper_list = english.BOOK_PAPER[book.book_id] # 用户是否布置过当前活动试卷 sql = ''' select DISTINCT yt.object_id paper_id, ytc.unit_class_id unit_id from yy_task yt inner join yy_task_class ytc on yt.id = ytc.task_id where ytc.status > 0 and yt.add_user = %s and yt.type = 103 and ytc.unit_class_id in (%s) ''' % (user.id, ",".join(str(i.id) for i in user.units)) pub_papers = db.tbkt_yingyu.fetchall_dict(sql) paper_map = defaultdict(list) for i in pub_papers: paper_map[i.paper_id].append(i.unit_id) units = map(Struct, user.units) if user.units else [] # 用户使用出版社 查询教材 for paper in paper_list: row = Struct() done_unit = len(paper_map.get(paper, [])) row.id = paper row.status = 1 if done_unit >= len(units) else 0 row.name = english.PAPER[paper] papers.append(row) except Exception as e: print e, 'choice_paper' data['papers'] = papers return data
def get_question_data(qid, ask_no=1): """ 功能说明: 返回一道大题的数据 参数: ask_no 小题题号 """ question = cache.yy_question.get(qid) if question: ask_list = [] for a in question.asks: a = Struct(a) a.options = [Struct(r) for r in a.options] a.subject = Struct(a.subject) a.parse = Struct(a.parse) ask_list.append(a) question.asks = ask_list question.subject = Struct(question.subject) if not question: sql = """select id,subject,classify,type,video_url,video_image from yy_question where id = %s;""" % qid question = db.ziyuan_slave.fetchone_dict(sql) if not question: return question.asks = get_question_ask(question, ask_no) # 补充一些特殊题型的格式 special_handle_question(question) cache.yy_question.set(qid, question) # 最后补充题号 question.ask_no = ask_no question.num = ask_no + 1 for ask in question.asks: ask["no"] = ask_no ask_no += 1 return question
def get_paper_detail(user_id, active_id, paper_id): """获取试卷结果qid,aid,oid,result,answer""" cache_data = cache.hd_paper_result.get((user_id, active_id, paper_id)) if cache_data: details = [Struct(d) for d in cache_data["details"]] else: details = db.tbkt_active_slave.active_paper_detail.select( 'text').filter(user_id=user_id, active_id=active_id, paper_id=paper_id).last() details = json.loads(details.text) details = [Struct(t) for t in details] return details
def get_test_result_game(test, cache_date=0): """ 获取 游戏作业结果 :param test: :return: """ if cache_date: details = [Struct(r) for r in cache_date['cache_details']] else: sql = """select id,word_id,result,score from yy_word_test_detail where test_id =%s""" % test.id details = db.tbkt_yingyu.fetchall_dict(sql) word_ids = [d.word_id for d in details] if not word_ids: return sql = """select id,word,translation from yy_word where id in(%s)""" % ",".join( str(i) for i in word_ids) words = db.ziyuan_slave.fetchall_dict(sql) word_dict = {w.id: w for w in words} for d in details: d.word = word_dict.get(d.word_id) context = Struct() context.test = test context.right_number = sum(1 for d in details if d.result == 1) context.score = test.score if test else 0 context.wrong_words = [d.word for d in details if d.result == 0 and d.word] context.words_unmber = len(words) return context
def get_answer_sheet(questions=[]): """ 功能说明: 对正确答案进行格式化 {ask_id:{qid:1,right_answer:用户答案}} ----------------------------------------------- 修改人 修改时间 ----------------------------------------------- 张帅男 2016-11-3 """ sheet = [] for q in questions: for ask in q.asks: ask = Struct(ask) row = Struct() row.qid = q.id row.aid = ask.id row.right_answer = '' if ask: sql = "SELECT `option` from yy_ask_options WHERE ask_id = %s and is_right = 1;" % ask.id _ask = db.ziyuan_slave.fetchall_dict(sql) if _ask: _ask = _ask[0] row.right_answer = _ask.option if _ask else '' if ask and q.type == 2: row.right_answer = format_link_answer(ask.options) sheet.append(row) return sheet
def p_yw_user_book(user_id): """ 获取语文教材信息 :param user_id: :return: """ # 获取用户设置的期末活动教材 book = db.tbkt_active.mid_term_user_book.select('book_id').get( user_id=user_id, is_work_book=0, subject_id=51) if book: book_id = book.book_id else: # 获取用户设置教材 _book = db.default.user_book.select('book_id').get(user_id=user_id, is_work_book=0, subject_id=51) if not _book: return {} data = db.tbkt_yw.yw_chinesetextbooks.select("ct_ID").filter( ct_VersionNum__in=("JC001001", "JC001008"), ct_status=0, enable_flag=0).flat("ct_ID")[:] if _book.book_id in data: # 说明用户设置的是人教版 则直接进行保存 db.tbkt_active.mid_term_user_book.create(book_id=_book.book_id, subject_id=51, user_id=user_id, add_time=int(time.time()), is_work_book=0) book_id = _book.book_id else: return {} sql = """ SELECT b.dict_Name name,a.ct_SeesawVolumes volumes,a.ct_Grade grade from yw_chinesetextbooks a INNER JOIN yw_dictionary b ON b.dict_Code = a.ct_VersionNum where a.ct_ID = %s AND a.ct_Status = 0 AND a.enable_flag = 0; """ % book_id info = db.tbkt_yw_slave.fetchone_dict(sql) if not info: return {} out = Struct() out.id = book_id grade = num_to_ch(info.grade.replace("NJ00100", "")) + u'年级' volume = {1: u'上册', 2: u'下册', 3: u'全册'}.get(info.volumes, u'') out.name = u"人教版" + grade + volume out.grade_id = info.grade[-1] return out
def get_test_question_number_sheet(test_id): """获取未完成test的question列表 和 答题卡详情{ask_id:{qid:1,answer:用户答案}},""" sql = """select id,test_id,question_id,ask_id,result,answer,option_id from yy_test_detail where test_id=%s """ % test_id details = db.tbkt_yingyu.fetchall_dict(sql) numbers = [Struct({'qid': d.question_id, 'aid': d.ask_id}) for d in details] qids = [n.qid for n in numbers] if not qids: return 0, 0 """优化""" # sql = """select id from yy_question where id in (%s) ORDER BY type""" % ','.join(str(q) for q in qids) # questions = db.ziyuan_slave.fetchall(sql) # qa_dict = get_aid_dict([q[0] for q in questions]) # que_list = [] # for q in questions: # que_list += qa_dict.get(list(q)[0]) que_list = [d.ask_id for d in details] sheet = {} for d in details: sheet[d.ask_id] = d new_sheet = [sheet.get(d) for d in que_list if sheet.get(d)] new_list = [] for sheet in new_sheet: new_list += [n for n in numbers if n.aid == sheet.ask_id] ask_no = 1 for q in new_list: q.ask_no = ask_no ask_no += 1 # new_list.sort(key=lambda x: x.qid) # new_sheet.sort(key=lambda x: x.question_id) return new_list, new_sheet
def get_paper_result_detail(user_id, active_id, paper_id, type_id): """试卷结果详细,1 全部, 2 错误 """ details = get_paper_detail(user_id, active_id, paper_id) questions, sheet = get_paper_question_detail(details) questions = get_special_user_answer(questions) questions.sort(lambda x, y: cmp(x['id'], y['id'])) ask_no = 1 for q in questions: for a in q.asks: a.no = ask_no ask_no += 1 _question = [] if type_id == 2: for q in questions: if len(q.asks) > 1: n = 0 asks = [] for a in q.asks: if not a.user_result: n += 1 if int(a.user_result) == 1: continue asks.append(a) q.asks = asks if n: _question.append(q) else: if not q.asks[0].user_result: _question.append(q) else: _question = questions _numbers = [ Struct({ 'qid': d.qid, 'aid': d.aid, "result": sheet.get(d.aid).result }) for d in details ] numbers = [] new_sheet = [] _question.sort(lambda x, y: cmp(x['id'], y['id'])) # zsn排序 for q in _question: for a in q.asks: numbers += [n for n in _numbers if n.aid == a.id] sheet.get(a.id) if sheet.get(a.id): sheet.get(a.id).no = a.no new_sheet += [sheet.get(a.id)] data = { "question": _question, "sheet": new_sheet, "numbers": numbers, } return data
def get_active_paper_question_number_sheet(user_id, active_id, paper_id): details = db.tbkt_active_slave.active_paper_detail.select('text')\ .filter(user_id=user_id, active_id=active_id, paper_id=paper_id).last() if not details: return [], [] numbers = [] ask_no = 1 text = details.text text = json.loads(text) text = [Struct(t) for t in text] text.sort(lambda x, y: cmp(x['qid'], y['qid'])) for d in text: number = Struct() number.ask_no = ask_no number.qid = d.qid number.aid = d.aid numbers.append(number) ask_no += 1 qids = [q.qid for q in numbers] if not qids: return [], [] ask_list = [d.aid for d in text] sheet = {} for d in text: detail = {} detail['question_id'] = d.qid detail['ask_id'] = d.aid detail['option_id'] = d.oid detail['answer'] = d.answer detail['result'] = d.result sheet[d.aid] = detail sheets = [sheet[d] for d in ask_list if sheet.get(d)] return numbers, sheets
def p_user_book(user_id, subject_id): """ 用户的教材信息 :param user_id: :param subject_id: :return: """ # 返回用户教材信息 没有设置教材 book = db.tbkt_active.mid_term_user_book.select('book_id').get( user_id=user_id, is_work_book=0, subject_id=subject_id) if book: book_id = book.book_id if not book: # 如果用户未设置教材 _book = db.default.user_book.select('book_id').get( user_id=user_id, is_work_book=0, subject_id=subject_id) result = set_subject_info(user_id, subject_id) if result: db.tbkt_active.mid_term_user_book.create(book_id=_book.book_id, subject_id=subject_id, user_id=user_id, add_time=int(time.time()), is_work_book=0) book_id = _book.book_id else: return sql = ''' select b.id,b.name, p.name name1, b.grade_id from zy_book b left join zy_press p on b.press_id = p.id where b.id = %s; ''' % book_id result = db.ziyuan_slave.fetchone_dict(sql) data = Struct() name = result.name.encode('utf-8') + '(' + result.name1.encode( 'utf-8') + ')' data.name = name data.id = result.id data.grade_id = result.grade_id return data
def get_test(user_id, task_id, paper_id): """ 功能说明: 获得试卷的测试情况, 没有的话,创建记录 ----------------------------------------------- 修改人 修改时间 ----------------------------------------------- 张帅男 2017-4-7 """ sql = """select id, nquestion from yy_test where user_id =%s and object_id=%s """ % ( user_id, task_id) test = db.tbkt_yingyu.fetchone_dict(sql) if test: return test # 创建测试记录 sql = """select id,text from yy_task_detail where task_id = %s and content_type=10 AND text like '%%%s%%'""" % \ (task_id, paper_id) details = db.tbkt_yingyu.fetchall_dict(sql) qids = [] for d in details: text = json.loads(d.text) for i in text: qids.append(i['qid']) print qids # qids.sort() aid_dict = com_method.get_aid_dict(qids) nowt = int(time.time()) sql = """insert into yy_test (user_id,object_id,catalog_id,nquestion,score,status,add_time) VALUES (%s,%s,%s,%s,0,0,%s)""" % (user_id, task_id, paper_id, len(qids), nowt) test_id = db.tbkt_yingyu.execute(sql)[1] test = Struct({'id': test_id, 'nquestion': len(qids)}) # 保存details details = [] for qid in qids: ask_ids = aid_dict.get(qid, []) for ask_id in ask_ids: str = [{"qid": qid, "aid": ask_id}] d = dict(object_id=test['id'], text=json.dumps(str), result=-1) details.append(d) db.tbkt_yingyu.yy_test_detail.bulk_create(details) return test
def get_paper_question_number_sheet(user_id, active_id, paper_id): """获得试卷中的 未完成的习题id和答题卡, 要是没有测试记录的话,创建测试记录""" data = Struct() test_flag = create_test(user_id, active_id, paper_id) if not test_flag: data.numbers = [] data.sheet = [] return data numbers, sheet = com_yy.get_active_paper_question_number_sheet( user_id, active_id, paper_id) data.numbers = numbers data.sheet = sheet return data
def paper_submit(user_id, active_id, paper_id, status, data): """ 提交试卷做题记录 """ nowt = int(time.time()) rows = [Struct(row) for row in data if row] for row in rows: row.question_id = int(row.question_id) row.ask_id = int(row.ask_id) row.option_id = int(row.option_id or 0) row.result = int(row.result) if row.result else 0 rows = {(r.question_id, r.ask_id): r for r in rows} cache_args = [] text_ed = db.tbkt_active_slave.active_paper_detail.select('text').get( user_id=user_id, active_id=active_id, paper_id=paper_id) text_ed = [Struct(t) for t in json.loads(text_ed.text)] # 全部问题 text = [] for row in text_ed: tmp = rows.get((row.qid, row.aid)) or '' if tmp: row.oid = tmp.option_id row.result = tmp.result row.answer = tmp.answer text.append(row) c_arg = (int(row.result), row.answer, int(row.oid), int(row.aid), int(row.qid)) cache_args.append(c_arg) with db.tbkt_active as ac: ac.active_paper_detail.filter(user_id=user_id, active_id=active_id, paper_id=paper_id).update( text=json.dumps(text), add_time=nowt) n = len(cache_args) nright = sum(1 for r in cache_args if r[0] == 1) score = int(100 * nright / n) if n > 0 else 0 ac.active_paper.filter(user_id=user_id, active_id=active_id, paper_id=paper_id).update(score=score, status=status, test_time=nowt) if int(status) == 1: fields = ["result", "answer", "oid", "aid", "qid"] details = [Struct(zip(fields, row)) for row in cache_args] cache_data = Struct() cache_data.details = details cache.hd_paper_result.set((user_id, active_id, paper_id), cache_data)
def test_submit_now(test_id, status, data): """提交答案""" sql = """select count(id) from u_yy_test_detail where test_id =%s""" % test_id details_len = db.tbkt_web.fetchone(sql)[0] if len(data) == details_len: status = 1 rows = [Struct(row) for row in data if row] for row in rows: row.question_id = int(row.question_id) row.ask_id = int(row.ask_id) row.option_id = int(row.option_id or 0) row.result = int(row.result) cache_args = [] for row in rows: c_arg = (row.result, row.answer, row.option_id, test_id, row.ask_id, row.question_id) cache_args.append(c_arg) sql = "update u_yy_test_detail set result=%s, answer=%s, option_id=%s where test_id=%s and ask_id=%s and question_id=%s" db.tbkt_web.execute_many(sql, cache_args) nowt = int(time.time()) n = len(cache_args) nright = sum(1 for r in cache_args if r[0] == 1) score = int(100 * nright / n) if n > 0 else 0 # 保存主记录 sql = """update u_yy_test set score=%s, status=%s,test_time=%s where id =%s""" % (score, status, nowt, test_id) if int(status) == 1: fields = ["result", "answer", "option_id", "test_id", "ask_id", "question_id"] details = [Struct(zip(fields, row)) for row in cache_args] test = {"id": test_id, "status": status} cache_data = Struct() cache_data.details = details cache_data.test = test cache.yy_test_result.set(test_id, cache_data) db.tbkt_web.execute(sql)
def get_answer_sheet_list(test_id, questions=[]): """ 功能说明: 返回答题卡 {ask_id:{qid:1,answer:用户答案,right_answer:正确答案}} ----------------------------------------------- 修改人 修改时间 ----------------------------------------------- 王晨光 2016-8-3 张帅男 2017-1-7 取消orm, 获得一道题的答案 """ ask_dict = {} for q in questions: for a in q.asks: a.qtype = q.type ask_dict[a.id] = a sheet = [] cache_data = cache.yy_test_result.get(int(test_id)) if cache_data: details = [Struct(d) for d in cache_data["details"]] else: sql = "SELECT id, question_id, ask_id, result, answer, option_id from yy_test_detail " \ "WHERE test_id = %s" % test_id details = db.tbkt_yingyu.fetchall_dict(sql) details.sort(key=lambda x: x.question_id) for d in details: if not ask_dict.has_key(d.ask_id): continue row = Struct() row.qid = d.question_id row.answer = d.answer row.result = d.result row.option_id = d.option_id row.right_answer = '' # 补充连线题原题答案 ask = ask_dict.get(d.ask_id) if ask: sql = "SELECT id, `option` as op from yy_ask_options WHERE ask_id = %s and is_right = 1;" % ask.id _ask = db.ziyuan_slave.fetchall_dict(sql) if _ask: _ask = _ask[0] row.right_answer = _ask.op if _ask else '' if ask and ask.qtype == 2: row.right_answer = format_link_answer(ask.options) # sheet[d.ask_id] = row row.ask_id = d.ask_id sheet.append(row) # sheet.sort(lambda x, y: (cmp(x.qid, y.qid))) return sheet
def get_dialog(cid): '情景对话' if not cid: return data = cache.yy_chapter_dialog.get(cid) if data: data = Struct(data) data.roles = [Struct(r) for r in data.roles if r] sentences = [] for r in data.sentences: r = Struct(r) r.role = Struct(r.role) if r.role else None sentences.append(r) data.sentences = sentences else: sql = """select id,title from yy_standard_pool where status<>-1 and catalog_id=%s and type in (1,3) and tts=1""" % cid lesson = db.ziyuan_slave.fetchone_dict(sql) if not lesson: return sql = """select id,pool_id,text,audio_text,translation,male_audio,female_audio,is_key,role_id,sequence,line,duration from yy_standard_pool_detail s where s.pool_id=%s and s.status<>-1 ORDER BY s.sequence """ % lesson.id sentences = db.ziyuan_slave.fetchall_dict(sql) role_dict = {} roles_list = [i.role_id for i in sentences if i.role_id] if roles_list: sql = """select r.id ,r.name_en,r.name_zh,r.code,r.image_id,r.sequence,i.portrait, i.gif_1, i.gif_2 from yy_role r LEFT JOIN yy_role_image i on i.id=r.image_id where r.id in (%s)""" % ','.join(str(i) for i in roles_list) rows = db.ziyuan_slave.fetchall_dict(sql) role_dict = {r.id: r for r in rows} for s in sentences: s.role = role_dict.get(s.role_id) path = s.female_audio or s.male_audio if path: s.audio = format_ziyuan_url(path) roles = OrderedDict() for s in sentences: if s.role and s.role.id not in roles: roles[s.role.id] = s.role if s.role.gif_1: s.role.gif_1 = format_ziyuan_url(s.role.gif_1) if s.role.gif_2: s.role.gif_2 = format_ziyuan_url(s.role.gif_2) s.role.portrait = format_ziyuan_url(s.role.portrait) rename_role(sentences) data = Struct() data.title = lesson.title data.roles = [] for role in roles.values(): row = Struct() row.id = role.id row.name_en = role.name_en row.portrait = role.portrait row.gif_1 = role.gif_1 row.gif_2 = role.gif_2 data.roles.append(row) data.sentences = [] for s in sentences: row = Struct() row.id = s.id row.role_id = s.role.id if s.role else None row.name_en = 'A' if s.role and s.role.name_en: row.name_en = s.role.name_en row.audio = s.audio row.audio_text = s.audio_text row.text = s.text row.translation = s.translation row.duration = math.ceil(s.duration) data.sentences.append(row) cache.yy_chapter_dialog.set(cid, data) return data
def test_submit(test_id, status, data): """"提交答案""" sql = """select count(id) from u_yy_test_detail where test_id =%s""" % test_id details_len = db.tbkt_web.fetchone(sql)[0] if len(data) == details_len: status = 1 rows = [Struct(row) for row in data if row] for row in rows: row.question_id = int(row.question_id) row.ask_id = int(row.ask_id) row.option_id = int(row.option_id or 0) nowt = int(time.time()) sql = """select question_id from u_yy_test_detail where test_id=%s""" % test_id question_ids = db.tbkt_web.fetchall(sql) question_ids = [q[0] for q in question_ids] questions = com_yy.get_task_questions_data(question_ids) q_dict = {q.id: q for q in questions} a_dict = {} for q in questions: for a in q.asks: a_dict[a.id] = a sql_args = [] # 遍历:判断对错 cache_args = [] for row in rows: q = q_dict.get(row.question_id) a = a_dict.get(row.ask_id) if status != 1 and not row.answer: result = -1 else: result = check_user_answer(q, a, row.answer, row.option_id) # arg = (result, row.answer, row.option_id, test_id, row.ask_id) db.tbkt_web.u_yy_test_detail.filter(test_id=test_id, ask_id=row.ask_id).update( result=result, answer=row.answer, option_id=row.option_id ) c_arg = (result, row.answer, row.option_id, test_id, row.ask_id, row.question_id) # sql_args.append(arg) cache_args.append(c_arg) # sql = """ # UPDATE u_yy_test_detail SET result=%s, answer=%s, option_id=%s # WHERE test_id=%s AND ask_id=%s # """ # db.tbkt_web.execute_many(sql, sql_args) n = len(sql_args) nright = sum(1 for r in sql_args if r[0] == 1) score = int(100 * nright / n) if n > 0 else 0 # 保存主记录 sql = """update u_yy_test set score=%s, status=%s,test_time=%s where id =%s""" % (score, status, nowt, test_id) if int(status) == 1: fields = ["result", "answer", "option_id", "test_id", "ask_id", "question_id"] details = [Struct(zip(fields, row)) for row in cache_args] test = {"id": test_id, "status": status} cache_data = Struct() cache_data.details = details cache_data.test = test # cache.yy_test_result.set(test_id, cache_data) db.tbkt_web.execute(sql)
def get_question_ask(question): """ 功能说明: 获取问题的小问 ----------------------------------------------- 修改人 修改时间 ----------------------------------------------- 张帅男 2017-12-21 """ asks = db.ziyuan_slave.yy_question_ask.\ select('id', 'subject', 'parse', 'video_url', 'video_image', 'listen_audio', 'listen_text', 'duration').\ filter(question_id=question.id)[:] ask_ids = [a.id for a in asks] options = db.ziyuan_slave.yy_ask_options.\ select('id', 'ask_id', 'content', 'image', '`option`', 'is_right', 'link_id').\ filter(ask_id__in=ask_ids).order_by('id')[:] for o in options: o.image = get_absurl(o.image) for ask in asks: ask.subject = json.loads(ask.subject) ask.parse = json.loads(ask.parse) ask.video_url = get_videourl(ask.video_url) ask.video_image = get_absurl(ask.video_image) ask.listen_audio = get_absurl(ask.listen_audio) ask.options = [o for o in options if o.ask_id == ask.id] right_options = [o for o in ask.options if o.is_right] ask.right_option = right_options[0] if right_options else None ask.subject = Struct(ask.subject) _ask_images = [] if ask.subject["images"]: for img in ask.subject["images"]: _ask_images.append(get_absurl(img)) ask.subject["images"] = _ask_images if ask.right_option and ask.right_option.get("image"): ask.right_option["image"] = get_absurl(ask.right_option["image"]) _options = [] for _option in ask.options: if _option.get("image"): _option["image"] = get_absurl(_option["image"]) _options.append(_option) ask.options = _options # 补充一些特殊题型的格式 if question.type == 9: # 填空题: 把<span></span>里的东西挖出来, 用____替换 words = re.findall(r'<span .*?>(.*?)</span>', ask.subject.content) ask.answer = words ask.subject.content = re.sub(r'<span .*?>(.*?)</span>', u'<input placeholder="点击填写">', ask.subject.content) elif question.type == 10: # 连词成句: 打乱分词顺序 words = re.split(r'<.*?>', ask.subject.content) words = [w.strip() for w in words] words = [w for w in words if w] answer = words[:] random.shuffle(words) ask.subject.content = ",".join(w for w in words) ask.words = words ask.answer = answer ask.answer_orders = {w: i for i, w in enumerate(answer)} elif question.type == 11: # 连句成段: 打乱句子顺序 sentences = re.split(r'<.*?>', ask.subject.content) sentences = [s.strip() for s in sentences] sentences = [s for s in sentences if s] _sentences = [] for s in sentences: if s and s.startswith("("): s = re.sub(r"\(.*?\)", "", s) _sentences.append(s) sentences = _sentences answer = sentences[:] random.shuffle(sentences) ask.sentences = sentences ask.subject.content = ",".join(s for s in sentences) ask.answer = answer ask.answer_orders = {i: s for i, s in enumerate(answer)} elif question.type == 2: # 连线题 options = [op for op in ask.options if op.link_id >= 0] answer = [] for obj in options: a = str(obj['id']) + '-' + str(obj['link_id']) answer.append(a) answer = '|'.join(answer) ask.answer = answer else: # 其他当成选择题 ask.answer = '' for op in ask.options: if op.is_right: ask.answer = op.option break if question.type == 4: # 判断正误题选项内容: A正确 B错误 for op in ask.options: if op.option == u'A': op.content = u'正确' elif op.option == u'B': op.content = u'错误' return asks
def get_questions_data(qid_list): """返回多道习题的数据""" if not qid_list: return questions = db.ziyuan_slave.yy_question.filter(id__in=qid_list) \ .select('id', 'subject', 'classify', 'type', 'video_url', 'video_image') questions = [Struct(q) for q in questions if q] if not questions: return qid_list = [q.id for q in questions] asks = db.ziyuan_slave.yy_question_ask.filter(question_id__in=qid_list) \ .select('id', 'subject', 'parse', 'video_url', 'video_image', 'listen_audio', 'listen_text', 'question_id', 'duration') asks = [Struct(a) for a in asks if a] ask_ids = [a.id for a in asks] options = db.ziyuan_slave.yy_ask_options.filter(ask_id__in=ask_ids) \ .select('id', 'ask_id', 'content', 'image', '`option`', 'is_right', 'link_id') options = [Struct(o) for o in options if o] for o in options: o.image = get_absurl(o.image) for ask in asks: ask.subject = json.loads(ask.subject) ask.parse = json.loads(ask.parse) ask.video_url = get_videourl(ask.video_url) ask.video_image = get_absurl(ask.video_image) ask.listen_audio = get_absurl(ask.listen_audio) ask.options = [o for o in options if o.ask_id == ask.id] right_options = [o for o in ask.options if o.is_right] ask.right_option = right_options[0] if right_options else None ask.subject = Struct(ask.subject) _ask_images = [] if ask.subject["images"]: for img in ask.subject["images"]: _ask_images.append(get_absurl(img)) ask.subject["images"] = _ask_images if ask.right_option and ask.right_option.get("image"): ask.right_option["image"] = get_absurl(ask.right_option["image"]) _options = [] for _option in ask.options: if _option.get("image"): _option["image"] = get_absurl(_option["image"]) _options.append(_option) ask.options = _options asks_dict = OrderedDict() for a in asks: asks_dict[a.question_id] = asks_dict.get(a.question_id, []) + [a] for question in questions: question.asks = asks_dict.get(question.id) for ask in question.asks: # 补充一些特殊题型的格式 if question.type == 9: # 填空题: 把<span></span>里的东西挖出来, 用____替换 words = re.findall(r'<span .*?>(.*?)</span>', ask.subject.content) ask.answer = words ask.subject.content = re.sub(r'<span .*?>(.*?)</span>', '<input>', ask.subject.content) elif question.type == 10: # 连词成句: 打乱分词顺序 words = re.split(r'<.*?>', ask.subject.content) words = [w.strip() for w in words] words = [w for w in words if w] answer = words[:] random.shuffle(words) ask.subject.content = ",".join(w for w in words) ask.words = words ask.answer = answer elif question.type == 11: # 连句成段: 打乱句子顺序 sentences = re.split(r'<.*?>', ask.subject.content) sentences = [s.strip() for s in sentences] sentences = [s for s in sentences if s] _sentences = [] for s in sentences: if s and s.startswith("("): s = re.sub(r"\(.*?\)", "", s) _sentences.append(s) sentences = _sentences answer = sentences[:] random.shuffle(sentences) ask.sentences = sentences ask.subject.content = ",".join(s for s in sentences) ask.answer = answer ask.answer_orders = {i: s for i, s in enumerate(answer)} else: # 其他当成选择题 ask.answer = '' for op in ask.options: if op.is_right: ask.answer = op.option break if question.type == 4: # 判断正误题选项内容: A正确 B错误 for op in ask.options: if op.option == u'A': op.content = u'正确' elif op.option == u'B': op.content = u'错误' # 补充一些特殊题型的格式 special_handle_question(question) return questions
def get_task_details(test_id): """返回 完成的 习题答案和 答题卡详情(补充正确答案)""" cache_data = cache.yy_test_result.get(int(test_id)) if cache_data: details = [Struct(d) for d in cache_data["details"]] else: sql = """select id,test_id,question_id,ask_id,result,answer,option_id from yy_test_detail where test_id=%s""" % test_id details = db.tbkt_yingyu.fetchall_dict(sql) # 大题ID列表 qid_list = [] for d in details: qid_list.append(d.question_id) qid_list = list(set(qid_list)) questions = get_task_questions_data(qid_list) # questions.sort(lambda x, y: cmp((x.classify, x.type), (y.classify, y.type))) # 补充用户答案 补充题号 detail_dict = {d.ask_id: d for d in details} ask_no = 1 for q in questions: for a in q.asks: a.no = ask_no ask_no += 1 # 结果页不显示"点击选择" if q.type == 7: q.subject.content = q.subject.content.replace(u'点击选择', u'') for a in q.asks: d = detail_dict.get(a.id) a.user_result = d.result if d else -1 a.user_answer = d.answer if d else '' if q.type == 9: # 填空题 user_answer = a.user_answer.split('|') user_answer = [s for s in user_answer if s] a.user_answer = user_answer elif q.type in (10, 11): # 连词连句 indexs = a.user_answer.split('|') user_answer = [] for n, i in enumerate(indexs): if i.isdigit(): i = int(i) ans = a.answer[i] if i != n: ans = "<em style='color:red'>%s</em>" % ans else: ans = "<em>%s</em>" % ans user_answer.append(ans) a.user_answer = user_answer a.user_option_id = d.option_id if d else 0 user_options = [o for o in a.options if o.id == a.user_option_id] a.user_option = user_options[0] if user_options else None if q.type == 7: # 完形 # 避免重复格式化浪费时间 if not q.answer: q.answer = [ a.right_option.option + ':' + a.right_option.content for a in q.asks if a ] q.user_answer = [a.user_answer or '' for a in q.asks] ask_dict = {} for q in questions: for a in q.asks: a.qtype = q.type ask_dict[a.id] = a sheet = Struct() for d in details: d.right_answer = '' # 补充连线题原题答案 ask = ask_dict.get(d.ask_id) if ask: d.right_answer = ask.answer if ask and ask.qtype == 2: rows = [ '%s-%s' % (o.id, o.link_id) for o in ask.options if o.link_id >= 0 ] d.right_answer = '|'.join(rows) sheet[d.ask_id] = d questions.sort(key=lambda x: x.id) return questions, sheet
def get_lesson(chapter): """获得课文数据""" lesson = cache.yy_chapter_lesson.get(chapter.id) if lesson: lesson = Struct(lesson) lesson.chapter = Struct(lesson.chapter) if lesson.chapter else None sentences = [] for r in lesson.sentences: r = Struct(r) r.role = Struct(r.role) if r.role else None sentences.append(r) lesson.sentences = sentences else: sql = """select id,title from yy_standard_pool where status<>-1 and catalog_id=%s and type in (1,3) and tts=1""" % chapter.id lesson = db.ziyuan_slave.fetchone_dict(sql) if not lesson: return sql = """select id, pool_id,text,audio_text,translation,male_audio,female_audio,is_key,role_id,duration from yy_standard_pool_detail s where s.pool_id=%s and s.status<>-1 ORDER BY s.sequence """ % lesson.id sentences = db.ziyuan_slave.fetchall_dict(sql) role_dict = {} roles_list = [i.role_id for i in sentences if i.role_id] if roles_list: sql = """select r.id ,r.name_en,r.name_zh,r.image_id,i.portrait from yy_role r LEFT JOIN yy_role_image i on i.id=r.image_id where r.id in (%s)""" % ",".join(str(i) for i in roles_list) rows = db.ziyuan_slave.fetchall_dict(sql) role_dict = {r.id: r for r in rows} for s in sentences: s.role = role_dict.get(s.role_id) path = s.female_audio or s.male_audio if path: s.audio = format_ziyuan_url(path) del s.female_audio del s.male_audio roles = OrderedDict() for s in sentences: if s.role and s.role.id not in roles: roles[s.role.id] = s.role s.role.portrait = format_ziyuan_url(s.role.portrait) s.name_en = s.role.name_en rename_role(sentences) for s in sentences: if not s.name_en: s.name_en = "A" lesson.sentences = sentences lesson.nrole = get_role_count(chapter, sentences) lesson.has_translation = False if sentences: lesson.has_translation = bool(sentences[0].translation) lesson.chapter = chapter cache.yy_chapter_lesson.set(chapter.id, lesson) return lesson
def get_task_questions_data(qid_list): """ 功能说明: 返回全都大题的数据 """ if not qid_list: return sql = """select id, subject ,classify,type,video_url,video_image from yy_question where id in(%s) """ % ','.join( str(i) for i in qid_list) questions = db.ziyuan_slave.fetchall_dict(sql) if not questions: return sql = """select id,subject,parse,video_url, video_image,listen_audio,listen_text,question_id,duration from yy_question_ask where question_id in(%s)""" % ','.join(str(q.id) for q in questions) asks = db.ziyuan_slave.fetchall_dict(sql) ask_ids = [a.id for a in asks] sql = """select id,ask_id,content,image,`option`,is_right,link_id from yy_ask_options where ask_id in (%s) ORDER BY id""" % ','.join(str(a) for a in ask_ids) options = db.ziyuan_slave.fetchall_dict(sql) for o in options: o.image = format_ziyuan_url(o.image) for ask in asks: ask.subject = json.loads(ask.subject) ask.parse = json.loads(ask.parse) ask.video_url = format_ziyuan_url(ask.video_url) ask.video_image = format_ziyuan_url(ask.video_image) ask.listen_audio = format_ziyuan_url(ask.listen_audio) ask.options = [o for o in options if o.ask_id == ask.id] right_options = [o for o in ask.options if o.is_right] ask.right_option = right_options[0] if right_options else None ask.subject = Struct(ask.subject) _ask_images = [] if ask.subject["images"]: for img in ask.subject["images"]: _ask_images.append(format_ziyuan_url(img)) ask.subject["images"] = _ask_images if ask.right_option and ask.right_option.get("image"): ask.right_option["image"] = format_ziyuan_url( ask.right_option["image"]) _options = [] for _option in ask.options: if _option.get("image"): _option["image"] = format_ziyuan_url(_option["image"]) _options.append(_option) ask.options = _options asks_dict = OrderedDict() for a in asks: asks_dict[a.question_id] = asks_dict.get(a.question_id, []) + [a] for question in questions: question.asks = asks_dict.get(question.id) for ask in question.asks: # 补充一些特殊题型的格式 if question.type == 9: # 填空题: 把<span></span>里的东西挖出来, 用____替换 words = re.findall(r'<span .*?>(.*?)</span>', ask.subject.content) ask.answer = words ask.subject.content = re.sub(r'<span .*?>(.*?)</span>', '<input>', ask.subject.content) elif question.type == 10: # 连词成句: 打乱分词顺序 words = re.split(r'<.*?>', ask.subject.content) words = [w.strip() for w in words] words = [w for w in words if w] answer = words[:] random.shuffle(words) ask.subject.content = ",".join(w for w in words) ask.words = words ask.answer = answer elif question.type == 11: # 连句成段: 打乱句子顺序 sentences = re.split(r'<.*?>', ask.subject.content) sentences = [s.strip() for s in sentences] sentences = [s for s in sentences if s] _sentences = [] for s in sentences: if s and s.startswith("("): s = re.sub(r"\(.*?\)", "", s) _sentences.append(s) sentences = _sentences answer = sentences[:] random.shuffle(sentences) ask.sentences = sentences ask.subject.content = ",".join(s for s in sentences) ask.answer = answer ask.answer_orders = {i: s for i, s in enumerate(answer)} else: # 其他当成选择题 ask.answer = '' for op in ask.options: if op.is_right: ask.answer = op.option break if question.type == 4: # 判断正误题选项内容: A正确 B错误 for op in ask.options: if op.option == u'A': op.content = u'正确' elif op.option == u'B': op.content = u'错误' # 补充一些特殊题型的格式 special_handle_question(question) return questions
def get_class_students_all(unit_class_id): """ 功能说明: 获取班级所有学生 ---------------------------------------------------------------------------- 修改人 修改时间 修改原因 ---------------------------------------------------------------------------- 张帅男 2017-1-7 增加首字母选项 :param unit_class_id: 班级ID 或 班级ID列表 """ if not isinstance(unit_class_id, list): unit_class_id = [unit_class_id] if unit_class_id and (not all([int(u) for u in unit_class_id])): return [] unit_class_id = [str(u) for u in unit_class_id] sql = """ select b.id, b.user_id, b.user_name, b.phone_number, u.portrait, p.sex from ketang.mobile_order_region rg inner join ketang.mobile_user_bind b on b.id=rg.user_bind_id and b.type=1 left join tbkt.auth_user u on u.id=b.user_id left join tbkt.account_profile p on p.user_id=u.id where rg.unit_class_id in (%s) and rg.is_update=0 """ % ','.join(unit_class_id) rows = db.slave.fetchall(sql) users = [] for row in rows: bind_id, uid, real_name, phone, portrait, sex = row student = Struct() student.id = uid or 0 student.bind_id = bind_id student.real_name = real_name or '' student.type = 1 student.portrait = portrait or '' student.portrait = get_portrait(portrait, 1) student.phone = phone or '' student.sex = sex or 1 student.first_name = get_first_pinyin(real_name) users.append(student) return users
def get_paper_question_detail(details): """获取试卷问题详情,和答题卡""" qid_list = [d.qid for d in details if d] questions = get_questions_data(qid_list) detail_dict = {d.aid: d for d in details} ask_no = 1 for q in questions: for a in q.asks: a.no = ask_no ask_no += 1 # 结果页不显示"点击选择" if q.type == 7: q.subject.content = q.subject.content.replace(u'点击选择', u'') for a in q.asks: d = detail_dict.get(a.id) a.user_result = d.result if d else -1 a.user_answer = d.answer if d else '' if q.type == 9: # 填空题 user_answer = a.user_answer.split('|') user_answer = [s for s in user_answer if s] a.user_answer = user_answer elif q.type in (10, 11): # 连词连句 indexs = a.user_answer.split('|') user_answer = [] for n, i in enumerate(indexs): if i.isdigit(): i = int(i) ans = a.answer[i] if i != n: ans = "<em style='color:red'>%s</em>" % ans else: ans = "<em>%s</em>" % ans user_answer.append(ans) a.user_answer = user_answer a.user_option_id = d.option_id if d else 0 user_options = [o for o in a.options if o.id == a.user_option_id] a.user_option = user_options[0] if user_options else None if q.type == 7: # 完形 # 避免重复格式化浪费时间 if not q.answer: q.answer = [ a.right_option.option + ':' + a.right_option.content for a in q.asks if a ] q.user_answer = [a.user_answer or '' for a in q.asks] ask_dict = {} for q in questions: for a in q.asks: a.qtype = q.type ask_dict[a.id] = a sheet = Struct() for d in details: d.right_answer = '' # 补充连线题原题答案 ask = ask_dict.get(d.aid) if ask: d.right_answer = ask.answer if ask and ask.qtype == 2: rows = [ '%s-%s' % (o.id, o.link_id) for o in ask.options if o.link_id >= 0 ] d.right_answer = '|'.join(rows) sheet[d.aid] = d return questions, sheet
def get_write_result(user_id, task_detail): """"返回单词测听写作业结果""" cache_data = cache.yy_study_result.get((user_id, task_detail.id)) if cache_data: study = Struct(cache_data["study"]) test_details = [Struct(r) for r in cache_data["details"]] else: sql = """select id,score from yy_study where user_id=%s and object_id=%s """ % ( user_id, task_detail.id) study = db.tbkt_yingyu.fetchone_dict(sql) if not study: return sql = """select id,object_id,score,answer,result,remote_audio from yy_study_detail where study_id=%s""" % study.id test_details = db.tbkt_yingyu.fetchall_dict(sql) word_ids = [d.object_id for d in test_details] sql = """select id, translation, male_audio,female_audio,phonetic,word,duration from yy_word where id in (%s)""" % ",".join( str(i) for i in word_ids) words = db.ziyuan_slave.fetchall_dict(sql) for word in words: word.female_audio = format_ziyuan_url(word.female_audio) word.male_audio = format_ziyuan_url(word.male_audio) word.audio = word.female_audio or word.male_audio word_dict = dict((w.id, w) for w in words) data = Struct() data.grade = score_to_en(study.score) rows = [] for d in test_details: row = Struct() word = word_dict[d.object_id] row.text = word.word.strip() row.phonetic = word.phonetic row.translation = word.translation row.mgrade = score_to_en(d.score) row.audio = word.audio row.user_answer = d.answer row.user_audio = d.remote_audio.strip() if d.remote_audio else None row.result = d.result rows.append(row) data.words = rows return data
def get_question_ask(question, ask_no): """返回大题小问""" sql = """select id,subject,parse,video_url, video_image,listen_audio,listen_text,duration from yy_question_ask where question_id=%s""" % question.id asks = db.ziyuan_slave.fetchall_dict(sql) ask_ids = [a.id for a in asks] sql = """select id,ask_id,content,image,`option`,is_right,link_id from yy_ask_options where ask_id in (%s) ORDER BY id""" % ','.join( str(a) for a in ask_ids) options = db.ziyuan_slave.fetchall_dict(sql) for o in options: o.image = format_ziyuan_url(o.image) for ask in asks: ask.subject = json.loads(ask.subject) ask.parse = json.loads(ask.parse) ask.video_url = format_ziyuan_url(ask.video_url) ask.video_image = format_ziyuan_url(ask.video_image) ask.listen_audio = format_ziyuan_url(ask.listen_audio) ask.options = [o for o in options if o.ask_id == ask.id] right_options = [o for o in ask.options if o.is_right] ask.right_option = right_options[0] if right_options else None ask.subject = Struct(ask.subject) _ask_images = [] if ask.subject["images"]: for img in ask.subject["images"]: _ask_images.append(format_ziyuan_url(img)) ask.subject["images"] = _ask_images if ask.right_option and ask.right_option.get("image"): ask.right_option["image"] = format_ziyuan_url( ask.right_option["image"]) _options = [] for _option in ask.options: if _option.get("image"): _option["image"] = format_ziyuan_url(_option["image"]) _options.append(_option) ask.options = _options # 补充一些特殊题型的格式 if question.type == 9: # 填空题: 把<span></span>里的东西挖出来, 用____替换 words = re.findall(r'<span .*?>(.*?)</span>', ask.subject.content) ask.answer = words ask.subject.content = re.sub(r'<span .*?>(.*?)</span>', u'<input placeholder="点击填写">', ask.subject.content) elif question.type == 10: # 连词成句: 打乱分词顺序 words = re.split(r'<.*?>', ask.subject.content) words = [w.strip() for w in words] words = [w for w in words if w] answer = words[:] random.shuffle(words) ask.subject.content = ",".join(w for w in words) ask.words = words ask.answer = answer elif question.type == 11: # 连句成段: 打乱句子顺序 sentences = re.split(r'<.*?>', ask.subject.content) sentences = [s.strip() for s in sentences] sentences = [s for s in sentences if s] _sentences = [] for s in sentences: if s and s.startswith("("): s = re.sub(r"\(.*?\)", "", s) _sentences.append(s) sentences = _sentences answer = sentences[:] random.shuffle(sentences) ask.sentences = sentences ask.subject.content = ",".join(s for s in sentences) ask.answer = answer ask.answer_orders = {i: s for i, s in enumerate(answer)} elif question.type == 2: # 连线题 options = [op for op in ask.options if op.link_id >= 0] answer = [] for obj in options: a = str(obj['id']) + '-' + str(obj['link_id']) answer.append(a) answer = '|'.join(answer) ask.answer = answer else: # 其他当成选择题 ask.answer = '' for op in ask.options: if op.is_right: ask.answer = op.option break if question.type == 4: # 判断正误题选项内容: A正确 B错误 for op in ask.options: if op.option == u'A': op.content = u'正确' elif op.option == u'B': op.content = u'错误' return asks