-
Notifications
You must be signed in to change notification settings - Fork 134
/
model.llms.quiz.attempt.question.php
341 lines (266 loc) · 6.56 KB
/
model.llms.quiz.attempt.question.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* Quiz Attempt Answer Question
*
* @package LifterLMS/Models/Classes
*
* @since 3.16.0
* @version 5.3.0
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_Quiz_Attempt_Question model class
*
* @since 3.16.0
* @since 3.16.15 Unknown.
*/
class LLMS_Quiz_Attempt_Question {
private $data = array();
/**
* Constructor
*
* @since 3.16.0
*
* @param array $data Question data array from attempt record.
* @return void
*/
public function __construct( $data = array() ) {
$this->data = wp_parse_args(
$data,
array(
'id' => null,
'earned' => null,
'points' => null,
'remarks' => null,
'answer' => null,
'correct' => null,
)
);
}
/**
* Determine if it's possible to manually grade the question
*
* @since 3.16.8
* @since 3.16.9 Unknown.
* @since 5.3.0 Early bail for deleted questions.
*
* @return boolean
*/
public function can_be_manually_graded() {
$question = $this->get_question();
if ( $question && $this->get( 'points' ) >= 1 ) {
// The question is auto-gradable so it cannot be manually graded.
if ( $question->get_auto_grade_type() ) {
return false;
} elseif ( $question->supports( 'grading', 'manual' ) || $question->supports( 'grading', 'conditional' ) ) {
return true;
}
}
return false;
}
/**
* Getter
*
* @since 3.16.0
*
* @param string $key Data key name
* @param mixed $default Optional. Default fallback value if key is unset. Default is empty string.
* @return mixed
*/
public function get( $key, $default = '' ) {
if ( isset( $this->data[ $key ] ) ) {
return $this->data[ $key ];
}
return $default;
}
/**
* Retrieve answer HTML for the question answers
*
* @since 3.16.0
* @since 3.16.15 Unknown.
*
* @return string
*/
public function get_answer() {
$question = $this->get_question();
$answers = $this->get_answer_array();
$ret = apply_filters( 'llms_quiz_attempt_question_get_answer_pre', '', $answers, $question, $this );
if ( ! $ret ) {
if ( $answers ) {
$ret = '<ul class="llms-quiz-attempt-answers">';
foreach ( $answers as $answer ) {
$ret .= sprintf( '<li class="llms-quiz-attempt-answer">%s</li>', wp_kses_post( $answer ) );
}
$ret .= '</ul>';
}
}
return apply_filters( 'llms_quiz_attempt_question_get_answer', $ret, $answers, $question, $this );
}
/**
* Get answer(s) as an array
*
* @since 3.16.15
* @since 3.27.0 Unknown.
*
* @return array
*/
public function get_answer_array() {
$ret = array();
$question = $this->get_question();
$answers = $this->get( 'answer' );
if ( $answers ) {
if ( $question->supports( 'choices' ) && $question->supports( 'grading', 'auto' ) ) {
foreach ( $answers as $aid ) {
$choice = $question->get_choice( $aid );
$ret[] = $choice ? $choice->get_choice() : _x( '[Deleted]', 'Selected quiz choice has been deleted.', 'lifterlms' );
}
} else {
$ret = $answers;
}
}
return apply_filters( 'llms_quiz_attempt_question_get_answer_array', $ret, $answers, $question, $this );
}
/**
* Retrieve answer HTML for the question correct answers
*
* @since 3.16.0
* @since 3.16.15 Unknown.
*
* @return string
*/
public function get_correct_answer() {
$ret = '';
$answers = $this->get_correct_answer_array();
if ( $answers ) {
$ret = '<ul class="llms-quiz-attempt-answers">';
foreach ( $answers as $answer ) {
$ret .= sprintf( '<li class="llms-quiz-attempt-answer">%s</li>', wp_kses_post( $answer ) );
}
$ret .= '</ul>';
}
return apply_filters( 'llms_quiz_attempt_question_get_correct_answer', $ret, $answers, $this->get_question(), $this );
}
/**
* Get correct answer(s) as an array
*
* @since 3.16.15
*
* @return array
*/
public function get_correct_answer_array() {
$ret = array();
$question = $this->get_question();
$type = $question->get_auto_grade_type();
if ( 'choices' === $type ) {
foreach ( $question->get_correct_choice() as $aid ) {
$choice = $question->get_choice( $aid );
$ret[] = $choice->get_choice();
}
} elseif ( 'conditional' === $type ) {
$ret = $question->get_conditional_correct_value();
}
return apply_filters( 'llms_quiz_attempt_question_get_correct_answer_array', $ret, $question, $this );
}
/**
* Retrieve an instance of the LLMS_Question
*
* @since 3.16.0
*
* @return LLMS_Question
*/
public function get_question() {
return llms_get_post( $this->get( 'id' ) );
}
/**
* Retrieve the status icon HTML based on the question's status/answer
*
* @since 3.16.0
*
* @return string
*/
public function get_status_icon() {
$icon = '';
switch ( $this->get_status() ) {
case 'graded':
if ( $this->is_correct() ) {
$icon = 'check';
$tip = esc_attr__( 'Correct answer', 'lifterlms' );
} else {
$icon = 'times';
$tip = esc_attr__( 'Incorrect answer', 'lifterlms' );
}
break;
case 'waiting':
$icon = 'clock-o';
$tip = esc_attr__( 'Awaiting review', 'lifterlms' );
break;
}
if ( $icon ) {
return sprintf( '<span class="llms-status-icon-tip tip--top-left" data-tip="%1$s"><i class="llms-status-icon fa fa-%2$s"></i><span>', $tip, $icon );
}
return '';
}
/**
* Receive the graded status of the question
*
* @since 3.16.0
* @since 3.16.9 Unknown.
* @since 5.3.0 Account for deleted questions.
*
* @return string Attempt's question status [graded|waiting|none].
*/
public function get_status() {
$question = $this->get_question();
if ( ! $question ) {
return 'graded';
}
$status = 'none';
if ( $this->get( 'points' ) >= 1 ) {
if ( $question->get_auto_grade_type() ) {
$status = 'graded';
} elseif ( $question->supports( 'grading', 'manual' ) || $question->supports( 'grading', 'conditional' ) ) {
if ( ! $this->get( 'correct' ) ) {
$status = 'waiting';
} else {
$status = 'graded';
}
}
}
return $status;
}
/**
* Determine if remarks are available for the question
*
* @since 3.16.0
*
* @return bool
*/
public function has_remarks() {
return ( $this->get( 'remarks' ) );
}
/**
* Determine if a question is correct
*
* @since 3.16.8
*
* @return bool
*/
public function is_correct() {
if ( 'graded' === $this->get_status() ) {
return llms_parse_bool( $this->get( 'correct' ) );
}
return false;
}
/**
* Setter
*
* @since 3.16.0
*
* @param string $key Data key name.
* @param mixed $val Value.
* @return void
*/
public function set( $key, $val ) {
$this->data[ $key ] = $val;
}
}