/** * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * * @global object * @param int $id glossary id * @return bool success */ function glossary_delete_instance($id) { global $DB, $CFG; if (!$glossary = $DB->get_record('glossary', array('id'=>$id))) { return false; } if (!$cm = get_coursemodule_from_instance('glossary', $id)) { return false; } if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) { return false; } $fs = get_file_storage(); if ($glossary->mainglossary) { // unexport entries $sql = "SELECT ge.id, ge.sourceglossaryid, cm.id AS sourcecmid FROM {glossary_entries} ge JOIN {modules} m ON m.name = 'glossary' JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = ge.sourceglossaryid) WHERE ge.glossaryid = ? AND ge.sourceglossaryid > 0"; if ($exported = $DB->get_records_sql($sql, array($id))) { foreach ($exported as $entry) { $entry->glossaryid = $entry->sourceglossaryid; $entry->sourceglossaryid = 0; $newcontext = get_context_instance(CONTEXT_MODULE, $entry->sourcecmid); if ($oldfiles = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id)) { foreach ($oldfiles as $oldfile) { $file_record = new stdClass(); $file_record->contextid = $newcontext->id; $fs->create_file_from_storedfile($file_record, $oldfile); } $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id); $entry->attachment = '1'; } else { $entry->attachment = '0'; } $DB->update_record('glossary_entries', $entry); } } } else { // move exported entries to main glossary $sql = "UPDATE {glossary_entries} SET sourceglossaryid = 0 WHERE sourceglossaryid = ?"; $DB->execute($sql, array($id)); } // Delete any dependent records $entry_select = "SELECT id FROM {glossary_entries} WHERE glossaryid = ?"; $DB->delete_records_select('comments', "contextid=? AND commentarea=? AND itemid IN ($entry_select)", array($id, 'glossary_entry', $context->id)); $DB->delete_records_select('glossary_alias', "entryid IN ($entry_select)", array($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); glossary_grade_item_delete($glossary); return $DB->delete_records('glossary', array('id'=>$id)); }
function glossary_delete_instance($id) { /// Given an ID of an instance of this module, /// this function will permanently delete the instance /// and any data that depends on it. if (!($glossary = get_record("glossary", "id", "{$id}"))) { return false; } $result = true; # Delete any dependent records here # if (!delete_records("glossary", "id", "{$glossary->id}")) { $result = false; } else { if ($categories = get_records("glossary_categories", "glossaryid", $glossary->id)) { $cats = ""; foreach ($categories as $cat) { $cats .= "{$cat->id},"; } $cats = substr($cats, 0, -1); if ($cats) { delete_records_select("glossary_entries_categories", "categoryid in ({$cats})"); delete_records("glossary_categories", "glossaryid", $glossary->id); } } if ($entries = get_records("glossary_entries", "glossaryid", $glossary->id)) { $ents = ""; foreach ($entries as $entry) { if ($entry->sourceglossaryid) { $entry->glossaryid = $entry->sourceglossaryid; $entry->sourceglossaryid = 0; update_record("glossary_entries", $entry); } else { $ents .= "{$entry->id},"; } } $ents = substr($ents, 0, -1); 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_attachments($glossary); delete_records("glossary_entries", "glossaryid", "{$glossary->id}"); } glossary_grade_item_delete($glossary); return $result; }