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 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 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)