Reports
Class for all reports related functions
Description
Source
File: bp-integrations/learndash/buddypress/Reports.php
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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | class Reports { protected $isRealJoins = false; /** * Constructor * * @since BuddyBoss 1.0.0 */ public function __construct() { add_action( 'bp_ld_sync/init' , [ $this , 'init' ]); } /** * Add actions once integration is ready * * @since BuddyBoss 1.0.0 */ public function init() { add_action( 'bp_enqueue_scripts' , [ $this , 'registerReportsScript' ]); // add plugable templates to report actions add_action( 'bp_ld_sync/reports' , [ $this , 'showReportFilters' ], 10); add_action( 'bp_ld_sync/reports' , [ $this , 'showReportUserStats' ], 20); add_action( 'bp_ld_sync/reports' , [ $this , 'showReportCourseStats' ], 20); add_action( 'bp_ld_sync/reports' , [ $this , 'showReportTables' ], 30); add_action( 'bp_ld_sync/reports' , [ $this , 'showReportExport' ], 40); add_filter( 'bp_ld_sync/reports_generator_params' , [ $this , 'forceOwnReportResults' ], 99); add_filter( 'bp_ld_sync/reports_generator_params' , [ $this , 'courseReportResults' ], 99); add_filter( 'bp_ld_sync/report_filters' , [ $this , 'removeCourseFilterIfOnlyOne' ]); add_filter( 'bp_ld_sync/report_filters' , [ $this , 'removeUserFilterIfStudent' ]); add_filter( 'learndash_user_activity_query_fields' , [ $this , 'reportAdditionalActivityFields' ], 10, 2); add_filter( 'learndash_user_activity_query_tables' , [ $this , 'reportAdditionalActivityTables' ], 10, 2); add_filter( 'learndash_user_activity_query_where' , [ $this , 'reportAdditionalActivityWheres' ], 10, 2); add_filter( 'learndash_user_activity_query_where' , [ $this , 'reportAdditionalActivityGroups' ], 15, 2); add_filter( 'bp_ld_sync/report_columns' , [ $this , 'removeUserColumnIfSelected' ], 10, 2); add_filter( 'bp_ld_sync/report_columns' , [ $this , 'removeCourseColumnIfSelected' ], 10, 2); add_filter( 'bp_ld_sync/report_columns' , [ $this , 'removePointsColumnIfNotAssigned' ], 10, 2); add_action( 'bp_ld_sync/export_report_column' , [ $this , 'export_report_column' ], 10, 2 ); } /** * Add scripts when it's on the reports page * * @since BuddyBoss 1.0.0 */ public function registerReportsScript() { if (! bp_is_groups_component() || ! bp_is_current_action( 'reports' )) { return ; } wp_enqueue_script( 'bp-ld-reports-datatable' , '//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js' , [ 'jquery' ], false, true); wp_enqueue_style( 'bp-ld-reports-datatable' , '//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css' , [], false); wp_enqueue_script( 'bp-ld-reports' , bp_learndash_url( $filePath = '/assets/scripts/bp-learndash.js' ), [ 'jquery' , 'bp-ld-reports-datatable' ], filemtime (bp_learndash_path( $filePath )), true ); $per_page = bp_ld_sync( 'settings' )->get( 'reports.per_page' , 20); wp_localize_script( 'bp-ld-reports' , 'BP_LD_REPORTS_DATA' , [ 'current_group' => groups_get_current_group()->id, 'nonce' => wp_create_nonce( 'bp_ld_report' ), 'ajax_url' => admin_url( 'admin-ajax.php' ), 'table_columns' => $this ->getCurrentTableColumns(), 'config' => [ 'perpage' => ( '' === $per_page ) ? 20 : $per_page ], 'text' => [ 'processing' => __( 'Loading…' , 'buddyboss' ), 'emptyTable' => __( 'No result found…' , 'buddyboss' ), 'paginate_first' => __( 'First' , 'buddyboss' ), 'paginate_last' => __( 'Last' , 'buddyboss' ), 'paginate_next' => __( 'Next' , 'buddyboss' ), 'export_failed' => __( 'Export failed, please refresh and try again.' , 'buddyboss' ), 'export_ready' => __( 'Export is ready.' , 'buddyboss' ), ] ]); wp_enqueue_style( 'bp-ld-reports' , bp_learndash_url( $filePath = '/assets/styles/bp-learndash.css' ), [], filemtime (bp_learndash_path( $filePath )) ); } /** * Output report filters html * * @since BuddyBoss 1.0.0 */ public function showReportFilters() { $filters = $this ->getReportFilters(); require bp_locate_template( 'groups/single/reports-filters.php' , false, false); } /** * Output report user stats html * * @since BuddyBoss 1.0.0 */ public function showReportUserStats() { if ( empty ( $_GET [ 'user' ])) { return ; } $courseId = bp_ld_sync()->getRequest( 'course' ); $course = $courseId ? get_post( $courseId ) : null; $group = groups_get_current_group(); $user = get_user_by( 'ID' , $_GET [ 'user' ]); require bp_locate_template( 'groups/single/reports-user-stats.php' , false, false); } /** * Output report course stats html * * @since BuddyBoss 1.0.0 */ public function showReportCourseStats() { if (! empty ( $_GET [ 'user' ]) || empty ( $_GET [ 'course' ])) { return ; } $course = get_post( $_GET [ 'course' ]); $group = groups_get_current_group(); $ldGroupId = bp_ld_sync( 'buddypress' )->helpers->getLearndashGroupId( $group ->id); $ldGroup = get_post( $ldGroupId ); $ldGroupUsers = learndash_get_groups_users( $ldGroupId ); $ldGroupUsersCompleted = array_filter ( $ldGroupUsers , function ( $user ) use ( $course ) { return learndash_course_completed( $user ->ID, $course ->ID); }); $courseHasPoints = !! $coursePoints = get_post_meta( $course ->ID, 'course_points' , true); $averagePoints = $courseHasPoints ? count ( $ldGroupUsersCompleted ) * $coursePoints : 0; require bp_locate_template( 'groups/single/reports-course-stats.php' , false, false); } /** * Output report results tables * * @since BuddyBoss 1.0.0 */ public function showReportTables() { $generator = $this ->getCurrentGenerator(); $completed_table_title = $generator ->completed_table_title ?: __( 'Completed' , 'buddyboss' ); $incompleted_table_title = $generator ->incompleted_table_title ?: __( 'Incomplete' , 'buddyboss' ); require bp_locate_template( 'groups/single/reports-tables.php' , false, false); } /** * Filter to sort the report by Course * * @since BuddyBoss 1.0.0 */ public function courseReportResults( $params ) { if ( ! empty ( $_REQUEST [ 'course' ] ) && is_string ( $_REQUEST [ 'course' ] ) ) { $params [ 'course_ids' ] = absint( $_REQUEST [ 'course' ] ); } return $params ; } /** * Only allow non admin/mod to view his own reports * * @since BuddyBoss 1.0.0 */ public function forceOwnReportResults( $params ) { if (! $currentGroup = groups_get_current_group()) { return $params ; } $userId = bp_loggedin_user_id(); $groupId = $currentGroup ->id; if (groups_is_user_admin( $userId , $groupId ) || groups_is_user_mod( $userId , $groupId ) || is_user_admin() ) { return $params ; } $params [ 'user_ids' ] = [bp_loggedin_user_id()]; return $params ; } /** * Output report export button html * * @since BuddyBoss 1.0.0 */ public function showReportExport() { require bp_locate_template( 'groups/single/reports-export.php' , false, false); } /** * Remove the filter if only 1 option available * * @since BuddyBoss 1.0.0 */ public function removeCourseFilterIfOnlyOne( $filters ) { if (! $currentGroup = groups_get_current_group()) { return $filters ; } if ( count (bp_learndash_get_group_courses( $currentGroup ->id)) < 2) { unset( $filters [ 'course' ]); } return $filters ; } /** * Remove user filter is nond admin/mod are viewing reports tab * * @since BuddyBoss 1.0.0 */ public function removeUserFilterIfStudent( $filters ) { if (! $currentGroup = groups_get_current_group()) { return $filters ; } // admin can always view if (learndash_is_admin_user()) { return $filters ; } $userId = bp_loggedin_user_id(); $groupId = $currentGroup ->id; if (! groups_is_user_admin( $userId , $groupId ) && ! groups_is_user_mod( $userId , $groupId )) { unset( $filters [ 'user' ]); } return $filters ; } /** * Sub filter for ld's activity sql query on fields * * @since BuddyBoss 1.0.0 */ public function reportAdditionalActivityFields( $strFields , $queryArgs ) { return apply_filters( 'bp_ld_sync/reports/activity_fields' , $strFields , $queryArgs ); } /** * Sub filter for ld's activity sql query on joins * * @since BuddyBoss 1.0.0 */ public function reportAdditionalActivityTables( $strJoins , $queryArgs ) { // Learndash Bug https://screencast.com/t/iBajWvdt if (! $this ->isRealJoins()) { $this ->isRealJoins = true; return $strJoins ; } return apply_filters( 'bp_ld_sync/reports/activity_joins' , $strJoins , $queryArgs ); } /** * Sub filter for ld's activity sql query on wheres * * @since BuddyBoss 1.0.0 */ public function reportAdditionalActivityWheres( $strWheres , $queryArgs ) { return apply_filters( 'bp_ld_sync/reports/activity_wheres' , $strWheres , $queryArgs ); } /** * Sub filter for ld's activity sql query on group_by * * @since BuddyBoss 1.0.0 */ public function reportAdditionalActivityGroups( $strWheres , $queryArgs ) { return apply_filters( 'bp_ld_sync/reports/activity_groups' , $strWheres , $queryArgs ); } /** * Remove the user column if a user is selected in the filter * * @since BuddyBoss 1.0.0 */ public function removeUserColumnIfSelected( $columns , $args ) { if ( $args [ 'user' ]) { unset( $columns [ 'user' ]); } return $columns ; } /** * Remove the course column if a course is selected in the filter * * @since BuddyBoss 1.0.0 */ public function removeCourseColumnIfSelected( $columns , $args ) { if ( $args [ 'course' ]) { unset( $columns [ 'course' ]); } return $columns ; } /** * Remove the points column if all courses doesn't have points * * @since BuddyBoss 1.0.0 */ public function removePointsColumnIfNotAssigned( $columns , $args ) { $shouldRemove = false; if ( $args [ 'course' ]) { $shouldRemove = '' === get_post_meta( $args [ 'course' ], 'course_points' , true); } else { $groupCourses = bp_ld_sync( 'buddypress' )->courses->getGroupCourses( $args [ 'group' ]); $shouldRemove = array_sum ( array_map ( function ( $course ) use ( $args ) { return get_post_meta( $args [ 'course' ], 'course_points' , true) ?: 0; }, $groupCourses )) > 0; } if (! in_array( $args [ 'step' ], [ 'all' , learndash_get_post_type_slug( 'course' )])) { unset( $columns [ 'points' ]); } if ( $shouldRemove ) { unset( $columns [ 'points' ]); } return $columns ; } /** * Add status columns to export * * @since BuddyBoss 1.0.0 */ public function export_report_column( $columns , $report_generator ) { if ( ! empty ( $report_generator ->args[ 'step' ] ) && in_array( $report_generator ->args[ 'step' ], array ( 'forum' ) ) ) { $columns [ 'status' ] = $report_generator ->column( 'status' ); } return $columns ; } /** * Get available report generators * * @since BuddyBoss 1.0.0 */ public function getGenerators() { return apply_filters( 'bp_ld_sync/reports_generators' , [ 'all' => [ 'name' => __( 'All Steps' , 'buddyboss' ), 'class' => '\Buddyboss\LearndashIntegration\Buddypress\Generators\AllReportsGenerator' ], learndash_get_post_type_slug( 'course' ) => [ 'name' => __( 'Courses' , 'buddyboss' ), 'class' => 'Buddyboss\LearndashIntegration\Buddypress\Generators\CoursesReportsGenerator' ], learndash_get_post_type_slug( 'lesson' ) => [ 'name' => __( 'Lessons' , 'buddyboss' ), 'class' => 'Buddyboss\LearndashIntegration\Buddypress\Generators\LessonsReportsGenerator' ], learndash_get_post_type_slug( 'topic' ) => [ 'name' => __( 'Topics' , 'buddyboss' ), 'class' => 'Buddyboss\LearndashIntegration\Buddypress\Generators\TopicsReportsGenerator' ], learndash_get_post_type_slug( 'quiz' ) => [ 'name' => __( 'Quizzes' , 'buddyboss' ), 'class' => 'Buddyboss\LearndashIntegration\Buddypress\Generators\QuizzesReportsGenerator' ], learndash_get_post_type_slug( 'essays' ) => [ 'name' => __( 'Essays' , 'buddyboss' ), 'class' => 'Buddyboss\LearndashIntegration\Buddypress\Generators\EssaysReportsGenerator' ], learndash_get_post_type_slug( 'assignment' ) => [ 'name' => __( 'Assignments' , 'buddyboss' ), 'class' => 'Buddyboss\LearndashIntegration\Buddypress\Generators\AssignmentsReportsGenerator' ], ]); } /** * Get available report filters * * @since BuddyBoss 1.0.0 */ protected function getReportFilters() { $filters = apply_filters( 'bp_ld_sync/report_filters' , [ 'user' => [ 'name' => __( 'User' , 'buddyboss' ), 'position' => 10, 'options' => $this ->getGroupUsersList() ], 'course' => [ 'name' => __( 'Course' , 'buddyboss' ), 'position' => 20, 'options' => $this ->getGroupCoursesList() ], 'step' => [ 'name' => __( 'Step' , 'buddyboss' ), 'position' => 30, 'options' => $this ->getStepTypes() ], ]); return wp_list_sort( $filters , 'position' , 'ASC' , true); } /** * Get group's member list for filter * * @since BuddyBoss 1.0.0 */ protected function getGroupUsersList() { $generator = bp_ld_sync( 'buddypress' )->sync->generator(groups_get_current_group()->id); $members = learndash_get_groups_users( $generator ->getLdGroupId()); array_unshift ( $members , (object) [ 'ID' => '' , 'display_name' => __( 'All Students' , 'buddyboss' ) ]); return wp_list_pluck( $members , 'display_name' , 'ID' ); } /** * Get group's course list for filter * * @since BuddyBoss 1.0.0 */ protected function getGroupCoursesList() { $ldGroupId = bp_ld_sync( 'buddypress' )->helpers->getLearndashGroupId( groups_get_current_group()->id ); $courseIds = learndash_group_enrolled_courses( $ldGroupId ); /** * Filter to update course lists */ $courses = array_map ( 'get_post' , apply_filters( 'bp_ld_learndash_group_enrolled_courses' , $courseIds , $ldGroupId ) ); array_unshift ( $courses , (object) [ 'ID' => '' , 'post_title' => __( 'All Courses' , 'buddyboss' ) ] ); return wp_list_pluck( $courses , 'post_title' , 'ID' ); } /** * Get list of steps available from all generators * * @since BuddyBoss 1.0.0 */ protected function getStepTypes() { return wp_list_pluck( $this ->getGenerators(), 'name' ); } /** * Get the table columns on the current generator * * @since BuddyBoss 1.0.0 */ protected function getCurrentTableColumns() { return array_map ([ $this , 'getGeneratorColumns' ], $this ->getGenerators()); } /** * Get the class name for the current generator based on request value * * @since BuddyBoss 1.0.0 */ protected function getCurrentGenerator() { $step = bp_ld_sync()->getRequest( 'step' , 'all' ); $generator = $this ->getGenerators()[ $step ]; return new $generator [ 'class' ]; } /** * Convert generator columns to datatable js format by current generator * * @since BuddyBoss 1.0.0 */ protected function getGeneratorColumns( $generator ) { $columns = ( new $generator [ 'class' ])->getColumns(); return array_map ([ $this , 'standarlizeGeneratorColumns' ], $columns , array_keys ( $columns )); } /** * Convert generator columns to datatable js format * * @since BuddyBoss 1.0.0 */ protected function standarlizeGeneratorColumns( $column , $key ) { return [ 'title' => $column [ 'label' ], 'data' => $key , 'name' => $key , 'orderable' => $column [ 'sortable' ] ]; } /** * Fix bug on LD where they typed the action name wrong * * @since BuddyBoss 1.0.0 */ protected function isRealJoins() { if (in_array(current_filter(), [ 'learndash_user_activity_query_joins' , 'learndash_user_activity_query_join' ])) { return true; } return $this ->isRealJoins; } } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.0.0 | Introduced. |
Methods
- __construct — Constructor
- courseReportResults — Filter to sort the report by Course
- export_report_column — Add status columns to export
- forceOwnReportResults — Only allow non admin/mod to view his own reports
- getCurrentGenerator — Get the class name for the current generator based on request value
- getCurrentTableColumns — Get the table columns on the current generator
- getGeneratorColumns — Convert generator columns to datatable js format by current generator
- getGenerators — Get available report generators
- getGroupCoursesList — Get group's course list for filter
- getGroupUsersList — Get group's member list for filter
- getReportFilters — Get available report filters
- getStepTypes — Get list of steps available from all generators
- init — Add actions once integration is ready
- isRealJoins — Fix bug on LD where they typed the action name wrong
- registerReportsScript — Add scripts when it's on the reports page
- removeCourseColumnIfSelected — Remove the course column if a course is selected in the filter
- removeCourseFilterIfOnlyOne — Remove the filter if only 1 option available
- removePointsColumnIfNotAssigned — Remove the points column if all courses doesn't have points
- removeUserColumnIfSelected — Remove the user column if a user is selected in the filter
- removeUserFilterIfStudent — Remove user filter is nond admin/mod are viewing reports tab
- reportAdditionalActivityFields — Sub filter for ld's activity sql query on fields
- reportAdditionalActivityGroups — Sub filter for ld's activity sql query on group_by
- reportAdditionalActivityTables — Sub filter for ld's activity sql query on joins
- reportAdditionalActivityWheres — Sub filter for ld's activity sql query on wheres
- showReportCourseStats — Output report course stats html
- showReportExport — Output report export button html
- showReportFilters — Output report filters html
- showReportTables — Output report results tables
- showReportUserStats — Output report user stats html
- standarlizeGeneratorColumns — Convert generator columns to datatable js format
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.