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/lifterlms/includes/admin/recount-activity.php +381 -0 7.9.2 → 8.0.5 View file →
@@ -1,0 +1,381 @@
1 +<?php
2 +/**
3 + * Recount Activity
4 + *
5 + * @package GamiPress\LifterLMS\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_lifterlms_recountable_activity_triggers( $recountable_activity_triggers ) {
21 +
22 + // LearnDash
23 + $recountable_activity_triggers[__( 'LifterLMS', 'gamipress' )] = array(
24 + 'lifterlms_quizzes' => __( 'Recount quizzes completed', 'gamipress' ),
25 + 'lifterlms_lessons' => __( 'Recount lessons completed', 'gamipress' ),
26 + 'lifterlms_sections' => __( 'Recount sections completed', 'gamipress' ),
27 + 'lifterlms_courses' => __( 'Recount courses completed', 'gamipress' ),
28 + );
29 +
30 + return $recountable_activity_triggers;
31 +
32 +}
33 +add_filter( 'gamipress_recountable_activity_triggers', 'gamipress_lifterlms_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_lifterlms_activity_recount_quizzes( $response, $loop, $limit, $offset ) {
49 +
50 + global $wpdb;
51 +
52 + // Get all stored users count
53 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u" ) ) );
54 +
55 + // On first loop send an informational text
56 + if( $loop === 0 && $users_count > $limit ) {
57 + $response['run_again'] = true;
58 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
59 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
60 +
61 + // Return early to inform
62 + return $response;
63 + }
64 +
65 + // Get all stored users
66 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LIMIT {$offset}, {$limit}" ) );
67 +
68 + foreach( $users as $user ) {
69 +
70 + $student_quizzes = new LLMS_Student_Quizzes( $user->ID );
71 +
72 + if( ! $student_quizzes ) {
73 + continue;
74 + }
75 +
76 + $quizzes = $student_quizzes->get_all();
77 +
78 + if( ! is_array( $quizzes ) ) {
79 + continue;
80 + }
81 +
82 + foreach( $quizzes as $quiz ) {
83 +
84 + $quiz_id = $quiz->get( 'id' );
85 +
86 + // Call to quiz completion listener
87 + gamipress_lifterlms_complete_quiz( $user->ID, $quiz_id, $quiz );
88 +
89 + $lesson_id = $quiz->get( 'lesson_id' );
90 + $course = llms_get_post_parent_course( $lesson_id );
91 + $course_id = 0;
92 +
93 + if( $course ) {
94 + $course_id = $course->get( 'id' );
95 + }
96 +
97 + if( $course_id !== 0 ) {
98 + $response['log'] .= sprintf( __( '[Complete quiz] Quiz: %s User: %s Course: %s', 'gamipress' ),
99 + '<a href="' . get_edit_post_link( $quiz_id ) . '" target="_blank">' . $quiz_id . '</a>',
100 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>',
101 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>'
102 + ) . "<br>\n";
103 + } else {
104 + $response['log'] .= sprintf( __( '[Complete quiz] Quiz: %s User: %s', 'gamipress' ),
105 + '<a href="' . get_edit_post_link( $quiz_id ) . '" target="_blank">' . $quiz_id . '</a>',
106 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>'
107 + ) . "<br>\n";
108 + }
109 + }
110 + }
111 +
112 + $recounted_users = $limit * ( $loop + 1 );
113 +
114 + // Check remaining posts
115 + if( $recounted_users < $users_count ) {
116 + $response['run_again'] = true;
117 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
118 + }
119 +
120 + return $response;
121 +
122 +}
123 +add_filter( 'gamipress_activity_recount_lifterlms_quizzes', 'gamipress_lifterlms_activity_recount_quizzes', 10, 4 );
124 +
125 +/**
126 + * Recount topics completed
127 + *
128 + * @since 1.0.9
129 + * @updated 1.1.0 Added $loop parameter
130 + *
131 + * @param array $response
132 + * @param int $loop
133 + * @param int $limit
134 + * @param int $offset
135 + *
136 + * @return array $response
137 + */
138 +function gamipress_lifterlms_activity_recount_lessons( $response, $loop, $limit, $offset ) {
139 +
140 + global $wpdb, $wp_current_filter;
141 +
142 + // Get all stored users count
143 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u" ) ) );
144 +
145 + // On first loop send an informational text
146 + if( $loop === 0 && $users_count > $limit ) {
147 + $response['run_again'] = true;
148 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
149 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
150 +
151 + // Return early to inform
152 + return $response;
153 + }
154 +
155 + // Get all stored users
156 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LIMIT {$offset}, {$limit}" ) );
157 +
158 + foreach( $users as $user ) {
159 +
160 + $student = new LLMS_Student( $user->ID );
161 +
162 + if( ! $student ) {
163 + continue;
164 + }
165 +
166 + // Get all user courses completed
167 + $courses = $student->get_completed_courses();
168 +
169 + if( ! is_array( $courses ) ) {
170 + continue;
171 + }
172 +
173 + foreach( $courses as $course_id ) {
174 +
175 + $llms_course = new LLMS_Course( $course_id );
176 +
177 + if( ! $llms_course ) {
178 + continue;
179 + }
180 +
181 + $lessons = $llms_course->get_lessons( 'ids' );
182 +
183 + if( ! is_array( $lessons ) ) {
184 + continue;
185 + }
186 +
187 + foreach( $lessons as $lesson_id ) {
188 + // Force current filter to be the one required by LifterLMS
189 + $wp_current_filter[] = 'lifterlms_lesson_completed';
190 +
191 + gamipress_lifterlms_common_listener( $user->ID, $lesson_id );
192 +
193 + $response['log'] .= sprintf( __( '[Complete course] Lesson: %s Course: %s User: %s', 'gamipress' ),
194 + '<a href="' . get_edit_post_link( $lesson_id ) . '" target="_blank">' . $lesson_id . '</a>',
195 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>',
196 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>'
197 + ) . "<br>\n";
198 + }
199 +
200 + }
201 + }
202 +
203 + $recounted_users = $limit * ( $loop + 1 );
204 +
205 + // Check remaining posts
206 + if( $recounted_users < $users_count ) {
207 + $response['run_again'] = true;
208 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
209 + }
210 +
211 + return $response;
212 +
213 +}
214 +add_filter( 'gamipress_activity_recount_lifterlms_lessons', 'gamipress_lifterlms_activity_recount_lessons', 10, 4 );
215 +
216 +/**
217 + * Recount lessons completed
218 + *
219 + * @since 1.0.9
220 + * @updated 1.1.0 Added $loop parameter
221 + *
222 + * @param array $response
223 + * @param int $loop
224 + * @param int $limit
225 + * @param int $offset
226 + *
227 + * @return array $response
228 + */
229 +function gamipress_lifterlms_activity_recount_sections( $response, $loop, $limit, $offset ) {
230 +
231 + global $wpdb, $wp_current_filter;
232 +
233 + // Get all stored users count
234 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u" ) ) );
235 +
236 + // On first loop send an informational text
237 + if( $loop === 0 && $users_count > $limit ) {
238 + $response['run_again'] = true;
239 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
240 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
241 +
242 + // Return early to inform
243 + return $response;
244 + }
245 +
246 + // Get all stored users
247 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LIMIT {$offset}, {$limit}" ) );
248 +
249 + foreach( $users as $user ) {
250 +
251 + $student = new LLMS_Student( $user->ID );
252 +
253 + if( ! $student ) {
254 + continue;
255 + }
256 +
257 + // Get all user courses completed
258 + $courses = $student->get_completed_courses();
259 +
260 + if( ! is_array( $courses ) ) {
261 + continue;
262 + }
263 +
264 + foreach( $courses as $course_id ) {
265 +
266 + $llms_course = new LLMS_Course( $course_id );
267 +
268 + if( ! $llms_course ) {
269 + continue;
270 + }
271 +
272 + $sections = $llms_course->get_sections( 'ids' );
273 +
274 + if( ! is_array( $sections ) ) {
275 + continue;
276 + }
277 +
278 + foreach( $sections as $section_id ) {
279 + // Force current filter to be the one required by LifterLMS
280 + $wp_current_filter[] = 'lifterlms_section_completed';
281 +
282 + gamipress_lifterlms_common_listener( $user->ID, $section_id );
283 +
284 + $response['log'] .= sprintf( __( '[Complete course] Section: %s Course: %s User: %s', 'gamipress' ),
285 + '<a href="' . get_edit_post_link( $section_id ) . '" target="_blank">' . $section_id . '</a>',
286 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>',
287 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>'
288 + ) . "<br>\n";
289 + }
290 +
291 + }
292 + }
293 +
294 + $recounted_users = $limit * ( $loop + 1 );
295 +
296 + // Check remaining posts
297 + if( $recounted_users < $users_count ) {
298 + $response['run_again'] = true;
299 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
300 + }
301 +
302 + return $response;
303 +
304 +}
305 +add_filter( 'gamipress_activity_recount_lifterlms_sections', 'gamipress_lifterlms_activity_recount_sections', 10, 4 );
306 +
307 +/**
308 + * Recount courses completed
309 + *
310 + * @since 1.0.9
311 + * @updated 1.1.0 Added $loop parameter
312 + *
313 + * @param array $response
314 + * @param int $loop
315 + * @param int $limit
316 + * @param int $offset
317 + *
318 + * @return array $response
319 + */
320 +function gamipress_lifterlms_activity_recount_courses( $response, $loop, $limit, $offset ) {
321 +
322 + global $wpdb, $wp_current_filter;
323 +
324 + // Get all stored users count
325 + $users_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->users} AS u" ) ) );
326 +
327 + // On first loop send an informational text
328 + if( $loop === 0 && $users_count > $limit ) {
329 + $response['run_again'] = true;
330 + $response['message'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count );
331 + $response['log'] = sprintf( __( '%d users found, recounting...', 'gamipress' ), $users_count ) . "<br>\n";
332 +
333 + // Return early to inform
334 + return $response;
335 + }
336 +
337 + // Get all stored users
338 + $users = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID FROM {$wpdb->users} AS u LIMIT {$offset}, {$limit}" ) );
339 +
340 + foreach( $users as $user ) {
341 +
342 + $student = new LLMS_Student( $user->ID );
343 +
344 + if( ! $student ) {
345 + continue;
346 + }
347 +
348 + // Get all user courses completed
349 + $courses = $student->get_completed_courses();
350 +
351 + if( ! is_array( $courses ) ) {
352 + continue;
353 + }
354 +
355 + foreach( $courses as $course_id ) {
356 +
357 + // Force current filter to be the one required by LifterLMS
358 + $wp_current_filter[] = 'lifterlms_course_completed';
359 +
360 + gamipress_lifterlms_common_listener( $user->ID, $course_id );
361 +
362 + $response['log'] .= sprintf( __( '[Complete course] Course: %s User: %s', 'gamipress' ),
363 + '<a href="' . get_edit_post_link( $course_id ) . '" target="_blank">' . $course_id . '</a>',
364 + '<a href="' . get_edit_user_link( $user->ID ) . '" target="_blank">' . $user->ID . '</a>'
365 + ) . "<br>\n";
366 +
367 + }
368 + }
369 +
370 + $recounted_users = $limit * ( $loop + 1 );
371 +
372 + // Check remaining posts
373 + if( $recounted_users < $users_count ) {
374 + $response['run_again'] = true;
375 + $response['message'] = sprintf( __( '%d remaining users to finish recount', 'gamipress' ), ( $users_count - $recounted_users ) );
376 + }
377 +
378 + return $response;
379 +
380 +}
381 +add_filter( 'gamipress_activity_recount_lifterlms_courses', 'gamipress_lifterlms_activity_recount_courses', 10, 4 );