Star trapping and metallicity enrichment in quasars and active galactic nuclei