def new_class(call, lang): d = UI.to_dtime(time.time()) cw_id = Course.Classwork(call.data['c_id'], name=d, date=d).number for user in Course.Course(call.data['c_id']).participants: Course.Attendance(call.data['c_id'], cw_id, user.id).value = 0 back(call, True)
def attend(call, lang): if bool(call.data.get('u_id')): Course.Attendance(call.data['c_id'], call.data['cw_id'], call.data['u_id']).value = \ not Course.Attendance(call.data['c_id'], call.data['cw_id'], call.data['u_id']).value else: for user in Course.Course(call.data['c_id']).participants: Course.Attendance(call.data['c_id'], call.data['cw_id'], user.id).value = \ not Course.Attendance(call.data['c_id'], call.data['cw_id'], user.id).value back(call, True)
def switch_lock(call, lang): if call.data['lock']: Course.Course(call.data['c_id']).entry_restriction = time.time() bot.answer_callback_query(call.id, msgs[lang]['callback']['course_closed']) else: Course.Course(call.data['c_id']).entry_restriction = None bot.answer_callback_query(call.id, msgs[lang]['callback']['course_opened']) back(call, True)
def del_task(call, lang): if call.message.chat.id == Course.Course(call.data['c_id']).owner.id: Course.Task(call.data['c_id'], call.data['t_id']).delete() bot.answer_callback_query(call.id, msgs[lang]['callback']['task_deleted']) else: bot.answer_callback_query(call.id, msgs[lang]['callback']['task_not_exists'], show_alert=True) back(call, True, 2)
def create(): name = task_info['name'] if not name: name = msgs[lang]['teacher']['task_create']['name'] + str( len(Course.Course(call.data['c_id']).tasks) + 1) Course.Task(course_id=call.data['c_id'], name=name, description=task_info['desc'].format(task_info['hmark']), highest_mark=task_info['hmark']) botHelper.send_mes(msgs[lang]['teacher']['task_create']['created'], call.message.chat.id) return_to_menu()
def task(call, lang): course_ = Course.Course(call.data['c_id']) task_ = course_.task(call.data['t_id']) p = UI.Paging(course_.participants, sort_key='name') text = msgs[lang]['teacher']['management']['task'].format(name=task_.name, hmark=int(task_.highest_mark)) \ + p.msg(call.data['page'], lang) c_text = msgs[lang]['confirm']['delete_task'].format(task_.name) markup = mkp.create_listed( lang, tbt.user_tasks_list(p.list(call.data['page']), call.data['c_id'], call.data['t_id']), tbt.task, call.data['c_id'], call.data['t_id'], lang, call.data['page']) mkp.add_before_back( markup, cbt.confirm_action('del_task', msgs[lang]['buttons']['confirm']['del_task'], c_text, call.message.chat.id, call.message.message_id, c_id=call.data['c_id'], t_id=call.data['t_id'])) botHelper.edit_mes(text, call, markup=markup)
def cw(call, lang): course_ = Course.Course(call.data['c_id']) classwork = course_.classwork(call.data['cw_id']) p = UI.Paging(course_.participants, sort_key='name') text = msgs[lang]['teacher']['management']['classwork'].format( date=classwork.date) + p.msg(call.data['page'], lang) c_text = msgs[lang]['confirm']['delete_class'].format(classwork.name) markup = mkp.create_listed( lang, tbt.user_attendance_list(p.list(call.data['page']), call.data['c_id'], call.data['cw_id']), tbt.classwork, call.data['c_id'], call.data['cw_id'], lang, call.data['page']) mkp.add_before_back( markup, tbt.invert_attendance(call.data['c_id'], call.data['cw_id'], lang)) mkp.add_before_back( markup, tbt.change_cw_date(call.data['c_id'], call.data['cw_id'], lang)) mkp.add_before_back( markup, cbt.confirm_action('del_class', msgs[lang]['buttons']['confirm']['del_class'], c_text, call.message.chat.id, call.message.message_id, c_id=call.data['c_id'], cw_id=call.data['cw_id'])) botHelper.edit_mes(text, call, markup=markup)
def course_owner(call, lang): course_ = Course.Course(call.data['c_id']) text = msgs[lang]['teacher']['course_owner_full'].format( name=course_.name, num=len(course_.participants), lock=UI.to_dtime(course_.entry_restriction), desc=course_.description) c_text = msgs[lang]['confirm']['delete_course'].format(course_.name) markup = mkp.create( lang, [tbt.user_list(call.data['c_id'], lang)], [tbt.task_list(call.data['c_id'], lang)], [tbt.classwork_list(call.data['c_id'], lang)], [tbt.announce(call.data['c_id'], lang)], [ tbt.switch_lock(call.data['c_id'], True if course_.is_open else False, lang) ], [ cbt.confirm_action( 'delete_course', msgs[lang]['buttons']['confirm']['delete_course'], c_text, call.message.chat.id, call.message.message_id, c_id=course_.id) ]) botHelper.edit_mes(text, call, markup=markup)
def get_date(message): if message.text == '/exit': return_to_menu() elif re.fullmatch(r'\d{1,2}\s\d{1,2}\s\d{1,2}', message.text): in_date = message.text.split() cw_ = Course.Classwork(call.data['c_id'], call.data['cw_id']) try: date = datetime.date(2000 + int(in_date[0]), int(in_date[1]), int(in_date[2])).strftime('%d %b %Y') except ValueError: botHelper.send_mes( msgs[get_lang(call.message.chat.id)]['teacher'] ['management']['wrong_input'], message.messasge_id) bot.register_next_step_handler(message, get_date) else: cw_.date = date cw_.name = date return_to_menu() else: botHelper.send_mes( msgs[get_lang(call.message.chat.id)]['teacher']['management'] ['wrong_input'], message.messasge_id) bot.register_next_step_handler(message, get_date)
def course_list(call, lang): if call.data['type'] == 'all': # TODO не добавлять закрытые курсы courses = [i for i in Course.fetch_all_courses()] text = msgs[lang]['common']['all'] elif call.data['type'] == 'my': courses = [i for i in User.User(call.message.chat.id).participation] text = msgs[lang]['common']['my'] elif call.data['type'] == 'teach': courses = [i for i in User.User(call.message.chat.id).possessions] text = msgs[lang]['common']['teach'] else: botHelper.error(call=call) return page = call.data['page'] p = UI.Paging(courses, sort_key='name') text += p.msg(call.data['page'], lang) if call.data['type'] == 'teach': markup = mkp.create_listed(lang, tbt.courses(p.list(page)), tbt.manage_list, lang, page) else: markup = mkp.create_listed(lang, cbt.courses(p.list(page)), cbt.course_list_of, call.data['type'], lang, page) botHelper.edit_mes(text, call, markup=markup)
def classwork(c_id, cw_id, page=0): return InlineKeyboardButton(Course.Classwork(c_id, cw_id).name, callback_data=dumps( dict(G='cw', c_id=c_id, cw_id=cw_id, page=page)))
def task(c_id, t_id, page=0): return InlineKeyboardButton(Course.Task(c_id, t_id).name, callback_data=dumps( dict(G='task', c_id=c_id, t_id=t_id, page=page)))
def st_tsk(call, lang): task_ = Course.Task(call.data['c_id'], call.data['t_id']) text = msgs[lang]['student']['student_task'].format( name=task_.name, desc=task_.description, mark=task_.mark(call.message.chat.id).value, hmark=int(task_.highest_mark)) botHelper.edit_mes(text, call, markup=mkp.create(lang))
def __init__(self): self.__courses_list=[] with open("courses.csv", mode = 'r+') as co: for line in co.read()[:-1].split("\n"): id_c, name, instructor=line.split(',') c=Course(id_c,name,instructor) self.__courses_list.append(c)
def st_task_list(call, lang): p = UI.Paging(Course.Course(call.data['c_id']).tasks, sort_key='name') text = msgs[lang]['student']['task_list'] + p.msg(call.data['page'], lang) markup = mkp.create_listed(lang, cbt.tasks(p.list(call.data['page'])), cbt.task_list, call.data['c_id'], lang, call.data['page']) botHelper.edit_mes(text, call, markup=markup)
def user_attendance_list(users_list, c_id, cw_id): cw = Course.Classwork(c_id, cw_id) arr = [] for usr in users_list: arr.append( InlineKeyboardButton( usr.name + ' #' if cw.attendance(usr.id).value else usr.name, callback_data=dumps( dict(G='attend', u_id=usr.id, c_id=c_id, cw_id=cw_id)))) return arr
def create_course(self, course_name): """ Вызывается у teacher, создает курс :param course_name: строка :return: Course """ user = UserDB.get_user(self.__id) if user and user['type_u']=='teacher': return Course.Course(owner_id=self.__id, name=course_name) else: raise TeacherAccessDeniedError("Expected teacher")
def task(id_tasks, id_course, back_page=0): arr = [] students = Course.Course(id_course).participants for i in id_tasks: star = '' for student in students: if Course.Task(id_course, i).mark(student.id).value is None: star = '*' break button = InlineKeyboardButton(Course.Task(id_course, i).name + star, callback_data=dumps( dict(type='task', id=id_course, id_t=i, page=back_page))) arr.append(button) return arr
def user_list(call, lang): p = UI.Paging(Course.Course(call.data['c_id']).participants, sort_key='name') text = msgs[lang]['teacher']['management']['user_list'] + p.msg( call.data['page'], lang) markup = mkp.create_listed( lang, tbt.users(p.list(call.data['page']), call.data['c_id']), tbt.user_list, call.data['c_id'], lang, call.data['page']) botHelper.edit_mes(text, call, markup=markup)
def delete_course(call, lang): course_ = Course.Course(call.data['c_id']) if call.message.chat.id == course_.owner.id: course_.delete() bot.answer_callback_query(call.id, msgs[lang]['callback']['course_deleted']) else: bot.answer_callback_query(call.id, msgs[lang]['callback']['not_owner'], show_alert=True) back(call, True, 2)
def task_list(call, lang): tasks = Course.Course(call.data['c_id']).tasks p = UI.Paging(tasks, sort_key='name') text = msgs[lang]['teacher']['management']['task_list'] + p.msg( call.data['page'], lang) markup = mkp.create_listed(lang, tbt.tasks(p.list(call.data['page'])), tbt.task_list, call.data['c_id'], lang, call.data['page']) mkp.add_before_back(markup, tbt.new_task(call.data['c_id'], lang)) botHelper.edit_mes(text, call, markup=markup)
def leave(call, lang): if call.data['c_id'] in ( c.id for c in User.User(call.message.chat.id).participation): c = Course.Course(call.data['c_id']) c.remove_student(call.message.chat.id) bot.answer_callback_query( call.id, msgs[lang]['callback']['course_leave'] + c.name) else: bot.answer_callback_query( call.id, msgs[lang]['callback']['course_not_enrolled'], show_alert=True) back(call, True)
def enroll(call, lang): if call.data['c_id'] not in ( c.id for c in User.User(call.message.chat.id).participation): c = Course.Course(call.data['c_id']) c.append_student(call.message.chat.id) bot.answer_callback_query( call.id, msgs[lang]['callback']['course_enrolled'] + c.name) else: bot.answer_callback_query( call.id, msgs[lang]['callback']['course_enrolled_already'], show_alert=True) back(call, True)
def do_tsk(call): def return_to_menu(): new_mes = botHelper.send_mes('empty', call.message.chat.id) botHelper.renew_menu(call, new_mes) back(call, True) def get_mark(message): if message.text == '/exit': return_to_menu() elif message.text == '-1': mark.value = None elif re.fullmatch(r'\d+', message.text): if int(message.text) <= task_.highest_mark: mark.value = int(message.text) return_to_menu() else: botHelper.send_mes(msgs[get_lang(call.message.chat.id)] ['teacher']['management']['error_mark']) bot.register_next_step_handler(message, get_mark) else: botHelper.send_mes(msgs[get_lang( call.message.chat.id)]['teacher']['management']['wrong_input']) bot.register_next_step_handler(message, get_mark) call.data = json.loads(call.data) task_ = Course.Task(call.data['c_id'], call.data['t_id']) mark = Course.Mark(call.data['c_id'], call.data['t_id'], call.data['u_id']) text = msgs['mark_one'].format(task=task_.name, user=User.User(call.data['u_id']).name, mark=mark.value, max=task_.highest_mark) botHelper.edit_mes(text, call) bot.register_next_step_handler(call.message, get_mark)
def task(id_tasks, id_course, id_user, back_page=0): arr = [] for i in id_tasks: task = Course.Task(id_course, i) button = InlineKeyboardButton('{} {}/{}'.format( task.name, task.mark(id_user).value, task.highest_mark), callback_data=dumps( dict(type='task_u', id=id_course, id_t=i, page=back_page))) arr.append(button) return arr
def user_attendance(users, course_id, class_id): arr = [] cw = Course.Classwork(course_id, class_id) for user in users: button = InlineKeyboardButton(User.User(user.id).name if cw.attendance( user.id).value else User.User(user.id).name + '*', callback_data=dumps( dict(type='sw_attend', id=course_id, id_u=user.id, id_cl=class_id))) arr.append(button) return arr
def user_mark(id_u, id_c, id_t): task = Course.Task(id_c, id_t) arr = [] for i in id_u: name = User.User(i).name if task.mark( i).value is not None else User.User(i).name + '*' button = InlineKeyboardButton(name, callback_data=dumps( dict(type='mark_u_o', id=id_c, id_u=i, id_t=id_t))) arr.append(button) return arr
def usr_mng(call, lang): course_ = Course.Course(call.data['c_id']) tasks = course_.tasks user = User.User(call.data['u_id']) cws = course_.classworks if tasks: total_mark = sum( filter(None, map(lambda x: x.mark(call.data['u_id']).value, tasks))) mean_mark = total_mark / len(tasks) else: total_mark = None mean_mark = None if cws: overall = len(cws) att = sum(map(lambda cw_: cw_.attendance(call.data['u_id']).value, cws)) else: overall = None att = None text = msgs[lang]['teacher']['management']['user'].format( course=course_.name, name=user.name, email='', mean=mean_mark, total=total_mark, attend=att, attend_tot=overall) c_text = msgs[lang]['confirm']['kick'].format(user.name) markup = mkp.create(lang, [ cbt.confirm_action('kick', msgs[lang]['buttons']['confirm']['kick'], c_text, call.message.chat.id, call.message.message_id, c_id=call.data['c_id'], u_id=call.data['u_id']) ]) botHelper.edit_mes(text, call, markup=markup)
def get_user_command(message): if message.text == '/name': text = msgs[lang]['teacher']['course_create']['name_input'].format( UI.constants.COURSE_NAME_LENGTH_MIN, UI.constants.COURSE_NAME_LENGTH_MAX) botHelper.send_mes(text, chat_id) bot.register_next_step_handler(message, name) elif message.text == '/desc': text = msgs[lang]['teacher']['course_create']['desc_input'].format( UI.constants.COURSE_DESC_LENGTH_MIN, UI.constants.COURSE_DESC_LENGTH_MAX) botHelper.send_mes(text, chat_id) bot.register_next_step_handler(message, desc) elif message.text == '/lock': text = msgs[lang]['teacher']['course_create']['lock_input'] botHelper.send_mes(text, chat_id) bot.register_next_step_handler(message, lock) elif message.text == '/create': if not course_info['name'] or not course_info['desc']: botHelper.send_mes( msgs[lang]['teacher']['course_create']['name_and_desc'], chat_id) bot.register_next_step_handler(message, get_user_command) else: c = Course.Course(owner_id=chat_id, name=course_info['name']) c.description = course_info['desc'] c.entry_restriction = course_info['lock'] botHelper.send_mes( msgs[lang]['teacher']['course_create']['created'], chat_id) menu_command(message) elif message.text == '/exit': botHelper.send_mes( msgs[lang]['teacher']['course_create']['canceled'], chat_id) menu_command(message) else: botHelper.send_mes( msgs[lang]['teacher']['course_create']['wrong_command'], chat_id) bot.register_next_step_handler(message, get_user_command)
def send(): course_ = Course.Course(call.data['c_id']) for part in course_.participants: try: botHelper.send_mes( msgs[lang]['teacher']['announce']['got_announce'].format( course_.name), part.id) botHelper.send_mes(announce_info['text'], part.id) for file, caption in zip(announce_info['file'], announce_info['file_caption']): bot.send_document(part.id, file, caption=caption) except TBotApiException: continue botHelper.send_mes(msgs[lang]['teacher']['announce']['sent'], call.message.chat.id) return_to_menu()