class-learndash-admin-data-upgrades-user-meta-quizzes.php
15.3 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
* LearnDash Data Upgrades for User Quizzes.
*
* @since 2.6.0
* @package LearnDash\Data_Upgrades
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'Learndash_Admin_Data_Upgrades' ) ) && ( ! class_exists( 'Learndash_Admin_Data_Upgrades_User_Meta_Quizzes' ) ) ) {
/**
* Class LearnDash Data Upgrades for User Quizzes.
*
* @since 2.6.0
* @uses Learndash_Admin_Data_Upgrades
*/
class Learndash_Admin_Data_Upgrades_User_Meta_Quizzes extends Learndash_Admin_Data_Upgrades {
/**
* Protected constructor for class
*
* @since 2.3.0
*/
protected function __construct() {
$this->data_slug = 'user-meta-quizzes';
parent::__construct();
parent::register_upgrade_action();
}
/**
* Show data upgrade row for this instance.
*
* @since 2.3.0
*/
public function show_upgrade_action() {
?>
<tr id="learndash-data-upgrades-container-<?php echo esc_attr( $this->data_slug ); ?>" class="learndash-data-upgrades-container">
<td class="learndash-data-upgrades-button-container">
<button class="learndash-data-upgrades-button button button-primary" data-nonce="<?php echo esc_attr( wp_create_nonce( 'learndash-data-upgrades-' . $this->data_slug . '-' . get_current_user_id() ) ); ?>" data-slug="<?php echo esc_attr( $this->data_slug ); ?>">
<?php
esc_html_e( 'Upgrade', 'learndash' );
?>
</button>
</td>
<td class="learndash-data-upgrades-status-container">
<span class="learndash-data-upgrades-name">
<?php
printf(
// translators: placeholder: Quiz.
esc_html_x( 'Upgrade User %s Data', 'placeholder: Quiz', 'learndash' ),
LearnDash_Custom_Label::get_label( 'quiz' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Method escapes output
);
?>
</span>
<p>
<?php
printf(
// translators: placeholder: quiz.
esc_html_x( 'This upgrade will sync your existing user data for %s into a new database table for better reporting. (Required)', 'placeholder: quiz', 'learndash' ),
esc_html( learndash_get_custom_label_lower( 'quiz' ) )
);
?>
</p>
<p class="description"><?php echo esc_html( $this->get_last_run_info() ); ?></p>
<?php
$show_progress = false;
$this->transient_key = $this->data_slug;
$this->transient_data = $this->get_transient( $this->transient_key );
if ( ! empty( $this->transient_data ) ) {
if ( isset( $this->transient_data['result_count'] ) ) {
$this->transient_data['result_count'] = intval( $this->transient_data['result_count'] );
} else {
$this->transient_data['result_count'] = 0;
}
if ( isset( $this->transient_data['total_count'] ) ) {
$this->transient_data['total_count'] = intval( $this->transient_data['total_count'] );
} else {
$this->transient_data['total_count'] = 0;
}
if ( ( ! empty( $this->transient_data['result_count'] ) ) && ( ! empty( $this->transient_data['total_count'] ) ) && ( $this->transient_data['result_count'] != $this->transient_data['total_count'] ) ) {
$show_progress = true;
?>
<p id="learndash-data-upgrades-continue-<?php echo esc_attr( $this->data_slug ); ?>" class="learndash-data-upgrades-continue"><input type="checkbox" name="learndash-data-upgrades-continue" value="1" /> <?php esc_html_e( 'Continue previous upgrade processing?', 'learndash' ); ?></p>
<?php
}
}
$progress_style = 'display:none;';
$progress_meter_style = '';
$progress_label = '';
$progress_slug = '';
if ( true === $show_progress ) {
$progress_style = '';
$data = $this->transient_data;
$data = $this->build_progress_output( $data );
if ( ( isset( $data['progress_percent'] ) ) && ( ! empty( $data['progress_percent'] ) ) ) {
$progress_meter_style = 'width: ' . $data['progress_percent'] . '%';
}
if ( ( isset( $data['progress_label'] ) ) && ( ! empty( $data['progress_label'] ) ) ) {
$progress_label = $data['progress_label'];
}
if ( ( isset( $data['progress_slug'] ) ) && ( ! empty( $data['progress_slug'] ) ) ) {
$progress_slug = 'progress-label-' . $data['progress_slug'];
}
}
?>
<div style="<?php echo esc_attr( $progress_style ); ?>" class="meter learndash-data-upgrades-status">
<div class="progress-meter">
<span class="progress-meter-image" style="<?php echo esc_attr( $progress_meter_style ); ?>"></span>
</div>
<div class="progress-label <?php echo esc_attr( $progress_slug ); ?>"><?php echo esc_attr( $progress_label ); ?></div>
</div>
</td>
</tr>
<?php
}
/**
* Class method for the AJAX update logic
* This function will determine what users need to be converted. Then the course and quiz functions
* will be called to convert each individual user data set.
*
* @since 2.3.0
*
* @param array $data Post data from AJAX call.
* @return array $data Post data from AJAX call
*/
public function process_upgrade_action( $data = array() ) {
global $wpdb;
$this->init_process_times();
if ( ( isset( $data['nonce'] ) ) && ( ! empty( $data['nonce'] ) ) ) {
if ( ( wp_verify_nonce( $data['nonce'], 'learndash-data-upgrades-' . $this->data_slug . '-' . get_current_user_id() ) ) && ( current_user_can( LEARNDASH_ADMIN_CAPABILITY_CHECK ) ) ) {
$this->transient_key = $this->data_slug;
if ( ( isset( $data['init'] ) ) && ( '1' === $data['init'] ) ) {
unset( $data['init'] );
if ( ( ! isset( $data['continue'] ) ) || ( 'true' !== $data['continue'] ) ) {
learndash_activity_clear_mismatched_users();
learndash_activity_clear_mismatched_posts();
/**
* Transient_data is used to store the local server state information and will
* saved in a transient type options variable.
*/
$this->transient_data = array();
// Hold the number of completed/processed items.
$this->transient_data['result_count'] = 0;
$this->transient_data['current_user'] = array();
$this->transient_data['progress_started'] = time();
$this->transient_data['progress_user'] = get_current_user_id();
$this->query_items();
} else {
$this->transient_data = $this->get_transient( $this->transient_key );
}
$this->set_option_cache( $this->transient_key, $this->transient_data );
} else {
$this->transient_data = $this->get_transient( $this->transient_key );
if ( ( ! isset( $this->transient_data['process_users'] ) ) || ( empty( $this->transient_data['process_users'] ) ) ) {
$this->query_items();
}
if ( ( isset( $this->transient_data['process_users'] ) ) && ( ! empty( $this->transient_data['process_users'] ) ) ) {
foreach ( $this->transient_data['process_users'] as $user_idx => $user_id ) {
$user_id = intval( $user_id );
if ( ( ! isset( $this->transient_data['current_user']['user_id'] ) ) || ( $this->transient_data['current_user']['user_id'] !== $user_id ) ) {
$this->transient_data['current_user'] = array(
'user_id' => $user_id,
'item_idx' => 0,
);
}
$user_complete = $this->convert_user_meta_quizzes_progress_to_activity( intval( $user_id ) );
if ( true === $user_complete ) {
$this->transient_data['current_user'] = array();
unset( $this->transient_data['process_users'][ $user_idx ] );
if ( ! isset( $this->transient_data['result_count'] ) ) {
$this->transient_data['result_count'] = 0;
}
$this->transient_data['result_count'] = (int) $this->transient_data['result_count'] + 1;
}
$this->set_option_cache( $this->transient_key, $this->transient_data );
if ( $this->out_of_timer() ) {
break;
}
}
}
}
}
}
$data = $this->build_progress_output( $data );
// If we are at 100% then we update the internal data settings so other parts of LD know the upgrade has been run.
if ( ( isset( $data['progress_percent'] ) ) && ( 100 == $data['progress_percent'] ) ) {
$this->set_last_run_info( $data );
$data['last_run_info'] = $this->get_last_run_info();
$this->remove_transient( $this->transient_key );
}
return $data;
}
/**
* Common function to query needed items.
*
* @since 2.6.0
*
* @param boolean $increment_paged default true to increment paged.
*/
protected function query_items( $increment_paged = true ) {
// Initialize or increment the current paged or items.
if ( ! isset( $this->transient_data['paged'] ) ) {
$this->transient_data['paged'] = 1;
} else {
if ( true === $increment_paged ) {
$this->transient_data['paged'] = (int) $this->transient_data['paged'] + 1;
}
}
$this->transient_data['query_args'] = array(
'fields' => 'ID',
'paged' => $this->transient_data['paged'],
'number' => LEARNDASH_LMS_DEFAULT_DATA_UPGRADE_BATCH_SIZE,
);
/** This filter is documented in includes/admin/classes-data-upgrades-actions/class-learndash-admin-data-upgrades-course-access-list-convert.php */
$this->transient_data['query_args'] = apply_filters( 'learndash_data_upgrade_query', $this->transient_data['query_args'], $this->data_slug );
$user_query = new WP_User_Query( $this->transient_data['query_args'] );
if ( is_a( $user_query, 'WP_User_Query' ) ) {
$this->transient_data['total_count'] = $user_query->get_total();
$this->transient_data['process_users'] = $user_query->get_results();
}
}
/**
* Common function to build the returned data progress output.
*
* @since 2.6.0
*
* @param array $data Array of existing data elements.
* @return array or data.
*/
protected function build_progress_output( $data = array() ) {
if ( isset( $this->transient_data['result_count'] ) ) {
$data['result_count'] = intval( $this->transient_data['result_count'] );
} else {
$data['result_count'] = 0;
}
if ( isset( $this->transient_data['total_count'] ) ) {
$data['total_count'] = intval( $this->transient_data['total_count'] );
} else {
$data['total_count'] = 0;
}
if ( ! empty( $data['total_count'] ) ) {
$data['progress_percent'] = ceil( ( intval( $data['result_count'] ) / intval( $data['total_count'] ) ) * 100 );
} else {
$data['progress_percent'] = 0;
}
if ( 100 == $data['progress_percent'] ) {
$progress_status = __( 'Complete', 'learndash' );
$data['progress_slug'] = 'complete';
} else {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$progress_status = __( 'In Progress', 'learndash' );
$data['progress_slug'] = 'in-progress';
} else {
$progress_status = __( 'Incomplete', 'learndash' );
$data['progress_slug'] = 'in-complete';
}
}
$data['progress_label'] = sprintf(
// translators: placeholders: result count, total count.
esc_html_x( '%1$s: %2$d of %3$d Users', 'placeholders: progress status, result count, total count', 'learndash' ),
$progress_status,
$data['result_count'],
$data['total_count']
);
return $data;
}
/**
* Convert single user quiz attempts to Activity DB entries.
*
* @since 2.3.0
*
* @param int $user_id User ID of user to convert.
* @return boolean true if complete, false if not.
*/
protected function convert_user_meta_quizzes_progress_to_activity( $user_id = 0 ) {
global $wpdb;
if ( ( empty( $user_id ) ) || ( ! isset( $this->transient_data['current_user']['user_id'] ) ) || ( $user_id !== $this->transient_data['current_user']['user_id'] ) ) {
return false;
}
delete_user_meta( $user_id, $this->meta_key );
if ( empty( $this->transient_data['current_user']['item_idx'] ) ) {
learndash_report_clear_user_activity_by_types( $user_id, array( 'quiz' ) );
}
$user_course_ids_used = array();
$user_meta_quizzes_progress = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ( ! empty( $user_meta_quizzes_progress ) ) && ( is_array( $user_meta_quizzes_progress ) ) ) {
$user_meta_quizzes_progress_changed = false;
foreach ( $user_meta_quizzes_progress as $idx => $quiz_data ) {
// Need a way to seek to a specific key starting point in an array.
if ( $this->transient_data['current_user']['item_idx'] > intval( $idx ) ) {
continue;
}
$this->transient_data['current_user']['item_idx'] = $idx;
if ( $this->out_of_timer() ) {
return false;
}
$quiz_post = get_post( intval( $quiz_data['quiz'] ) );
if ( ( ! $quiz_post ) || ( ! is_a( $quiz_post, 'WP_Post' ) ) || ( 'sfwd-quiz' !== $quiz_post->post_type ) ) {
continue;
}
if ( ! isset( $quiz_data['course'] ) ) {
$quiz_data['course'] = learndash_get_course_id( intval( $quiz_data['quiz'] ) );
$user_meta_quizzes_progress[ $idx ]['course'] = $quiz_data['course'];
$user_meta_quizzes_progress_changed = true;
}
if ( ( ! isset( $quiz_data['completed'] ) ) || ( empty( $quiz_data['completed'] ) ) ) {
if ( ( isset( $quiz_data['time'] ) ) && ( ! empty( $quiz_data['time'] ) ) ) {
$quiz_data['completed'] = $quiz_data['time'];
} else {
$quiz_data['completed'] = 0;
}
}
if ( ( ! isset( $quiz_data['started'] ) ) || ( empty( $quiz_data['started'] ) ) ) {
if ( ( isset( $quiz_data['time'] ) ) && ( ! empty( $quiz_data['time'] ) ) && ( isset( $quiz_data['timespent'] ) ) && ( ! empty( $quiz_data['timespent'] ) ) ) {
$quiz_data['started'] = abs( intval( $quiz_data['time'] - round( $quiz_data['timespent'], 0 ) ) );
} elseif ( isset( $quiz_data['completed'] ) ) {
$quiz_data['started'] = $quiz_data['completed'];
} else {
$quiz_data['started'] = 0;
}
}
$quiz_data_meta = $quiz_data;
// Remove many fields that we either don't need or are duplicate of the main table columns.
unset( $quiz_data_meta['quiz'] );
unset( $quiz_data_meta['pro_quizid'] );
unset( $quiz_data_meta['time'] );
unset( $quiz_data_meta['completed'] );
unset( $quiz_data_meta['started'] );
if ( isset( $quiz_data_meta['course'] ) ) {
unset( $quiz_data_meta['course'] );
}
if ( isset( $quiz_data_meta['lesson'] ) ) {
unset( $quiz_data_meta['lesson'] );
}
if ( isset( $quiz_data_meta['topic'] ) ) {
unset( $quiz_data_meta['topic'] );
}
if ( '-' == $quiz_data_meta['rank'] ) {
unset( $quiz_data_meta['rank'] );
}
if ( true == $quiz_data['pass'] ) {
$quiz_data_pass = true;
} else {
$quiz_data_pass = false;
}
$activity_id = learndash_update_user_activity(
array(
'course_id' => $quiz_data['course'],
'post_id' => $quiz_data['quiz'],
'user_id' => $user_id,
'activity_type' => 'quiz',
'activity_status' => $quiz_data_pass,
'activity_started' => $quiz_data['started'],
'activity_completed' => $quiz_data['completed'],
'activity_meta' => $quiz_data_meta,
)
);
}
if ( true === $user_meta_quizzes_progress_changed ) {
update_user_meta( $user_id, '_sfwd-quizzes', $user_meta_quizzes_progress );
}
}
return true;
}
}
}
add_action(
'learndash_data_upgrades_init',
function() {
Learndash_Admin_Data_Upgrades_User_Meta_Quizzes::add_instance();
}
);