function wiki_reset_userdata($data) { global $CFG, $DB; require_once $CFG->dirroot . '/mod/wiki/pagelib.php'; require_once $CFG->dirroot . '/tag/lib.php'; $componentstr = get_string('modulenameplural', 'wiki'); $status = array(); //get the wiki(s) in this course. if (!($wikis = $DB->get_records('wiki', array('course' => $data->courseid)))) { return false; } $errors = false; foreach ($wikis as $wiki) { // remove all comments if (!empty($data->reset_wiki_comments)) { if (!($cm = get_coursemodule_from_instance('wiki', $wiki->id))) { continue; } $context = context_module::instance($cm->id); $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id)); $status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false); } if (!empty($data->reset_wiki_tags)) { # Get subwiki information # $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id)); foreach ($subwikis as $subwiki) { if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) { foreach ($pages as $page) { $tags = tag_get_tags_array('wiki_pages', $page->id); foreach ($tags as $tagid => $tagname) { // Delete the related tag_instances related to the wiki page. $errors = tag_delete_instance('wiki_pages', $page->id, $tagid); $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => $errors); } } } } } } return $status; }
/** * Delete pages and all related data * * @param mixed $context context in which page needs to be deleted. * @param mixed $pageids id's of pages to be deleted * @param int $subwikiid id of the subwiki for which all pages should be deleted */ function wiki_delete_pages($context, $pageids = null, $subwikiid = null) { global $DB; if (!empty($pageids) && is_int($pageids)) { $pageids = array($pageids); } else { if (!empty($subwikiid)) { $pageids = wiki_get_page_list($subwikiid); } } //If there is no pageid then return as we can't delete anything. if (empty($pageids)) { return; } /// Delete page and all it's relevent data foreach ($pageids as $pageid) { if (is_object($pageid)) { $pageid = $pageid->id; } //Delete page comments $comments = wiki_get_comments($context->id, $pageid); foreach ($comments as $commentid => $commentvalue) { wiki_delete_comment($commentid, $context, $pageid); } //Delete page tags $tags = tag_get_tags_array('wiki_pages', $pageid); foreach ($tags as $tagid => $tagvalue) { tag_delete_instance('wiki_pages', $pageid, $tagid); } //Delete Synonym wiki_delete_synonym($subwikiid, $pageid); //Delete all page versions wiki_delete_page_versions(array($pageid => array(0))); //Delete all page locks wiki_delete_locks($pageid); //Delete all page links wiki_delete_links(null, $pageid); //Delete page $params = array('id' => $pageid); $DB->delete_records('wiki_pages', $params); } }
/** * Test the tag removed event. */ public function test_tag_removed() { global $DB; $this->setAdminUser(); // Create a course to tag. $course = $this->getDataGenerator()->create_course(); // Create a wiki page to tag. $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); $wiki = $wikigenerator->create_instance(array('course' => $course->id)); $subwikiid = wiki_add_subwiki($wiki->id, 0); $wikipageid = wiki_create_page($subwikiid, 'Title', FORMAT_HTML, '2'); // Create the tag. $tag = $this->getDataGenerator()->create_tag(); // Assign a tag to a course. tag_assign('course', $course->id, $tag->id, 1, 2, 'core', context_course::instance($course->id)->id); // Trigger and capture the event for untagging a course. $sink = $this->redirectEvents(); coursetag_delete_keyword($tag->id, 2, $course->id); $events = $sink->get_events(); $event = reset($events); // Check that the tag was removed from the course and the event data is valid. $this->assertEquals(0, $DB->count_records('tag_instance')); $this->assertInstanceOf('\\core\\event\\tag_removed', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); // Create the tag. $tag = $this->getDataGenerator()->create_tag(); // Assign a tag to a wiki this time. tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id); // Trigger and capture the event for deleting this tag instance. $sink = $this->redirectEvents(); tag_delete_instance('wiki_pages', $wikipageid, $tag->id); $events = $sink->get_events(); $event = reset($events); // Check that tag was removed from the wiki page and the event data is valid. $this->assertEquals(0, $DB->count_records('tag_instance')); $this->assertInstanceOf('\\core\\event\\tag_removed', $event); $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context()); // Create a tag again - the other would have been deleted since there were no more instances associated with it. $tag = $this->getDataGenerator()->create_tag(); // Assign a tag to the wiki again. tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id); // Now we want to delete this tag, and because there is only one tag instance // associated with it, it should get deleted as well. $sink = $this->redirectEvents(); tag_delete($tag->id); $events = $sink->get_events(); $event = reset($events); // Check that tag was removed from the wiki page and the event data is valid. $this->assertEquals(0, $DB->count_records('tag_instance')); $this->assertInstanceOf('\\core\\event\\tag_removed', $event); $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context()); // Create a tag again - the other would have been deleted since there were no more instances associated with it. $tag = $this->getDataGenerator()->create_tag(); // Assign a tag to the wiki again. tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id); // Delete all tag instances for this wiki instance. $sink = $this->redirectEvents(); tag_delete_instances('mod_wiki', context_module::instance($wiki->cmid)->id); $events = $sink->get_events(); $event = reset($events); // Check that tag was removed from the wiki page and the event data is valid. $this->assertEquals(0, $DB->count_records('tag_instance')); $this->assertInstanceOf('\\core\\event\\tag_removed', $event); $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context()); // Create another wiki. $wiki2 = $wikigenerator->create_instance(array('course' => $course->id)); $subwikiid2 = wiki_add_subwiki($wiki2->id, 0); $wikipageid2 = wiki_create_page($subwikiid2, 'Title', FORMAT_HTML, '2'); // Assign a tag to both wiki pages. tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id); tag_assign('wiki_pages', $wikipageid2, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki2->cmid)->id); // Now remove all tag_instances associated with all wikis. $sink = $this->redirectEvents(); tag_delete_instances('mod_wiki'); $events = $sink->get_events(); // There will be two events - one for each wiki instance removed. $event1 = reset($events); $event2 = $events[1]; // Check that the tags were removed from the wiki pages. $this->assertEquals(0, $DB->count_records('tag_instance')); // Check the first event data is valid. $this->assertInstanceOf('\\core\\event\\tag_removed', $event1); $this->assertEquals(context_module::instance($wiki->cmid), $event1->get_context()); // Check that the second event data is valid. $this->assertInstanceOf('\\core\\event\\tag_removed', $event2); $this->assertEquals(context_module::instance($wiki2->cmid), $event2->get_context()); }
/** * Clean up the tag tables, making sure all tagged object still exists. * * This should normally not be necessary, but in case related tags are not deleted * when the tagged record is removed, this should be done once in a while, perhaps on * an occasional cron run. On a site with lots of tags, this could become an expensive * function to call: don't run at peak time. */ function tag_cleanup() { global $CFG; $instances = get_recordset('tag_instance'); // cleanup tag instances while ($instance = rs_fetch_next_record($instances)) { $delete = false; if (!record_exists('tag', 'id', $instance->tagid)) { // if the tag has been removed, instance should be deleted. $delete = true; } else { switch ($instance->itemtype) { case 'user': // users are marked as deleted, but not actually deleted if (record_exists('user', 'id', $instance->itemid, 'deleted', 1)) { $delete = true; } break; default: // anything else, if the instance is not there, delete. if (!record_exists($instance->itemtype, 'id', $instance->itemid)) { $delete = true; } break; } } if ($delete) { tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid); //debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER); } } rs_close($instances); // TODO: this will only clean tags of type 'default'. This is good as // it won't delete 'official' tags, but the day we get more than two // types, we need to fix this. $unused_tags = get_recordset_sql("SELECT tg.id FROM {$CFG->prefix}tag tg WHERE tg.tagtype = 'default' AND NOT EXISTS (" . "SELECT 'x' FROM {$CFG->prefix}tag_instance ti WHERE ti.tagid = tg.id)"); // cleanup tags while ($unused_tag = rs_fetch_next_record($unused_tags)) { tag_delete($unused_tag->id); //debugging('deleting unused tag #'. $unused_tag->id, DEBUG_DEVELOPER); } rs_close($unused_tags); }
/** * Deletes a personal tag for a user for a course. * * @package core_tag * @category tag * @param int $tagid the tag we wish to delete * @param int $userid the user that the tag is associated with * @param int $courseid the course that the tag is associated with */ function coursetag_delete_keyword($tagid, $userid, $courseid) { tag_delete_instance('course', $courseid, $tagid, $userid); }
/** * Clean up the tag tables, making sure all tagged object still exists. * * This should normally not be necessary, but in case related tags are not deleted * when the tagged record is removed, this should be done once in a while, perhaps on * an occasional cron run. On a site with lots of tags, this could become an expensive * function to call: don't run at peak time. */ function tag_cleanup() { global $DB; $instances = $DB->get_recordset('tag_instance'); // cleanup tag instances foreach ($instances as $instance) { $delete = false; if (!$DB->record_exists('tag', array('id' => $instance->tagid))) { // if the tag has been removed, instance should be deleted. $delete = true; } else { switch ($instance->itemtype) { case 'user': // users are marked as deleted, but not actually deleted if ($DB->record_exists('user', array('id' => $instance->itemid, 'deleted' => 1))) { $delete = true; } break; default: // anything else, if the instance is not there, delete. if (!$DB->record_exists($instance->itemtype, array('id' => $instance->itemid))) { $delete = true; } break; } } if ($delete) { tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid); //debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER); } } $instances->close(); // TODO: this will only clean tags of type 'default'. This is good as // it won't delete 'official' tags, but the day we get more than two // types, we need to fix this. $unused_tags = $DB->get_recordset_sql("SELECT tg.id\n FROM {tag} tg\n WHERE tg.tagtype = 'default'\n AND NOT EXISTS (\n SELECT 'x'\n FROM {tag_instance} ti\n WHERE ti.tagid = tg.id\n )"); // cleanup tags foreach ($unused_tags as $unused_tag) { tag_delete($unused_tag->id); //debugging('deleting unused tag #'. $unused_tag->id, DEBUG_DEVELOPER); } $unused_tags->close(); }
/** * Course tagging function used only during the deletion of a course (called by lib/moodlelib.php) to clean up associated tags * * @package core_tag * @param int $courseid the course we wish to delete tag instances from * @param bool $showfeedback if we should output a notification of the delete to the end user */ function coursetag_delete_course_tags($courseid, $showfeedback = false) { global $DB, $OUTPUT; if ($taginstances = $DB->get_recordset_select('tag_instance', "itemtype = 'course' AND itemid = :courseid", array('courseid' => $courseid), '', 'tagid, tiuserid')) { foreach ($taginstances as $record) { tag_delete_instance('course', $courseid, $record->tagid, $record->tiuserid); } $taginstances->close(); } if ($showfeedback) { echo $OUTPUT->notification(get_string('deletedcoursetags', 'tag'), 'notifysuccess'); } }
/** * Set the tags assigned to a record. This overwrites the current tags. * * This function is meant to be fed the string coming up from the user interface, which contains all tags assigned to a record. * * @package core_tag * @category tag * @access public * @param string $record_type the type of record to tag ('post' for blogs, 'user' for users, 'tag' for tags, etc.) * @param int $record_id the id of the record to tag * @param array $tags the array of tags to set on the record. If given an empty array, all tags will be removed. * @param string|null $component the component that was tagged * @param int|null $contextid the context id of where this tag was assigned * @return bool|null */ function tag_set($record_type, $record_id, $tags, $component = null, $contextid = null) { static $in_recursion_semaphore = false; // this is to prevent loops when tagging a tag if ($record_type == 'tag' && !$in_recursion_semaphore) { $current_tagged_tag_name = tag_get_name($record_id); } $tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY); // force an array, even if we only have one tag. $cleaned_tags = tag_normalize($tags); //echo 'tags-in-tag_set'; var_dump($tags); var_dump($tags_ids); var_dump($cleaned_tags); $current_ids = tag_get_tags_ids($record_type, $record_id); //var_dump($current_ids); // for data coherence reasons, it's better to remove deleted tags // before adding new data: ordering could be duplicated. foreach ($current_ids as $current_id) { if (!in_array($current_id, $tags_ids)) { tag_delete_instance($record_type, $record_id, $current_id); if ($record_type == 'tag' && !$in_recursion_semaphore) { // if we are removing a tag-on-a-tag (manually related tag), // we need to remove the opposite relationship as well. tag_delete_instance('tag', $current_id, $record_id); } } } if (empty($tags)) { return true; } foreach ($tags as $ordering => $tag) { $tag = trim($tag); if (!$tag) { continue; } $clean_tag = $cleaned_tags[$tag]; $tag_current_id = $tags_ids[$clean_tag]; if (is_null($tag_current_id)) { // create new tags //echo "call to add tag $tag\n"; $new_tag = tag_add($tag); $tag_current_id = $new_tag[$clean_tag]; } tag_assign($record_type, $record_id, $tag_current_id, $ordering, 0, $component, $contextid); // if we are tagging a tag (adding a manually-assigned related tag), we // need to create the opposite relationship as well. if ($record_type == 'tag' && !$in_recursion_semaphore) { $in_recursion_semaphore = true; tag_set_add('tag', $tag_current_id, $current_tagged_tag_name, $component, $contextid); $in_recursion_semaphore = false; } } }
/** * * */ function bookmarks_untag($itemid, $tagname) { $tag = tag_get('name', $tagname); return tag_delete_instance('bookmark', $itemid, $tag->id); }
function wiki_reset_userdata($data) { global $CFG, $DB; require_once $CFG->dirroot . '/mod/wiki/pagelib.php'; require_once $CFG->dirroot . '/tag/lib.php'; require_once $CFG->dirroot . "/mod/wiki/locallib.php"; $componentstr = get_string('modulenameplural', 'wiki'); $status = array(); //get the wiki(s) in this course. if (!($wikis = $DB->get_records('wiki', array('course' => $data->courseid)))) { return false; } $errors = false; foreach ($wikis as $wiki) { if (!($cm = get_coursemodule_from_instance('wiki', $wiki->id))) { continue; } $context = context_module::instance($cm->id); // Remove tags or all pages. if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) { // Get subwiki information. $subwikis = wiki_get_subwikis($wiki->id); foreach ($subwikis as $subwiki) { // Get existing pages. if ($pages = wiki_get_page_list($subwiki->id)) { // If the wiki page isn't selected then we are only removing tags. if (empty($data->reset_wiki_pages)) { // Go through each page and delete the tags. foreach ($pages as $page) { $tags = tag_get_tags_array('wiki_pages', $page->id); foreach ($tags as $tagid => $tagname) { // Delete the related tag_instances related to the wiki page. $errors = tag_delete_instance('wiki_pages', $page->id, $tagid); $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => $errors); } } } else { // Otherwise we are removing pages and tags. wiki_delete_pages($context, $pages, $subwiki->id); } } if (!empty($data->reset_wiki_pages)) { // Delete any subwikis. $DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING); // Delete any attached files. $fs = get_file_storage(); $fs->delete_area_files($context->id, 'mod_wiki', 'attachments'); $status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'), 'error' => $errors); } } } // Remove all comments. if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) { $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id)); $status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false); } } return $status; }
/** * Delete pages and all related data * * @param mixed $context context in which page needs to be deleted. * @param mixed $pageids id's of pages to be deleted * @param int $subwikiid id of the subwiki for which all pages should be deleted */ function wiki_delete_pages($context, $pageids = null, $subwikiid = null) { global $DB, $CFG; if (!empty($pageids) && is_int($pageids)) { $pageids = array($pageids); } else { if (!empty($subwikiid)) { $pageids = wiki_get_page_list($subwikiid); } } //If there is no pageid then return as we can't delete anything. if (empty($pageids)) { return; } require_once $CFG->dirroot . '/tag/lib.php'; /// Delete page and all it's relevent data foreach ($pageids as $pageid) { if (is_object($pageid)) { $pageid = $pageid->id; } //Delete page comments $comments = wiki_get_comments($context->id, $pageid); foreach ($comments as $commentid => $commentvalue) { wiki_delete_comment($commentid, $context, $pageid); } //Delete page tags $tags = tag_get_tags_array('wiki_pages', $pageid); foreach ($tags as $tagid => $tagvalue) { tag_delete_instance('wiki_pages', $pageid, $tagid); } //Delete Synonym wiki_delete_synonym($subwikiid, $pageid); //Delete all page versions wiki_delete_page_versions(array($pageid => array(0)), $context); //Delete all page locks wiki_delete_locks($pageid); //Delete all page links wiki_delete_links(null, $pageid); $params = array('id' => $pageid); // Get page before deleting. $page = $DB->get_record('wiki_pages', $params); //Delete page $DB->delete_records('wiki_pages', $params); // Trigger page_deleted event. $event = \mod_wiki\event\page_deleted::create(array('context' => $context, 'objectid' => $pageid, 'other' => array('subwikiid' => $subwikiid))); $event->add_record_snapshot('wiki_pages', $page); $event->trigger(); } }