public static function list_templates($competencyid, $onlyvisible) {
        global $DB;

        $sql = 'SELECT tpl.*
                  FROM {' . template::TABLE . '} tpl
                  JOIN {' . self::TABLE . '} tplcomp
                    ON tplcomp.templateid = tpl.id
                 WHERE tplcomp.competencyid = ? ';
        $params = array($competencyid);

        if ($onlyvisible) {
            $sql .= ' AND tpl.visible = ?';
            $params[] = 1;
        }

        $sql .= ' ORDER BY tpl.id ASC';

        $results = $DB->get_records_sql($sql, $params);

        $instances = array();
        foreach ($results as $result) {
            array_push($instances, new template(0, $result));
        }

        return $instances;
    }