This function is duplicated [here](https://github.com/dimagi/commcare-hq/pull/3520/files#diff-6ac57ddf88e55009abbdc2bedfbfadfaR1).