LLMS_Student_Quizzes::get_attempts_remaining_for_quiz( int $quiz_id, bool $allow_negative = false )
Get the number of attempts remaining by a student for a given quiz.
Parameters Parameters
- $quiz_id
-
(int) (Required) WP Post ID of the Quiz.
- $allow_negative
-
(bool) (Optional) Allow returning negative remaining attempts.
Default value: false
Return Return
(mixed)
Source Source
File: includes/models/model.llms.student.quizzes.php
public function get_attempts_remaining_for_quiz( $quiz_id, $allow_negative = false ) {
$quiz = llms_get_post( $quiz_id );
$ret = _x( 'Unlimited', 'quiz attempts remaining', 'lifterlms' );
if ( $quiz->has_attempt_limit() ) {
$allowed = $quiz->get( 'allowed_attempts' );
$used = $this->count_attempts_by_quiz( $quiz->get( 'id' ) );
// Ensure undefined, null, '', etc. show as an int.
if ( ! $allowed ) {
$allowed = 0;
}
$remaining = ( $allowed - $used );
// Don't show negative attempts.
$ret = $allow_negative ? $remaining : max( 0, $remaining );
}
/**
* Filters the number of attempts remaining by a student for a given quiz.
*
* @since 3.16.0
*
* @param mixed $ret The number of attempts remaining by a student for a given quiz,
* or 'Unlimited' for quizzes with no attempts limit.
* @param LLMS_Quiz $quiz Quiz object.
* @param LLMS_Student_Quizzes $student_quizzes Student quizzes object.
*/
return apply_filters( 'llms_student_quiz_attempts_remaining_for_quiz', $ret, $quiz, $this );
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 6.4.0 | Added parameter $allow_negative to allow remaining negative remaining attempts. It can happen when the allowed attempts number is decreased to a number lower than the number of the attempts already made by a given student. |
| 3.16.0 | Introduced. |