Galactic star formation enhanced and quenched by ram pressure in groups and clusters