public static function count_records_for_template($templateid, $proficiency=null) {
        global $DB;

        $params = array('templateid' => $templateid);
        $sql = 'SELECT ' . " COUNT('x') " .
                 'FROM {' . self::TABLE . '} ucp
                  JOIN {' . plan::TABLE . '} p
                    ON ucp.planid = p.id
                 WHERE p.templateid = :templateid';
        if ($proficiency === true) {
            $sql .= ' AND ucp.proficiency = :proficiency';
            $params['proficiency'] = true;
        } else if ($proficiency === false) {
            $sql .= ' AND (ucp.proficiency = :proficiency OR ucp.proficiency IS NULL)';
            $params['proficiency'] = false;
        }

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