Documentation

QuizSubmission extends AbstractBaseApi

Canvas LMS Quiz Submissions API

Provides functionality to manage quiz submissions and attempts in Canvas LMS. This class handles creating, reading, updating quiz submissions for a specific quiz.

Usage Examples:

// Set course and quiz context (required for all operations)
$course = Course::find(123);
$quiz = Quiz::find(456);
QuizSubmission::setCourse($course);
QuizSubmission::setQuiz($quiz);

// Start a new quiz submission
$submission = QuizSubmission::start(['access_code' => 'secret123']);

// Get current user's submission
$submission = QuizSubmission::getCurrentUserSubmission();

// List all submissions for the quiz
$submissions = QuizSubmission::get();

// Get submissions with specific parameters
$submissions = QuizSubmission::get(['include' => ['user', 'quiz']]);

// Find specific submission
$submission = QuizSubmission::find(789);

// Update submission scores manually
$updatedSubmission = QuizSubmission::update(789, [
    'fudge_points' => 2.5,
    'quiz_submissions' => [
        ['attempt' => 1, 'fudge_points' => 2.5]
    ]
]);

// Complete a submission
$submission = QuizSubmission::find(789);
$success = $submission->complete();

// Check submission state
if ($submission->isComplete()) {
    echo "Submission completed with score: " . $submission->getScore();
}

Table of Contents

Properties

$attempt  : int|null
Attempt number (1-based)
$endAt  : DateTime|null
When the submission will end (time limit)
$extraAttempts  : int|null
Extra attempts allowed for this user
$extraTime  : int|null
Extra time allowed for this user (in minutes)
$finishedAt  : DateTime|null
When the submission was finished
$fudgePoints  : float|null
Fudge points added to the score
$hasSeenResults  : bool|null
Whether user has seen the results
$id  : int|null
Quiz submission unique identifier
$keptScore  : float|null
Score kept after regrade
$manuallyUnlocked  : bool|null
Whether submission was manually unlocked
$overdueAndNeedsSubmission  : bool|null
Whether submission is overdue and needs submission
$quizData  : array<string|int, mixed>|null
Associated quiz data (when included)
$quizId  : int|null
Quiz ID this submission belongs to
$score  : float|null
Current score for the submission
$scoreBeforeRegrade  : float|null
Score before any regrade
$startedAt  : DateTime|null
When the submission was started
$submission  : array<string|int, mixed>|null
Associated submission data (when included)
$submissionId  : int|null
Associated submission ID
$timeSpent  : int|null
Time spent on the quiz (in seconds)
$user  : array<string|int, mixed>|null
Associated user data (when included)
$userId  : int|null
User ID who made the submission
$validationToken  : string|null
Validation token for submission completion
$workflowState  : string|null
Workflow state (untaken, pending_review, complete, settings_only, preview)
$course  : Course|null
$methodAliases  : array<string, array<string|int, string>>
Define method aliases
$quiz  : Quiz|null

Methods

