How to test if user is filtering post list in dashboard

I have found a solution, using $query->get('cat'): when restricted users select “all categories” $query->get('cat') will return '0', so in this case I will apply filtering, otherwise my code won’t run, allowing user-selected filter (in a restricted range) to be applied

/********** post & media lists filtered by user role & category ****************
function posts_for_current_role($query) {
global $students_allowed_cat;
global $teachers_allowed_cat;
global $pagenow;
    if($query->is_admin && !current_user_can('administrator')) {
        $user = wp_get_current_user();

        if( 'edit.php' == $pagenow   ){
            if ($query->get('cat') == '0'){
                if ( in_array( 'teacher', (array) $user->roles ) ) {
                    //The user has the "teacher" role
                    $filtered_cat = $teachers_allowed_cat;
                }
                if ( in_array( 'student', (array) $user->roles ) ) {
                    //The user has the "student" role
                    $filtered_cat = $students_allowed_cat;
                }
                $query->set('cat',  $filtered_cat);
            }
        }
        if( in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' )  ) ){
            // hide media from admin
            $query->set('author',  '-1');
        }

    }
    return $query;
}