OK. The punctuated version is:
> James, while John had had "had", had had "had had"; "had had" had had a better effect on the teacher
If you take the first clause:
> James, while John had had "had"
You could replace it with:
> James, while John had "had"
Similarly, most of the (non-quoted) instances of "had had" could be replaced with "had", keeping the meaning almost exactly the same. (edit: this is the point of the sentence. "had had" and "had" are almost equivalent, but the teacher happens to prefer "had had".) This is like the original, where "had said" can be contracted to "said" without loss of meaning, so
> She had said goodbye too many times before
is equivalent to:
> She said goodbye too many times before