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 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 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 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 __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 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 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_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 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 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 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 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 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 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 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()
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 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 kick(call, lang): course_ = Course.Course(call.data['c_id']) if User.User(call.message.chat.id).type_u == 'teacher': if course_.id in (c.id for c in User.User(call.data['u_id']).participation): course_.remove_student(call.data['u_id']) bot.answer_callback_query( call.id, msgs[lang]['callback']['student_kicked'].format( name=User.User(call.data['u_id']).name)) else: bot.answer_callback_query( call.id, msgs[lang]['callback']['student_not_enrolled'], show_alert=True) else: bot.answer_callback_query(call.id, msgs[lang]['callback']['not_teacher'], show_alert=True) back(call, True, 2)
def course(id, prev='', back_page=0): """ Берет на вход список 'id' и создает список кнопок с айди курса. :param id: list of int. Список айди курсов. :param prev: str. Меню в которое попадет при нажатии кнопки "Назад". :param back_page: int. Страница для возврата. Чтобы, если зашел на курс из страницы 3, на ту же страницу и возвратился. :return: list of InlineKeyboardButton. Возвращает список кнопок с айди курсов. """ arr = [] for i in id: name = Course.Course(i).name button = InlineKeyboardButton(name, callback_data=dumps( dict(type='courses', id=i, prev=prev, page=back_page))) arr.append(button) return arr
def __init__(self): with open('courses.csv') as file_obj: courses = csv.reader(file_obj) for course in courses: course = Course(course[0], course[1], course[2]) self.all_course.append(course)
def course(call, lang): course_ = Course.Course(call.data['c_id']) num_par = len(course_.participants) owner = course_.owner if owner.id == call.message.chat.id: # owner if course_.is_open: lock = msgs[lang]['teacher']['lock_open'] else: lock = msgs[lang]['teacher']['lock_close'] desc = course_.description if len(desc) > UI.constants.COURSE_INFO_DESC_LENGTH: desc = botHelper.remove_danger( desc[:UI.constants.COURSE_INFO_DESC_LENGTH]) + '...' botHelper.edit_mes( msgs[lang]['teacher']['course_owner_min'].format(name=course_.name, num=num_par, lock=lock, desc=desc), call, markup=mkp.create(lang, [tbt.manage(call.data['c_id'], lang)])) elif course_.id in (c.id for c in User.User(call.message.chat.id).participation ): # enrolled cws = course_.classworks attend_text = '' overall = len(cws) if overall: att = sum( map(lambda cw_: cw_.attendance(call.message.chat.id).value, cws)) attend_text = msgs[lang]['student']['attendance'].format( count=att, overall=overall, ratio=int(att / overall * 100)) tasks = course_.tasks if tasks: total_mark = sum( map(lambda task_: task_.mark(call.message.chat.id).value, tasks)) mean_mark = total_mark / len(tasks) mark_text = msgs[lang]['student']['marks'].format( total_mark, round(mean_mark, 2)) else: mark_text = '' text = msgs[lang]['student']['course'].format(name=course_.name, fio=owner.name, num=num_par, mail='', marks=mark_text, attend=attend_text) c_text = msgs[lang]['confirm']['leave_course'].format(course_.name) if not course_.is_open: c_text += msgs[lang]['confirm']['leave_course_append'] markup = mkp.create(lang, [cbt.task_list(call.data['c_id'], lang)], [ cbt.confirm_action('leave', msgs[lang]['buttons']['confirm']['leave'], c_text, call.message.chat.id, call.message.message_id, c_id=course_.id) ]) botHelper.edit_mes(text, call, markup=markup) else: # not enrolled locked = '' if course_.is_open else msgs[lang]['student'][ 'course_closed'] end_entry = course_.entry_restriction lock = UI.to_dtime( end_entry) if end_entry else msgs[lang]['student']['lock_absent'] text = msgs[lang]['student']['course_not_enroll'].format( name=course_.name, fio=owner.name, desc=course_.description, num=num_par, lock=lock, mail='', locked=locked) # TODO mail c_text = msgs[lang]['confirm']['enroll_course'].format(course_.name) if locked: markup = mkp.create(lang) else: markup = mkp.create(lang, [ cbt.confirm_action('enroll', msgs[lang]['buttons']['confirm']['enroll'], c_text, call.message.chat.id, call.message.message_id, c_id=course_.id) ]) botHelper.edit_mes(text, call, markup=markup)
def possessions(self): # Курсы под владением user = UserDB.get_user(self.__id) return tuple(Course.Course(course_id) for course_id in UserDB.get_teacher_courses(self.__id)) if user else []
def participation(self): # Участие в курсах user = UserDB.get_user(self.__id) return tuple(Course.Course(course_id) for course_id in UserDB.get_user_courses(self.__id)) if user else []
def delete_course(self, course_id): course_to_delete = Course.Course(course_id) if course_to_delete.owner.id == self.__id: course_to_delete.delete() else: raise TeacherAccessDeniedError("Expected owner of the course")