Documentation

Module extends AbstractBaseApi
in package

Module Class

Modules are collections of learning materials useful for organizing courses and optionally providing a linear flow through them. Module items can be accessed linearly or sequentially depending on module configuration. Items can be unlocked by various criteria such as reading a page or achieving a minimum score on a quiz. Modules themselves can be unlocked by the completion of other Modules.

Usage:

$course = Course::find(1); // or $course = new Course(['id' => 1]); to avoid making an API request
Module::setCourse($course);

// Get all modules for a course (first page only)
$modules = Module::get();

// Get modules with query parameters
$modules = Module::get([
    'include' => ['items', 'content_details'],
    'search_term' => 'Introduction',
    'student_id' => '123'
]);

// Find a specific module with includes
$module = Module::find(1, ['include' => ['items']]);

// Fetch modules with pagination support
$paginationResult = Module::paginate(['per_page' => 10]);
$modules = $paginationResult->getData();
$hasNext = $paginationResult->hasNext();

// Fetch all modules from all pages
$allModules = Module::all(['per_page' => 50]);

// Create a new module
$module = Module::create([
   'name' => 'Module 1',
   'position' => 1,
   'requireSequentialProgress' => true,
   'prerequisiteModuleIds' => [1, 2],
   'publishFinalGrade' => true
]);

// Update a module
$module->setName('Module 1 Updated');
$module->save();

// Update statically
$module = Module::update(1, [
  'name' => 'Module 1 Updated',
  'position' => 1,
  'requireSequentialProgress' => true,
  'prerequisiteModuleIds' => [1, 2],
  'publishFinalGrade' => true,
  'published' => true
]);

// Re-lock module progressions
$module->relock();

// Get module items
$items = $module->items();
$items = $module->items(['include' => ['content_details']]);

// Create a module item
$item = $module->createModuleItem([
    'title' => 'Assignment 1',
    'type' => 'Assignment',
    'content_id' => 123
]);

// List module assignment overrides
$overrides = $module->listOverrides();

// Bulk update module assignment overrides
$dto = new BulkUpdateModuleAssignmentOverridesDTO();
$dto->addSectionOverride(123)
    ->addStudentOverride([456, 789], null, 'Special Students')
    ->addGroupOverride(321);
$module->bulkUpdateOverrides($dto);

// Or update with array
$module->bulkUpdateOverrides([
    ['course_section_id' => 123],
    ['student_ids' => [456, 789], 'title' => 'Special Students'],
    ['id' => 999, 'course_section_id' => 321] // Update existing override
]);

// Clear all overrides
$module->bulkUpdateOverrides([]);

// Delete a module
$module = Module::find(1);
$module->delete();

Table of Contents

Properties

$completedAt  : string|null
The date the calling user completed the module (Optional).
$id  : int
The unique identifier for the module.
$items  : array<string|int, mixed>|null
The contents of this module, as an array of Module Items.
$itemsCount  : int
The number of items in the module.
$itemsUrl  : string
The API URL to retrieve this module's items.
$name  : string
The name of this module.
$position  : int
The position of this module in the course (1-based).
$prerequisiteModuleIds  : array<string|int, int>
IDs of Modules that must be completed before this one is unlocked.
$published  : bool|null
Whether this module is published.
$publishFinalGrade  : bool|null
If the student's final grade for the course should be published to the SIS upon completion of this module.
$requirementType  : string|null
Whether module requires all required items or one required item to be considered complete (one of 'all' or 'one').
$requireSequentialProgress  : bool
Whether module items must be unlocked in order.
$state  : string|null
The state of this Module for the calling user: 'locked', 'unlocked', 'started', 'completed' (Optional).
$unlockAt  : string|null
The date this module will unlock (Optional).
$workflowState  : string
The state of the module: 'active', 'deleted'.
$apiClient  : HttpClientInterface
$course  : Course
Course
$methodAliases  : array<string|int, mixed>
Define method aliases

Methods

