diff --git a/lang/en/question.php b/lang/en/question.php index d0cf1a45189..dfa1aeb2df3 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -230,6 +230,8 @@ $string['novirtualquestiontype'] = 'No virtual question type for question type { $string['numqas'] = 'No. question attempts'; $string['numquestions'] = 'No. questions'; $string['numquestionsandhidden'] = '{$a->numquestions} (+{$a->numhidden} hidden)'; +$string['orphanedquestionscategory'] = 'Questions saved from deleted categories'; +$string['orphanedquestionscategoryinfo'] = 'Occasionally, typically due to old software bugs, questions can remain in the database even though the corresponding question category has been deleted. Of course, this should not happen, it has happened in the past on this site. This category has been created automatically, and the orphaned questions moved here so that you can manage them. Note that any images or media files used by these questions have probably been lost.'; $string['page-question-x'] = 'Any question page'; $string['page-question-edit'] = 'Question editing page'; $string['page-question-category'] = 'Question category page'; diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index c1a77a37251..24d6120482f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1060,5 +1060,14 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2012072400.00); } + if ($oldversion < 2012072401.00) { + + // Saves orphaned questions from the Dark Side + upgrade_save_orphaned_questions(); + + // Main savepoint reached + upgrade_main_savepoint(true, 2012072401.00); + } + return true; } diff --git a/lib/upgradelib.php b/lib/upgradelib.php index c94f063522b..95578fc8d49 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -1841,3 +1841,44 @@ function upgrade_course_completion_remove_duplicates($table, $uniques, $fieldsto } } } + +/** + * Find questions missing an existing category and associate them with + * a category which purpose is to gather them. + * + * @return void + */ +function upgrade_save_orphaned_questions() { + global $DB; + + // Looking for orphaned questions + $orphans = $DB->record_exists_select('question', + 'NOT EXISTS (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)'); + if (!$orphans) { + return; + } + + // Generate a unique stamp for the orphaned questions category, easier to identify it later on + $uniquestamp = "unknownhost+120719170400+orphan"; + $systemcontext = context_system::instance(); + + // Create the orphaned category at system level + $cat = $DB->get_record('question_categories', array('stamp' => $uniquestamp, + 'contextid' => $systemcontext->id)); + if (!$cat) { + $cat = new stdClass(); + $cat->parent = 0; + $cat->contextid = $systemcontext->id; + $cat->name = get_string('orphanedquestionscategory', 'question'); + $cat->info = get_string('orphanedquestionscategoryinfo', 'question'); + $cat->sortorder = 999; + $cat->stamp = $uniquestamp; + $cat->id = $DB->insert_record("question_categories", $cat); + } + + // Set a category to those orphans + $params = array('catid' => $cat->id); + $DB->execute('UPDATE {question} SET category = :catid WHERE NOT EXISTS + (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)', $params); +} + diff --git a/version.php b/version.php index 8cc3f2b1444..4c6e1554084 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2012072400.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2012072401.00; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes