File: postgres7.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (169 lines) | stat: -rw-r--r-- 7,138 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?PHP  //$Id: postgres7.php,v 1.17 2006/04/13 02:55:27 vyshane Exp $
//
// This file keeps track of upgrades to Moodle's
// blocks system.
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installtion to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// Versions are defined by backup_version.php
//
// This file is tailored to PostgreSQL

function blocks_upgrade($oldversion=0) {

global $CFG;
    
    $result = true;
    
    if ($oldversion < 2004041000 and $result) {
        $result = execute_sql("
                                CREATE TABLE {$CFG->prefix}blocks 
                                (
              				    id SERIAL8 PRIMARY KEY,
  			            	    name varchar(40) NOT NULL default '',
  				                version INT8 NOT NULL default '0',
  				                cron INT8  NOT NULL default '0',
  				                lastcron INT8  NOT NULL default '0',
  				                visible int NOT NULL default '1'
                                )
        ") ;

    }

    if ($oldversion < 2004101900 && $result) {
        $result = execute_sql("CREATE TABLE {$CFG->prefix}block (
                        id SERIAL8 PRIMARY KEY,
                        name varchar(40) NOT NULL default '',
                        version INT8 NOT NULL default '0',
                        cron INT8 NOT NULL default '0',
                        lastcron INT8 NOT NULL default '0',
                        visible int NOT NULL default '1',
                        multiple int NOT NULL default '0'
                     ) 
                     ");

        if(!$result) {
            return false;
        }

        $records = get_records('blocks');
        if(!empty($records)) {
            foreach($records as $block) {
                $block->multiple = 0;
                insert_record('block', $block, false);
            }
            execute_sql("SELECT setval('{$CFG->prefix}block_id_seq', (SELECT MAX(id) FROM {$CFG->prefix}block), true)");
        }

        execute_sql("DROP TABLE {$CFG->prefix}blocks");
        
        $result = execute_sql("CREATE TABLE {$CFG->prefix}block_instance (
                        id SERIAL8 PRIMARY KEY,
                        blockid INT8 not null default '0',
                        pageid INT8 not null default '0',
                        pagetype varchar(12) not null default '',
                        position char not null default 'l' check (position in ('l', 'r')) ,
                        weight int not null default '0',
                        visible int not null default '0',
                        configdata text not null default ''
                    )");

        if(!$result) {
            return false;
        }
        
        $records = get_records('course', '','','', 'id, shortname, blockinfo');
        if(!empty($records)) {
            foreach($records as $thiscourse) {
                // The @ suppresses a notice emitted if there is no : in the string
                @list($left, $right) = split(':', $thiscourse->blockinfo);
                if(!empty($left)) {
                    $arr = explode(',', $left);
                    foreach($arr as $weight => $blk) {
                        $instance = new stdClass;
                        $instance->blockid    = abs($blk);
                        $instance->pageid     = $thiscourse->id;
                        $instance->pagetype   = PAGE_COURSE_VIEW;
                        $instance->position   = BLOCK_POS_LEFT;
                        $instance->weight     = $weight;
                        $instance->visible    = ($blk > 0) ? 1 : 0;
                        $instance->configdata = '';
                        insert_record('block_instance', $instance, false);
                    }
                }
                if(!empty($right)) {
                    $arr = explode(',', $right);
                    foreach($arr as $weight => $blk) {
                        $instance = new stdClass;
                        $instance->blockid    = abs($blk);
                        $instance->pageid     = $thiscourse->id;
                        $instance->pagetype   = PAGE_COURSE_VIEW;
                        $instance->position   = BLOCK_POS_RIGHT;
                        $instance->weight     = $weight;
                        $instance->visible    = ($blk > 0) ? 1 : 0;
                        $instance->configdata = '';
                        insert_record('block_instance', $instance, false);
                    }
                }
            }
        }

        execute_sql("ALTER TABLE {$CFG->prefix}course DROP COLUMN blockinfo");
    }

    if ($oldversion < 2004112900 && $result) {
        $result = $result && table_column('block_instance', 'pagetype', 'pagetype', 'varchar', '20', '');
        $result = $result && table_column('block_instance', 'position', 'position', 'varchar', '10', '');
    }

    if ($oldversion < 2005043000 && $result) {
        $records = get_records('block');
        if(!empty($records)) {
            foreach($records as $block) {
                if(!block_is_compatible($block->name)) {
                    $block->visible = 0;
                    update_record('block', $block);
                    notify('The '.$block->name.' block has been disabled because it is not compatible with Moodle 1.5 and needs to be updated by a programmer.');
                }
            }
        }
    }

    if ($oldversion < 2005022401 && $result) { // Mass cleanup of bad upgrade scripts
        execute_sql("CREATE INDEX {$CFG->prefix}block_instance_pageid_idx ON {$CFG->prefix}block_instance (pageid)",false); // this one should be quiet...
        modify_database('','ALTER TABLE prefix_block_instance ALTER pagetype SET DEFAULT \'\'');
        modify_database('','ALTER TABLE prefix_block_instance ALTER position SET DEFAULT \'\'');
        modify_database('','ALTER TABLE prefix_block_instance ALTER pagetype SET NOT NULL');
        modify_database('','ALTER TABLE prefix_block_instance ALTER position SET NOT NULL');
    }

    if ($oldversion < 2005081600) {
        modify_database('',"CREATE TABLE prefix_block_pinned ( 
            id SERIAL8 PRIMARY KEY,
            blockid INT8 NOT NULL default 0,
            pagetype varchar(20) NOT NULL default '',
            position varchar(10) NOT NULL default '',
            weight INT NOT NULL default 0,
            visible INT NOT NULL default 0,
            configdata text NOT NULL default 0
          );");
     }

    if ($oldversion < 2005090200) {
        execute_sql("CREATE INDEX {$CFG->prefix}block_instance_pagetype_idx ON {$CFG->prefix}block_instance (pagetype);",false); // do it silently, in case it's already there from 1.5
        modify_database('','CREATE INDEX prefix_block_pinned_pagetype_idx ON prefix_block_pinned (pagetype);');
    }

    //Finally, return result
    return $result;
}
?>