def folder(update: Update, context: CoreContext): datas = context.query.data.split(CALLBACK_SEPARATOR) # FOLDER|course_id|folder_id course_id = int(datas[1]) folder_id = int(datas[2]) base_folder = BaseFolder(context.moodle) folders = base_folder.get_folders_by_courses([course_id]) context.query.answer() if not folders: context.query.edit_message_text("Data tidak ditemukan!") return -1 for folder in folders: if folder.id == folder_id: break buttons = list() for file in folder.introfiles: if file.isexternalfile: button = InlineKeyboardButton(file.filename, file.fileurl) else: button = InlineKeyboardButton( file.filename, switch_inline_query_current_chat=file.fileurl, ) buttons.append(button) text = folder.name + "\n\n" text += clean_html(folder.intro, **BLEACH_CONFIG) keyboard = build_menu(buttons) context.query.edit_message_text( text, reply_markup=InlineKeyboardMarkup(keyboard)) return -1
def url(update: Update, context: CoreContext): datas = context.query.data.split(CALLBACK_SEPARATOR) # URL|course_id|url_id course_id = int(datas[1]) url_id = int(datas[2]) base_url = BaseUrl(context.moodle) urls = base_url.get_urls_by_courses([course_id]) context.query.answer() url_ = None for url in urls.urls: if url.id == url_id: url_ = url break if not url_ or url_.id != url_id: return -1 buttons = list() name = unquote(url_.name) disable_preview = True if url_.externalurl: disable_preview = False button = InlineKeyboardButton(name, url_.externalurl) buttons.append(button) name = format_html.href(name, url_.externalurl) text = name + "\n" text += clean_html(url_.intro, **BLEACH_CONFIG) for file in url_.introfiles: url = file.fileurl if not file.isexternalfile: url += "?token=" + context.moodle.token button = InlineKeyboardButton(file.filename, url) buttons.append(button) # TODO : Use CONTENT back_button = make_data("COURSE", course_id) footer = [ InlineKeyboardButton("< Kembali", callback_data=back_button), InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"), ] keyboard = build_menu(buttons, footer_buttons=footer) context.chat.send_message( text, reply_markup=InlineKeyboardMarkup(keyboard), disable_web_page_preview=disable_preview, ) return -1
def discussion(update: Update, context: CoreContext): datas = context.query.data.split(CALLBACK_SEPARATOR) # DISCUSSION|course_id|forum_id|discussion_id|page course_id = int(datas[1]) forum_id = int(datas[2]) discussion_id = int(datas[3]) page = int(datas[4]) base_forum = BaseForum(context.moodle) posts = [] posts = base_forum.get_discussion_posts(discussion_id) context.query.answer() text = "Diskusi" for post in posts: if post.isdeleted: continue subject = post.replysubject or post.subject title = f"{subject} [{post.author.fullname}]" url = f"https://elearning.ut.ac.id/mod/forum/discuss.php?d={post.id}" text += format_html.href(title, url) + "\n" text += clean_html(post.message, **BLEACH_CONFIG) + "\n\n" header = list() if len(text) > MAX_MESSAGE_LENGTH: MESSAGE_LENGTH = len(text) if page > 0: data = make_data("DISCUSSION", course_id, forum_id, discussion_id, page - 1) button = InlineKeyboardButton("Sebelumnya", callback_data=data) header.append(button) if MESSAGE_LENGTH >= (page + 1) * MAX_MESSAGE_LENGTH: data = make_data("DISCUSSION", course_id, forum_id, discussion_id, page + 1) button = InlineKeyboardButton("Selanjutnya", callback_data=data) header.append(button) back_data = make_data("DISCUSSIONS", course_id, forum_id, 1) footer = [ InlineKeyboardButton("< Kembali", callback_data=back_data), InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"), ] keyboard = build_menu(header_buttons=header, footer_buttons=footer) context.query.edit_message_text( text, reply_markup=InlineKeyboardMarkup(keyboard)) return -1
def content(update: Update, context: CoreContext): datas = context.query.data.split(CALLBACK_SEPARATOR) # CONTENT|course_id|section_id|page course_id = int(datas[1]) section_id = int(datas[2]) page = int(datas[3]) options = [ContentOption("sectionid", str(section_id))] sections = BaseCourse(context.moodle).get_contents(course_id, options) context.query.answer() num = 0 text = "" completions: List[InlineKeyboardButton] = list() keyboard: List[List[InlineKeyboardButton]] = list() for section in sections: text += clean_html(section.summary, **BLEACH_CONFIG) for module in section.modules: if module.modname == "label" and not module.completion: continue num += 1 button_text = "" buttons: List[InlineKeyboardButton] = list() if module.completion and module.completion != 0: state = module.completiondata.state if state == 0: # incomplete if module.completion == 1: button_text += str(num) data = make_data("COMPLETION", course_id, module.id) button = InlineKeyboardButton( f"{num} ☑️", callback_data=data, ) completions.append(button) button_text += "❌ " elif state == 1: # complete button_text += "✅ " elif state == 2: # complete pass button_text += "✔️ " elif state == 3: # complete fail button_text += "❎ " button_text += unquote(module.name) if module.modname in SUPPORTED_MOD: # FORUM|course_id|forum_id data = make_data(module.modname.upper(), course_id, module.instance) button = InlineKeyboardButton(button_text, callback_data=data) buttons.append(button) else: url = MOODLE_D + f"mod/{module.modname}/view.php?id={module.id}" button = InlineKeyboardButton(button_text, url) buttons.append(button) keyboard.append(buttons) if len(text) > MAX_MESSAGE_LENGTH: MESSAGE_LENGTH = len(text) header = list() if page > 0: data = make_data("CONTENT", course_id, section_id, page - 1) button = InlineKeyboardButton("Sebelumnya", callback_data=data) header.append(button) if MESSAGE_LENGTH >= (page + 1) * MAX_MESSAGE_LENGTH: data = make_data("CONTENT", course_id, section_id, page + 1) button = InlineKeyboardButton("Selanjutnya", callback_data=data) header.append(button) if header: keyboard.insert(0, header) back_data = make_data("COURSE", course_id) down_data = context.query.data.rstrip("|") + "|" footer = [ InlineKeyboardButton("< Kembali", callback_data=back_data), InlineKeyboardButton("⬇️ Turunkan", callback_data=down_data), InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"), ] if completions: keyboard.append(completions) keyboard.append(footer) if len(datas) > 4: context.chat.send_message( text[MAX_MESSAGE_LENGTH * page : MAX_MESSAGE_LENGTH * (page + 1)], reply_markup=InlineKeyboardMarkup(keyboard), ) else: context.query.edit_message_text( text[MAX_MESSAGE_LENGTH * page : MAX_MESSAGE_LENGTH * (page + 1)], reply_markup=InlineKeyboardMarkup(keyboard), ) return -1
def sanitize_text(text: str) -> str: return clean_html(text, tags=ALLOWED_TAGS, strip=True)