public static function get_missing_plans($templateid, $cohortid, $unlinkedaremissing = false) {
        global $DB;

        $skipsql = '';
        $skipparams = array();
        if (!$unlinkedaremissing) {
            $skipsql = 'OR p.origtemplateid = :origtemplateid';
            $skipparams = array('origtemplateid' => $templateid);
        }

        $sql = "SELECT cm.userid
                  FROM {cohort_members} cm
             LEFT JOIN {" . plan::TABLE . "} p
                    ON p.userid = cm.userid
                   AND (p.templateid = :templateid
                        $skipsql)
                 WHERE cm.cohortid = :cohortid
                   AND p.id IS NULL";
        $params = array('templateid' => $templateid, 'cohortid' => $cohortid) + $skipparams;

        return $DB->get_fieldset_sql($sql, $params);
    }