LLMS_Quiz

LLMS_Quiz model class.


Source Source

File: includes/models/model.llms.quiz.php

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
 * Post Type Database name (as registered via `register_post_type()`).
 *
 * @var string
 */
protected $db_post_type = 'llms_quiz';
 
/**
 * Post type name (without prefix).
 *
 * @var string
 */
protected $model_post_type = 'quiz';
 
/**
 * Post type meta properties
 *
 * Array key is the meta_key and array values is property's type.
 *
 * @var string[]
 */
protected $properties = array(
    'lesson_id'           => 'absint',
    'allowed_attempts'    => 'int',
    'limit_attempts'      => 'yesno',
    'limit_time'          => 'yesno',
    'passing_percent'     => 'float',
    'random_questions'    => 'yesno',
    'show_correct_answer' => 'yesno',
    'time_limit'          => 'int',
);
 
/**
 * Retrieve the LLMS_Course for the quiz.
 *
 * @since 3.16.0
 *
 * @return LLMS_Course|false
 */
public function get_course() {
    $lesson = $this->get_lesson();
    if ( $lesson ) {
        return $lesson->get_course();
    }
    return false;
}
 
/**
 * Retrieve LLMS_Lesson for the quiz's parent lesson.
 *
 * @since 3.16.0
 * @since 3.16.12 Unknown.
 *
 * @return LLMS_Lesson|false|null The lesson object on success, `false` if no id stored, and `null` if the stored ID doesn't exist.
 */
public function get_lesson() {
    $id = $this->get( 'lesson_id' );
    if ( ! $id ) {
        return false;
    }
    return llms_get_post( $id );
}
 
/**
 * Retrieve the quizzes child questions.
 *
 * @since 3.16.0
 *
 * @param string $return Optional. Type of return [ids|posts|questions]. Default `'questions'`.
 * @return array
 */
public function get_questions( $return = 'questions' ) {
    return $this->questions()->get_questions( $return );
}
 
/**
 * Get questions count.
 *
 * @since 7.4.0
 *
 * @return int Question Count.
 */
public function get_questions_count() {
 
    /**
     * Filter the count of questions in a quiz.
     *
     * @since 7.4.0
     *
     * @param int       $questions_count Number of questions in a quiz.
     * @param LLMS_Quiz $quiz            Current quiz object.
     */
    return apply_filters( 'llms_quiz_questions_count', count( $this->get_questions( 'ids' ) ), $this );
}
 
/**
 * Retrieve the time limit formatted as a human readable string.
 *
 * @since 3.16.0
 *
 * @return string
 */
public function get_time_limit_string() {
    return LLMS_Date::convert_to_hours_minutes_string( $this->get( 'time_limit' ) );
}
 
/**
 * Determine if the quiz defines limited attempts.
 *
 * @since 3.16.0
 *
 * @return bool
 */
public function has_attempt_limit() {
    return ( 'yes' === $this->get( 'limit_attempts' ) );
}
 
/**
 * Determine if a time limit is enabled for the quiz.
 *
 * @since 3.16.0
 *
 * @return bool
 */
public function has_time_limit() {
    return ( 'yes' === $this->get( 'limit_time' ) );
}
 
/**
 * Determine if the quiz is an orphan.
 *
 * @since 3.16.12
 * @since 4.2.0 Added the $deep parameter.
 *
 * @param bool $deep Optional. Whether or not deeply check this quiz is orphan. Default `false`.
 *                   When set to true will ensure not only that this quiz as a `lesson_id` property set
 *                   But also that the lesson with id `lesson_id` has a `quiz` property as equal as this quiz id.
 * @return bool
 */
public function is_orphan( $deep = false ) {
 
    $parent_id = $this->get( 'lesson_id' );
 
    if ( ! $parent_id ) {
        return true;
    }
 
    /**
     * This is to take into account possible data inconsistency.
     *
     */
    if ( $deep ) {
        $lesson = llms_get_post( $parent_id );
        // Both the ids are already absint, see LLMS_Post_Model::___get().
        if ( ! $lesson || $this->get( 'id' ) !== $lesson->get( 'quiz' ) ) {
            return true;
        }
    }
 
    return false;
 
}
 
/**
 * Determine if a student can take the quiz.
 *
 * @since 3.0.0
 * @since 3.16.0 Unkwnown.
 * @since 3.37.2 Added `llms_quiz_is_open` filter hook.
 *
 * @param int $user_id Optional. WP User ID, none supplied uses current user. Default `null`.
 * @return boolean
 */