__callStatic()  : mixed
Magic method to handle function aliases
__construct()  : mixed
BaseApi constructor.
all()  : array<string|int, self>
Fetch all pages of quiz submissions
canBeRetaken()  : bool
Check if submission can be retaken
complete()  : self
Complete a quiz submission
create()  : self
find()  : static
Find a specific quiz submission by ID
get()  : array<string|int, self>
Fetch all quiz submissions for the quiz
getAttempt()  : int|null
Get attempt number
getCurrentUserSubmission()  : self|null
Get current user's submission for the quiz
getEndAt()  : DateTime|null
Get end at timestamp
getExtraAttempts()  : int|null
Get extra attempts
getExtraTime()  : int|null
Get extra time in minutes
getFinishedAt()  : DateTime|null
Get finished at timestamp
getFudgePoints()  : float|null
Get fudge points
getHasSeenResults()  : bool|null
Get has seen results status
getId()  : int|null
Get quiz submission ID
getKeptScore()  : float|null
Get kept score
getManuallyUnlocked()  : bool|null
Get manually unlocked status
getOverdueAndNeedsSubmission()  : bool|null
Get overdue and needs submission status
getQuizData()  : array<string|int, mixed>|null
Get associated quiz data
getQuizId()  : int|null
Get quiz ID
getRemainingTime()  : int|null
Get remaining time in seconds
getScore()  : float|null
Get current score
getScoreBeforeRegrade()  : float|null
Get score before regrade
getStartedAt()  : DateTime|null
Get started at timestamp
getSubmission()  : array<string|int, mixed>|null
Get associated submission data
getSubmissionId()  : int|null
Get submission ID
getTimeSpent()  : int|null
Get time spent in seconds
getUser()  : array<string|int, mixed>|null
Get associated user data
getUserId()  : int|null
Get user ID
getValidationToken()  : string|null
Get validation token
getWorkflowState()  : string|null
Get workflow state
hasTimeLimit()  : bool
Check if quiz has time limit
isComplete()  : bool
Check if submission is complete
isInProgress()  : bool
Check if submission is in progress
isOverdue()  : bool
Check if submission is overdue
overrideApiClient()  : void
Set an API client for this class only, leaving other resources on the shared default.
paginate()  : PaginationResult
Get paginated quiz submissions
resetApiClients()  : void
Clear the shared default client and all per-class overrides.
save()  : self
Save the quiz submission (update if exists)
setApiClient()  : void
Set the shared default API client used by ALL resource classes.
setAttempt()  : void
Set attempt number
setCourse()  : void
Set the course context for quiz submissions
setEndAt()  : void
Set end at timestamp
setExtraAttempts()  : void
Set extra attempts
setExtraTime()  : void
Set extra time in minutes
setFinishedAt()  : void
Set finished at timestamp
setFudgePoints()  : void
Set fudge points
setHasSeenResults()  : void
Set has seen results status
setId()  : void
Set quiz submission ID
setKeptScore()  : void
Set kept score
setManuallyUnlocked()  : void
Set manually unlocked status
setOverdueAndNeedsSubmission()  : void
Set overdue and needs submission status
setQuiz()  : void
Set the quiz context for quiz submissions
setQuizData()  : void
Set associated quiz data
setQuizId()  : void
Set quiz ID
setScore()  : void
Set current score
setScoreBeforeRegrade()  : void
Set score before regrade
setStartedAt()  : void
Set started at timestamp
setSubmission()  : void
Set associated submission data
setSubmissionId()  : void
Set submission ID
setTimeSpent()  : void
Set time spent in seconds
setUser()  : void
Set associated user data
setUserId()  : void
Set user ID
setValidationToken()  : void
Set validation token
setWorkflowState()  : void
Set workflow state
start()  : self
Start a quiz submission (alias for create)
stream()  : Generator<int, static>
Stream all items across all pages one at a time.
toArray()  : array<string|int, mixed>
Convert quiz submission to array
toDtoArray()  : array<string|int, mixed>
Convert quiz submission to DTO-compatible array
update()  : self
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
checkContext()  : bool
Check if both course and quiz contexts are set
checkCourse()  : bool
Check if course context is set
checkQuiz()  : bool
Check if quiz context is set
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
getAliasMap()  : array<string, string>
Build a flat alias-to-method lookup from $methodAliases.
getApiClient()  : HttpClientInterface
Get the API client, initializing if necessary
getCourse()  : Course
Get the Course instance, ensuring it is set
getEndpoint()  : string
Get the API endpoint for this resource
getPaginatedResponse()  : PaginatedResponse
Helper method to get paginated response from API endpoint
getQuiz()  : Quiz
Get the Quiz instance, ensuring it is set
hydrate()  : void
Assign API response data to properties with type coercion.
parseJsonResponse()  : array<string|int, mixed>
Parse JSON response from API safely handling StreamInterface
populate()  : void
Populate the object with new data
validateContext()  : void
Validate a context type path segment against an allowlist.

Properties

$attempt

Attempt number (1-based)

public int|null $attempt = null

$endAt

When the submission will end (time limit)

