ModuleItem
extends AbstractBaseApi
in package
Module Item Class
Module items represent individual pieces of content within a module. Items can be files, pages, assignments, quizzes, external tools, or other types of content. Module items can have completion requirements and support sequential progression.
Usage:
$course = Course::find(1);
$module = Module::find(1);
ModuleItem::setCourse($course);
ModuleItem::setModule($module);
// Get all module items
$items = ModuleItem::fetchAll();
// Create new assignment module item
$item = ModuleItem::create([
'title' => 'Assignment 1',
'type' => 'Assignment',
'content_id' => 123,
'position' => 1
]);
// Create page module item
$item = ModuleItem::create([
'title' => 'Course Introduction',
'type' => 'Page',
'page_url' => 'course-introduction'
]);
// Create external tool module item
$item = ModuleItem::create([
'title' => 'External Learning Tool',
'type' => 'ExternalTool',
'external_url' => 'https://example.com/tool',
'new_tab' => true,
'iframe' => ['width' => 800, 'height' => 600]
]);
// Update module item
$item->setTitle('Updated Assignment');
$item->save();
// Mark item as read (fulfills must_view requirement)
$item->markAsRead();
// Mark item as done (manual completion)
$item->markAsDone();
// Delete module item
$item->delete();
Table of Contents
Constants
- VALID_COMPLETION_TYPES = [ 'must_view', // All types 'must_contribute', // Assignment, Discussion, Page 'must_submit', // Assignment, Quiz 'min_score', // Assignment, Quiz 'must_mark_done', ]
- Canvas API supported completion requirement types
- VALID_TYPES = ['File', 'Page', 'Discussion', 'Assignment', 'Quiz', 'SubHeader', 'ExternalUrl', 'ExternalTool']
- Canvas API supported module item types
Properties
- $completionRequirement : array<string, mixed>|null
- Completion requirement structure.
- $contentDetails : array<string, mixed>|null
- Type-specific details.
- $contentId : int|null
- ID of associated content object (not required for ExternalUrl, Page, SubHeader).
- $externalUrl : string|null
- For ExternalUrl/ExternalTool types.
- $htmlUrl : string
- Canvas URL for this item.
- $id : int
- Unique identifier for the module item.
- $iframe : array<string, int>|null
- External tool iframe configuration (when type = ExternalTool).
- $indent : int
- 0-based hierarchy indent level (0-5 allowed).
- $moduleId : int
- ID of the module containing this item.
- $newTab : bool|null
- Whether external tool opens in new tab.
- $pageUrl : string|null
- Only for 'Page' type - wiki page URL slug.
- $position : int
- 1-based position in module.
- $published : bool|null
- Visibility flag (optional, requires permissions).
- $title : string
- Item title.
- $type : string
- Content type (File, Page, Discussion, Assignment, Quiz, SubHeader, ExternalUrl, ExternalTool).
- $url : string|null
- Optional API endpoint URL.
- $apiClient : HttpClientInterface
- $course : Course
- Course context (required)
- $methodAliases : array<string|int, mixed>
- Define method aliases
- $module : Module
- Module context (required)
Methods
- __callStatic() : mixed
- Magic method to handle function aliases
- __construct() : mixed
- BaseApi constructor.
- checkCourse() : bool
- Check if course exists and has id
- checkModule() : bool
- Check if module exists and has id
- create() : self
- Create a new module item
- delete() : bool
- Delete a module item
- fetchAll() : array<string|int, mixed>
- Get all module items for a module.
- fetchAllPages() : array<string|int, ModuleItem>
- Fetch all module items from all pages
- fetchAllPaginated() : PaginatedResponse
- Fetch module items with pagination support
- fetchPage() : PaginationResult
- Fetch module items from a specific page
- find() : self
- Find a module item by its ID.
- getCompletionRequirement() : array<string, mixed>|null
- getContentDetails() : array<string, mixed>|null
- getContentId() : int|null
- getExternalUrl() : string|null
- getHtmlUrl() : string
- getId() : int
- getIframe() : array<string, int>|null
- getIndent() : int
- getModuleId() : int
- getNewTab() : bool|null
- getPageUrl() : string|null
- getPosition() : int
- getPublished() : bool|null
- getTitle() : string
- getType() : string
- getUrl() : string|null
- markAsDone() : bool
- Mark module item as done (manual completion marking)
- markAsNotDone() : bool
- Mark module item as not done (removes manual completion marking)
- markAsRead() : bool
- Mark module item as read (fulfills must_view completion requirement) Cannot be used on locked or unpublished items
- save() : bool
- Save the module item
- setApiClient() : void
- Set the API client
- setCompletionRequirement() : void
- setContentDetails() : void
- setContentId() : void
- setCourse() : void
- Set the course context
- setExternalUrl() : void
- setHtmlUrl() : void
- setId() : void
- setIframe() : void
- setIndent() : void
- setModule() : void
- Set the module context
- setModuleId() : void
- setNewTab() : void
- setPageUrl() : void
- setPosition() : void
- setPublished() : void
- setTitle() : void
- setType() : void
- setUrl() : void
- update() : self
- Update a module item
- 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
Constants
VALID_COMPLETION_TYPES
Canvas API supported completion requirement types
public
mixed
VALID_COMPLETION_TYPES
= [
'must_view',
// All types
'must_contribute',
// Assignment, Discussion, Page
'must_submit',
// Assignment, Quiz
'min_score',
// Assignment, Quiz
'must_mark_done',
]
VALID_TYPES
Canvas API supported module item types
public
mixed
VALID_TYPES
= ['File', 'Page', 'Discussion', 'Assignment', 'Quiz', 'SubHeader', 'ExternalUrl', 'ExternalTool']
Properties
$completionRequirement
Completion requirement structure.
public
array<string, mixed>|null
$completionRequirement
Format: ['type' => 'completion_type', 'min_score' => int]
$contentDetails
Type-specific details.
public
array<string, mixed>|null
$contentDetails
Format: ['points_possible' => int, 'due_at' => string]
$contentId
ID of associated content object (not required for ExternalUrl, Page, SubHeader).
public
int|null
$contentId
$externalUrl
For ExternalUrl/ExternalTool types.
public
string|null
$externalUrl
$htmlUrl
Canvas URL for this item.
public
string
$htmlUrl
$id
Unique identifier for the module item.
public
int
$id
$iframe
External tool iframe configuration (when type = ExternalTool).
public
array<string, int>|null
$iframe
Format: ['width' => int, 'height' => int]
$indent
0-based hierarchy indent level (0-5 allowed).
public
int
$indent
$moduleId
ID of the module containing this item.
public
int
$moduleId
$newTab
Whether external tool opens in new tab.
public
bool|null
$newTab
$pageUrl
Only for 'Page' type - wiki page URL slug.
public
string|null
$pageUrl
$position
1-based position in module.
public
int
$position
$published
Visibility flag (optional, requires permissions).
public
bool|null
$published
$title
Item title.
public
string
$title
$type
Content type (File, Page, Discussion, Assignment, Quiz, SubHeader, ExternalUrl, ExternalTool).
public
string
$type
$url
Optional API endpoint URL.
public
string|null
$url
$apiClient
protected
static HttpClientInterface
$apiClient
$course
Course context (required)
protected
static Course
$course
$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']]
$module
Module context (required)
protected
static Module
$module
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 exists and has id
public
static checkCourse() : bool
Tags
Return values
boolcheckModule()
Check if module exists and has id
public
static checkModule() : bool
Tags
Return values
boolcreate()
Create a new module item
public
static create(CreateModuleItemDTO|array<string|int, mixed> $data) : self
Parameters
- $data : CreateModuleItemDTO|array<string|int, mixed>
Tags
Return values
selfdelete()
Delete a module item
public
delete() : bool
Tags
Return values
boolfetchAll()
Get all module items for a module.
public
static fetchAll([array<string|int, mixed> $params = [] ]) : array<string|int, mixed>
Parameters
- $params : array<string|int, mixed> = []
Tags
Return values
array<string|int, mixed>fetchAllPages()
Fetch all module items from all pages
public
static fetchAllPages([array<string|int, mixed> $params = [] ]) : array<string|int, ModuleItem>
Parameters
- $params : array<string|int, mixed> = []
-
Query parameters for the request
Tags
Return values
array<string|int, ModuleItem>fetchAllPaginated()
Fetch module items with pagination support
public
static fetchAllPaginated([array<string|int, mixed> $params = [] ]) : PaginatedResponse
Parameters
- $params : array<string|int, mixed> = []
-
Query parameters for the request
Tags
Return values
PaginatedResponsefetchPage()
Fetch module items from a specific page
public
static fetchPage([array<string|int, mixed> $params = [] ]) : PaginationResult
Parameters
- $params : array<string|int, mixed> = []
-
Query parameters for the request
Tags
Return values
PaginationResultfind()
Find a module item by its ID.
public
static find(int $id) : self
Parameters
- $id : int
Tags
Return values
selfgetCompletionRequirement()
public
getCompletionRequirement() : array<string, mixed>|null
Return values
array<string, mixed>|nullgetContentDetails()
public
getContentDetails() : array<string, mixed>|null
Return values
array<string, mixed>|nullgetContentId()
public
getContentId() : int|null
Return values
int|nullgetExternalUrl()
public
getExternalUrl() : string|null
Return values
string|nullgetHtmlUrl()
public
getHtmlUrl() : string
Return values
stringgetId()
public
getId() : int
Return values
intgetIframe()
public
getIframe() : array<string, int>|null
Return values
array<string, int>|nullgetIndent()
public
getIndent() : int
Return values
intgetModuleId()
public
getModuleId() : int
Return values
intgetNewTab()
public
getNewTab() : bool|null
Return values
bool|nullgetPageUrl()
public
getPageUrl() : string|null
Return values
string|nullgetPosition()
public
getPosition() : int
Return values
intgetPublished()
public
getPublished() : bool|null
Return values
bool|nullgetTitle()
public
getTitle() : string
Return values
stringgetType()
public
getType() : string
Return values
stringgetUrl()
public
getUrl() : string|null
Return values
string|nullmarkAsDone()
Mark module item as done (manual completion marking)
public
markAsDone() : bool
Tags
Return values
boolmarkAsNotDone()
Mark module item as not done (removes manual completion marking)
public
markAsNotDone() : bool
Tags
Return values
boolmarkAsRead()
Mark module item as read (fulfills must_view completion requirement) Cannot be used on locked or unpublished items
public
markAsRead() : bool
Tags
Return values
boolsave()
Save the module item
public
save() : bool
Tags
Return values
boolsetApiClient()
Set the API client
public
static setApiClient(HttpClientInterface $apiClient) : void
Parameters
- $apiClient : HttpClientInterface
setCompletionRequirement()
public
setCompletionRequirement(array<string, mixed>|null $completionRequirement) : void
Parameters
- $completionRequirement : array<string, mixed>|null
setContentDetails()
public
setContentDetails(array<string, mixed>|null $contentDetails) : void
Parameters
- $contentDetails : array<string, mixed>|null
setContentId()
public
setContentId(int|null $contentId) : void
Parameters
- $contentId : int|null
setCourse()
Set the course context
public
static setCourse(Course $course) : void
Parameters
- $course : Course
setExternalUrl()
public
setExternalUrl(string|null $externalUrl) : void
Parameters
- $externalUrl : string|null
setHtmlUrl()
public
setHtmlUrl(string $htmlUrl) : void
Parameters
- $htmlUrl : string
setId()
public
setId(int $id) : void
Parameters
- $id : int
setIframe()
public
setIframe(array<string, int>|null $iframe) : void
Parameters
- $iframe : array<string, int>|null
setIndent()
public
setIndent(int $indent) : void
Parameters
- $indent : int
setModule()
Set the module context
public
static setModule(Module $module) : void
Parameters
- $module : Module
setModuleId()
public
setModuleId(int $moduleId) : void
Parameters
- $moduleId : int
setNewTab()
public
setNewTab(bool|null $newTab) : void
Parameters
- $newTab : bool|null
setPageUrl()
public
setPageUrl(string|null $pageUrl) : void
Parameters
- $pageUrl : string|null
setPosition()
public
setPosition(int $position) : void
Parameters
- $position : int
setPublished()
public
setPublished(bool|null $published) : void
Parameters
- $published : bool|null
setTitle()
public
setTitle(string $title) : void
Parameters
- $title : string
setType()
public
setType(string $type) : void
Parameters
- $type : string
setUrl()
public
setUrl(string|null $url) : void
Parameters
- $url : string|null
update()
Update a module item
public
static update(int $id, UpdateModuleItemDTO|array<string|int, mixed> $data) : self
Parameters
- $id : int
- $data : UpdateModuleItemDTO|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>createPaginationResult()
Helper method to create PaginationResult from paginated response
protected
static createPaginationResult(PaginatedResponse $paginatedResponse) : PaginationResult
Parameters
- $paginatedResponse : PaginatedResponse
Return values
PaginationResultfetchAllPagesAsModels()
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
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 object to an array
protected
toDtoArray() : array<string|int, mixed>