__callStatic()  : mixed
Magic method to handle function aliases
__construct()  : mixed
Module constructor.
all()  : array<string|int, static>
Get all pages of results
bulkUpdateOverrides()  : self
Bulk update module assignment overrides
checkCourse()  : bool
Check if course exits and has id
course()  : Course|null
Get the course this module belongs to
create()  : self
Create a new module
createModuleItem()  : ModuleItem
Create a module item
delete()  : self
Delete a module
find()  : self
Find a module by its ID.
get()  : array<string|int, static>
Get first page of results
getCompletedAt()  : string|null
getCompletionRate()  : float
Get completion rate for this module
getId()  : int
getItems()  : array<string|int, mixed>
getItemsCount()  : int
getItemsUrl()  : string
getName()  : string
getPosition()  : int
getPrerequisiteModuleIds()  : array<string|int, mixed>
getPublished()  : bool|null
getPublishFinalGrade()  : bool|null
getRequirementType()  : string|null
getState()  : string|null
getUnlockAt()  : string|null
getWorkflowState()  : string
isRequireSequentialProgress()  : bool
items()  : array<string|int, ModuleItem>
Get module items
listOverrides()  : array<string|int, ModuleAssignmentOverride>
List module assignment overrides
paginate()  : PaginationResult
Get paginated results with metadata
prerequisites()  : array<string|int, Module>
Get prerequisite modules
relock()  : self
Re-lock module progressions Resets module progressions to their default locked state and recalculates them based on the current requirements.
save()  : self
Save the module
setApiClient()  : void
Set the API client
setCompletedAt()  : void
setCourse()  : void
Set the course
setId()  : void
setItems()  : void
setItemsCount()  : void
setItemsUrl()  : void
setName()  : void
setPosition()  : void
setPrerequisiteModuleIds()  : void
setPublished()  : void
setPublishFinalGrade()  : void
setRequirementType()  : void
setRequireSequentialProgress()  : void
setState()  : void
setUnlockAt()  : void
setWorkflowState()  : void
update()  : self
Update a module
castValue()  : DateTime|mixed
Cast a value to the correct type
checkApiClient()  : void
Check if the API client is set, if not, instantiate a new one
convertPaginatedResponseToModels()  : array<string|int, static>
Helper method to convert paginated response data to model instances
createConfiguredHttpClient()  : HttpClient
Create an HttpClient with configured middleware
createPaginationResult()  : PaginationResult
Helper method to create PaginationResult from paginated response
getEndpoint()  : string
Get the API endpoint for this resource
getPaginatedResponse()  : PaginatedResponse
Helper method to get paginated response from API endpoint
populate()  : void
Populate the object with new data
toDtoArray()  : array<string|int, mixed>
Convert the module to a DTO array
validatePrerequisitePositions()  : void
Validate that prerequisite modules have lower position values

Properties

$completedAt

The date the calling user completed the module (Optional).

public string|null $completedAt

(Present only if the caller is a student or if the optional parameter 'student_id' is included)

$id

The unique identifier for the module.

public int $id

$items

The contents of this module, as an array of Module Items.

public array<string|int, mixed>|null $items

(Present only if requested via include[]=items AND the module is not deemed too large by Canvas.)

$itemsCount

The number of items in the module.

public int $itemsCount

$itemsUrl

The API URL to retrieve this module's items.

public string $itemsUrl

$name

The name of this module.

public string $name

$position

The position of this module in the course (1-based).

public int $position

$prerequisiteModuleIds

IDs of Modules that must be completed before this one is unlocked.

public array<string|int, int> $prerequisiteModuleIds

$published

Whether this module is published.

public bool|null $published

This field is present only if the caller has permission to view unpublished modules.

$publishFinalGrade

If the student's final grade for the course should be published to the SIS upon completion of this module.

public bool|null $publishFinalGrade

$requirementType

Whether module requires all required items or one required item to be considered complete (one of 'all' or 'one').

public string|null $requirementType

$requireSequentialProgress

Whether module items must be unlocked in order.

public bool $requireSequentialProgress

