/** * Does a bulk_delete on a list of artefacts, grouping artefacts of * the same type. * * Currently only tested for folders and their contents. */ public static function delete_by_artefacttype($artefactids) { if (empty($artefactids)) { return; } db_begin(); artefact_watchlist_notification($artefactids); // Delete comments first safe_require('artefact', 'comment'); ArtefactTypeComment::delete_comments_onartefacts($artefactids); $records = get_records_select_assoc('artefact', 'id IN (' . join(',', array_map('intval', $artefactids)) . ')', null, 'artefacttype', 'id,parent,artefacttype,container'); $containers = array(); $leaves = array(); foreach ($records as $r) { if ($r->container) { $containers[$r->artefacttype][] = (int) $r->id; } else { $leaves[$r->artefacttype][] = $r->id; } } // Delete non-containers grouped by artefacttype foreach ($leaves as $artefacttype => $ids) { $classname = generate_artefact_class_name($artefacttype); call_static_method($classname, 'bulk_delete', $ids); } // Delete containers grouped by artefacttype foreach ($containers as $artefacttype => $ids) { $classname = generate_artefact_class_name($artefacttype); if (is_mysql()) { set_field_select('artefact', 'parent', null, 'id IN (' . join(',', $ids) . ')', array()); } call_static_method($classname, 'bulk_delete', $ids); } handle_event('deleteartefacts', $artefactids); db_commit(); }
/** * This function provides basic delete functionality. It gets rid of the * artefact's row in the artefact table, and the tables that reference the * artefact table. It also recursively deletes child artefacts. * * If your artefact has additional data in another table, you should * override this function, but you MUST call parent::delete() after you * have done your own thing. */ public function delete() { if (empty($this->id)) { $this->dirty = false; return; } db_begin(); // Call delete() on children (if there are any) if ($children = $this->get_children_instances()) { foreach ($children as $child) { $child->delete(); } } artefact_watchlist_notification($this->id); // Delete any references to this artefact from non-artefact places. delete_records_select('artefact_parent_cache', 'artefact = ? OR parent = ?', array($this->id, $this->id)); // Make sure that the artefact is removed from any view blockinstances that have it if ($records = get_column('view_artefact', 'block', 'artefact', $this->id)) { foreach ($records as $blockid) { require_once get_config('docroot') . 'blocktype/lib.php'; $bi = new BlockInstance($blockid); $bi->delete_artefact($this->id); } } delete_records('view_artefact', 'artefact', $this->id); delete_records('artefact_feedback', 'artefact', $this->id); delete_records('artefact_tag', 'artefact', $this->id); delete_records('artefact_access_role', 'artefact', $this->id); delete_records('artefact_access_usr', 'artefact', $this->id); if ($this->can_be_logged()) { $this->log('deleted'); } // Delete the record itself. delete_records('artefact', 'id', $this->id); handle_event('deleteartefact', $this); // Set flags. $this->dirty = false; $this->parentdirty = true; $this->deleted = true; db_commit(); }