Skip to content

Module

py_moodle.module

Generic Moodle module management helpers. All code and comments are in English.

Classes:

Name Description
MoodleModuleError

Generic exception for module operations.

Functions:

Name Description
add_generic_module

Adds a new module to a course section by simulating a form post to modedit.php.

update_generic_module

Updates an existing module by fetching its edit form, modifying fields, and submitting it.

delete_module

Deletes any module by its cmid.

get_module_info

Return raw info for a single module.

get_module_context_id

Gets the context ID for a given course module by scraping its edit page.

format_module_table

Pretty table with Rich if available, else plain text.

get_module_types

Fetches a list of all available module types that the user can add to a specific course.

rename_module_name

Renames the name of any course module using the generic 'inplace_editable' AJAX endpoint.

Classes

MoodleModuleError

Bases: Exception

Generic exception for module operations.

Functions

add_generic_module

add_generic_module(
    session: Session,
    base_url: str,
    sesskey: str,
    module_name: str,
    course_id: int,
    section_id: int,
    specific_payload: Dict[str, Any],
    module_id: Optional[int] = None,
) -> int

Adds a new module to a course section by simulating a form post to modedit.php. This is a generic function that handles the common logic for all module types.

Returns:

Type Description
int

The new course module ID (cmid) as an integer.

update_generic_module

update_generic_module(
    session: Session,
    base_url: str,
    cmid: int,
    specific_payload: Dict[str, Any],
) -> bool

Updates an existing module by fetching its edit form, modifying fields, and submitting it. This generic approach ensures that all existing form values are preserved.

delete_module

delete_module(
    session: Session, base_url: str, sesskey: str, cmid: int
) -> bool

Deletes any module by its cmid. It automatically discovers the course_id from the cmid.

get_module_info

get_module_info(
    session: Session, base_url: str, sesskey: str, cmid: int
) -> Dict[str, Any]

Return raw info for a single module.

Preference order: 1. WebService REST call with token (core_course_get_course_module) 2. Legacy AJAX call (fallback)

Raises MoodleModuleError if both methods fail.

get_module_context_id

get_module_context_id(
    session: Session, base_url: str, cmid: int
) -> int

Gets the context ID for a given course module by scraping its edit page. This is the most reliable method.

Parameters:

Name Type Description Default
session Session

An authenticated requests.Session object.

required
base_url str

The base URL of the Moodle instance.

required
cmid int

The course module ID (cmid).

required

Returns:

Type Description
int

The integer context ID of the module.

Raises:

Type Description
MoodleModuleError

If the context ID cannot be found.

format_module_table

format_module_table(module: Dict[str, Any]) -> str

Pretty table with Rich if available, else plain text.

get_module_types

get_module_types(
    session: Session,
    base_url: str,
    sesskey: str,
    course_id: int,
) -> list[dict[str, Any]]

Fetches a list of all available module types that the user can add to a specific course.

Parameters:

Name Type Description Default
session Session

Authenticated requests.Session object.

required
base_url str

Base URL of the Moodle instance.

required
sesskey str

Session key for the AJAX call.

required
course_id int

The ID of the course to check capabilities against. Can be 1, the default internal course in Moodle.

required

Returns:

Name Type Description
list[dict[str, Any]]

A list of dictionaries, where each dict represents a module type.

Example list[dict[str, Any]]

[{'id': 12, 'name': 'label', 'title': 'Etiqueta'}, ...]

rename_module_name

rename_module_name(
    session: Session,
    base_url: str,
    sesskey: str,
    cmid: int,
    name: str,
) -> bool

Renames the name of any course module using the generic 'inplace_editable' AJAX endpoint.

Parameters:

Name Type Description Default
session Session

Authenticated requests.Session object.

required
base_url str

Base URL of the Moodle instance.

required
sesskey str

Session key for the AJAX call.

required
cmid int

The course module ID (cmid) of the activity to rename.

required
name str

The new name for the module.

required

Returns:

Type Description
bool

True if the renaming was successful.

Raises:

Type Description
MoodleModuleError

If the AJAX call fails or returns an error.