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
$apiClient
protected
static HttpClientInterface
$apiClient
= null
$course
Course
protected
static Course
$course
= null
$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
__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>bulkUpdateOverrides()
Bulk update module assignment overrides
public
bulkUpdateOverrides(array<string|int, mixed>|BulkUpdateModuleAssignmentOverridesDTO $overrides) : self
Parameters
- $overrides : array<string|int, mixed>|BulkUpdateModuleAssignmentOverridesDTO
Tags
Return values
selfcheckCourse()
Check if course exits and has id
public
static checkCourse() : bool
Tags
Return values
boolcourse()
Get the course this module belongs to
public
course() : Course|null
Tags
Return values
Course|nullcreate()
Create a new module
public
static create(CreateModuleDTO|array<string|int, mixed> $data) : self
Parameters
- $data : CreateModuleDTO|array<string|int, mixed>
Tags
Return values
selfcreateModuleItem()
Create a module item
public
createModuleItem(array<string|int, mixed>|CreateModuleItemDTO $data) : ModuleItem
Parameters
- $data : array<string|int, mixed>|CreateModuleItemDTO
Tags
Return values
ModuleItemdelete()
Delete a module
public
delete() : self
Tags
Return values
selffind()
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
Return values
selfget()
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|nullgetCompletionRate()
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
Return values
float —Completion rate as a percentage (0.0 to 100.0)
getId()
public
getId() : int
Return values
intgetItems()
public
getItems() : array<string|int, mixed>
Return values
array<string|int, mixed>getItemsCount()
public
getItemsCount() : int
Return values
intgetItemsUrl()
public
getItemsUrl() : string
Return values
stringgetName()
public
getName() : string
Return values
stringgetPosition()
public
getPosition() : int
Return values
intgetPrerequisiteModuleIds()
public
getPrerequisiteModuleIds() : array<string|int, mixed>
Return values
array<string|int, mixed>getPublished()
public
getPublished() : bool|null
Return values
bool|nullgetPublishFinalGrade()
public
getPublishFinalGrade() : bool|null
Return values
bool|nullgetRequirementType()
public
getRequirementType() : string|null
Return values
string|nullgetState()
public
getState() : string|null
Return values
string|nullgetUnlockAt()
public
getUnlockAt() : string|null
Return values
string|nullgetWorkflowState()
public
getWorkflowState() : string
Return values
stringisRequireSequentialProgress()
public
isRequireSequentialProgress() : bool
Return values
boolitems()
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
Return values
array<string|int, ModuleItem>listOverrides()
List module assignment overrides
public
listOverrides([array<string|int, mixed> $params = [] ]) : array<string|int, ModuleAssignmentOverride>
Parameters
- $params : array<string|int, mixed> = []
-
Query parameters
Tags
Return values
array<string|int, ModuleAssignmentOverride>paginate()
Get paginated results with metadata
public
static paginate([array<string, mixed> $params = [] ]) : PaginationResult
Parameters
- $params : array<string, mixed> = []
-
Query parameters
Return values
PaginationResultprerequisites()
Get prerequisite modules
public
prerequisites() : array<string|int, Module>
Tags
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
Return values
selfsave()
Save the module
public
save() : self
Tags
Return values
selfsetApiClient()
Set the API client
public
static setApiClient(HttpClientInterface $apiClient) : void
Parameters
- $apiClient : HttpClientInterface
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
update()
Update a module
public
static update(int $id, UpdateModuleDTO|array<string|int, mixed> $data) : self
Parameters
- $id : int
- $data : UpdateModuleDTO|array<string|int, mixed>
Tags
Return values
selfcastValue()
Cast a value to the correct type
protected
castValue(string $key, mixed $value) : DateTime|mixed
Parameters
- $key : string
- $value : mixed
Tags
Return values
DateTime|mixedcheckApiClient()
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>createConfiguredHttpClient()
Create an HttpClient with configured middleware
protected
static createConfiguredHttpClient() : HttpClient
Return values
HttpClientcreatePaginationResult()
Helper method to create PaginationResult from paginated response
protected
static createPaginationResult(PaginatedResponse $paginatedResponse) : PaginationResult
Parameters
- $paginatedResponse : PaginatedResponse
Return values
PaginationResultgetEndpoint()
Get the API endpoint for this resource
protected
static getEndpoint() : string
Tags
Return values
stringgetPaginatedResponse()
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
PaginatedResponsepopulate()
Populate the object with new data
protected
populate(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
Tags
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