MDL-15107 feedback dml conversion
[moodle.git] / mod / feedback / db / upgrade.php
CommitLineData
c70ad9f7 1<?php //$Id$
2
3// This file keeps track of upgrades to
4// the feedback module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
12// your older installtion to the current version.
13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
c70ad9f7 19
20function xmldb_feedback_upgrade($oldversion=0) {
21
f33e1ed4 22 global $CFG, $THEME, $DB;
23
24 $dbman = $DB->get_manager();
c70ad9f7 25
26 $result = true;
27
28 if ($result && $oldversion < 2007012310) {
29
30 //create a new table feedback_completedtmp and the field-definition
a8cb94f6 31 $table = new xmldb_table('feedback_completedtmp');
c70ad9f7 32
a8cb94f6 33 $field = new xmldb_field('id');
69b80cc2 34 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, true, null, null, null, null);
c70ad9f7 35 $table->addField($field);
36
a8cb94f6 37 $field = new xmldb_field('feedback');
69b80cc2 38 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 39 $table->addField($field);
40
a8cb94f6 41 $field = new xmldb_field('userid');
69b80cc2 42 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 43 $table->addField($field);
44
a8cb94f6 45 $field = new xmldb_field('guestid');
69b80cc2 46 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, null, false, null, null, '', null);
c70ad9f7 47 $table->addField($field);
48
a8cb94f6 49 $field = new xmldb_field('timemodified');
69b80cc2 50 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 51 $table->addField($field);
52
a8cb94f6 53 $key = new xmldb_key('PRIMARY');
69b80cc2 54 $key->set_attributes(XMLDB_KEY_PRIMARY, array('id'));
c70ad9f7 55 $table->addKey($key);
56
a8cb94f6 57 $key = new xmldb_key('feedback');
69b80cc2 58 $key->set_attributes(XMLDB_KEY_FOREIGN, array('feedback'), 'feedback', 'id');
c70ad9f7 59 $table->addKey($key);
60
f33e1ed4 61 $result = $result && $dbman->create_table($table);
c70ad9f7 62 ////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////
64 //create a new table feedback_valuetmp and the field-definition
a8cb94f6 65 $table = new xmldb_table('feedback_valuetmp');
c70ad9f7 66
a8cb94f6 67 $field = new xmldb_field('id');
69b80cc2 68 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, true, null, null, null, null);
c70ad9f7 69 $table->addField($field);
70
a8cb94f6 71 $field = new xmldb_field('course_id');
69b80cc2 72 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 73 $table->addField($field);
74
a8cb94f6 75 $field = new xmldb_field('item');
69b80cc2 76 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 77 $table->addField($field);
78
a8cb94f6 79 $field = new xmldb_field('completed');
69b80cc2 80 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 81 $table->addField($field);
82
a8cb94f6 83 $field = new xmldb_field('tmp_completed');
69b80cc2 84 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 85 $table->addField($field);
86
a8cb94f6 87 $field = new xmldb_field('value');
69b80cc2 88 $field->set_attributes(XMLDB_TYPE_TEXT, null, null, null, false, null, null, '', null);
c70ad9f7 89 $table->addField($field);
90
a8cb94f6 91 $key = new xmldb_key('PRIMARY');
69b80cc2 92 $key->set_attributes(XMLDB_KEY_PRIMARY, array('id'));
c70ad9f7 93 $table->addKey($key);
94
a8cb94f6 95 $key = new xmldb_key('feedback');
69b80cc2 96 $key->set_attributes(XMLDB_KEY_FOREIGN, array('item'), 'feedback_item', 'id');
c70ad9f7 97 $table->addKey($key);
98
f33e1ed4 99 $result = $result && $dbman->create_table($table);
c70ad9f7 100 ////////////////////////////////////////////////////////////
101 }
102
103 if ($result && $oldversion < 2007050504) {
104
105 /// Define field random_response to be added to feedback_completed
a8cb94f6 106 $table = new xmldb_table('feedback_completed');
69b80cc2 107 $field = new xmldb_field('random_response', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 108 /// Launch add field1
f33e1ed4 109 $result = $result && $dbman->add_field($table, $field);
c70ad9f7 110
111 /// Define field anonymous_response to be added to feedback_completed
a8cb94f6 112 $table = new xmldb_table('feedback_completed');
69b80cc2 113 $field = new xmldb_field('anonymous_response', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '1', null);
c70ad9f7 114 /// Launch add field2
f33e1ed4 115 $result = $result && $dbman->add_field($table, $field);
c70ad9f7 116
117 /// Define field random_response to be added to feedback_completed
a8cb94f6 118 $table = new xmldb_table('feedback_completedtmp');
69b80cc2 119 $field = new xmldb_field('random_response', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '0', null);
c70ad9f7 120 /// Launch add field1
f33e1ed4 121 $result = $result && $dbman->add_field($table, $field);
c70ad9f7 122
123 /// Define field anonymous_response to be added to feedback_completed
a8cb94f6 124 $table = new xmldb_table('feedback_completedtmp');
69b80cc2 125 $field = new xmldb_field('anonymous_response', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '1', null);
c70ad9f7 126 /// Launch add field2
f33e1ed4 127 $result = $result && $dbman->add_field($table, $field);
c70ad9f7 128
129 ////////////////////////////////////////////////////////////
130 }
131
132 if ($result && $oldversion < 2007102600) {
133 // public is a reserved word on Oracle
134
a8cb94f6 135 $table = new xmldb_table('feedback_template');
69b80cc2 136 $field = new xmldb_field('ispublic', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, false, null, null, '1', null);
f33e1ed4 137 if (!$dbman->field_exists($table, $field)) {
138 $result = $result && $dbman->add_field($table, $field);
c70ad9f7 139 }
140 }
141
6ee09cfe 142 if ($result && $oldversion < 2008042400) { //New version in version.php
f33e1ed4 143 if ($all_nonanonymous_feedbacks = $DB->get_records('feedback', 'anonymous', 2)) {
144 $update_sql = 'UPDATE {feedback_completed} SET anonymous_response = 2 WHERE feedback = ';
6ee09cfe 145 foreach ($all_nonanonymous_feedbacks as $fb) {
f33e1ed4 146 $result = $result && $DB->execute($update_sql.$fb->id);
6ee09cfe 147 }
148 }
149 }
150
151 if ($result && $oldversion < 2008042401) { //New version in version.php
f33e1ed4 152 if ($result) {
153 $concat_radio = $DB->sql_concat("'r>>>>>'",'presentation');
bfc59a67 154 $concat_check = $DB->sql_concat("'c>>>>>'",'presentation');
155 $concat_dropdown = $DB->sql_concat("'d>>>>>'",'presentation');
64da83c6 156
f33e1ed4 157 $update_sql1 = "UPDATE {feedback_item} SET presentation = ".$concat_radio." WHERE typ IN('radio','radiorated')";
158 $update_sql2 = "UPDATE {feedback_item} SET presentation = ".$concat_dropdown." WHERE typ IN('dropdown','dropdownrated')";
159 $update_sql3 = "UPDATE {feedback_item} SET presentation = ".$concat_check." WHERE typ = 'check'";
64da83c6 160
f33e1ed4 161 $result = $result && $DB->execute($update_sql1);
162 $result = $result && $DB->execute($update_sql2);
8b54b5cd 163 $result = $result && $DB->execute($update_sql3);
6ee09cfe 164 }
f33e1ed4 165 if ($result) {
166 $update_sql1 = "UPDATE {feedback_item} SET typ = 'multichoice' WHERE typ IN('radio','check','dropdown')";
167 $update_sql2 = "UPDATE {feedback_item} SET typ = 'multichoicerated' WHERE typ IN('radiorated','dropdownrated')";
168 $result = $result && $DB->execute($update_sql1);
169 $result = $result && $DB->execute($update_sql2);
6ee09cfe 170 }
171 }
172
a78b88e7 173 if ($result && $oldversion < 2008042801) {
62a0ae10 174 $new_log_display = new object();
175 $new_log_display->module = 'feedback';
176 $new_log_display->action = 'startcomplete';
177 $new_log_display->mtable = 'feedback';
178 $new_log_display->field = 'name';
f33e1ed4 179 $result = $result && $DB->insert_record('log_display', $new_log_display);
62a0ae10 180
181 $new_log_display = clone($new_log_display);
182 $new_log_display->action = 'submit';
f33e1ed4 183 $result = $result && $DB->insert_record('log_display', $new_log_display);
62a0ae10 184
185 $new_log_display = clone($new_log_display);
186 $new_log_display->action = 'delete';
f33e1ed4 187 $result = $result && $DB->insert_record('log_display', $new_log_display);
62a0ae10 188
189 $new_log_display = clone($new_log_display);
190 $new_log_display->action = 'view';
f33e1ed4 191 $result = $result && $DB->insert_record('log_display', $new_log_display);
62a0ae10 192
193 $new_log_display = clone($new_log_display);
194 $new_log_display->action = 'view all';
195 $new_log_display->mtable = 'course';
196 $new_log_display->field = 'shortname';
f33e1ed4 197 $result = $result && $DB->insert_record('log_display', $new_log_display);
a78b88e7 198 }
c70ad9f7 199
efc59167 200 if ($result && $oldversion < 2008042900) {
201 /// Define field autonumbering to be added to feedback
a8cb94f6 202 $table = new xmldb_table('feedback');
69b80cc2 203 $field = new xmldb_field('autonumbering', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'multiple_submit');
efc59167 204 /// Launch add field2
f33e1ed4 205 $result = $result && $dbman->add_field($table, $field);
efc59167 206 }
c184660d 207
208 if ($result && $oldversion < 2008050104) {
209 /// Define field site_after_submit to be added to feedback
a8cb94f6 210 $table = new xmldb_table('feedback');
69b80cc2 211 $field = new xmldb_field('site_after_submit', XMLDB_TYPE_CHAR, '255', null, null, false, null, null, '', 'autonumbering');
c184660d 212 /// Launch add field2
f33e1ed4 213 $result = $result && $dbman->add_field($table, $field);
c184660d 214 }
239ba4ee 215
216 if ($result && $oldversion < 2008050105) {
217 //field count is not more needed
a8cb94f6 218 $table = new xmldb_table('feedback_tracking');
219 $field = new xmldb_field('count');
239ba4ee 220 $result = $result && $dbman->drop_field($table, $field);
221 }
c70ad9f7 222 return $result;
223}
224
225?>