diff --git a/includes_class-sensei-analysis.php.html b/includes_class-sensei-analysis.php.html index 464c1f3dad..0ad0ca7b6a 100644 --- a/includes_class-sensei-analysis.php.html +++ b/includes_class-sensei-analysis.php.html @@ -952,9 +952,8 @@

Source: includes/class-sensei-analysis.php

if ( $user_id - && ( - ! in_array( $user_id, Sensei()->teacher->get_learner_ids_for_courses_with_edit_permission(), true ) - ) + && ! current_user_can( 'manage_options' ) // Admins can access any user. + && ! in_array( $user_id, Sensei()->teacher->get_learner_ids_for_courses_with_edit_permission(), true ) ) { wp_die( esc_html__( 'Invalid user', 'sensei-lms' ), 404 ); } diff --git a/includes_class-sensei-teacher.php.html b/includes_class-sensei-teacher.php.html index fc807493c6..4b16f7b4a6 100644 --- a/includes_class-sensei-teacher.php.html +++ b/includes_class-sensei-teacher.php.html @@ -1238,51 +1238,35 @@

Source: includes/class-sensei-teacher.php

return []; } - $learner_ids_for_courses_with_edit_permission = []; + $course_ids = wp_list_pluck( $courses_with_edit_permission, 'ID' ); + $course_ids_placeholder = implode( ',', array_fill( 0, count( $course_ids ), '%d' ) ); - foreach ( $courses_with_edit_permission as $course ) { - - $course_learner_ids = array(); - $activity_comments = Sensei_Utils::sensei_check_for_activity( - array( - 'post_id' => $course->ID, - 'type' => 'sensei_course_status', - 'field' => 'user_id', - ), - true + global $wpdb; + if ( Progress_Storage_Settings::is_tables_repository() ) { + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using a direct query for performance reasons. + $learner_ids = (array) $wpdb->get_col( + $wpdb->prepare( + "SELECT DISTINCT user_id + FROM {$wpdb->prefix}sensei_lms_progress + WHERE type = 'course' + AND post_id IN ( $course_ids_placeholder )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders created dynamically. + $course_ids + ) + ); + } else { + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using a direct query for performance reasons. + $learner_ids = (array) $wpdb->get_col( + $wpdb->prepare( + "SELECT DISTINCT user_id + FROM {$wpdb->comments} + WHERE comment_type = 'sensei_course_status' + AND comment_post_ID IN ( $course_ids_placeholder )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders created dynamically. + $course_ids + ) ); - - if ( empty( $activity_comments ) || ( is_array( $activity_comments ) && ! ( count( $activity_comments ) > 0 ) ) ) { - continue; // skip to the next course as there are no users on this course - } - - // it could be an array of comments or a single comment - if ( is_array( $activity_comments ) ) { - - foreach ( $activity_comments as $comment ) { - - $user = get_userdata( $comment->user_id ); - - if ( empty( $user ) ) { - // next comment in this array - continue; - } - - $course_learner_ids[] = $user->ID; - } - } else { - - $user = get_userdata( $activity_comments->user_id ); - $course_learner_ids[] = $user->ID; - - } - - // add learners on this course to the all courses learner list - $learner_ids_for_courses_with_edit_permission = array_merge( $learner_ids_for_courses_with_edit_permission, $course_learner_ids ); - } - return $learner_ids_for_courses_with_edit_permission; + return array_map( 'intval', $learner_ids ); } /** diff --git a/sensei_restrict_posts_menu_page.html b/sensei_restrict_posts_menu_page.html index c1494ba1bd..51ef02acbe 100644 --- a/sensei_restrict_posts_menu_page.html +++ b/sensei_restrict_posts_menu_page.html @@ -144,7 +144,7 @@
Parameters:
Source: