public static function get_blocks_from_path($path) {
        global $DB;

        $blocks = array(); // To return results

        static $availableblocks = array(); // Get and cache available blocks
        if (empty($availableblocks)) {
            $availableblocks = array_keys(core_component::get_plugin_list('block'));
        }

        $path = $path . '/blocks'; // Always look under blocks subdir

        if (!is_dir($path)) {
            return array();
        }

        if (!$dir = opendir($path)) {
            return array();
        }
        while (false !== ($file = readdir($dir))) {
            if ($file == '.' || $file == '..') { // Skip dots
                continue;
            }
            if (is_dir($path .'/' . $file)) { // Dir found, check it's a valid block
                if (!file_exists($path .'/' . $file . '/block.xml')) { // Skip if xml file not found
                    continue;
                }
                // Extract block name
                $blockname = preg_replace('/(.*)_\d+/', '\\1', $file);
                // Check block exists and is installed
                if (in_array($blockname, $availableblocks) && $DB->record_exists('block', array('name' => $blockname))) {
                    $blocks[$path .'/' . $file] = $blockname;
                }
            }
        }
        closedir($dir);

        return $blocks;
    }