public function is_open( $user_id = null ) {
 
    $student = llms_get_student( $user_id );
    if ( ! $student ) {
        $quiz_open = false;
    } else {
 
        $remaining = $student->quizzes()->get_attempts_remaining_for_quiz( $this->get( 'id' ) );
 
        // string for "unlimited" or number of attempts.
        $quiz_open = ! is_numeric( $remaining ) || $remaining > 0;
    }
 
    /**
     * Filters whether the quiz is open to a student or not.
     *
     * @param boolean            $quiz_open Whether the quiz is open.
     * @param int|null           $user_id   WP User ID, can be `null`.
     * @param int                $quiz_id   The Quiz id.
     * @param LLMS_Quiz          $quiz      The LLMS_Quiz instance.
     * @param LLMS_Student|false $student   LLMS_Student instance or false if user not found.
     */
    return apply_filters( 'llms_quiz_is_open', $quiz_open, $user_id, $this->get( 'id' ), $this, $student );
 
}
 
/**
 * Retrieve an instance of the question manager for the quiz.
 *
 * @since 3.16.0
 *
 * @return LLMS_Question_Manager
 */
public function questions() {
    return new LLMS_Question_Manager( $this );
}
 
/**
 * Called before data is sorted and returned by $this->toArray().
 * Extending classes should override this data if custom data should
 * be added when object is converted to an array or json.
 *
 * @since 3.3.0
 * @since 3.19.2 Unknown.
 * @since 3.38.0 Only add theme metadata to the quiz array when the `llms_get_quiz_theme_settings` filter is being used.
 *
 * @param array $arr Array of data to be serialized.
 * @return array
 */
protected function toArrayAfter( $arr ) {
 
    $arr['questions'] = array();
 
    // Builder lazy loads questions via ajax.
    global $llms_builder_lazy_load;
    if ( ! $llms_builder_lazy_load ) {
        foreach ( $this->get_questions() as $question ) {
            $arr['questions'][] = $question->toArray();
        }
    }
 
    // If theme has legacy support quiz layouts, add theme metadata to the array.
    if ( get_theme_support( 'lifterlms-quizzes' ) && has_filter( 'llms_get_quiz_theme_settings' ) ) {
        $layout = llms_get_quiz_theme_setting( 'layout' );
        if ( $layout ) {
            $arr[ $layout['id'] ] = get_post_meta( $this->get( 'id' ), $layout['id'], true );
        }
    }
 
    return $arr;
 
}
 
/**
 * Get the (points) value of a question.
 *
 * @since 3.3.0
 * @since 3.37.2 Use strict comparison '===' in place of '=='.
 *
 * @param int $question_id  WP Post ID of the LLMS_Question.
 * @return int
 */
public function get_question_value( $question_id ) {
 
    foreach ( $this->get_questions_raw() as $q ) {
        if ( $question_id === $q['id'] ) {
            return absint( $q['points'] );
        }
    }


Top ↑

Properties Properties

The following post and post meta properties are accessible for this class. See LLMS_Post_Model::get() and LLMS_Post_Model::set() for more information.

$allowed_attempts

(int) Number of times a student is allowed to take the quiz before being locked out of it.

$passing_percent

(float) Grade required for a student to "pass" the quiz.

$random_answers

(yesno) Whether or not to randomize the order of answers to the quiz questions.

$random_questions

(yesno) Whether or not to randomize the order of questions for each attempt.

$show_correct_answer

(yesno) Whether or not to show the correct answer(s) to students on the quiz results screen.

$show_options_description_right_answer

(yesno) If yes, displays the question description when the student chooses the correct answer.

$show_options_description_wrong_answer

(yesno) If yes, displays the question description when the student chooses the wrong answer.

$show_results

(yesno) If yes, results will be shown to the student at the conclusion of the quiz.

$time_limit

(int) Quiz time limit (in minutes), empty denotes unlimited (untimed) quiz.


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Remove previously deprecated method LLMS_Quiz::get_lessons().
4.2.0 Added a parameter to the is_orphan() method to deeply check the quiz is not really attached to any lesson.
4.0.0 Remove deprecated methods.
3.38.0 Only add theme metadata to the quiz array when the llms_get_quiz_theme_settings filter is being used.
3.37.2 Added llms_quiz_is_open filter hook.
3.3.0
3.19.2 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.