예제 #1
0
def course(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # COURSE|course_id
    course_id = int(datas[-1])

    moodle_course = BaseCourse(context.moodle)
    courses = moodle_course.get_courses_by_field("id", course_id)
    context.query.answer()
    if not courses:
        context.query.edit_message_text("Kursus tidak ditemukan.")
        return -1
    course_ = courses[0]

    buttons = list()
    sections = moodle_course.get_contents(course_.id)
    for section in sections:
        if not section.uservisible:
            continue
        data = make_data("CONTENT", course_id, section.id, 0)
        button = InlineKeyboardButton(section.name, callback_data=data)
        buttons.append(button)
    if not buttons:
        data = make_data("FORUMS", course_id)
        button = InlineKeyboardButton("Daftar Forum", callback_data=data)
        buttons.append(button)
    keyboard = build_menu(
        buttons,
        footer_buttons=InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    )
    context.query.edit_message_text(
        text=course_text(course_),
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return -1
예제 #2
0
def module(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # MODULE|module_id
    module_id = int(datas[1])
    cm = None
    cm = BaseCourse(context.moodle).get_course_module(module_id)
    context.query.answer()
    if not cm:
        context.query.edit_message_text("Data tidak ditemukan.")
        return -1

    course_module = cm.cm
    text = course_module.name + "\n"
    text += "<i>Fitur masih dikembangkan, tolong tekan tombol Buka di elearning</i>"
    buttons: List[InlineKeyboardButton] = list()

    url = MOODLE_D + f"mod/{course_module.modname}/view.php?id={module_id}"
    button = InlineKeyboardButton("Buka di elearning", url)
    buttons.append(button)

    data = make_data(
        course_module.modname.upper(), course_module.course, course_module.instance
    )
    button = InlineKeyboardButton(course_module.name, callback_data=data)
    buttons.append(button)

    back_data = make_data("CONTENT", course_module.course, course_module.section, 0)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(buttons, footer_buttons=footer)

    context.query.edit_message_text(text, reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
예제 #3
0
def discussions(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # DISCUSSIONS|course_id|forum_id|page
    course_id = int(datas[1])
    forum_id = int(datas[2])
    page = int(datas[3])

    base_forum = BaseForum(context.moodle)
    discussions = base_forum.get_forum_discussions(
        forumid=forum_id,
        sortorder=1,
        page=page,
    )
    context.query.answer()

    buttons = list()
    for discussion in discussions:
        name = discussion.name
        text = name[:30]
        if len(name) > 30:
            text += "..."
        text += f" by {discussion.userfullname}"
        data = make_data("DISCUSSION", course_id, forum_id, discussion.id, 0)
        button = InlineKeyboardButton(text, callback_data=data)
        buttons.append(button)
    header = list()
    if page > 1:
        data = make_data("DISCUSSIONS", course_id, forum_id, page - 1)
        button = InlineKeyboardButton("Sebelumnya", callback_data=data)
        header.append(button)
    if len(buttons) >= 9:
        data = make_data("DISCUSSIONS", course_id, forum_id, page + 1)
        button = InlineKeyboardButton("Selanjutnya", callback_data=data)
        header.append(button)
    back_data = make_data("FORUM", course_id, forum_id)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(buttons,
                          header_buttons=header,
                          footer_buttons=footer)

    context.query.edit_message_text(
        "Daftar diskusi", reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
예제 #4
0
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
예제 #5
0
def forum(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # FORUM|course_id|forum_id
    course_id = int(datas[1])
    base_forum = BaseForum(context.moodle)
    forums = base_forum.get_forums_by_courses([course_id])
    context.query.answer()
    if not forums:
        context.query.edit_message_text("Forum tidak ditemukan.")
        return -1
    forum_id = int(datas[2])
    for fo in forums:
        if fo.id == forum_id:
            base_forum.view_forum(fo.id)
            break
    buttons = list()
    if fo.numdiscussions and fo.numdiscussions > 0:
        data = make_data("DISCUSSIONS", course_id, forum_id, 1)
        button = InlineKeyboardButton("Diskusi", callback_data=data)
        buttons.append(button)
    text = forum_text(fo)
    header = InlineKeyboardButton(
        fo.name,
        url=f"https://elearning.ut.ac.id/mod/forum/view.php?id={fo.cmid}")
    back_data = make_data("COURSE", course_id)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(
        buttons,
        header_buttons=header,
        footer_buttons=footer,
    )

    context.query.edit_message_text(
        text, reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
예제 #6
0
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
예제 #7
0
def view_lesson(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # LESSON|course_id|lesson_id
    course_id = int(datas[1])
    lesson_id = int(datas[2])
    base_lesson = BaseLesson(context.moodle)
    res = base_lesson.view_lesson(lesson_id)
    context.query.answer()
    logger.debug(repr(res))
    back_data = make_data("COURSE", course_id)
    keyboard = [[
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="Tutup ❌"),
    ]]
    context.query.edit_message_text(
        "Berhasil ✅" if res else "Gagal ❌",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return -1
예제 #8
0
def update_completion(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # COMPLETION|course_id|module_id
    course_id = int(datas[1])
    module_id = int(datas[2])
    cmplt = BaseCompletion(context.moodle)
    res = cmplt.update_activity_completion_status_manually(module_id, 1)
    context.query.answer()
    logger.debug(repr(res))
    back_data = make_data("COURSE", course_id)
    keyboard = [
        [
            InlineKeyboardButton("< Kembali", callback_data=back_data),
            InlineKeyboardButton("Tutup ❌", callback_data="Tutup ❌"),
        ]
    ]
    context.query.edit_message_text(
        "Berhasil ✅" if res.status else "Gagal ❌",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return -1
예제 #9
0
def courses(update: Update, context: CoreContext):
    message = context.message.reply_text("Mendapatkan kursus...")
    message = resolve(message, Message)
    try:
        courses = BaseCourse(
            context.moodle).get_enrolled_courses_by_timeline_classification(
                "all")
    except ConnectionError:
        message.edit_text(
            "Elearning tidak merespon, coba beberapa saat lagi...")
        return -1
    except Exception as e:
        message.edit_text("Gagal mendapatkan kursus.")
        raise e
    if not courses:
        message.edit_text("Tidak ada kursus yang sedang diikuti.")
        return -1
    buttons: List[InlineKeyboardButton] = list()
    for course in courses:
        name = course.fullname or course.shortname
        text = name[:30]
        if len(name) > 30:
            text += "..."
        if course.progress:
            text += f" ({course.progress}%)"
        data = make_data("COURSE", course.id)
        button = InlineKeyboardButton(text, callback_data=data)
        buttons.append(button)
    keyboard = build_menu(
        buttons=buttons,
        footer_buttons=InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    )
    message.edit_text(
        "Daftar kursus yang diikuti",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return -1
예제 #10
0
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