Fix submit history

Open for review.

Kind of big hard-to-follow refactor commit, but the point of it was to break out the very terse es query into something a little more understandable. I may have gone a little overboard in changing the return value of a couple utility functions to be namedtuples instead of dicts, but I think it's a better pattern.

http://manage.dimagi.com/default.asp?113056