1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* The file containing the upgrade functions.
|
|
|
19 |
* @package mod_questionnaire
|
|
|
20 |
* @copyright 2016 Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
21 |
* @author Mike Churchward
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* The module upgrade function.
|
|
|
27 |
* @param int $oldversion
|
|
|
28 |
* @return bool
|
|
|
29 |
*/
|
|
|
30 |
function xmldb_questionnaire_upgrade($oldversion=0) {
|
|
|
31 |
global $CFG, $DB;
|
|
|
32 |
|
|
|
33 |
$dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
|
|
|
34 |
|
|
|
35 |
$result = true;
|
|
|
36 |
|
|
|
37 |
if ($oldversion < 2007120101) {
|
|
|
38 |
$result &= questionnaire_upgrade_2007120101();
|
|
|
39 |
|
|
|
40 |
// Questionnaire savepoint reached.
|
|
|
41 |
upgrade_mod_savepoint(true, 2007120101, 'questionnaire');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
if ($oldversion < 2007120102) {
|
|
|
45 |
// Change enum values to lower case for all tables using them.
|
|
|
46 |
$table = new xmldb_table('questionnaire_question');
|
|
|
47 |
|
|
|
48 |
$field = new xmldb_field('required');
|
|
|
49 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
50 |
$dbman->change_field_enum($table, $field);
|
|
|
51 |
$DB->set_field('questionnaire_question', 'required', 'y', array('required' => 'Y'));
|
|
|
52 |
$DB->set_field('questionnaire_question', 'required', 'n', array('required' => 'N'));
|
|
|
53 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'n');
|
|
|
54 |
$dbman->change_field_enum($table, $field);
|
|
|
55 |
$dbman->change_field_default($table, $field);
|
|
|
56 |
unset($field);
|
|
|
57 |
|
|
|
58 |
$field = new xmldb_field('deleted');
|
|
|
59 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
60 |
$dbman->change_field_enum($table, $field);
|
|
|
61 |
$DB->set_field('questionnaire_question', 'deleted', 'y', array('deleted' => 'Y'));
|
|
|
62 |
$DB->set_field('questionnaire_question', 'deleted', 'n', array('deleted' => 'N'));
|
|
|
63 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'n');
|
|
|
64 |
$dbman->change_field_enum($table, $field);
|
|
|
65 |
$dbman->change_field_default($table, $field);
|
|
|
66 |
unset($field);
|
|
|
67 |
|
|
|
68 |
$field = new xmldb_field('public');
|
|
|
69 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
70 |
$dbman->change_field_enum($table, $field);
|
|
|
71 |
$DB->set_field('questionnaire_question', 'public', 'y', array('public' => 'Y'));
|
|
|
72 |
$DB->set_field('questionnaire_question', 'public', 'n', array('public' => 'N'));
|
|
|
73 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y');
|
|
|
74 |
$dbman->change_field_enum($table, $field);
|
|
|
75 |
$dbman->change_field_default($table, $field);
|
|
|
76 |
unset($field);
|
|
|
77 |
|
|
|
78 |
unset($table);
|
|
|
79 |
|
|
|
80 |
$table = new xmldb_table('questionnaire_question_type');
|
|
|
81 |
|
|
|
82 |
$field = new xmldb_field('has_choices');
|
|
|
83 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
84 |
$dbman->change_field_enum($table, $field);
|
|
|
85 |
$DB->set_field('questionnaire_question_type', 'has_choices', 'y', array('has_choices' => 'Y'));
|
|
|
86 |
$DB->set_field('questionnaire_question_type', 'has_choices', 'n', array('has_choices' => 'N'));
|
|
|
87 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y');
|
|
|
88 |
$dbman->change_field_enum($table, $field);
|
|
|
89 |
$dbman->change_field_default($table, $field);
|
|
|
90 |
unset($field);
|
|
|
91 |
|
|
|
92 |
unset($table);
|
|
|
93 |
|
|
|
94 |
$table = new xmldb_table('questionnaire_response');
|
|
|
95 |
|
|
|
96 |
$field = new xmldb_field('complete');
|
|
|
97 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
98 |
$dbman->change_field_enum($table, $field);
|
|
|
99 |
$DB->set_field('questionnaire_response', 'complete', 'y', array('complete' => 'Y'));
|
|
|
100 |
$DB->set_field('questionnaire_response', 'complete', 'n', array('complete' => 'N'));
|
|
|
101 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'n');
|
|
|
102 |
$dbman->change_field_enum($table, $field);
|
|
|
103 |
$dbman->change_field_default($table, $field);
|
|
|
104 |
unset($field);
|
|
|
105 |
|
|
|
106 |
unset($table);
|
|
|
107 |
|
|
|
108 |
$table = new xmldb_table('questionnaire_response_bool');
|
|
|
109 |
|
|
|
110 |
$field = new xmldb_field('choice_id');
|
|
|
111 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
112 |
$dbman->change_field_enum($table, $field);
|
|
|
113 |
$DB->set_field('questionnaire_response_bool', 'choice_id', 'y', array('choice_id' => 'Y'));
|
|
|
114 |
$DB->set_field('questionnaire_response_bool', 'choice_id', 'n', array('choice_id' => 'N'));
|
|
|
115 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y');
|
|
|
116 |
$dbman->change_field_enum($table, $field);
|
|
|
117 |
$dbman->change_field_default($table, $field);
|
|
|
118 |
unset($field);
|
|
|
119 |
|
|
|
120 |
unset($table);
|
|
|
121 |
|
|
|
122 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
123 |
|
|
|
124 |
$field = new xmldb_field('public');
|
|
|
125 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, false, null, 'n');
|
|
|
126 |
$dbman->change_field_enum($table, $field);
|
|
|
127 |
$DB->set_field('questionnaire_survey', 'public', 'y', array('public' => 'Y'));
|
|
|
128 |
$DB->set_field('questionnaire_survey', 'public', 'n', array('public' => 'N'));
|
|
|
129 |
$field->set_attributes(XMLDB_TYPE_CHAR, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, XMLDB_ENUM, array('y', 'n'), 'y');
|
|
|
130 |
$dbman->change_field_enum($table, $field);
|
|
|
131 |
$dbman->change_field_default($table, $field);
|
|
|
132 |
unset($field);
|
|
|
133 |
|
|
|
134 |
// Upgrade question_type table with corrected 'response_table' fields.
|
|
|
135 |
$DB->set_field('questionnaire_question_type', 'response_table', 'resp_single',
|
|
|
136 |
array('response_table' => 'response_single'));
|
|
|
137 |
$DB->set_field('questionnaire_question_type', 'response_table', 'resp_multiple',
|
|
|
138 |
array('response_table' => 'response_multiple'));
|
|
|
139 |
|
|
|
140 |
// Questionnaire savepoint reached..
|
|
|
141 |
upgrade_mod_savepoint(true, 2007120102, 'questionnaire');
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
if ($oldversion < 2008031902) {
|
|
|
145 |
$table = new xmldb_table('questionnaire');
|
|
|
146 |
$field = new xmldb_field('grade');
|
|
|
147 |
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', false, true, false, false, null, 0, 'navigate');
|
|
|
148 |
$dbman->add_field($table, $field);
|
|
|
149 |
|
|
|
150 |
unset($field);
|
|
|
151 |
unset($table);
|
|
|
152 |
$table = new xmldb_table('questionnaire_response');
|
|
|
153 |
$field = new xmldb_field('grade');
|
|
|
154 |
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', false, true, false, false, null, 0, 'complete');
|
|
|
155 |
$dbman->add_field($table, $field);
|
|
|
156 |
|
|
|
157 |
// Questionnaire savepoint reached.
|
|
|
158 |
upgrade_mod_savepoint(true, 2008031902, 'questionnaire');
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
if ($oldversion < 2008031904) {
|
|
|
162 |
$sql = "SELECT q.id, q.resp_eligible, q.resp_view, cm.id as cmid
|
|
|
163 |
FROM {questionnaire} q, {course_modules} cm, {modules} m
|
|
|
164 |
WHERE m.name='questionnaire' AND m.id=cm.module AND cm.instance=q.id";
|
|
|
165 |
if ($rs = $DB->get_recordset_sql($sql)) {
|
|
|
166 |
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
|
|
|
167 |
$editteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
|
|
|
168 |
$teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
|
|
|
169 |
$capview = 'mod/questionnaire:view';
|
|
|
170 |
$capsubmit = 'mod/questionnaire:submit';
|
|
|
171 |
|
|
|
172 |
foreach ($rs as $questionnaire) {
|
|
|
173 |
$context = get_context_instance(CONTEXT_MODULE, $questionnaire->cmid);
|
|
|
174 |
|
|
|
175 |
// Convert questionnaires with resp_eligible = 'all' so that students & teachers have view and submit.
|
|
|
176 |
if ($questionnaire->resp_eligible == 'all') {
|
|
|
177 |
assign_capability($capsubmit, CAP_ALLOW, $editteacherroleid, $context->id, true);
|
|
|
178 |
assign_capability($capsubmit, CAP_ALLOW, $teacherroleid, $context->id, true);
|
|
|
179 |
// Convert questionnaires with resp_eligible = 'students' so that just students have view and submit.
|
|
|
180 |
} else if ($questionnaire->resp_eligible == 'teachers') {
|
|
|
181 |
assign_capability($capsubmit, CAP_ALLOW, $editteacherroleid, $context->id, true);
|
|
|
182 |
assign_capability($capsubmit, CAP_ALLOW, $teacherroleid, $context->id, true);
|
|
|
183 |
assign_capability($capview, CAP_PREVENT, $studentroleid, $context->id, true);
|
|
|
184 |
assign_capability($capsubmit, CAP_PREVENT, $studentroleid, $context->id, true);
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
$rs->close();
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
// Questionnaire savepoint reached.
|
|
|
191 |
upgrade_mod_savepoint(true, 2008031904, 'questionnaire');
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
if ($oldversion < 2008031905) {
|
|
|
195 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
196 |
$field = new xmldb_field('changed');
|
|
|
197 |
$dbman->drop_field($table, $field);
|
|
|
198 |
|
|
|
199 |
// Questionnaire savepoint reached.
|
|
|
200 |
upgrade_mod_savepoint(true, 2008031905, 'questionnaire');
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
if ($oldversion < 2008031906) {
|
|
|
204 |
$table = new xmldb_table('questionnaire_response_rank');
|
|
|
205 |
$field = new xmldb_field('rank');
|
|
|
206 |
$field->set_attributes(XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, '0', 'choice_id');
|
|
|
207 |
$field->setUnsigned(false);
|
|
|
208 |
$dbman->change_field_unsigned($table, $field);
|
|
|
209 |
|
|
|
210 |
// Questionnaire savepoint reached.
|
|
|
211 |
upgrade_mod_savepoint(true, 2008031906, 'questionnaire');
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if ($oldversion < 2008060401) {
|
|
|
215 |
$table = new xmldb_table('questionnaire_question');
|
|
|
216 |
$field = new xmldb_field('name');
|
|
|
217 |
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null, 'survey_id');
|
|
|
218 |
$field->setNotnull(false);
|
|
|
219 |
$dbman->change_field_notnull($table, $field);
|
|
|
220 |
|
|
|
221 |
// Questionnaire savepoint reached.
|
|
|
222 |
upgrade_mod_savepoint(true, 2008060401, 'questionnaire');
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
if ($oldversion < 2008070702) {
|
|
|
226 |
$table = new xmldb_table('questionnaire_question_type');
|
|
|
227 |
$field = new xmldb_field('response_table');
|
|
|
228 |
$field->set_attributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null, null, 'has_choices');
|
|
|
229 |
$field->setNotnull(false);
|
|
|
230 |
$dbman->change_field_notnull($table, $field);
|
|
|
231 |
|
|
|
232 |
// Questionnaire savepoint reached.
|
|
|
233 |
upgrade_mod_savepoint(true, 2008070702, 'questionnaire');
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
if ($oldversion < 2008070703) {
|
|
|
237 |
$table = new xmldb_table('questionnaire_resp_multiple');
|
|
|
238 |
$index = new xmldb_index('response_question');
|
|
|
239 |
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('response_id', 'question_id', 'choice_id'));
|
|
|
240 |
if (!$dbman->index_exists($table, $index)) {
|
|
|
241 |
$dbman->add_index($table, $index);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
// Questionnaire savepoint reached.
|
|
|
245 |
upgrade_mod_savepoint(true, 2008070703, 'questionnaire');
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
if ($oldversion < 2008070704) {
|
|
|
249 |
// CONTRIB-1542.
|
|
|
250 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
251 |
$field = new xmldb_field('email');
|
|
|
252 |
$field->set_attributes(XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, null, null, 'title');
|
|
|
253 |
$field->setLength('255');
|
|
|
254 |
$dbman->change_field_precision($table, $field);
|
|
|
255 |
|
|
|
256 |
// Questionnaire savepoint reached.
|
|
|
257 |
upgrade_mod_savepoint(true, 2008070704, 'questionnaire');
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
if ($oldversion < 2008070705) {
|
|
|
261 |
// Rename summary field to 'intro' to adhere to new Moodle standard.
|
|
|
262 |
$table = new xmldb_table('questionnaire');
|
|
|
263 |
$field = new xmldb_field('summary');
|
|
|
264 |
$field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null, 'name');
|
|
|
265 |
$dbman->rename_field($table, $field, 'intro');
|
|
|
266 |
|
|
|
267 |
// Add 'introformat' to adhere to new Moodle standard.
|
|
|
268 |
$field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
|
|
|
269 |
$dbman->add_field($table, $field);
|
|
|
270 |
// Set all existing records to HTML format.
|
|
|
271 |
$DB->set_field('questionnaire', 'introformat', 1);
|
|
|
272 |
|
|
|
273 |
// Questionnaire savepoint reached.
|
|
|
274 |
upgrade_mod_savepoint(true, 2008070705, 'questionnaire');
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
if ($oldversion < 2008070706) {
|
|
|
278 |
// CONTRIB-1153.
|
|
|
279 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
280 |
$field = new xmldb_field('public');
|
|
|
281 |
if ($dbman->field_exists($table, $field)) {
|
|
|
282 |
$dbman->drop_field($table, $field);
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
$table = new xmldb_table('questionnaire_question');
|
|
|
286 |
$field = new xmldb_field('public');
|
|
|
287 |
if ($dbman->field_exists($table, $field)) {
|
|
|
288 |
$dbman->drop_field($table, $field);
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
// Questionnaire savepoint reached.
|
|
|
292 |
upgrade_mod_savepoint(true, 2008070706, 'questionnaire');
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
if ($oldversion < 2010110100) {
|
|
|
296 |
// Drop list of values (enum) from field has_choices on table questionnaire_question_type.
|
|
|
297 |
$table = new xmldb_table('questionnaire_question_type');
|
|
|
298 |
$field = new xmldb_field('has_choices', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, 'y', 'type');
|
|
|
299 |
|
|
|
300 |
// Launch drop of list of values from field has_choices.
|
|
|
301 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
302 |
|
|
|
303 |
// Questionnaire savepoint reached.
|
|
|
304 |
upgrade_mod_savepoint(true, 2010110100, 'questionnaire');
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
if ($oldversion < 2010110101) {
|
|
|
308 |
// Drop list of values (enum) from field respondenttype on table questionnaire.
|
|
|
309 |
$table = new xmldb_table('questionnaire');
|
|
|
310 |
$field = new xmldb_field('respondenttype', XMLDB_TYPE_CHAR, '9', null, XMLDB_NOTNULL, null, 'fullname', 'qtype');
|
|
|
311 |
// Launch drop of list of values from field respondenttype.
|
|
|
312 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
313 |
// Drop list of values (enum) from field resp_eligible on table questionnaire.
|
|
|
314 |
$field = new xmldb_field('resp_eligible', XMLDB_TYPE_CHAR, '8', null, XMLDB_NOTNULL, null, 'all', 'respondenttype');
|
|
|
315 |
// Launch drop of list of values from field resp_eligible.
|
|
|
316 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
317 |
|
|
|
318 |
// Drop list of values (enum) from field required on table questionnaire_question.
|
|
|
319 |
$table = new xmldb_table('questionnaire_question');
|
|
|
320 |
$field = new xmldb_field('required', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, 'n', 'content');
|
|
|
321 |
// Launch drop of list of values from field required.
|
|
|
322 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
323 |
// Drop list of values (enum) from field deleted on table questionnaire_question.
|
|
|
324 |
$field = new xmldb_field('deleted', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, 'n', 'required');
|
|
|
325 |
// Launch drop of list of values from field deleted.
|
|
|
326 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
327 |
|
|
|
328 |
// Drop list of values (enum) from field complete on table questionnaire_response.
|
|
|
329 |
$table = new xmldb_table('questionnaire_response');
|
|
|
330 |
$field = new xmldb_field('complete', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, 'n', 'submitted');
|
|
|
331 |
// Launch drop of list of values from field complete.
|
|
|
332 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
333 |
|
|
|
334 |
// Drop list of values (enum) from field choice_id on table questionnaire_response_bool.
|
|
|
335 |
$table = new xmldb_table('questionnaire_response_bool');
|
|
|
336 |
$field = new xmldb_field('choice_id', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, 'y', 'question_id');
|
|
|
337 |
// Launch drop of list of values from field choice_id.
|
|
|
338 |
$dbman->drop_enum_from_field($table, $field);
|
|
|
339 |
|
|
|
340 |
// Questionnaire savepoint reached.
|
|
|
341 |
upgrade_mod_savepoint(true, 2010110101, 'questionnaire');
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
if ($oldversion < 2012100800) {
|
|
|
345 |
// Changing precision of field name on table questionnaire_survey to (255).
|
|
|
346 |
|
|
|
347 |
// First drop the index.
|
|
|
348 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
349 |
$index = new xmldb_index('name');
|
|
|
350 |
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('name'));
|
|
|
351 |
$dbman->drop_index($table, $index);
|
|
|
352 |
|
|
|
353 |
// Launch change of precision for field name.
|
|
|
354 |
$field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
|
|
|
355 |
$dbman->change_field_precision($table, $field);
|
|
|
356 |
|
|
|
357 |
// Add back in the index.
|
|
|
358 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
359 |
$index = new xmldb_index('name');
|
|
|
360 |
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('name'));
|
|
|
361 |
$dbman->add_index($table, $index);
|
|
|
362 |
|
|
|
363 |
// Questionnaire savepoint reached.
|
|
|
364 |
upgrade_mod_savepoint(true, 2012100800, 'questionnaire');
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
if ($oldversion < 2013062302) {
|
|
|
368 |
// Adding completionsubmit field to table questionnaire.
|
|
|
369 |
|
|
|
370 |
$table = new xmldb_table('questionnaire');
|
|
|
371 |
$field = new xmldb_field('completionsubmit', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timemodified');
|
|
|
372 |
|
|
|
373 |
// Conditionally launch add field.
|
|
|
374 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
375 |
$dbman->add_field($table, $field);
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
// Questionnaire savepoint reached.
|
|
|
379 |
upgrade_mod_savepoint(true, 2013062302, 'questionnaire');
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
if ($oldversion < 2013062501) {
|
|
|
383 |
// Skip logic new feature.
|
|
|
384 |
// Define field dependquestion to be added to questionnaire_question table.
|
|
|
385 |
$table = new xmldb_table('questionnaire_question');
|
|
|
386 |
$field = new xmldb_field('dependquestion', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'deleted');
|
|
|
387 |
|
|
|
388 |
// Conditionally launch add field.
|
|
|
389 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
390 |
$dbman->add_field($table, $field);
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
$table = new xmldb_table('questionnaire_question');
|
|
|
394 |
$field = new xmldb_field('dependchoice', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'dependquestion');
|
|
|
395 |
|
|
|
396 |
// Conditionally launch add field.
|
|
|
397 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
398 |
$dbman->add_field($table, $field);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
// Replace the = separator with :: separator in quest_choice content.
|
|
|
402 |
// This fixes radio button options using old "value"="display" formats.
|
|
|
403 |
require_once($CFG->dirroot.'/mod/questionnaire/locallib.php');
|
|
|
404 |
$choices = $DB->get_recordset('questionnaire_quest_choice', null);
|
|
|
405 |
$total = $DB->count_records('questionnaire_quest_choice');
|
|
|
406 |
if ($total > 0) {
|
|
|
407 |
$pbar = new progress_bar('convertchoicevalues', 500, true);
|
|
|
408 |
$i = 1;
|
|
|
409 |
foreach ($choices as $choice) {
|
|
|
410 |
if (($choice->value == null || $choice->value == 'NULL')
|
|
|
411 |
&& !preg_match("/^([0-9]{1,3}=.*|!other=.*)$/", $choice->content)) {
|
|
|
412 |
$content = questionnaire_choice_values($choice->content);
|
|
|
413 |
if (strpos($content->text, '=')) {
|
|
|
414 |
$newcontent = str_replace('=', '::', $content->text);
|
|
|
415 |
$choice->content = $newcontent;
|
|
|
416 |
$DB->update_record('questionnaire_quest_choice', $choice);
|
|
|
417 |
}
|
|
|
418 |
}
|
|
|
419 |
$pbar->update($i, $total, "Convert questionnaire choice value separator - $i/$total.");
|
|
|
420 |
$i++;
|
|
|
421 |
}
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
// Questionnaire savepoint reached.
|
|
|
425 |
upgrade_mod_savepoint(true, 2013062501, 'questionnaire');
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
if ($oldversion < 2013100500) {
|
|
|
429 |
// Add autonumbering option for questions and pages.
|
|
|
430 |
$table = new xmldb_table('questionnaire');
|
|
|
431 |
$field = new xmldb_field('autonum', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '3', 'completionsubmit');
|
|
|
432 |
|
|
|
433 |
// Conditionally launch add field.
|
|
|
434 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
435 |
$dbman->add_field($table, $field);
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
// Questionnaire savepoint reached.
|
|
|
439 |
upgrade_mod_savepoint(true, 2013100500, 'questionnaire');
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
if ($oldversion < 2013122202) {
|
|
|
443 |
// Personality test feature.
|
|
|
444 |
|
|
|
445 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
446 |
$field = new xmldb_field('feedbacksections', XMLDB_TYPE_INTEGER, '2', null, null, null, null, null);
|
|
|
447 |
// Conditionally launch add field.
|
|
|
448 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
449 |
$dbman->add_field($table, $field);
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
unset($field);
|
|
|
453 |
$field = new xmldb_field('feedbacknotes', XMLDB_TYPE_TEXT, null, null, null, null, null);
|
|
|
454 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
455 |
$dbman->add_field($table, $field);
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
unset($table);
|
|
|
459 |
unset($field);
|
|
|
460 |
|
|
|
461 |
// Define table questionnaire_fb_sections to be created.
|
|
|
462 |
$table = new xmldb_table('questionnaire_fb_sections');
|
|
|
463 |
$table->add_field('id', XMLDB_TYPE_INTEGER, '18', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
|
|
464 |
$table->add_field('survey_id', XMLDB_TYPE_INTEGER, '18', null, XMLDB_NOTNULL, null, null);
|
|
|
465 |
$table->add_field('section', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
|
|
|
466 |
$table->add_field('scorecalculation', XMLDB_TYPE_TEXT, null, null, null, null, null);
|
|
|
467 |
$table->add_field('sectionlabel', XMLDB_TYPE_CHAR, '50', null, null, null, null);
|
|
|
468 |
$table->add_field('sectionheading', XMLDB_TYPE_TEXT, null, null, null, null, null);
|
|
|
469 |
$table->add_field('sectionheadingformat', XMLDB_TYPE_INTEGER, '2', null, null, null, '1');
|
|
|
470 |
|
|
|
471 |
// Adding keys to table questionnaire_fb_sections.
|
|
|
472 |
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
|
|
473 |
|
|
|
474 |
// Conditionally launch create table for assign_user_mapping.
|
|
|
475 |
if (!$dbman->table_exists($table)) {
|
|
|
476 |
$dbman->create_table($table);
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
unset($table);
|
|
|
480 |
|
|
|
481 |
// Define table questionnaire_feedback to be created.
|
|
|
482 |
$table = new xmldb_table('questionnaire_feedback');
|
|
|
483 |
$table->add_field('id', XMLDB_TYPE_INTEGER, '18', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
|
|
484 |
$table->add_field('section_id', XMLDB_TYPE_INTEGER, '18', null, XMLDB_NOTNULL, null, null);
|
|
|
485 |
$table->add_field('feedbacklabel', XMLDB_TYPE_CHAR, '30', null, null, null, null);
|
|
|
486 |
$table->add_field('feedbacktext', XMLDB_TYPE_TEXT, null, null, null, null, null);
|
|
|
487 |
$table->add_field('feedbacktextformat', XMLDB_TYPE_INTEGER, '2', null, null, null, '1');
|
|
|
488 |
$table->add_field('minscore', XMLDB_TYPE_NUMBER, '10,5', null, null, null, '0.00000');
|
|
|
489 |
$table->add_field('maxscore', XMLDB_TYPE_NUMBER, '10,5', null, null, null, '101.00000');
|
|
|
490 |
|
|
|
491 |
// Adding keys to table questionnaire_fb_sections.
|
|
|
492 |
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
|
|
493 |
|
|
|
494 |
// Conditionally launch create table for assign_user_mapping.
|
|
|
495 |
if (!$dbman->table_exists($table)) {
|
|
|
496 |
$dbman->create_table($table);
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
// Questionnaire savepoint reached.
|
|
|
500 |
upgrade_mod_savepoint(true, 2013122202, 'questionnaire');
|
|
|
501 |
}
|
|
|
502 |
|
|
|
503 |
if ($oldversion < 2014010300) {
|
|
|
504 |
// Personality test with chart.
|
|
|
505 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
506 |
$field = new xmldb_field('chart_type', XMLDB_TYPE_CHAR, '64', null, null, null, null, null);
|
|
|
507 |
|
|
|
508 |
// Conditionally launch add field.
|
|
|
509 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
510 |
$dbman->add_field($table, $field);
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
$field = new xmldb_field('feedbackscores', XMLDB_TYPE_INTEGER, '1', null, null, null, '0');
|
|
|
514 |
|
|
|
515 |
// Conditionally launch add field.
|
|
|
516 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
517 |
$dbman->add_field($table, $field);
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
// Questionnaire savepoint reached.
|
|
|
521 |
upgrade_mod_savepoint(true, 2014010300, 'questionnaire');
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
if ($oldversion < 2015051101) {
|
|
|
525 |
// Move the global config value for 'usergraph' to the plugin config setting instead.
|
|
|
526 |
if (isset($CFG->questionnaire_usergraph)) {
|
|
|
527 |
set_config('usergraph', $CFG->questionnaire_usergraph, 'questionnaire');
|
|
|
528 |
unset_config('questionnaire_usergraph');
|
|
|
529 |
}
|
|
|
530 |
upgrade_mod_savepoint(true, 2015051101, 'questionnaire');
|
|
|
531 |
}
|
|
|
532 |
|
|
|
533 |
// Add index to reduce load on the questionnaire_quest_choice table.
|
|
|
534 |
if ($oldversion < 2015051102) {
|
|
|
535 |
// Conditionally add an index to the question_id field.
|
|
|
536 |
$table = new xmldb_table('questionnaire_quest_choice');
|
|
|
537 |
$index = new xmldb_index('quest_choice_quesidx', XMLDB_INDEX_NOTUNIQUE, array('question_id'));
|
|
|
538 |
// Only add the index if it does not exist.
|
|
|
539 |
if (!$dbman->index_exists($table, $index)) {
|
|
|
540 |
$dbman->add_index($table, $index);
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
// Questionnaire savepoint reached.
|
|
|
544 |
upgrade_mod_savepoint(true, 2015051102, 'questionnaire');
|
|
|
545 |
}
|
|
|
546 |
|
|
|
547 |
// Ensuring database matches XML state for some known anomalies.
|
|
|
548 |
if ($oldversion < 2016020204) {
|
|
|
549 |
// Ensure the feedbackscores field can be null (CONTRIB-6445).
|
|
|
550 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
551 |
$field = new xmldb_field('feedbackscores', XMLDB_TYPE_INTEGER, '1', null, null, null, '0');
|
|
|
552 |
$dbman->change_field_notnull($table, $field);
|
|
|
553 |
|
|
|
554 |
// Ensure the feddbacklabel field is 50 characters (CONTRIB-6445).
|
|
|
555 |
$table = new xmldb_table('questionnaire_feedback');
|
|
|
556 |
$field = new xmldb_field('feedbacklabel', XMLDB_TYPE_CHAR, '50', null, null, null, null);
|
|
|
557 |
$dbman->change_field_precision($table, $field);
|
|
|
558 |
|
|
|
559 |
// Ensure the response field is text.
|
|
|
560 |
$table = new xmldb_table('questionnaire_response_date');
|
|
|
561 |
$field = new xmldb_field('response', XMLDB_TYPE_TEXT, null, null, null, null, null);
|
|
|
562 |
$dbman->change_field_precision($table, $field);
|
|
|
563 |
|
|
|
564 |
// Questionnaire savepoint reached.
|
|
|
565 |
upgrade_mod_savepoint(true, 2016020204, 'questionnaire');
|
|
|
566 |
}
|
|
|
567 |
|
|
|
568 |
// Ensuring database matches XML state for some known anomalies.
|
|
|
569 |
if ($oldversion < 2016111105) {
|
|
|
570 |
$table = new xmldb_table('questionnaire');
|
|
|
571 |
$field = new xmldb_field('notifications', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'resp_view');
|
|
|
572 |
|
|
|
573 |
// Conditionally launch add field.
|
|
|
574 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
575 |
$dbman->add_field($table, $field);
|
|
|
576 |
}
|
|
|
577 |
|
|
|
578 |
// Questionnaire savepoint reached.
|
|
|
579 |
upgrade_mod_savepoint(true, 2016111105, 'questionnaire');
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
// Redoing the 2017050100 upgrade in 2017050101. If it already completed in 2017050100, skip it.
|
|
|
583 |
if ($oldversion < 2017050101) {
|
|
|
584 |
// Changing type of field username from char to int.
|
|
|
585 |
$table = new xmldb_table('questionnaire_response');
|
|
|
586 |
$field = new xmldb_field('username', XMLDB_TYPE_INTEGER, '10');
|
|
|
587 |
// If it already completed in 2017050100, skip it.
|
|
|
588 |
if ($dbman->field_exists($table, $field)) {
|
|
|
589 |
// Before we change the field 'username' to an int, ensure there are only numeric values there.
|
|
|
590 |
$sql = 'SELECT qr.id, qr.username, qa.rid, qa.userid ' .
|
|
|
591 |
'FROM {questionnaire_response} qr ' .
|
|
|
592 |
'INNER JOIN {questionnaire_attempts} qa ON qr.id = qa.rid ' .
|
|
|
593 |
'WHERE qr.username = ?';
|
|
|
594 |
$rs = $DB->get_recordset_sql($sql, ["Anonymous"]);
|
|
|
595 |
// Set all "Anonymous" records to the userid in the matching attempt record.
|
|
|
596 |
foreach ($rs as $record) {
|
|
|
597 |
$DB->set_field('questionnaire_response', 'username', "{$record->userid}", ['id' => $record->id]);
|
|
|
598 |
}
|
|
|
599 |
// If there are any leftover "Anonymous" records, set them all to userid zero (there shouldn't be).
|
|
|
600 |
$rs = $DB->get_recordset('questionnaire_response', ['username' => 'Anonymous']);
|
|
|
601 |
foreach ($rs as $record) {
|
|
|
602 |
$DB->set_field('questionnaire_response', 'username', '0', ['id' => $record->id]);
|
|
|
603 |
}
|
|
|
604 |
|
|
|
605 |
// Launch change of type for field username.
|
|
|
606 |
$dbman->change_field_type($table, $field);
|
|
|
607 |
|
|
|
608 |
// Change the name from username to userid.
|
|
|
609 |
$dbman->rename_field($table, $field, 'userid');
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
// Changing type of field owner from char to int.
|
|
|
613 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
614 |
$field = new xmldb_field('owner', XMLDB_TYPE_INTEGER, '10');
|
|
|
615 |
// If it already completed in 2017050100, skip it.
|
|
|
616 |
if ($dbman->field_exists($table, $field)) {
|
|
|
617 |
// Drop the old 'owner' index before modifying the field.
|
|
|
618 |
$index = new xmldb_index('owner', XMLDB_INDEX_NOTUNIQUE, ['owner']);
|
|
|
619 |
$dbman->drop_index($table, $index);
|
|
|
620 |
|
|
|
621 |
// Launch change of type for field owner.
|
|
|
622 |
$dbman->change_field_type($table, $field);
|
|
|
623 |
|
|
|
624 |
// Change the name from owner to courseid.
|
|
|
625 |
$dbman->rename_field($table, $field, 'courseid');
|
|
|
626 |
|
|
|
627 |
// Add the index back with the new name.
|
|
|
628 |
$index = new xmldb_index('courseid', XMLDB_INDEX_NOTUNIQUE, ['courseid']);
|
|
|
629 |
if (!$dbman->index_exists($table, $index)) {
|
|
|
630 |
$dbman->add_index($table, $index);
|
|
|
631 |
}
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
// Questionnaire savepoint reached.
|
|
|
635 |
upgrade_mod_savepoint(true, 2017050101, 'questionnaire');
|
|
|
636 |
}
|
|
|
637 |
|
|
|
638 |
// Converting to new dependency system.
|
|
|
639 |
if ($oldversion < 2017111101) {
|
|
|
640 |
// MOD Multiparent Advanceddependencies START.
|
|
|
641 |
// Define table questionnaire_dependency to be created.
|
|
|
642 |
$table = new xmldb_table('questionnaire_dependency');
|
|
|
643 |
|
|
|
644 |
// Adding fields to table questionnaire_depenencies.
|
|
|
645 |
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
|
|
646 |
$table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
647 |
$table->add_field('surveyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
|
|
|
648 |
$table->add_field('dependquestionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
649 |
$table->add_field('dependchoiceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
650 |
$table->add_field('dependlogic', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
|
|
|
651 |
$table->add_field('dependandor', XMLDB_TYPE_CHAR, '4', null, XMLDB_NOTNULL, null, null);
|
|
|
652 |
|
|
|
653 |
// Adding keys to table questionnaire_depenencies.
|
|
|
654 |
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
|
|
655 |
|
|
|
656 |
// Adding indexes to table questionnaire_dependency.
|
|
|
657 |
$table->add_index('quest_dependency_quesidx', XMLDB_INDEX_NOTUNIQUE, array('questionid'));
|
|
|
658 |
|
|
|
659 |
// Conditionally launch create table for questionnaire_dependencies.
|
|
|
660 |
if (!$dbman->table_exists($table)) {
|
|
|
661 |
$dbman->create_table($table);
|
|
|
662 |
|
|
|
663 |
// Copy all existing branching data into new branching structure.
|
|
|
664 |
$branchingrs = $DB->get_recordset_select('questionnaire_question', 'dependquestion > 0 AND deleted = \'n\'',
|
|
|
665 |
null, '', 'id, survey_id, dependquestion, dependchoice');
|
|
|
666 |
foreach ($branchingrs as $qid => $qinfo) {
|
|
|
667 |
$newrec = new stdClass();
|
|
|
668 |
$newrec->questionid = $qid;
|
|
|
669 |
$newrec->surveyid = $qinfo->survey_id;
|
|
|
670 |
$newrec->dependquestionid = $qinfo->dependquestion;
|
|
|
671 |
$newrec->dependchoiceid = $qinfo->dependchoice;
|
|
|
672 |
$newrec->dependlogic = 1; // Set to "answer given", previously the only option.
|
|
|
673 |
$newrec->dependandor = 'and'; // Not used previously.
|
|
|
674 |
$DB->insert_record('questionnaire_dependency', $newrec);
|
|
|
675 |
}
|
|
|
676 |
$branchingrs->close();
|
|
|
677 |
|
|
|
678 |
// After copying all old data, remove the unused fields.
|
|
|
679 |
$table = new xmldb_table('questionnaire_question');
|
|
|
680 |
$field1 = new xmldb_field('dependquestion');
|
|
|
681 |
$field2 = new xmldb_field('dependchoice');
|
|
|
682 |
if ($dbman->field_exists($table, $field1)) {
|
|
|
683 |
$dbman->drop_field($table, $field1);
|
|
|
684 |
}
|
|
|
685 |
if ($dbman->field_exists($table, $field2)) {
|
|
|
686 |
$dbman->drop_field($table, $field2);
|
|
|
687 |
}
|
|
|
688 |
// MOD Multiparent Advanceddependencies END.
|
|
|
689 |
|
|
|
690 |
// Add a new index for survey_id to the question table.
|
|
|
691 |
$index = new xmldb_index('quest_question_sididx', XMLDB_INDEX_NOTUNIQUE, ['survey_id', 'deleted']);
|
|
|
692 |
// Only add the index if it does not exist.
|
|
|
693 |
if (!$dbman->index_exists($table, $index)) {
|
|
|
694 |
$dbman->add_index($table, $index);
|
|
|
695 |
}
|
|
|
696 |
}
|
|
|
697 |
// Questionnaire savepoint reached.
|
|
|
698 |
upgrade_mod_savepoint(true, 2017111101, 'questionnaire');
|
|
|
699 |
}
|
|
|
700 |
|
|
|
701 |
// Converting to new dependency system.
|
|
|
702 |
if ($oldversion < 2017111103) {
|
|
|
703 |
|
|
|
704 |
// If these fields exist, possibly due to incorrect creation from a new install (see CONTRIB-7300), remove them.
|
|
|
705 |
$table = new xmldb_table('questionnaire_question');
|
|
|
706 |
$field1 = new xmldb_field('dependquestion');
|
|
|
707 |
$field2 = new xmldb_field('dependchoice');
|
|
|
708 |
if ($dbman->field_exists($table, $field1)) {
|
|
|
709 |
$dbman->drop_field($table, $field1);
|
|
|
710 |
}
|
|
|
711 |
if ($dbman->field_exists($table, $field2)) {
|
|
|
712 |
$dbman->drop_field($table, $field2);
|
|
|
713 |
}
|
|
|
714 |
|
|
|
715 |
// Questionnaire savepoint reached.
|
|
|
716 |
upgrade_mod_savepoint(true, 2017111103, 'questionnaire');
|
|
|
717 |
}
|
|
|
718 |
|
|
|
719 |
// Get rid of questionnaire_attempts table and migrate necessary data to the questionnaire_response table.
|
|
|
720 |
if ($oldversion < 2018050102) {
|
|
|
721 |
$table = new xmldb_table('questionnaire_response');
|
|
|
722 |
$field1 = new xmldb_field('questionnaireid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
|
|
|
723 |
$field2 = new xmldb_field('survey_id');
|
|
|
724 |
|
|
|
725 |
// Create the new questionnaireid field, if it doesn't already exist (it shouldn't).
|
|
|
726 |
if (!$dbman->field_exists($table, $field1)) {
|
|
|
727 |
$dbman->add_field($table, $field1);
|
|
|
728 |
}
|
|
|
729 |
|
|
|
730 |
// Get all of the attempts records, and add the questionnaire id to the corresponding response record.
|
|
|
731 |
$sql = 'UPDATE {questionnaire_response} qr ' .
|
|
|
732 |
'INNER JOIN {questionnaire_attempts} qa ON qr.id = qa.rid ' .
|
|
|
733 |
'SET qr.questionnaireid = qa.qid';
|
|
|
734 |
$DB->execute($sql, []);
|
|
|
735 |
|
|
|
736 |
// Get all of the response records with a '0' questionnaireid, and extract the questionnaireid from the survey_id field.
|
|
|
737 |
$rs = $DB->get_recordset('questionnaire_response', ['questionnaireid' => 0]);
|
|
|
738 |
foreach ($rs as $response) {
|
|
|
739 |
if ($questionnaire = $DB->get_record('questionnaire', ['sid' => $response->survey_id], 'id,sid', IGNORE_MULTIPLE)) {
|
|
|
740 |
$DB->set_field('questionnaire_response', 'questionnaireid', $questionnaire->id, ['id' => $response->id]);
|
|
|
741 |
}
|
|
|
742 |
}
|
|
|
743 |
$rs->close();
|
|
|
744 |
|
|
|
745 |
// Remove the survey_id field from the response table. It is now redundant.
|
|
|
746 |
if ($dbman->field_exists($table, $field2)) {
|
|
|
747 |
$dbman->drop_field($table, $field2);
|
|
|
748 |
}
|
|
|
749 |
|
|
|
750 |
// Add an index for the new questionnaireid field.
|
|
|
751 |
$index = new xmldb_index('questionnaireidx');
|
|
|
752 |
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, ['questionnaireid']);
|
|
|
753 |
$dbman->add_index($table, $index);
|
|
|
754 |
|
|
|
755 |
// Now drop the unnecessary attempts table.
|
|
|
756 |
$table = new xmldb_table('questionnaire_attempts');
|
|
|
757 |
$dbman->drop_table($table);
|
|
|
758 |
|
|
|
759 |
// Questionnaire savepoint reached.
|
|
|
760 |
upgrade_mod_savepoint(true, 2018050102, 'questionnaire');
|
|
|
761 |
}
|
|
|
762 |
|
|
|
763 |
// Rename the mdl_questionnaire_response_rank.rank field as it is reserved in MySQL as of 8.0.2. This step may have already
|
|
|
764 |
// been executed in 3.4 with version 2017111105, so check first.
|
|
|
765 |
if ($oldversion < 2018050104) {
|
|
|
766 |
// Change the name from username to userid.
|
|
|
767 |
// Due to MDL-63310, the 'rename_field' function cannot be used for MySQL. Create special code for this. This can be
|
|
|
768 |
// replaces when MDL-63310 is fixed and released.
|
|
|
769 |
if ($DB->get_dbfamily() !== 'mysql') {
|
|
|
770 |
$table = new xmldb_table('questionnaire_response_rank');
|
|
|
771 |
$field = new xmldb_field('rank', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null, null, '0', 'choice_id');
|
|
|
772 |
if ($dbman->field_exists($table, $field)) {
|
|
|
773 |
$dbman->rename_field($table, $field, 'rankvalue');
|
|
|
774 |
}
|
|
|
775 |
} else {
|
|
|
776 |
if ($dbman->field_exists('questionnaire_response_rank', 'rank')) {
|
|
|
777 |
$rankoldfieldname = $DB->get_manager()->generator->getEncQuoted('rank');
|
|
|
778 |
$ranknewfieldname = $DB->get_manager()->generator->getEncQuoted('rankvalue');
|
|
|
779 |
$sql = 'ALTER TABLE {questionnaire_response_rank} ' .
|
|
|
780 |
'CHANGE ' . $rankoldfieldname . ' ' . $ranknewfieldname . ' BIGINT(11) NOT NULL';
|
|
|
781 |
$DB->execute($sql);
|
|
|
782 |
}
|
|
|
783 |
}
|
|
|
784 |
|
|
|
785 |
// Questionnaire savepoint reached.
|
|
|
786 |
upgrade_mod_savepoint(true, 2018050104, 'questionnaire');
|
|
|
787 |
}
|
|
|
788 |
|
|
|
789 |
// Now 'feedbacksections' field is used differently.
|
|
|
790 |
if ($oldversion < 2018050105) {
|
|
|
791 |
// Get all of the survey records where feedbacksection is greater than 2 and set them to 2.
|
|
|
792 |
$DB->set_field_select('questionnaire_survey', 'feedbacksections', 2, 'feedbacksections > 2');
|
|
|
793 |
// Questionnaire savepoint reached.
|
|
|
794 |
upgrade_mod_savepoint(true, 2018050105, 'questionnaire');
|
|
|
795 |
}
|
|
|
796 |
|
|
|
797 |
// Rename all of the survey_id fields to surveyid, and the section_id fields to sectionid to meet Moodle coding rules.
|
|
|
798 |
if ($oldversion < 2018050106) {
|
|
|
799 |
$table1 = new xmldb_table('questionnaire_fb_sections');
|
|
|
800 |
$field1 = new xmldb_field('survey_id', XMLDB_TYPE_INTEGER, '18');
|
|
|
801 |
$table2 = new xmldb_table('questionnaire_feedback');
|
|
|
802 |
$field2 = new xmldb_field('section_id', XMLDB_TYPE_INTEGER, '18');
|
|
|
803 |
$table3 = new xmldb_table('questionnaire_question');
|
|
|
804 |
$field3 = new xmldb_field('survey_id', XMLDB_TYPE_INTEGER, '10');
|
|
|
805 |
|
|
|
806 |
$dbman->rename_field($table1, $field1, 'surveyid');
|
|
|
807 |
$dbman->rename_field($table2, $field2, 'sectionid');
|
|
|
808 |
$dbman->rename_field($table3, $field3, 'surveyid');
|
|
|
809 |
|
|
|
810 |
// Questionnaire savepoint reached.
|
|
|
811 |
upgrade_mod_savepoint(true, 2018050106, 'questionnaire');
|
|
|
812 |
}
|
|
|
813 |
|
|
|
814 |
if ($oldversion < 2018110103) {
|
|
|
815 |
|
|
|
816 |
// Define field id to be added to questionnaire_question.
|
|
|
817 |
$table = new xmldb_table('questionnaire_question');
|
|
|
818 |
$field = new xmldb_field('extradata', XMLDB_TYPE_TEXT, null, null, null, null, null, 'deleted');
|
|
|
819 |
|
|
|
820 |
// Conditionally launch add field id.
|
|
|
821 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
822 |
$dbman->add_field($table, $field);
|
|
|
823 |
}
|
|
|
824 |
|
|
|
825 |
// Need to move rank named degree choices to the new field.
|
|
|
826 |
\mod_questionnaire\question\rate::move_all_nameddegree_choices();
|
|
|
827 |
|
|
|
828 |
// Questionnaire savepoint reached.
|
|
|
829 |
upgrade_mod_savepoint(true, 2018110103, 'questionnaire');
|
|
|
830 |
}
|
|
|
831 |
|
|
|
832 |
if ($oldversion < 2020011507) {
|
|
|
833 |
// This operation might take a while. Cancel PHP timeouts for this.
|
|
|
834 |
\core_php_time_limit::raise();
|
|
|
835 |
|
|
|
836 |
// Making the database tables standard across the board.
|
|
|
837 |
$table = new xmldb_table('questionnaire');
|
|
|
838 |
$field1 = new xmldb_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
839 |
$field2 = new xmldb_field('sid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
840 |
|
|
|
841 |
// Changing fields that are used in indexes and keys generates errors (sometimes). Drop all foreign keys and indexes first;
|
|
|
842 |
// recreate them after. And, the might be a key or an index, so drop both and fix after.
|
|
|
843 |
$key1 = new xmldb_key('course', XMLDB_KEY_FOREIGN, ['course'], 'course', ['id']);
|
|
|
844 |
$dbman->drop_key($table, $key1);
|
|
|
845 |
$index1 = new xmldb_index('course', XMLDB_INDEX_NOTUNIQUE, ['course']);
|
|
|
846 |
if ($dbman->index_exists($table, $index1)) {
|
|
|
847 |
$dbman->drop_index($table, $index1);
|
|
|
848 |
}
|
|
|
849 |
$key2 = new xmldb_key('sid', XMLDB_KEY_FOREIGN, ['sid'], 'questionnaire_survey', ['id']);
|
|
|
850 |
$dbman->drop_key($table, $key2);
|
|
|
851 |
$index2 = new xmldb_index('sid', XMLDB_INDEX_NOTUNIQUE, ['sid']);
|
|
|
852 |
if ($dbman->index_exists($table, $index2)) {
|
|
|
853 |
$dbman->drop_index($table, $index2);
|
|
|
854 |
}
|
|
|
855 |
$index3 = new xmldb_index('respview', XMLDB_INDEX_NOTUNIQUE, ['resp_view']);
|
|
|
856 |
if ($dbman->index_exists($table, $index3)) {
|
|
|
857 |
$dbman->drop_index($table, $index3);
|
|
|
858 |
}
|
|
|
859 |
$dbman->change_field_type($table, $field1);
|
|
|
860 |
$dbman->change_field_type($table, $field2);
|
|
|
861 |
$dbman->add_key($table, $key1);
|
|
|
862 |
$dbman->add_key($table, $key2);
|
|
|
863 |
$dbman->add_index($table, $index3);
|
|
|
864 |
|
|
|
865 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
866 |
$index = new xmldb_index('courseid', XMLDB_INDEX_NOTUNIQUE, ['courseid']);
|
|
|
867 |
if ($dbman->index_exists($table, $index)) {
|
|
|
868 |
$dbman->drop_index($table, $index);
|
|
|
869 |
}
|
|
|
870 |
$key = new xmldb_key('courseid', XMLDB_KEY_FOREIGN, ['courseid'], 'course', ['id']);
|
|
|
871 |
$dbman->drop_key($table, $key);
|
|
|
872 |
$dbman->add_key($table, $key);
|
|
|
873 |
|
|
|
874 |
$table = new xmldb_table('questionnaire_question');
|
|
|
875 |
$field = new xmldb_field('surveyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
876 |
$index = new xmldb_index('quest_question_sididx', XMLDB_INDEX_NOTUNIQUE, ['surveyid', 'deleted']);
|
|
|
877 |
if ($dbman->index_exists($table, $index)) {
|
|
|
878 |
$dbman->drop_index($table, $index);
|
|
|
879 |
}
|
|
|
880 |
$dbman->change_field_type($table, $field);
|
|
|
881 |
$dbman->add_index($table, $index);
|
|
|
882 |
$field = new xmldb_field('length', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
883 |
$dbman->change_field_type($table, $field);
|
|
|
884 |
$field = new xmldb_field('precise', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
885 |
$dbman->change_field_type($table, $field);
|
|
|
886 |
|
|
|
887 |
$table = new xmldb_table('questionnaire_quest_choice');
|
|
|
888 |
$index = new xmldb_index('questionid', XMLDB_INDEX_NOTUNIQUE, ['question_id']);
|
|
|
889 |
if ($dbman->index_exists($table, $index)) {
|
|
|
890 |
$dbman->drop_index($table, $index);
|
|
|
891 |
}
|
|
|
892 |
$key = new xmldb_key('questionid', XMLDB_KEY_FOREIGN, ['question_id'], 'questionnaire_question', ['id']);
|
|
|
893 |
$dbman->drop_key($table, $key);
|
|
|
894 |
$dbman->add_key($table, $key);
|
|
|
895 |
|
|
|
896 |
$table = new xmldb_table('questionnaire_response');
|
|
|
897 |
$index = new xmldb_index('questionnaireid', XMLDB_INDEX_NOTUNIQUE, ['questionnaireid']);
|
|
|
898 |
if ($dbman->index_exists($table, $index)) {
|
|
|
899 |
$dbman->drop_index($table, $index);
|
|
|
900 |
}
|
|
|
901 |
$key = new xmldb_key('questionnaireid', XMLDB_KEY_FOREIGN, ['questionnaireid'], 'questionnaire', ['id']);
|
|
|
902 |
$dbman->drop_key($table, $key);
|
|
|
903 |
$dbman->add_key($table, $key);
|
|
|
904 |
|
|
|
905 |
// Postgres and MSSQL have a bug that impacts changing fields with a sequence defined (see bug MDL-68799), so don't change
|
|
|
906 |
// this for Postgres or MSSQL.
|
|
|
907 |
if (($DB->get_dbfamily() !== 'postgres') && ($DB->get_dbfamily() !== 'mssql')) {
|
|
|
908 |
$idfield = new xmldb_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
|
|
|
909 |
|
|
|
910 |
$table = new xmldb_table('questionnaire_response_bool');
|
|
|
911 |
$dbman->change_field_type($table, $idfield);
|
|
|
912 |
|
|
|
913 |
$table = new xmldb_table('questionnaire_response_date');
|
|
|
914 |
$dbman->change_field_type($table, $idfield);
|
|
|
915 |
|
|
|
916 |
$table = new xmldb_table('questionnaire_response_other');
|
|
|
917 |
$dbman->change_field_type($table, $idfield);
|
|
|
918 |
|
|
|
919 |
$table = new xmldb_table('questionnaire_response_rank');
|
|
|
920 |
$dbman->change_field_type($table, $idfield);
|
|
|
921 |
|
|
|
922 |
$table = new xmldb_table('questionnaire_resp_single');
|
|
|
923 |
$dbman->change_field_type($table, $idfield);
|
|
|
924 |
|
|
|
925 |
$table = new xmldb_table('questionnaire_response_text');
|
|
|
926 |
$dbman->change_field_type($table, $idfield);
|
|
|
927 |
|
|
|
928 |
$table = new xmldb_table('questionnaire_fb_sections');
|
|
|
929 |
$dbman->change_field_type($table, $idfield);
|
|
|
930 |
|
|
|
931 |
$table = new xmldb_table('questionnaire_feedback');
|
|
|
932 |
$dbman->change_field_type($table, $idfield);
|
|
|
933 |
}
|
|
|
934 |
|
|
|
935 |
$table = new xmldb_table('questionnaire_response_rank');
|
|
|
936 |
$field = new xmldb_field('rankvalue', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
937 |
$dbman->change_field_type($table, $field);
|
|
|
938 |
|
|
|
939 |
$table = new xmldb_table('questionnaire_fb_sections');
|
|
|
940 |
$field = new xmldb_field('surveyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
941 |
$key = new xmldb_key('surveyid', XMLDB_KEY_FOREIGN, ['surveyid'], 'questionnaire_survey', ['id']);
|
|
|
942 |
$dbman->drop_key($table, $key);
|
|
|
943 |
$dbman->change_field_type($table, $field);
|
|
|
944 |
$dbman->add_key($table, $key);
|
|
|
945 |
|
|
|
946 |
$table = new xmldb_table('questionnaire_feedback');
|
|
|
947 |
$field = new xmldb_field('sectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
|
|
|
948 |
$key = new xmldb_key('sectionid', XMLDB_KEY_FOREIGN, ['sectionid'], 'questionnaire_fb_sections', ['id']);
|
|
|
949 |
$dbman->drop_key($table, $key);
|
|
|
950 |
$dbman->change_field_type($table, $field);
|
|
|
951 |
$dbman->add_key($table, $key);
|
|
|
952 |
|
|
|
953 |
$table = new xmldb_table('questionnaire_survey');
|
|
|
954 |
$field = new xmldb_field('feedbacksections', XMLDB_TYPE_INTEGER, '2', null, null, null, '0');
|
|
|
955 |
$dbman->change_field_type($table, $field);
|
|
|
956 |
|
|
|
957 |
$table = new xmldb_table('questionnaire_dependency');
|
|
|
958 |
$index = new xmldb_index('questionid', XMLDB_INDEX_NOTUNIQUE, ['questionid']);
|
|
|
959 |
if ($dbman->index_exists($table, $index)) {
|
|
|
960 |
$dbman->drop_index($table, $index);
|
|
|
961 |
}
|
|
|
962 |
$key = new xmldb_key('questionid', XMLDB_KEY_FOREIGN, ['questionid'], 'questionnaire_question', ['id']);
|
|
|
963 |
$dbman->drop_key($table, $key);
|
|
|
964 |
$dbman->add_key($table, $key);
|
|
|
965 |
$key = new xmldb_key('surveyid', XMLDB_KEY_FOREIGN, ['surveyid'], 'questionnaire_survey', ['id']);
|
|
|
966 |
$dbman->add_key($table, $key);
|
|
|
967 |
|
|
|
968 |
// Questionnaire savepoint reached.
|
|
|
969 |
upgrade_mod_savepoint(true, 2020011507, 'questionnaire');
|
|
|
970 |
}
|
|
|
971 |
|
|
|
972 |
if ($oldversion < 2020062301) {
|
|
|
973 |
// Add show progress bar setting.
|
|
|
974 |
$table = new xmldb_table('questionnaire');
|
|
|
975 |
$field = new xmldb_field('progressbar', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'autonum');
|
|
|
976 |
|
|
|
977 |
// Conditionally launch add field.
|
|
|
978 |
if (!$dbman->field_exists($table, $field)) {
|
|
|
979 |
$dbman->add_field($table, $field);
|
|
|
980 |
}
|
|
|
981 |
|
|
|
982 |
// Questionnaire savepoint reached.
|
|
|
983 |
upgrade_mod_savepoint(true, 2020062301, 'questionnaire');
|
|
|
984 |
}
|
|
|
985 |
|
|
|
986 |
if ($oldversion < 2022092200) {
|
|
|
987 |
// Add new slider question type.
|
|
|
988 |
$exist = $DB->record_exists('questionnaire_question_type', ['typeid' => 11]);
|
|
|
989 |
if (!$exist) {
|
|
|
990 |
$questiontype = new stdClass();
|
|
|
991 |
$questiontype->typeid = 11;
|
|
|
992 |
$questiontype->type = 'Slider';
|
|
|
993 |
$questiontype->has_choices = 'n';
|
|
|
994 |
$questiontype->response_table = 'response_text';
|
|
|
995 |
$DB->insert_record('questionnaire_question_type', $questiontype);
|
|
|
996 |
}
|
|
|
997 |
upgrade_mod_savepoint(true, 2022092200, 'questionnaire');
|
|
|
998 |
}
|
|
|
999 |
|
|
|
1000 |
return $result;
|
|
|
1001 |
}
|
|
|
1002 |
|
|
|
1003 |
/**
|
|
|
1004 |
* Supporting functions used once.
|
|
|
1005 |
* @return bool
|
|
|
1006 |
*/
|
|
|
1007 |
function questionnaire_upgrade_2007120101() {
|
|
|
1008 |
global $DB;
|
|
|
1009 |
|
|
|
1010 |
$dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
|
|
|
1011 |
$status = true;
|
|
|
1012 |
|
|
|
1013 |
// Shorten table names to bring them in accordance with the XML DB schema.
|
|
|
1014 |
$qtable = new xmldb_table('questionnaire_question_choice');
|
|
|
1015 |
$dbman->rename_table($qtable, 'questionnaire_quest_choice', false);
|
|
|
1016 |
unset($qtable);
|
|
|
1017 |
|
|
|
1018 |
$qtable = new xmldb_table('questionnaire_response_multiple');
|
|
|
1019 |
$dbman->rename_table($qtable, 'questionnaire_resp_multiple', false);
|
|
|
1020 |
unset($qtable);
|
|
|
1021 |
|
|
|
1022 |
$qtable = new xmldb_table('questionnaire_response_single');
|
|
|
1023 |
$dbman->rename_table($qtable, 'questionnaire_resp_single', false);
|
|
|
1024 |
unset($qtable);
|
|
|
1025 |
|
|
|
1026 |
// Upgrade the questionnaire_question_type table to use typeid.
|
|
|
1027 |
$table = new xmldb_table('questionnaire_question_type');
|
|
|
1028 |
$field = new xmldb_field('typeid');
|
|
|
1029 |
$field->set_attributes(XMLDB_TYPE_CHAR, '20', true, true, false, false, null, '0', 'id');
|
|
|
1030 |
$dbman->add_field($table, $field);
|
|
|
1031 |
if (($numrecs = $dbman->count_records('questionnaire_question_type')) > 0) {
|
|
|
1032 |
$recstart = 0;
|
|
|
1033 |
$recstoget = 100;
|
|
|
1034 |
while ($recstart < $numrecs) {
|
|
|
1035 |
if ($records = $dbman->get_records('questionnaire_question_type', array(), '', '*', $recstart, $recstoget)) {
|
|
|
1036 |
foreach ($records as $record) {
|
|
|
1037 |
$dbman->set_field('questionnaire_question_type', 'typeid', $record->id, array('id' => $record->id));
|
|
|
1038 |
}
|
|
|
1039 |
}
|
|
|
1040 |
$recstart += $recstoget;
|
|
|
1041 |
}
|
|
|
1042 |
}
|
|
|
1043 |
|
|
|
1044 |
return $status;
|
|
|
1045 |
}
|