]> git.mjollnir.org Git - moodle.git/commitdiff
- Properly deleting attached files when a glossary is deleted.
authorwillcast <willcast>
Sun, 23 Nov 2003 02:46:29 +0000 (02:46 +0000)
committerwillcast <willcast>
Sun, 23 Nov 2003 02:46:29 +0000 (02:46 +0000)
mod/glossary/lib.php

index b27f6900937f29eca110ecb6e0e0cc4486f17aec..983a36d49c418cf39136201eff6c71b90db83a14 100644 (file)
@@ -127,8 +127,10 @@ function glossary_delete_instance($id) {
             if ($ents) {
                 delete_records_select("glossary_comments", "entryid in ($ents)");
                 delete_records_select("glossary_alias", "entryid in ($ents)");
+                delete_records_select("glossary_ratings", "entryid in ($ents)");
             }
         }
+        glossary_delete_glossary_attachments($glossary);
         delete_records("glossary_entries", "glossaryid", "$glossary->id");
     }
 
@@ -761,16 +763,32 @@ function glossary_search_entries($searchterms, $glossary, $includedefinition) {
 }
 
 function glossary_file_area_name($entry) {
-//  Creates a directory file name, suitable for make_upload_directory()
     global $CFG;
+//  Creates a directory file name, suitable for make_upload_directory()
 
-    return "$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
+    // I'm doing this workaround for make it works for delete_instance also 
+    //  (when called from delete_instance, glossary is already deleted so
+    //   getting the course from mdl_glossary does not work)
+    $module = get_record("modules","name","glossary");
+    $cm = get_record("course_modules","module",$module->id,"instance",$entry->glossaryid);
+    return "$cm->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
 }
 
 function glossary_file_area($entry) {
     return make_upload_directory( glossary_file_area_name($entry) );
 }
 
+function glossary_main_file_area($glossary) {
+    $modarea = glossary_mod_file_area($glossary);
+    return "$modarea/$glossary->id";
+}
+
+function glossary_mod_file_area($glossary) {
+    global $CFG;
+    
+    return make_upload_directory( "$glossary->course/$CFG->moddata/glossary" );
+}
+
 function glossary_delete_old_attachments($entry, $exception="") {
 // Deletes all the user files in the attachments area for a entry
 // EXCEPT for any file named $exception
@@ -789,6 +807,34 @@ function glossary_delete_old_attachments($entry, $exception="") {
         }
     }
 }
+function glossary_delete_glossary_attachments($glossary) {
+// Deletes all the user files in the attachments area for the glossary
+    if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
+        $deleted = 0;
+        foreach ($entries as $entry) {
+            if ( $entry->attachment ) {
+                if ($basedir = glossary_file_area($entry)) {
+                    if ($files = get_directory_list($basedir)) {
+                        foreach ($files as $file) {
+                            unlink("$basedir/$file");
+                        }
+                    }
+                    rmdir("$basedir");
+                    $deleted++;
+                }
+            }            
+        }
+        if ( $deleted ) {
+            $attachmentdir = glossary_main_file_area($glossary);
+            $glossarydir = glossary_mod_file_area($glossary);
+            
+            rmdir("$attachmentdir");
+            if (!$files = get_directory_list($glossarydir) ) {
+                rmdir( "$glossarydir" );
+            }
+        }        
+    }
+}
 
 function glossary_copy_attachments($entry, $newentry) {
 /// Given a entry object that is being copied to glossaryid,