3 * This file contains dtabase upgrade code that is called from lib/db/upgrade.php,
4 * and also check methods that can be used for pre-install checks via
5 * admin/environment.php and lib/environmentlib.php.
7 * @copyright © 2007 The Open University
8 * @author T.J.Hunt@open.ac.uk
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package package_name
14 * This test is becuase the RQP question type was included in core
15 * up to and including Moodle 1.8, and was removed before Moodle 1.9.
17 * Therefore, we want to check whether any rqp questions exist in the database
18 * before doing the upgrade. However, the check is not relevant if that
19 * question type was never installed, or if the person has chosen to
20 * manually reinstall the rqp question type from contrib.
22 * @param $result the result object that can be modified.
23 * @return null if the test is irrelevant, or true or false depending on whether the test passes.
25 function question_check_no_rqp_questions($result) {
28 if (empty($CFG->qtype_rqp_version
) ||
is_dir($CFG->dirroot
. '/question/type/rqp')) {
31 $result->setStatus(count_records('question', 'qtype', 'rqp') == 0);
36 function question_remove_rqp_qtype() {
41 // Only remove the question type if the code is gone.
42 if (!is_dir($CFG->dirroot
. '/question/type/rqp')) {
43 $table = new XMLDBTable('question_rqp_states');
44 $result = $result && drop_table($table);
46 $table = new XMLDBTable('question_rqp');
47 $result = $result && drop_table($table);
49 $table = new XMLDBTable('question_rqp_types');
50 $result = $result && drop_table($table);
52 $table = new XMLDBTable('question_rqp_servers');
53 $result = $result && drop_table($table);
55 $result = $result && unset_config('qtype_rqp_version');
61 function question_remove_rqp_qtype_config_string() {
66 // An earlier, buggy version of the previous function missed out the unset_config call.
67 if (!empty($CFG->qtype_rqp_version
) && !is_dir($CFG->dirroot
. '/question/type/rqp')) {
68 $result = $result && unset_config('qtype_rqp_version');
75 * @param $result the result object that can be modified.
76 * @return null if the test is irrelevant, or true or false depending on whether the test passes.
78 function question_random_check($result){
80 if (!empty($CFG->running_installer
) //no test on first installation, no questions to test yet
81 ||
$CFG->version
>= 2007081000){//no test after upgrade seperates question cats into contexts.
84 if (!$toupdate = question_cwqpfs_to_update()){
85 $result->setStatus(true);//pass test
87 //set the feedback string here and not in xml file since we need something
88 //more complex than just a string picked from admin.php lang file
90 $a->reporturl
= "{$CFG->wwwroot}/{$CFG->admin}/report/question/";
91 $lang = str_replace('_utf8', '', current_language());
92 $a->docsurl
= "{$CFG->docroot}/$lang/admin/report/question/index";
93 $result->feedback_str
= get_string('questioncwqpfscheck', 'admin', $a);
94 $result->setStatus(false);//fail test
99 * Delete all 'random' questions that are not been used in a quiz.
101 function question_delete_unused_random(){
105 //delete all 'random' questions that are not been used in a quiz.
106 if ($qqis = get_records_sql("SELECT q.* FROM {$CFG->prefix}question q LEFT JOIN ".
107 "{$CFG->prefix}quiz_question_instances qqi ".
108 "ON q.id = qqi.question WHERE q.qtype='random' AND qqi.question IS NULL")){
109 $qqilist = join(array_keys($qqis), ',');
110 $result = $result && delete_records_select('question', "id IN ($qqilist)");
114 function question_cwqpfs_to_update($categories = null){
120 //any cats with questions picking from subcats?
121 if (!$cwqpfs = get_records_sql_menu("SELECT DISTINCT qc.id, 1 ".
122 "FROM {$CFG->prefix}question q, {$CFG->prefix}question_categories qc ".
123 "WHERE q.qtype='random' AND qc.id = q.category AND ".
124 sql_compare_text('q.questiontext'). " = '1'")){
127 if ($categories === null){
128 $categories = get_records('question_categories');
130 $categorychildparents = array();
131 foreach ($categories as $id => $category){
132 $categorychildparents[$category->course
][$id] = $category->parent
;
134 foreach ($categories as $id => $category){
135 if (FALSE !== array_key_exists($category->parent
, $categorychildparents[$category->course
])){
136 //this is not a top level cat
137 continue;//go to next category
139 $tofix +
= question_cwqpfs_check_children($id, $categories, $categorychildparents[$category->course
], $cwqpfs);
147 function question_cwqpfs_check_children($checkid, $categories, $categorychildparents, $cwqpfs){
149 if (array_key_exists($checkid, $cwqpfs)){//cwqpfs in this cat
150 $getchildren = array();
151 $getchildren[] = $checkid;
152 //search down tree and find all children
153 while ($nextid = array_shift($getchildren)){//repeat until $getchildren
155 $childids = array_keys($categorychildparents, $nextid);
156 foreach ($childids as $childid){
157 if ($categories[$childid]->publish
!= $categories[$checkid]->publish
){
158 $tofix[$childid] = $categories[$checkid]->publish
;
161 $getchildren = array_merge($getchildren, $childids);
163 } else { // check children for cwqpfs
164 $childrentocheck = array_keys($categorychildparents, $checkid);
165 foreach ($childrentocheck as $childtocheck){
166 $tofix +
= question_cwqpfs_check_children($childtocheck, $categories, $categorychildparents, $cwqpfs);
172 function question_category_next_parent_in($contextid, $question_categories, $id){
173 $nextparent = $question_categories[$id]->parent
;
174 if ($nextparent == 0){
176 } elseif (!array_key_exists($nextparent, $question_categories)){
177 //finished searching up the category hierarchy. For some reason
178 //the top level items is not 0. We'll return 0 though.
180 } elseif ($contextid == $question_categories[$nextparent]->contextid
){
183 //parent is not in the same context look further up.
184 return question_category_next_parent_in($contextid, $question_categories, $nextparent);
190 * Check that either category parent is 0 or a category shared in the same context.
191 * Fix any categories to point to grand or grand grand parent etc in the same context or 0.
193 function question_category_checking($question_categories){
194 //make an array that is easier to search
195 $newparents = array();
196 foreach ($question_categories as $id => $category){
197 $newparents[$id] = question_category_next_parent_in($category->contextid
, $question_categories, $id);
199 foreach (array_Keys($question_categories) as $id){
200 $question_categories[$id]->parent
= $newparents[$id];
202 return $question_categories;
205 function question_upgrade_context_etc(){
208 $result = $result && question_delete_unused_random();
210 $question_categories = get_records('question_categories');
212 $tofix = question_cwqpfs_to_update($question_categories);
213 foreach ($tofix as $catid => $publish){
214 $question_categories[$catid]->publish
= $publish;
217 foreach ($question_categories as $id => $question_category){
218 $course = $question_categories[$id]->course
;
219 unset($question_categories[$id]->course
);
220 if ($question_categories[$id]->publish
){
221 $context = get_context_instance(CONTEXT_SYSTEM
);
223 $context = get_context_instance(CONTEXT_COURSE
, $course);
225 $question_categories[$id]->contextid
= $context->id
;
226 unset($question_categories[$id]->publish
);
229 $question_categories = question_category_checking($question_categories);
231 /// Define index course (not unique) to be dropped form question_categories
232 $table = new XMLDBTable('question_categories');
233 $index = new XMLDBIndex('course');
234 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE
, array('course'));
236 /// Launch drop index course
237 $result = $result && drop_index($table, $index);
239 /// Define field course to be dropped from question_categories
240 $field = new XMLDBField('course');
242 /// Launch drop field course
243 $result = $result && drop_field($table, $field);
245 /// Define field context to be added to question_categories
246 $field = new XMLDBField('contextid');
247 $field->setAttributes(XMLDB_TYPE_INTEGER
, '10', XMLDB_UNSIGNED
, XMLDB_NOTNULL
, null, null, null, '0', 'name');
248 $field->comment
= 'context that this category is shared in';
250 /// Launch add field context
251 $result = $result && add_field($table, $field);
253 /// Define index context (not unique) to be added to question_categories
254 $index = new XMLDBIndex('contextid');
255 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE
, array('contextid'));
256 $index->comment
= 'links to context table';
258 /// Launch add index context
259 $result = $result && add_index($table, $index);
261 $field = new XMLDBField('publish');
263 /// Launch drop field publish
264 $result = $result && drop_field($table, $field);
267 /// update table contents with previously calculated new contents.
269 foreach ($question_categories as $question_category){
270 if (!$result = update_record('question_categories', $question_category)){
271 notify('Couldn\'t update question_categories "'. $question_category->name
.'"!');
275 /// Define field timecreated to be added to question
276 $table = new XMLDBTable('question');
277 $field = new XMLDBField('timecreated');
278 $field->setAttributes(XMLDB_TYPE_INTEGER
, '10', XMLDB_UNSIGNED
, XMLDB_NOTNULL
, null, null, null, '0', 'hidden');
280 /// Launch add field timecreated
281 $result = $result && add_field($table, $field);
283 /// Define field timemodified to be added to question
284 $table = new XMLDBTable('question');
285 $field = new XMLDBField('timemodified');
286 $field->setAttributes(XMLDB_TYPE_INTEGER
, '10', XMLDB_UNSIGNED
, XMLDB_NOTNULL
, null, null, null, '0', 'timecreated');
288 /// Launch add field timemodified
289 $result = $result && add_field($table, $field);
291 /// Define field createdby to be added to question
292 $table = new XMLDBTable('question');
293 $field = new XMLDBField('createdby');
294 $field->setAttributes(XMLDB_TYPE_INTEGER
, '10', XMLDB_UNSIGNED
, null, null, null, null, null, 'timemodified');
296 /// Launch add field createdby
297 $result = $result && add_field($table, $field);
299 /// Define field modifiedby to be added to question
300 $table = new XMLDBTable('question');
301 $field = new XMLDBField('modifiedby');
302 $field->setAttributes(XMLDB_TYPE_INTEGER
, '10', XMLDB_UNSIGNED
, null, null, null, null, null, 'createdby');
304 /// Launch add field modifiedby
305 $result = $result && add_field($table, $field);
307 /// Define key createdby (foreign) to be added to question
308 $table = new XMLDBTable('question');
309 $key = new XMLDBKey('createdby');
310 $key->setAttributes(XMLDB_KEY_FOREIGN
, array('createdby'), 'user', array('id'));
312 /// Launch add key createdby
313 $result = $result && add_key($table, $key);
315 /// Define key modifiedby (foreign) to be added to question
316 $table = new XMLDBTable('question');
317 $key = new XMLDBKey('modifiedby');
318 $key->setAttributes(XMLDB_KEY_FOREIGN
, array('modifiedby'), 'user', array('id'));
320 /// Launch add key modifiedby
321 $result = $result && add_key($table, $key);