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::fetchAll();

// Fetch modules with pagination support
$paginatedResponse = Module::fetchAllPaginated(['per_page' => 10]);
$modules = $paginatedResponse->getJsonData();
$pagination = $paginatedResponse->toPaginationResult($modules);

// Fetch a specific page of modules
$paginationResult = Module::fetchPage(['page' => 2, 'per_page' => 10]);
$modules = $paginationResult->getData();
$hasNext = $paginationResult->hasNext();

// Fetch all modules from all pages
$allModules = Module::fetchAllPages(['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 staticaly
$module = Module::update(1, [
  'name' => 'Module 1 Updated',
  'position' => 1,
  'requireSequentialProgress' => true,
  'prerequisiteModuleIds' => [1, 2],
  'publishFinalGrade' => true
]);

// 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>
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, mixed>
IDs of Modules that must be completed before this one is unlocked.
$published  : bool
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.
$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
BaseApi constructor.
checkCourse()  : bool
Check if course exits and has id
create()  : self
Create a new module
delete()  : bool
Delete a module
fetchAll()  : array<string|int, mixed>
Get all modules for a course.
fetchAllPages()  : array<string|int, Module>
Fetch all modules from all pages
fetchAllPaginated()  : PaginatedResponse
Fetch modules with pagination support
fetchPage()  : PaginationResult
Fetch modules from a specific page
find()  : self
Find a module by its ID.
getCompletedAt()  : string|null
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
getState()  : string|null
getUnlockAt()  : string|null
getWorkflowState()  : string
isRequireSequentialProgress()  : bool
save()  : bool
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
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
createPaginationResult()  : PaginationResult
Helper method to create PaginationResult from paginated response
fetchAllPagesAsModels()  : array<string|int, static>
Helper method to fetch all pages and convert to model instances
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 object to an array

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> $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, mixed> $prerequisiteModuleIds

$published

Whether this module is published.

public bool $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

$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 = ['fetchAll' => ['all', 'get', 'getAll'], 'find' => ['one', 'getOne'], 'fetchAllPaginated' => ['allPaginated', 'getPaginated'], 'fetchAllPages' => ['allPages', 'getPages'], 'fetchPage' => ['page', 'getPage']]

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>

__construct()

BaseApi constructor.

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

checkCourse()

Check if course exits and has id

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

fetchAll()

Get all modules for a course.

public static fetchAll([array<string|int, mixed> $params = [] ]) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed> = []
Tags
throws
CanvasApiException
Return values
array<string|int, mixed>

fetchAllPages()

Fetch all modules from all pages

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

Query parameters for the request

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

fetchAllPaginated()

Fetch modules with pagination support

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

Query parameters for the request

Tags
throws
CanvasApiException
Return values
PaginatedResponse

find()

Find a module by its ID.

public static find(int $id) : self
Parameters
$id : int
Tags
throws
CanvasApiException
Return values
self

getCompletedAt()

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

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

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

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

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>

fetchAllPagesAsModels()

Helper method to fetch all pages and convert to model instances

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

The API endpoint path

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

Query parameters for the request

Return values
array<string|int, static>

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 object to an array

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

        
On this page

Search results