PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
← All changes | integrations/learndash/includes/admin/recount-activity.php +422 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,422 @@
1 +<?php
2 +/**
3 + * Recount Activity
4 + *
5 + * @package GamiPress\LearnDash\Admin\Recount_Activity
6 + * @since 1.0.9
7 + */
8 +// Exit if accessed directly
9 +if( !defined( 'ABSPATH' ) ) exit;
10 +
11 +/**
12 + * Add recountable options to the Recount Activity Tool
13 + *
14 + * @since 1.0.9
15 + *
16 + * @param array $recountable_activity_triggers
17 + *
18 + * @return array
19 + */
20 +function gamipress_ld_recountable_activity_triggers( $recountable_activity_triggers ) {
21 +
22 + // LearnDash
23 + $recountable_activity_triggers[__( 'LearnDash', 'gamipress' )] = array(
24 + 'ld_quizzes' => __( 'Recount quizzes completed', 'gamipress' ),
25 + 'ld_topics' => __( 'Recount topics completed', 'gamipress' ),
26 + 'ld_lessons' => __( 'Recount lessons completed', 'gamipress' ),
27 + 'ld_courses' => __( 'Recount courses completed', 'gamipress' ),
28 + );
29 +
30 + return $recountable_activity_triggers;
31 +
32 +}
33 +add_filter( 'gamipress_recountable_activity_triggers', 'gamipress_ld_recountable_activity_triggers' );
34 +
35 +/**
36 + * Recount quizzes completed
37 + *
38 + * @since 1.0.9
39 + * @updated 1.1.0 Added $loop parameter
40 + *
41 + * @param array $response
42 + * @param int $loop
43 + * @param int $limit
44 + * @param int $offset
45 + *
46 + * @return array $response
47 + */
48 +function gamipress_ld_activity_recount_quizzes( $response, $loop, $limit, $offset ) {
49 +
50 + global $wpdb;
51 +
52 + // Meta where information resides
53 + $meta_key = '_sfwd-quizzes';
54 +
55 + // Get all stored users count
56 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL" ) ) );
57 +
58 + // On first loop send an informational text
59 + if( $loop === 0 && $users_count > $limit ) {
60 + $response['run_again'] = true;
61 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
62 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
63 +
64 + // Return early to inform
65 + return $response;
66 + }
67 +
68 + // Get all stored users
69 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL LIMIT {$offset}, {$limit}" ) );
70 +
71 + foreach( $users as $user ) {
72 +
73 + // Get all user quizzes completed
74 + $quizzes = get_user_meta( $user->ID, $meta_key, true );
75 +
76 + // Skip if user does not have completed any quizzes
77 + if( ! is_array( $quizzes ) ) {
78 + continue;
79 + }
80 +
81 + foreach( $quizzes as $quiz ) {
82 +
83 + $quiz['quiz'] = get_post( $quiz['quiz'] );
84 +
85 + // Skip incorrect quizzes
86 + if( ! $quiz['quiz'] )
87 + continue;
88 +
89 + // Ensure required data
90 + $quiz['course'] = get_post( $quiz['course'] );
91 +
92 + if( ! isset( $quiz['percentage'] ) )
93 + $quiz['percentage'] = 0;
94 +
95 + if( ! isset( $quiz['pass'] ) )
96 + $quiz['pass'] = 0;
97 +
98 + // Call to all separated quiz completion listeners
99 + gamipress_ld_complete_quiz_listener( $quiz, $user );
100 + gamipress_ld_complete_quiz_min_grade_listener( $quiz, $user );
101 + gamipress_ld_complete_quiz_max_grade_listener( $quiz, $user );
102 + gamipress_ld_complete_quiz_between_grade_listener( $quiz, $user );
103 + gamipress_ld_pass_quiz_listener( $quiz, $user );
104 + gamipress_ld_fail_quiz_listener( $quiz, $user );
105 +
106 + $quiz_id = $quiz['quiz']->ID;
107 + $course_id = ( $quiz['course'] instanceof WP_Post ? $quiz['course']->ID : 0 );
108 +
109 + if( $course_id !== 0 ) {
110 + $response['log'] .= sprintf( __( '[Complete quiz] Quiz: %s User: %s Course: %s Quiz Score: %s', 'gamipress' ),
111 + '<a href="' . get_edit_post_link( $quiz_id ) . '" target="_blank">' . $quiz_id . '</a>',
112 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>',
113 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>',
114 + absint( $quiz['percentage'] )
115 + ) . "<br>\n";
116 + } else {
117 + $response['log'] .= sprintf( __( '[Complete quiz] Quiz: %s User: %s Quiz Score: %s', 'gamipress' ),
118 + '<a href="' . get_edit_post_link( $quiz_id ) . '" target="_blank">' . $quiz_id . '</a>',
119 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>',
120 + absint( $quiz['percentage'] )
121 + ) . "<br>\n";
122 + }
123 + }
124 + }
125 +
126 + $recounted_users = $limit * ( $loop + 1 );
127 +
128 + // Check remaining posts
129 + if( $recounted_users < $users_count ) {
130 + $response['run_again'] = true;
131 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
132 + }
133 +
134 + return $response;
135 +
136 +}
137 +add_filter( 'gamipress_activity_recount_ld_quizzes', 'gamipress_ld_activity_recount_quizzes', 10, 4 );
138 +
139 +/**
140 + * Recount topics completed
141 + *
142 + * @since 1.0.9
143 + * @updated 1.1.0 Added $loop parameter
144 + *
145 + * @param array $response
146 + * @param int $loop
147 + * @param int $limit
148 + * @param int $offset
149 + *
150 + * @return array $response
151 + */
152 +function gamipress_ld_activity_recount_topics( $response, $loop, $limit, $offset ) {
153 +
154 + global $wpdb;
155 +
156 + // Meta where information resides
157 + $meta_key = '_sfwd-course_progress';
158 +
159 + // Get all stored users count
160 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL" ) ) );
161 +
162 + // On first loop send an informational text
163 + if( $loop === 0 && $users_count > $limit ) {
164 + $response['run_again'] = true;
165 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
166 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
167 +
168 + // Return early to inform
169 + return $response;
170 + }
171 +
172 + // Get all stored users
173 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL LIMIT {$offset}, {$limit}" ) );
174 +
175 + foreach( $users as $user ) {
176 +
177 + // Get all user courses completed
178 + $courses = get_user_meta( $user->ID, $meta_key, true );
179 +
180 + // Skip if user does not have completed any courses
181 + if( ! is_array( $courses ) ) {
182 + continue;
183 + }
184 +
185 + foreach( $courses as $course_id => $course ) {
186 +
187 + // Skip if user does not have completed any topics
188 + if( ! is_array( $course['topics'] ) ) {
189 + continue;
190 + }
191 +
192 + // Loop all topics completed (topics are separated in lessons)
193 + foreach( $course['topics'] as $lesson_id => $topics ) {
194 +
195 + // Skip if user does not have completed any topics
196 + if( ! is_array( $topics ) ) {
197 + continue;
198 + }
199 +
200 + // Loop all lesson topics completed
201 + foreach( $topics as $topic_id => $completed ) {
202 +
203 + if( $completed ) {
204 +
205 + $args = array(
206 + 'topic' => get_post( $topic_id ),
207 + 'user' => $user,
208 + 'lesson' => get_post( $lesson_id ),
209 + 'course' => get_post( $course_id ),
210 + );
211 +
212 + // Skip incorrect topics
213 + if( ! $args['topic'] )
214 + continue;
215 +
216 + gamipress_ld_complete_topic( $args );
217 +
218 + $response['log'] .= sprintf( __( '[Complete topic] Topic: %s User: %s Lesson: %s Course: %s', 'gamipress' ),
219 + '<a href="' . get_edit_post_link( $topic_id ) . '" target="_blank">' . $topic_id . '</a>',
220 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>',
221 + '<a href="' . get_edit_post_link( $lesson_id ) . '" target="_blank">' . $lesson_id . '</a>',
222 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>'
223 + ) . "<br>\n";
224 + }
225 +
226 + }
227 +
228 + }
229 + }
230 + }
231 +
232 + $recounted_users = $limit * ( $loop + 1 );
233 +
234 + // Check remaining posts
235 + if( $recounted_users < $users_count ) {
236 + $response['run_again'] = true;
237 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
238 + }
239 +
240 + return $response;
241 +
242 +}
243 +add_filter( 'gamipress_activity_recount_ld_topics', 'gamipress_ld_activity_recount_topics', 10, 4 );
244 +
245 +/**
246 + * Recount lessons completed
247 + *
248 + * @since 1.0.9
249 + * @updated 1.1.0 Added $loop parameter
250 + *
251 + * @param array $response
252 + * @param int $loop
253 + * @param int $limit
254 + * @param int $offset
255 + *
256 + * @return array $response
257 + */
258 +function gamipress_ld_activity_recount_lessons( $response, $loop, $limit, $offset ) {
259 +
260 + global $wpdb;
261 +
262 + // Meta where information resides
263 + $meta_key = '_sfwd-course_progress';
264 +
265 + // Get all stored users count
266 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL" ) ) );
267 +
268 + // On first loop send an informational text
269 + if( $loop === 0 && $users_count > $limit ) {
270 + $response['run_again'] = true;
271 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
272 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
273 +
274 + // Return early to inform
275 + return $response;
276 + }
277 +
278 + // Get all stored users
279 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL LIMIT {$offset}, {$limit}" ) );
280 +
281 + foreach( $users as $user ) {
282 +
283 + // Get all user courses completed
284 + $courses = get_user_meta( $user->ID, $meta_key, true );
285 +
286 + // Skip if user does not have completed any courses
287 + if( ! is_array( $courses ) ) {
288 + continue;
289 + }
290 +
291 + foreach( $courses as $course_id => $course ) {
292 +
293 + // Skip if user does not have completed any lessons
294 + if( ! is_array( $course['lessons'] ) ) {
295 + continue;
296 + }
297 +
298 + // Loop all lessons completed
299 + foreach( $course['lessons'] as $lesson_id => $completed ) {
300 +
301 + if( $completed ) {
302 +
303 + $args = array(
304 + 'user' => $user,
305 + 'lesson' => get_post( $lesson_id ),
306 + 'course' => get_post( $course_id ),
307 + );
308 +
309 + // Skip incorrect lessons
310 + if( ! $args['lesson'] )
311 + continue;
312 +
313 + gamipress_ld_complete_lesson( $args );
314 +
315 + $response['log'] .= sprintf( __( '[Complete lesson] Lesson: %s User: %s Course: %s', 'gamipress' ),
316 + '<a href="' . get_edit_post_link( $lesson_id ) . '" target="_blank">' . $lesson_id . '</a>',
317 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>',
318 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>'
319 + ) . "<br>\n";
320 + }
321 +
322 + }
323 + }
324 + }
325 +
326 + $recounted_users = $limit * ( $loop + 1 );
327 +
328 + // Check remaining posts
329 + if( $recounted_users < $users_count ) {
330 + $response['run_again'] = true;
331 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
332 + }
333 +
334 + return $response;
335 +
336 +}
337 +add_filter( 'gamipress_activity_recount_ld_lessons', 'gamipress_ld_activity_recount_lessons', 10, 4 );
338 +
339 +/**
340 + * Recount courses completed
341 + *
342 + * @since 1.0.9
343 + * @updated 1.1.0 Added $loop parameter
344 + *
345 + * @param array $response
346 + * @param int $loop
347 + * @param int $limit
348 + * @param int $offset
349 + *
350 + * @return array $response
351 + */
352 +function gamipress_ld_activity_recount_courses( $response, $loop, $limit, $offset ) {
353 +
354 + global $wpdb;
355 +
356 + // Meta where information resides
357 + $meta_key = '_sfwd-course_progress';
358 +
359 + // Get all stored users count
360 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL" ) ) );
361 +
362 + // On first loop send an informational text
363 + if( $loop === 0 && $users_count > $limit ) {
364 + $response['run_again'] = true;
365 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
366 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
367 +
368 + // Return early to inform
369 + return $response;
370 + }
371 +
372 + // Get all stored users
373 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LEFT JOIN {$wpdb->usermeta} AS um ON um.user_id = u.ID AND um.meta_key = '{$meta_key}' WHERE um.meta_key IS NOT NULL LIMIT {$offset}, {$limit}" ) );
374 +
375 + foreach( $users as $user ) {
376 +
377 + // Get all user courses completed
378 + $courses = get_user_meta( $user->ID, $meta_key, true );
379 +
380 + // Skip if user does not have completed any courses
381 + if( ! is_array( $courses ) ) {
382 + continue;
383 + }
384 +
385 + foreach( $courses as $course_id => $course ) {
386 +
387 + $completed = get_user_meta( $user->ID, 'course_completed_' . $course_id, true );
388 +
389 + if( $completed ) {
390 +
391 + $args = array(
392 + 'user' => $user,
393 + 'course' => get_post( $course_id ),
394 + );
395 +
396 + // Skip incorrect courses
397 + if( ! $args['course'] )
398 + continue;
399 +
400 + gamipress_ld_complete_course( $args );
401 +
402 + $response['log'] .= sprintf( __( '[Complete course] Course: %s User: %s', 'gamipress' ),
403 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>',
404 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>'
405 + ) . "<br>\n";
406 + }
407 +
408 + }
409 + }
410 +
411 + $recounted_users = $limit * ( $loop + 1 );
412 +
413 + // Check remaining posts
414 + if( $recounted_users < $users_count ) {
415 + $response['run_again'] = true;
416 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
417 + }
418 +
419 + return $response;
420 +
421 +}
422 +add_filter( 'gamipress_activity_recount_ld_courses', 'gamipress_ld_activity_recount_courses', 10, 4 );