/**
 * load a bulk template in databse
 * @param object $vmoodle
 * @param string $bulfile a bulk file of queries to process on the database
 * @param handle $cnx
 * @param array $vars an array of vars to inject in the bulk file before processing
 */
function vmoodle_load_db_template(&$vmoodle, $bulkfile, $cnx = null, $vars = null, $filter = null)
{
    global $CFG;
    $local_cnx = 0;
    if (is_null($cnx) || $vmoodle->vdbtype == 'postgres') {
        // Postgress MUST make a new connection to ensure db is bound to handle.
        $cnx = vmoodle_make_connection($vmoodle, true);
        $local_cnx = 1;
    }
    // Get dump file.
    if (file_exists($bulkfile)) {
        $sql = file($bulkfile);
        // converts into an array of text lines
        $dumpfile = implode("", $sql);
        if ($filter) {
            foreach ($filter as $from => $to) {
                $dumpfile = mb_ereg_replace(preg_quote($from), $to, $dumpfile);
            }
        }
        // Insert any external vars.
        if (!empty($vars)) {
            foreach ($vars as $key => $value) {
                $dumpfile = str_replace("<%%{$key}%%>", $value, $dumpfile);
            }
        }
        $sql = explode("\n", $dumpfile);
        // Cleanup unuseful things.
        if ($vmoodle->vdbtype == 'mysql') {
            $sql = preg_replace("/^--.*/", "", $sql);
            $sql = preg_replace("/^\\/\\*.*/", "", $sql);
        }
        $dumpfile = implode("\n", $sql);
    } else {
        echo "vmoodle_load_db_template : Bulk file not found";
        return false;
    }
    /// split into single queries
    $dumpfile = str_replace("\r\n", "\n", $dumpfile);
    // Translates to Unix LF.
    $queries = preg_split("/;\n/", $dumpfile);
    /// feed queries in database
    $i = 0;
    $j = 0;
    if (!empty($queries)) {
        foreach ($queries as $query) {
            $query = trim($query);
            // Get rid of trailing spaces and returns
            if ($query == '') {
                continue;
                // Avoid empty queries.
            }
            $query = mb_convert_encoding($query, 'iso-8859-1', 'auto');
            if (!($res = vmoodle_execute_query($vmoodle, $query, $cnx))) {
                echo "<hr/>load error on <br/>" . $cnx . "<hr/>";
                $j++;
            } else {
                $i++;
            }
        }
    }
    echo "loaded : {$i} queries succeeded, {$j} queries failed<br/>";
    if ($local_cnx) {
        vmoodle_close_connection($vmoodle, $cnx);
    }
    return false;
}
 }
 // Try to create database.
 $side_cnx = vmoodle_make_connection($form, false);
 if (!$side_cnx) {
     $erroritem->message = get_string('couldnotconnecttodb', 'local_vmoodle');
     $erroritem->on = 'db';
     $errors[] = $erroritem;
 } else {
     /// drop any previous database that could be in the way
     @vmoodle_drop_database($form, $side_cnx);
     if ($form->vdbtype == 'mysql') {
         $sql = "\n                       CREATE DATABASE `{$form->vdbname}` CHARACTER SET 'utf8'\n                    ";
     } elseif ($form->vdbtype == 'posstgres') {
         $sql = "\n                       CREATE DATABASE {$form->vdbname} WITH OWNER={$form->vdblogin} ENCODING=UTF8\n                    ";
     }
     $res = vmoodle_execute_query($form, $sql, $side_cnx);
     if (!$res) {
         $erroritem->message = get_string('couldnotcreatedb', 'local_vmoodle');
         $erroritem->on = 'db';
         $errors[] = $erroritem;
     } else {
         $done[] = 'database';
         print_string('databasecreated', 'local_vmoodle');
         echo "<br/>";
     }
     // make a new connection so we can bind to database
     vmoodle_close_connection($form, $side_cnx);
     $side_cnx = vmoodle_make_connection($form, true);
     /// prepare a filter for absolute www roots
     $manifest = vmoodle_get_vmanifest($v);
     $filter[$manifest->templatehost] = $form->vhostname;