public DateTime|null $endAt = null

$extraAttempts

Extra attempts allowed for this user

public int|null $extraAttempts = null

$extraTime

Extra time allowed for this user (in minutes)

public int|null $extraTime = null

$finishedAt

When the submission was finished

public DateTime|null $finishedAt = null

$fudgePoints

Fudge points added to the score

public float|null $fudgePoints = null

$hasSeenResults

Whether user has seen the results

public bool|null $hasSeenResults = null

$id

Quiz submission unique identifier

public int|null $id = null

$keptScore

Score kept after regrade

public float|null $keptScore = null

$manuallyUnlocked

Whether submission was manually unlocked

public bool|null $manuallyUnlocked = null

$overdueAndNeedsSubmission

Whether submission is overdue and needs submission

public bool|null $overdueAndNeedsSubmission = null

$quizData

Associated quiz data (when included)

public array<string|int, mixed>|null $quizData = null

$quizId

Quiz ID this submission belongs to

public int|null $quizId = null

$score

Current score for the submission

public float|null $score = null

$scoreBeforeRegrade

Score before any regrade

public float|null $scoreBeforeRegrade = null

$startedAt

When the submission was started

public DateTime|null $startedAt = null

$submission

Associated submission data (when included)

public array<string|int, mixed>|null $submission = null

$submissionId

Associated submission ID

public int|null $submissionId = null

$timeSpent

Time spent on the quiz (in seconds)

public int|null $timeSpent = null

$user

Associated user data (when included)

public array<string|int, mixed>|null $user = null

$userId

User ID who made the submission

public int|null $userId = null

$validationToken

Validation token for submission completion

public string|null $validationToken = null

$workflowState

Workflow state (untaken, pending_review, complete, settings_only, preview)

public string|null $workflowState = null

$methodAliases

Define method aliases

