A teacher went to police after being bashed and robbed at his home, only to be charged with attempting to procure a child for sex, a court has been told.