The sentence is much easier to understand with added punctuation and emphasis: James, while John had had "had", had had "had had"; "had had" had had a better effect on the teacher.
Traducerea aproximativă în română sună așa: James, după ce John folosise „had”, folosi „had had”; „had had” a fost mai bine apreciat de către învățător.