protected static array<string, array<string|int, string>> $methodAliases = ['get' => ['fetch', 'list'], 'all' => ['fetchAllPages', 'getAll', 'fetchAll'], 'paginate' => ['getPaginated', 'withPagination'], '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()

BaseApi constructor.

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

all()

Fetch all pages of quiz submissions

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

Optional parameters for filtering

Tags
throws
CanvasApiException

If course/quiz not set or API error

Return values
array<string|int, self> —

Array of all quiz submission instances

canBeRetaken()

Check if submission can be retaken

public canBeRetaken() : bool
Return values
bool

find()

Find a specific quiz submission by ID

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

Quiz submission ID

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

Optional query parameters

Tags
throws
CanvasApiException

If course/quiz not set or API error

Return values
static —

Quiz submission instance

get()

Fetch all quiz submissions for the quiz

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

Optional parameters for filtering

Tags
throws
CanvasApiException

If course/quiz not set or API error

Return values
array<string|int, self> —

Array of quiz submission instances

getAttempt()

Get attempt number

public getAttempt() : int|null
Return values
int|null

getCurrentUserSubmission()

Get current user's submission for the quiz

public static getCurrentUserSubmission() : self|null
Tags
throws
CanvasApiException

If course/quiz not set or API error

Return values
self|null —

Quiz submission instance or null if no submission

getEndAt()

Get end at timestamp

public getEndAt() : DateTime|null
Return values
DateTime|null

getExtraAttempts()

Get extra attempts

public getExtraAttempts() : int|null
Return values
int|null

getExtraTime()

Get extra time in minutes

public getExtraTime() : int|null
Return values
int|null

getFinishedAt()

Get finished at timestamp

public getFinishedAt() : DateTime|null
Return values
DateTime|null

getFudgePoints()

Get fudge points

public getFudgePoints() : float|null
Return values
float|null

getHasSeenResults()

Get has seen results status

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

getId()

Get quiz submission ID

public getId() : int|null
Return values
int|null

getKeptScore()

Get kept score

public getKeptScore() : float|null
Return values
float|null

getManuallyUnlocked()

Get manually unlocked status

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

getOverdueAndNeedsSubmission()

Get overdue and needs submission status

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

getQuizData()

Get associated quiz data

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

getQuizId()

Get quiz ID

public getQuizId() : int|null
Return values
int|null

getRemainingTime()

Get remaining time in seconds

public getRemainingTime() : int|null
Return values
int|null

getScore()

Get current score

public getScore() : float|null
Return values
float|null

getScoreBeforeRegrade()

Get score before regrade

public getScoreBeforeRegrade() : float|null
Return values
float|null

getStartedAt()

Get started at timestamp

public getStartedAt() : DateTime|null
Return values
DateTime|null

getSubmission()

Get associated submission data

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

getSubmissionId()

Get submission ID

public getSubmissionId() : int|null
Return values
int|null

getTimeSpent()

Get time spent in seconds

public getTimeSpent() : int|null
Return values
int|null

getUser()

Get associated user data

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

getUserId()

Get user ID

public getUserId() : int|null
Return values
int|null

getValidationToken()

Get validation token

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

getWorkflowState()

Get workflow state

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

hasTimeLimit()

Check if quiz has time limit

public hasTimeLimit() : bool
Return values
bool

isComplete()

Check if submission is complete

public isComplete() : bool
Return values
bool

isInProgress()

Check if submission is in progress

public isInProgress() : bool
Return values
bool

isOverdue()

Check if submission is overdue

public isOverdue() : bool
Return values
bool

paginate()

Get paginated quiz submissions

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

Optional parameters for filtering

Tags
throws
CanvasApiException

If course/quiz not set or API error

Return values
PaginationResult —

Pagination result with submissions

resetApiClients()

Clear the shared default client and all per-class overrides.

public static resetApiClients() : void

save()

Save the quiz submission (update if exists)

public save() : self
Tags
throws
CanvasApiException

If no ID set or validation fails

Return values
self

setApiClient()

Set the shared default API client used by ALL resource classes.

public static setApiClient(HttpClientInterface $apiClient) : void

Calling this on any resource (e.g. Course::setApiClient()) replaces the client for every resource, because relationship methods cross class boundaries ($course->enrollments() calls Enrollment internally). Use overrideApiClient() to scope a client to a single class, and resetApiClients() in test teardown to avoid state leaking.

Parameters
$apiClient : HttpClientInterface

setAttempt()

Set attempt number

public setAttempt(int|null $attempt) : void
Parameters
$attempt : int|null

setCourse()

Set the course context for quiz submissions

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

The course instance

setEndAt()

Set end at timestamp

public setEndAt(DateTime|null $endAt) : void
Parameters
$endAt : DateTime|null

setExtraAttempts()

Set extra attempts

public setExtraAttempts(int|null $extraAttempts) : void
Parameters
$extraAttempts : int|null

setExtraTime()

Set extra time in minutes

public setExtraTime(int|null $extraTime) : void
Parameters
$extraTime : int|null

setFinishedAt()

Set finished at timestamp

public setFinishedAt(DateTime|null $finishedAt) : void
Parameters
$finishedAt : DateTime|null

setFudgePoints()

Set fudge points

public setFudgePoints(float|null $fudgePoints) : void
Parameters
$fudgePoints : float|null

setHasSeenResults()

Set has seen results status

public setHasSeenResults(bool|null $hasSeenResults) : void
Parameters
$hasSeenResults : bool|null

setId()

Set quiz submission ID

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

setKeptScore()

Set kept score

public setKeptScore(float|null $keptScore) : void
Parameters
$keptScore : float|null

setManuallyUnlocked()

Set manually unlocked status

public setManuallyUnlocked(bool|null $manuallyUnlocked) : void
Parameters
$manuallyUnlocked : bool|null

setOverdueAndNeedsSubmission()

Set overdue and needs submission status

public setOverdueAndNeedsSubmission(bool|null $overdueAndNeedsSubmission) : void
Parameters
$overdueAndNeedsSubmission : bool|null

setQuiz()

Set the quiz context for quiz submissions

public static setQuiz(Quiz $quiz) : void
Parameters
$quiz : Quiz

The quiz instance

setQuizData()

Set associated quiz data

public setQuizData(array<string|int, mixed>|null $quizData) : void
Parameters
$quizData : array<string|int, mixed>|null

setQuizId()

Set quiz ID

public setQuizId(int|null $quizId) : void
Parameters
$quizId : int|null

setScore()

Set current score

public setScore(float|null $score) : void
Parameters
$score : float|null

setScoreBeforeRegrade()

Set score before regrade

public setScoreBeforeRegrade(float|null $scoreBeforeRegrade) : void
Parameters
$scoreBeforeRegrade : float|null

setStartedAt()

Set started at timestamp

public setStartedAt(DateTime|null $startedAt) : void
Parameters
$startedAt : DateTime|null

setSubmission()

Set associated submission data

public setSubmission(array<string|int, mixed>|null $submission) : void
Parameters
$submission : array<string|int, mixed>|null

setSubmissionId()

Set submission ID

public setSubmissionId(int|null $submissionId) : void
Parameters
$submissionId : int|null

setTimeSpent()

Set time spent in seconds

public setTimeSpent(int|null $timeSpent) : void
Parameters
$timeSpent : int|null

setUser()

Set associated user data

public setUser(array<string|int, mixed>|null $user) : void
Parameters
$user : array<string|int, mixed>|null

setUserId()

Set user ID

public setUserId(int|null $userId) : void
Parameters
$userId : int|null

setValidationToken()

Set validation token

public setValidationToken(string|null $validationToken) : void
Parameters
$validationToken : string|null

setWorkflowState()

Set workflow state

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

start()

Start a quiz submission (alias for create)

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

Optional parameters like access_code

Tags
throws
CanvasApiException

If course/quiz not set or API error

Return values
self —

Created quiz submission instance

stream()

Stream all items across all pages one at a time.

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

Unlike all(), only one page of raw data is held in memory at a time, making this safe for very large datasets (e.g. tens of thousands of enrollments):

foreach (User::stream(['per_page' => 100]) as $user) {
    processUser($user);
}
Parameters
$params : array<string|int, mixed> = []

Query parameters for the request

Tags
throws
CanvasApiException
Return values
Generator<int, static>

toArray()

Convert quiz submission to array

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

Quiz submission data

toDtoArray()

Convert quiz submission to DTO-compatible array

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

DTO-compatible data

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

checkContext()

Check if both course and quiz contexts are set

protected static checkContext() : bool
Tags
throws
CanvasApiException

If course or quiz is not set

Return values
bool —

True if both are set

checkCourse()

Check if course context is set

protected static checkCourse() : bool
Tags
throws
CanvasApiException

If course is not set

Return values
bool —

True if course is set

checkQuiz()

Check if quiz context is set

protected static checkQuiz() : bool
Tags
throws
CanvasApiException

If quiz is not set

Return values
bool —

True if quiz is set

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>

getAliasMap()

Build a flat alias-to-method lookup from $methodAliases.

protected static getAliasMap() : array<string, string>
Return values
array<string, 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

hydrate()

Assign API response data to properties with type coercion.

protected hydrate(array<string|int, mixed> $data) : void

Shared by the constructor and populate() so objects keep the same type guarantees after save()/update() round-trips as on creation.

Parameters
$data : array<string|int, mixed>

parseJsonResponse()

Parse JSON response from API safely handling StreamInterface

protected static parseJsonResponse(ResponseInterface $response) : array<string|int, mixed>
Parameters
$response : ResponseInterface
Return values
array<string|int, mixed>

populate()

Populate the object with new data

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

validateContext()

Validate a context type path segment against an allowlist.

protected static validateContext(string|null $contextType, array<int, string> $allowed) : void

Context types are interpolated into URL paths; validating against the contexts Canvas actually supports prevents crafted values from injecting extra path segments or query parameters.

Parameters
$contextType : string|null

The context type (plural, e.g. 'courses'); null is ignored

$allowed : array<int, string>

Allowed context types

Tags
throws
CanvasApiException

If the context type is not allowed

On this page

Search results