Permalink
Browse files

MDL-27083 Questions: Orphaned questions are adopted on upgrade

  • Loading branch information...
1 parent 947ba9c commit c7f65058d0817332e301975ae19a90e07d0723ab @FMCorz FMCorz committed Jul 23, 2012
Showing with 52 additions and 1 deletion.
  1. +2 −0 lang/en/question.php
  2. +9 −0 lib/db/upgrade.php
  3. +40 −0 lib/db/upgradelib.php
  4. +1 −1 version.php
View
@@ -230,6 +230,8 @@
$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';
View
@@ -7112,5 +7112,14 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2011120503.09);
}
+ if ($oldversion < 2011120504.03) {
+
+ // Saves orphaned questions from the Dark Side
+ upgrade_save_orphaned_questions();
+
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2012062501.04);
+ }
+
return true;
}
View
@@ -724,3 +724,43 @@ function upgrade_populate_default_messaging_prefs() {
}
$transaction->allow_commit();
}
+
+/**
+* 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);
+}
View
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2011120504.02; // 20111205 = branching date YYYYMMDD - do not modify!
+$version = 2011120504.03; // 20111205 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit c7f6505

Please sign in to comment.