MDL-25227 glossary - fix delete_instance() and clean orphaned entries
authorEloy Lafuente <stronk7@moodle.org>
Mon, 15 Nov 2010 15:16:57 +0000 (15:16 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Mon, 15 Nov 2010 15:16:57 +0000 (15:16 +0000)
mod/glossary/db/upgrade.php
mod/glossary/lib.php
mod/glossary/version.php

index fd76e86..7d155b6 100644 (file)
@@ -299,6 +299,16 @@ function xmldb_glossary_upgrade($oldversion) {
         upgrade_mod_savepoint(true, 2010042800, 'glossary');
     }
 
+    if ($oldversion < 2010111500) {
+        // Delete orphaned glossary_entries not belonging to any glossary (MDL-25227)
+        $sql = "DELETE FROM {glossary_entries}
+                WHERE NOT EXISTS (
+                    SELECT 'x' FROM {glossary} g
+                    WHERE g.id = glossaryid)";
+        $DB->execute($sql);
+
+        upgrade_mod_savepoint(true, 2010111500, 'glossary');
+    }
     return true;
 }
 
index 6799d9a..491cddc 100644 (file)
@@ -194,6 +194,7 @@ function glossary_delete_instance($id) {
     $category_select = "SELECT id FROM {glossary_categories} WHERE glossaryid = ?";
     $DB->delete_records_select('glossary_entries_categories', "categoryid IN ($category_select)", array($id));
     $DB->delete_records('glossary_categories', array('glossaryid'=>$id));
+    $DB->delete_records('glossary_entries', array('glossaryid'=>$id));
 
     // delete all files
     $fs->delete_area_files($context->id);
index 34de91d..440ac75 100644 (file)
@@ -5,7 +5,7 @@
 ///  This fragment is called by moodle_needs_upgrading() and /admin/index.php
 /////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2010080300;
+$module->version  = 2010111500;
 $module->requires = 2010080300;  // Requires this Moodle version
 $module->cron     = 0;           // Period for cron to check this module (secs)