Location: PHPKode > projects > Remote Labs for Moodle > virtual/db/postgres7.php
<?php // $Id: postgres7.php,v 1.45 2006/10/26 22:46:08 stronk7 Exp $

// THIS FILE IS DEPRECATED!  PLEASE DO NOT MAKE CHANGES TO IT!
//
// IT IS USED ONLY FOR UPGRADES FROM BEFORE MOODLE 1.7, ALL 
// LATER CHANGES SHOULD USE upgrade.php IN THIS DIRECTORY.

function virtual_upgrade($oldversion) {
// This function does anything necessary to upgrade
// older versions to match current functionality
    global $CFG;
    if ($oldversion < 2004033000) {
        table_column("virtual", "", "auto", "integer", "1", "", "0", "NOT NULL", "summary"); 
    }
    if ($oldversion < 2004040900) {
        table_column("virtual_sco_users", "", "cmi_core_score_raw", "real", "3", "", "0", "NOT NULL", "cmi_core_session_time");
    }
    if ($oldversion < 2004061800) {
        table_column("virtual", "", "popup", "varchar", "255", "", "", "NOT NULL", "auto");
        table_column("virtual", "reference", "reference", "varchar", "255", "", "", "NOT NULL");
    }
    if ($oldversion < 2004070800) {
        table_column("virtual_scoes", "", "datafromlms", "TEXT", "", "", "", "NOT NULL", "title");
        modify_database("", "ALTER TABLE {$CFG->prefix}virtual_sco_users DROP cmi_launch_data;");
    }
    if ($oldversion < 2004071700) {
        table_column("virtual_scoes", "", "manifest", "VARCHAR", "255", "", "", "NOT NULL", "virtual");
        table_column("virtual_scoes", "", "organization", "VARCHAR", "255", "", "", "NOT NULL", "manifest");
    }
    if ($oldversion < 2004071900) {
        table_column("virtual", "", "maxgrade", "real", "3", "", "0", "NOT NULL", "reference");
        table_column("virtual", "", "grademethod", "integer", "", "", "0", "NOT NULL", "maxgrade");
    }
    
    if ($oldversion < 2004111200) {
        execute_sql("DROP INDEX {$CFG->prefix}virtual_course_idx;",false);
        execute_sql("DROP INDEX {$CFG->prefix}virtual_scoes_virtual_idx;",false); 
        execute_sql("DROP INDEX {$CFG->prefix}virtual_sco_users_userid_idx;",false); 
        execute_sql("DROP INDEX {$CFG->prefix}virtual_sco_users_virtualid_idx;",false);
        execute_sql("DROP INDEX {$CFG->prefix}virtual_sco_users_scoid_idx;",false);

        modify_database('','CREATE INDEX prefix_virtual_course_idx ON prefix_virtual (course);');
        modify_database('','CREATE INDEX prefix_virtual_scoes_virtual_idx ON prefix_virtual_scoes (virtual);');
        modify_database('','CREATE INDEX prefix_virtual_sco_users_userid_idx ON  prefix_virtual_sco_users (userid);');
        modify_database('','CREATE INDEX prefix_virtual_sco_users_virtualid_idx ON  prefix_virtual_sco_users (virtualid);');
        modify_database('','CREATE INDEX prefix_virtual_sco_users_scoid_idx ON  prefix_virtual_sco_users (scoid);');
    }
    
    if ($oldversion < 2005031300) {
        table_column("virtual_scoes", "", "prerequisites", "VARCHAR", "200", "", "", "NOT NULL", "title");
        table_column("virtual_scoes", "", "maxtimeallowed", "VARCHAR", "13", "", "", "NOT NULL", "prerequisites");
        table_column("virtual_scoes", "", "timelimitaction", "VARCHAR", "19", "", "", "NOT NULL", "maxtimeallowed");
        table_column("virtual_scoes", "", "masteryscore", "VARCHAR", "200", "", "", "NOT NULL", "datafromlms");

        $oldscoes = get_records_select("virtual_scoes",null,"id ASC");
        table_column("virtual_scoes", "type", "virtualtype", "VARCHAR", "5", "", "", "NOT NULL");
        if(!empty($oldscoes)) {
            foreach ($oldscoes as $sco) {
                $sco->virtualtype = $sco->type;
                unset($sco->type);
                update_record("virtual_scoes",$sco);
            }
        }

        execute_sql("CREATE TABLE {$CFG->prefix}virtual_scoes_track (
                        id SERIAL,
                        userid integer NOT NULL default '0',
                        virtualid integer NOT NULL default '0',
                        scoid integer NOT NULL default '0',
                        element varchar(255) NOT NULL default '',
                        value text NOT NULL default '',
                        PRIMARY KEY (userid, virtualid, scoid, element),
                        UNIQUE (userid, virtualid, scoid, element)
                   );",true); 
 
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_userdata_idx ON  prefix_virtual_scoes_track (userid, virtualid, scoid);');
    
        $oldtracking = get_records_select('virtual_sco_users',null,'id ASC');
        $oldelements = array ('cmi_core_lesson_location',
                              'cmi_core_lesson_status',
                              'cmi_core_exit',
                              'cmi_core_total_time',
                              'cmi_core_score_raw',
                              'cmi_suspend_data');

        if(!empty($oldtrackings)) {
            foreach ($oldtrackings as $oldtrack) {
                $newtrack = '';
                $newtrack->userid = $oldtrack->userid;
                $newtrack->virtualid = $oldtrack->virtualid;
                $newtrack->scoid = $oldtrack->scoid;

                foreach ( $oldelements as $element) {
                    $newtrack->element = $element;
                    $newtrack->value = $oldtrack->$element;
                    if ($newtrack->value == NULL) {
                        $newtrack->value = '';
                    }
                    insert_record('virtual_scoes_track',$newtrack,false);
                }
            }
        }

        modify_database('',"DROP TABLE prefix_virtual_sco_users");
        modify_database('',"INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('resource', 'review', 'resource', 'name')");   // Wrong line!   See MDL-6516
    }

    if ($oldversion < 2005040200) {
        execute_sql('ALTER TABLE '.$CFG->prefix.'virtual DROP popup');    // Old field
    }
    
    if ($oldversion < 2005040400) {
        table_column('virtual_scoes', '', 'parameters', 'VARCHAR', '255', '', '', 'NOT NULL', 'launch');
    }
    
    if ($oldversion < 2005040700) {
        //execute_sql('DROP PRIMARY KEY '.$CFG->prefix.'virtual_scoes_track_pkey;',false);
        execute_sql('DROP INDEX '.$CFG->prefix.'virtual_scoes_track_userdata_idx;',false);
        execute_sql('DROP INDEX '.$CFG->prefix.'virtual_scoes_track_userid_key;',false);
        modify_database('','CREATE UNIQUE INDEX prefix_virtual_scoes_track_track_idx ON  prefix_virtual_scoes_track (userid, virtualid, scoid, element);');
        execute_sql('ALTER TABLE '.$CFG->prefix.'virtual_scoes_track ADD PRIMARY KEY ("id");',false);
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_virtualid_idx ON  prefix_virtual_scoes_track (virtualid);');
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_userid_idx ON  prefix_virtual_scoes_track (userid);');
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_scoid_idx ON  prefix_virtual_scoes_track (scoid);');
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_element_idx ON  prefix_virtual_scoes_track (element);');
    }
    
    if ($oldversion < 2005041500) {
        if ($virtuals = get_records_select('virtual',null,'id ASC')) {
            foreach ($virtuals as $virtual) {
                if (strlen($virtual->datadir) == 14) {
                    $basedir = $CFG->dataroot.'/'.$virtual->course;
                    $virtualdir = '/moddata/virtual';
                    rename($basedir.$virtualdir.$virtual->datadir,$basedir.$virtualdir.'/'.$virtual->id);
                }
            }
        }
        execute_sql('ALTER TABLE '.$CFG->prefix.'virtual DROP datadir');    // Old field
    }
    
    if ($oldversion < 2005041600) {
       table_column("virtual", "", "version", "VARCHAR", "9", "", "virtual_1.2", "NOT NULL", "reference");
    }

    if ($oldversion < 2005042700) {
        $trackingdata = get_records_select("virtual_scoes_track",null,"id ASC");
        if (!empty($trackingdata)) {
            $oldelements = array ('cmi_core_lesson_location',
                                  'cmi_core_lesson_status',
                                  'cmi_core_exit',
                                  'cmi_core_total_time',
                                  'cmi_core_score_raw',
                                  'cmi_suspend_data');
            $newelements = array ('cmi.core.lesson_location',
                                  'cmi.core.lesson_status',
                                  'cmi.core.exit',
                                  'cmi.core.total_time',
                                  'cmi.core.score.raw',
                                  'cmi.suspend_data');
            foreach ($trackingdata as $track) {
                if (($pos = array_search($track->element,$oldelements)) !== false) {
                    $track->element = $newelements[$pos];
                    update_record('virtual_scoes_track',$track);
                }
            }
        }
    }

    if ($oldversion < 2005042800) {
       table_column("virtual", "", "browsemode", "integer", "", "", "1", "NOT NULL", "summary");
    }

    if ($oldversion < 2005050800) {
       table_column("virtual", "", "width", "integer", "", "", "800", "NOT NULL", "auto");
       table_column("virtual", "", "height", "integer", "", "", "600", "NOT NULL", "width");
    }

     if ($oldversion < 2005052200) {
       table_column("virtual_scoes_track", "", "timemodified", "integer", "", "", "0", "NOT NULL", "value");
    }
    
    if ($oldversion < 2005052301) { // Mass cleanup of bad upgrade scripts
        execute_sql("DROP INDEX {$CFG->prefix}virtual_scoes_track_virtualid_idx", false);
        execute_sql("DROP INDEX {$CFG->prefix}virtual_scoes_track_userid_idx", false);
        execute_sql("DROP INDEX {$CFG->prefix}virtual_scoes_track_scoid_idx", false);
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_virtual_idx ON  prefix_virtual_scoes_track (virtualid);');
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_user_idx ON  prefix_virtual_scoes_track (userid);');
        modify_database('','CREATE INDEX prefix_virtual_scoes_track_sco_idx ON  prefix_virtual_scoes_track (scoid);');
        notify('The above errors can be ignored if the indexes already exists, its possible that it was cleaned up already before running this upgrade');
        execute_sql("DROP INDEX {$CFG->prefix}virtual_scoes_track_track_idx", false);
        modify_database('','ALTER TABLE ONLY prefix_virtual_scoes_track DROP CONSTRAINT prefix_virtual_scoes_track_pkey');
        modify_database('','ALTER TABLE ONLY prefix_virtual_scoes_track ADD CONSTRAINT prefix_virtual_scoes_track_pkey PRIMARY KEY (id)');
        modify_database('','ALTER TABLE ONLY prefix_virtual_scoes_track ADD UNIQUE (userid, virtualid, scoid, element)');
        modify_database('','ALTER TABLE prefix_virtual ALTER browsemode SET DEFAULT 0');
        modify_database('','ALTER TABLE prefix_virtual DROP version');
        notify('You can ignore the error above as version column does not exists, this will be properly upgraded later');
        table_column('virtual_scoes','datafromlms','datafromlms','VARCHAR','255','','');
        table_column('virtual_scoes','maxtimeallowed','maxtimeallowed','VARCHAR','19','','');
    }

    if ($oldversion < 2005062700) {
       table_column("virtual", "", "popup", "integer", "", "", "0", "NOT NULL", "auto");
    }
    
    if ($oldversion < 2005070600) {
        table_column("virtual", "", "hidetoc", "integer", "", "", "0", "NOT NULL", "browsemode");
        $virtuals = get_records_select("virtual","","id ASC");
        table_column("virtual", "browsemode", "hidebrowse", "integer", "", "", "0", "NOT NULL", "");
        if (!empty($virtuals)) {
            foreach($virtuals as $virtual) {
                if ($virtual->browsemode = 1) {
                    $virtual->hidebrowse = 0;
                } else {
                    $virtual->hidebrowse = 1;
                }
                update_record('virtual',$virtual);
            }
        }
    }

    if ($oldversion < 2005092500) {
        table_column("virtual", "", "hidenav", "integer", "", "", "0", "NOT NULL", "hidetoc");
        table_column("virtual", "", "options", "varchar", "255", "", "", "NOT NULL","popup");
    }
    
    if ($oldversion < 2005092600) {
        table_column("virtual_scoes_track", "", "attempt", "integer", "", "", "1", "NOT NULL", "scoid"); 
        execute_sql("DROP INDEX {$CFG->prefix}virtual_scoes_track_virtualid_idx", false);
        modify_database('',"CREATE UNIQUE INDEX prefix_virtual_scoes_track_uk ON prefix_virtual_scoes_track(userid,virtualid,scoid,attempt,element);");
    }

    if ($oldversion < 2005102800) {
        table_column("virtual", "", "maxattempt", "integer", "", "UNSIGNED", "1", "NOT NULL", "maxgrade"); 
    }


    if ($oldversion < 2006021401) {
       table_column("virtual", "", "version", "VARCHAR", "9", "", "", "NOT NULL", "reference");

       modify_database('', 'ALTER TABLE prefix_virtual ALTER COLUMN width SET DEFAULT 100');
       modify_database('', 'ALTER TABLE prefix_virtual ALTER COLUMN height SET DEFAULT 500');
       // there is an extra constraint key1, we should drop this
       modify_database('', 'ALTER TABLE prefix_virtual_scoes_track DROP CONSTRAINT prefix_virtual_scoes_track_userid_key1');
       modify_database('', 'ALTER TABLE prefix_virtual_scoes_track DROP CONSTRAINT prefix_virtual_scoes_track_userid_key');
       modify_database('', 'ALTER TABLE prefix_virtual_scoes_track ADD CONSTRAINT prefix_virtual_scoes_track_userid_key UNIQUE (userid, virtualid, scoid, attempt, element)');
    }

    if ($oldversion < 2006102600) {
        table_column("virtual", "", "skipview", "integer", "", "", "1", "NOT NULL"); 
    }

    if ($oldversion < 2006102702) {   // This is a month in advance!!!!!
        execute_sql("DELETE FROM {$CFG->prefix}log_display WHERE module = 'resource' AND action = 'review' AND mtable = 'resource' AND field = 'name';", false);      // See MDL-6516
        execute_sql("INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('virtual', 'review', 'virtual', 'name');", false);
    }

    //////  DO NOT ADD NEW THINGS HERE!!  USE upgrade.php and the lib/ddllib.php functions.

    return true;
}
?>
Return current item: Remote Labs for Moodle