$state

The state of this Module for the calling user: 'locked', 'unlocked', 'started', 'completed' (Optional).

public string|null $state

(Present only if the caller is a student or if the optional parameter 'student_id' is included)

$unlockAt

The date this module will unlock (Optional).

public string|null $unlockAt

$workflowState

The state of the module: 'active', 'deleted'.

public string $workflowState

$methodAliases

Define method aliases

protected static array<string|int, mixed> $methodAliases = ['get' => ['fetch', 'list', 'fetchAll'], 'all' => ['fetchAllPages', 'getAll'], 'paginate' => ['getPaginated', 'withPagination', 'fetchPage'], 'find' => ['one', 'getOne']]

Methods

__callStatic()

Magic method to handle function aliases

public static __callStatic(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
$name : string
$arguments : array<string|int, mixed>
Tags
throws
InvalidArgumentException

__construct()

Module constructor.

public __construct([array<string|int, mixed> $data = [] ]) : mixed
Parameters
$data : array<string|int, mixed> = []

all()

Get all pages of results

public static all([array<string, mixed> $params = [] ]) : array<string|int, static>
Parameters
$params : array<string, mixed> = []

Query parameters

Return values
array<string|int, static>

checkCourse()

Check if course exits and has id

public static checkCourse() : bool
Tags
throws
CanvasApiException
Return values
bool

find()

Find a module by its ID.

public static find(int $id[, array<string|int, mixed> $params = [] ]) : self
Parameters
$id : int
$params : array<string|int, mixed> = []

Query parameters (include[], student_id)

Tags
throws
CanvasApiException
Return values
self

get()

Get first page of results

public static get([array<string, mixed> $params = [] ]) : array<string|int, static>
Parameters
$params : array<string, mixed> = []

Query parameters

Return values
array<string|int, static>

getCompletedAt()

public getCompletedAt() : string|null
Return values
string|null

getCompletionRate()

Get completion rate for this module

public getCompletionRate([int|null $userId = null ]) : float
Parameters
$userId : int|null = null

Optional user ID to get completion rate for specific user

Tags
example
$course = Course::find(123);
Module::setCourse($course);

$module = Module::find(456);

// Get overall completion rate (current user)
$rate = $module->getCompletionRate();
echo "Module is {$rate}% complete\n";

// Get completion rate for specific student
$studentRate = $module->getCompletionRate(789);
if ($studentRate === 100.0) {
    echo "Student has completed this module!\n";
}

// Track progress across all modules
$modules = Module::get();
foreach ($modules as $module) {
    $rate = $module->getCompletionRate();
    echo "{$module->name}: {$rate}% complete\n";
}
throws
CanvasApiException
Return values
float

Completion rate as a percentage (0.0 to 100.0)

getId()

public getId() : int
Return values
int

getItems()

public getItems() : array<string|int, mixed>
Return values
array<string|int, mixed>

getItemsCount()

public getItemsCount() : int
Return values
int

getItemsUrl()

public getItemsUrl() : string
Return values
string

getName()

public getName() : string
Return values
string

getPosition()

public getPosition() : int
Return values
int

getPrerequisiteModuleIds()

public getPrerequisiteModuleIds() : array<string|int, mixed>
Return values
array<string|int, mixed>

getPublished()

public getPublished() : bool|null
Return values
bool|null

getPublishFinalGrade()

public getPublishFinalGrade() : bool|null
Return values
bool|null

getRequirementType()

public getRequirementType() : string|null
Return values
string|null

getState()

public getState() : string|null
Return values
string|null

getUnlockAt()

public getUnlockAt() : string|null
Return values
string|null

getWorkflowState()

public getWorkflowState() : string
Return values
string

isRequireSequentialProgress()

public isRequireSequentialProgress() : bool
Return values
bool

items()

Get module items

public items([array<string|int, mixed> $params = [] ]) : array<string|int, ModuleItem>
Parameters
$params : array<string|int, mixed> = []

Query parameters (include[], search_term, student_id)

Tags
throws
CanvasApiException
Return values
array<string|int, ModuleItem>

prerequisites()

Get prerequisite modules

public prerequisites() : array<string|int, Module>
Tags
example
$course = Course::find(123);
Module::setCourse($course);

$module = Module::find(456);
$prerequisites = $module->prerequisites();

foreach ($prerequisites as $prereq) {
    echo "Must complete: {$prereq->name}\n";
}
throws
CanvasApiException
Return values
array<string|int, Module>

Array of prerequisite Module objects

relock()

Re-lock module progressions Resets module progressions to their default locked state and recalculates them based on the current requirements.

public relock() : self
Tags
throws
CanvasApiException
Return values
self

setCompletedAt()

public setCompletedAt(string|null $completedAt) : void
Parameters
$completedAt : string|null

setCourse()

Set the course

public static setCourse(Course $course) : void
Parameters
$course : Course

setId()

public setId(int $id) : void
Parameters
$id : int

setItems()

public setItems(array<string|int, mixed> $items) : void
Parameters
$items : array<string|int, mixed>

setItemsCount()

public setItemsCount(int $itemsCount) : void
Parameters
$itemsCount : int

setItemsUrl()

public setItemsUrl(string $itemsUrl) : void
Parameters
$itemsUrl : string

setName()

public setName(string $name) : void
Parameters
$name : string

setPosition()

public setPosition(int $position) : void
Parameters
$position : int

setPrerequisiteModuleIds()

public setPrerequisiteModuleIds(array<string|int, mixed> $prerequisiteModuleIds) : void
Parameters
$prerequisiteModuleIds : array<string|int, mixed>

setPublished()

public setPublished(bool|null $published) : void
Parameters
$published : bool|null

setPublishFinalGrade()

public setPublishFinalGrade(bool|null $publishFinalGrade) : void
Parameters
$publishFinalGrade : bool|null

setRequirementType()

public setRequirementType(string|null $requirementType) : void
Parameters
$requirementType : string|null

setRequireSequentialProgress()

public setRequireSequentialProgress(bool $requireSequentialProgress) : void
Parameters
$requireSequentialProgress : bool

setState()

public setState(string|null $state) : void
Parameters
$state : string|null

setUnlockAt()

public setUnlockAt(string|null $unlockAt) : void
Parameters
$unlockAt : string|null

setWorkflowState()

public setWorkflowState(string $workflowState) : void
Parameters
$workflowState : string

castValue()

Cast a value to the correct type

protected castValue(string $key, mixed $value) : DateTime|mixed
Parameters
$key : string
$value : mixed
Tags
throws
Exception
Return values
DateTime|mixed

checkApiClient()

Check if the API client is set, if not, instantiate a new one

protected static checkApiClient() : void

convertPaginatedResponseToModels()

Helper method to convert paginated response data to model instances

protected static convertPaginatedResponseToModels(PaginatedResponse $paginatedResponse) : array<string|int, static>
Parameters
$paginatedResponse : PaginatedResponse
Return values
array<string|int, static>

getEndpoint()

Get the API endpoint for this resource

protected static getEndpoint() : string
Tags
throws
CanvasApiException
Return values
string

getPaginatedResponse()

Helper method to get paginated response from API endpoint

protected static getPaginatedResponse(string $endpoint[, array<string|int, mixed> $params = [] ]) : PaginatedResponse
Parameters
$endpoint : string

The API endpoint path

$params : array<string|int, mixed> = []

Query parameters for the request

Return values
PaginatedResponse

populate()

Populate the object with new data

protected populate(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>
Tags
throws
Exception

toDtoArray()

Convert the module to a DTO array

protected toDtoArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

validatePrerequisitePositions()

Validate that prerequisite modules have lower position values

protected static validatePrerequisitePositions(array<string|int, int> $prerequisiteModuleIds, int $position) : void
Parameters
$prerequisiteModuleIds : array<string|int, int>
$position : int
Tags
throws
CanvasApiException

        
On this page

Search results