def get_modules(course: Course) -> List[Union[Module, ModuleItem]]: """ Returns a list of all modules for the given course. """ all_modules = [] for module in course.get_modules(): all_modules.append(module) for item in module.get_module_items(): all_modules.append(item) return all_modules
def download_modules(course: Course): """ Download all modules for a Canvas course, storing each module's URL (or name/title if the url does not exist) in {COURSES_DIRECTORY}/{course.id}/modules.txt. Assumption: {COURSES_DIRECTORY}/{course.id}/modules.txt exists. """ modules_file = f"{COURSES_DIRECTORY}/{course.id}/modules.txt" with open(modules_file, "w") as f: for module in course.get_modules(): if hasattr(module, "name"): f.write(module.name + "\n") for item in module.get_module_items(): if hasattr(item, "title"): if hasattr(item, "html_url"): f.write(item.html_url + "\n") else: f.write(item.title + "\n")
def get_all_modules(course: Course, incl_unpublished: bool) -> list[Module | ModuleItem]: """ Returns a list of all modules for the given course. Includes unpublished modules if `incl_unpublished` is `True` and we have access to unpublished modules for the course. """ all_modules = [] for module in course.get_modules(): # If module does not have the "published" attribute, then the host of the bot does # not have access to unpublished modules. Reference: https://canvas.instructure.com/doc/api/modules.html if incl_unpublished or not hasattr( module, "published") or module.published: all_modules.append(module) for item in module.get_module_items(): # See comment about the "published" attribute above. if incl_unpublished or not hasattr( item, "published") or item.published: all_modules.append(item) return all_modules