prepare_items(); include AIMSLMS_PLUGIN_DIR . 'includes/admin/views/students.php'; } /** * Overrides the globally-localized `aimslmsLocation` JS object (registered in * Aims Infosoft LMS::assets() for the *current* admin user) with the states list * and saved state for the student being viewed/edited on this screen. * * @param int $user_id Student user ID. * @return void */ private static function localize_student_location( $user_id ) { wp_add_inline_script( 'aimslms-location', 'window.aimslmsLocation = ' . wp_json_encode( array( 'states' => aimslms_get_states(), 'savedState' => get_user_meta( $user_id, '_aimslms_billing_state', true ), ) ) . ';', 'after' ); } /** * CSV export handler for the students list. * * @return void */ public static function export_students() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only display filter on a manage_options-gated admin screen. if ( ! is_admin() || ! isset( $_GET['page'], $_GET['action'] ) || 'aimslms-students' !== $_GET['page'] || 'export_students' !== $_GET['action'] ) { // phpcs:enable WordPress.Security.NonceVerification.Recommended return; } if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'Permission denied' ); } global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $students = $wpdb->get_results( "SELECT u.ID, u.display_name, u.user_email, COUNT(DISTINCT e.course_id) AS enrollments, MAX(e.enrollment_date) AS last_enrolled, COUNT(DISTINCT CASE WHEN cp.avg_completion = 100 THEN cp.course_id END) AS completed_courses, ROUND(AVG(cp.avg_completion)) AS avg_progress FROM {$wpdb->prefix}users u JOIN {$wpdb->prefix}aimslms_course_enrollments e ON u.ID = e.user_id LEFT JOIN ( SELECT user_id, course_id, ROUND(AVG(completion_percentage)) AS avg_completion FROM {$wpdb->prefix}aimslms_course_progress GROUP BY user_id, course_id ) cp ON cp.user_id = u.ID AND cp.course_id = e.course_id GROUP BY u.ID ORDER BY u.ID DESC", ARRAY_A ); if ( empty( $students ) ) { wp_die( 'No students found' ); } // 🚨 NO OUTPUT BEFORE THIS nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=students-' . gmdate( 'Y-m-d' ) . '.csv' ); $output = fopen( 'php://output', 'w' ); fputcsv( $output, array( 'Name', 'Email', 'Enrollments', 'Completed Courses', 'Last Enrolled', ) ); foreach ( $students as $student ) { $first = get_user_meta( $student['ID'], 'first_name', true ); $last = get_user_meta( $student['ID'], 'last_name', true ); $full_name = trim( $first . ' ' . $last ); $name = $full_name ? $full_name : $student['display_name']; fputcsv( $output, array( aimslms_csv_safe( $name ), aimslms_csv_safe( $student['user_email'] ), $student['enrollments'], $student['completed_courses'], $student['last_enrolled'], ) ); } fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- streaming CSV directly to php://output, WP_Filesystem does not support this stream. exit; } /** * Handles the student edit form submission: verifies the nonce and * capability, then updates the core user data and billing meta. * * @return void */ public static function update_student() { if ( empty( $_POST['student_id'] ) ) { return; } $user_id = absint( $_POST['student_id'] ); if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'aimslms_update_student_' . $user_id ) ) { wp_die( esc_html__( 'Security check failed.', 'aims-infosoft-lms' ) ); } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized action.', 'aims-infosoft-lms' ) ); } // Update core user data. wp_update_user( array( 'ID' => $user_id, 'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '', 'last_name' => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '', ) ); // Update user meta. $meta_fields = array( '_aimslms_billing_address_1' => 'billing_address_1', '_aimslms_billing_address_2' => 'billing_address_2', '_aimslms_billing_city' => 'billing_city', '_aimslms_billing_postcode' => 'billing_postcode', '_aimslms_billing_country' => 'billing_country', '_aimslms_billing_state' => 'billing_state', ); foreach ( $meta_fields as $meta_key => $post_key ) { if ( isset( $_POST[ $post_key ] ) ) { update_user_meta( $user_id, $meta_key, sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) ); } } // Redirect with success. wp_safe_redirect( admin_url( 'admin.php?page=aimslms-students&edit=' . $user_id . '&updated=1' ) ); exit; } /** * Handles single and bulk student deletion requests from the students list. * * @return void */ public static function delete_students() { if ( ! is_admin() || ! isset( $_GET['page'], $_GET['action'] ) || 'aimslms-students' !== $_GET['page'] ) { return; } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Permission denied', 'aims-infosoft-lms' ) ); } $student_ids = array(); if ( 'delete' === $_GET['action'] && ! empty( $_GET['student'] ) ) { $student_id = absint( $_GET['student'] ); if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'delete_student_' . $student_id ) ) { wp_die( esc_html__( 'Security check failed', 'aims-infosoft-lms' ) ); } $student_ids[] = $student_id; } if ( 'delete_students' === $_GET['action'] && ! empty( $_GET['student'] ) ) { // Rendered by WP_List_Table with a 'bulk-students' nonce (from the // 'plural' arg in AIMSLMS_Students_Table's constructor). check_admin_referer( 'bulk-students' ); $student_ids = array_map( 'absint', (array) $_GET['student'] ); } if ( empty( $student_ids ) ) { return; } foreach ( $student_ids as $student_id ) { aimslms_delete_student_data( $student_id ); } wp_safe_redirect( admin_url( 'admin.php?page=aimslms-students&deleted=' . count( $student_ids ) ) ); exit; } } AIMSLMS_Admin_Students::init();