public function process_tag($data) { global $CFG, $DB; $data = (object)$data; if (!empty($CFG->usetags)) { // if enabled in server // TODO: This is highly inneficient. Each time we add one tag // we fetch all the existing because tag_set() deletes them // so everything must be reinserted on each call $tags = array(); $existingtags = tag_get_tags('course', $this->get_courseid()); // Re-add all the existitng tags foreach ($existingtags as $existingtag) { $tags[] = $existingtag->rawname; } // Add the one being restored $tags[] = $data->rawname; // Send all the tags back to the course tag_set('course', $this->get_courseid(), $tags); } }
protected function process_tag($data) { global $CFG, $DB; $data = (object) $data; $newquestion = $this->get_new_parentid('question'); if (!empty($CFG->usetags)) { // if enabled in server // TODO: This is highly inefficient. Each time we add one tag // we fetch all the existing because tag_set() deletes them // so everything must be reinserted on each call $tags = array(); $existingtags = tag_get_tags('question', $newquestion); // Re-add all the existitng tags foreach ($existingtags as $existingtag) { $tags[] = $existingtag->rawname; } // Add the one being restored $tags[] = $data->rawname; // Get the category, so we can then later get the context. $categoryid = $this->get_new_parentid('question_category'); if (empty($this->cachedcategory) || $this->cachedcategory->id != $categoryid) { $this->cachedcategory = $DB->get_record('question_categories', array('id' => $categoryid)); } // Send all the tags back to the question tag_set('question', $newquestion, $tags, 'core_question', $this->cachedcategory->contextid); } }
function backup_user_info($bf, $preferences) { global $CFG; require_once $CFG->dirroot . '/tag/lib.php'; $status = true; // Use a recordset to for the memory handling on to // the DB and run faster // Note the outer join with mnet_host: It shouldn't be neccesary // but there are some sites having mnet_host records missing // and that causes backup to fail (no users). Being a bit more // flexible here (outer joing) we bypass the problem and doesn't // cause more troubles. Eloy - MDL-16879 $users = get_recordset_sql("SELECT b.old_id, b.table_name, b.info,\n u.*, m.wwwroot\n FROM {$CFG->prefix}backup_ids b\n JOIN {$CFG->prefix}user u ON b.old_id=u.id\n LEFT JOIN {$CFG->prefix}mnet_host m ON u.mnethostid=m.id\n WHERE b.backup_code = '{$preferences->backup_unique_code}' AND\n b.table_name = 'user'"); //If we have users to backup if ($users && !rs_EOF($users)) { //Begin Users tag fwrite($bf, start_tag("USERS", 2, true)); $counter = 0; //With every user while ($user = rs_fetch_next_record($users)) { //Begin User tag fwrite($bf, start_tag("USER", 3, true)); //Output all user data fwrite($bf, full_tag("ID", 4, false, $user->id)); fwrite($bf, full_tag("AUTH", 4, false, $user->auth)); fwrite($bf, full_tag("CONFIRMED", 4, false, $user->confirmed)); fwrite($bf, full_tag("POLICYAGREED", 4, false, $user->policyagreed)); fwrite($bf, full_tag("DELETED", 4, false, $user->deleted)); fwrite($bf, full_tag("USERNAME", 4, false, $user->username)); // Prevent user passwords in backup files unless // $CFG->includeuserpasswordsinbackup is defined. MDL-20838 if (!empty($CFG->includeuserpasswordsinbackup)) { fwrite($bf, full_tag("PASSWORD", 4, false, $user->password)); } fwrite($bf, full_tag("IDNUMBER", 4, false, $user->idnumber)); fwrite($bf, full_tag("FIRSTNAME", 4, false, $user->firstname)); fwrite($bf, full_tag("LASTNAME", 4, false, $user->lastname)); fwrite($bf, full_tag("EMAIL", 4, false, $user->email)); fwrite($bf, full_tag("EMAILSTOP", 4, false, $user->emailstop)); fwrite($bf, full_tag("ICQ", 4, false, $user->icq)); fwrite($bf, full_tag("SKYPE", 4, false, $user->skype)); fwrite($bf, full_tag("YAHOO", 4, false, $user->yahoo)); fwrite($bf, full_tag("AIM", 4, false, $user->aim)); fwrite($bf, full_tag("MSN", 4, false, $user->msn)); fwrite($bf, full_tag("PHONE1", 4, false, $user->phone1)); fwrite($bf, full_tag("PHONE2", 4, false, $user->phone2)); fwrite($bf, full_tag("INSTITUTION", 4, false, $user->institution)); fwrite($bf, full_tag("DEPARTMENT", 4, false, $user->department)); fwrite($bf, full_tag("ADDRESS", 4, false, $user->address)); fwrite($bf, full_tag("CITY", 4, false, $user->city)); fwrite($bf, full_tag("COUNTRY", 4, false, $user->country)); fwrite($bf, full_tag("LANG", 4, false, $user->lang)); fwrite($bf, full_tag("THEME", 4, false, $user->theme)); fwrite($bf, full_tag("TIMEZONE", 4, false, $user->timezone)); fwrite($bf, full_tag("FIRSTACCESS", 4, false, $user->firstaccess)); fwrite($bf, full_tag("LASTACCESS", 4, false, $user->lastaccess)); fwrite($bf, full_tag("LASTLOGIN", 4, false, $user->lastlogin)); fwrite($bf, full_tag("CURRENTLOGIN", 4, false, $user->currentlogin)); fwrite($bf, full_tag("LASTIP", 4, false, $user->lastip)); fwrite($bf, full_tag("PICTURE", 4, false, $user->picture)); fwrite($bf, full_tag("URL", 4, false, $user->url)); fwrite($bf, full_tag("DESCRIPTION", 4, false, $user->description)); fwrite($bf, full_tag("MAILFORMAT", 4, false, $user->mailformat)); fwrite($bf, full_tag("MAILDIGEST", 4, false, $user->maildigest)); fwrite($bf, full_tag("MAILDISPLAY", 4, false, $user->maildisplay)); fwrite($bf, full_tag("HTMLEDITOR", 4, false, $user->htmleditor)); fwrite($bf, full_tag("AJAX", 4, false, $user->ajax)); fwrite($bf, full_tag("AUTOSUBSCRIBE", 4, false, $user->autosubscribe)); fwrite($bf, full_tag("TRACKFORUMS", 4, false, $user->trackforums)); if ($user->mnethostid != $CFG->mnet_localhost_id && !empty($user->wwwroot)) { fwrite($bf, full_tag("MNETHOSTURL", 4, false, $user->wwwroot)); } fwrite($bf, full_tag("TIMEMODIFIED", 4, false, $user->timemodified)); /// write assign/override code for context_userid $user->isneeded = strpos($user->info, "needed"); fwrite($bf, start_tag("ROLES", 4, true)); if ($user->info != "needed" && $user->info != "") { //PRINT ROLE INFO $roles = explode(",", $user->info); foreach ($roles as $role) { if ($role != "" && $role != "needed") { fwrite($bf, start_tag("ROLE", 5, true)); //Print Role info fwrite($bf, full_tag("TYPE", 6, false, $role)); //Print ROLE end fwrite($bf, end_tag("ROLE", 5, true)); } } } //Needed if ($user->isneeded !== false) { //Print ROLE start fwrite($bf, start_tag("ROLE", 5, true)); //Print Role info fwrite($bf, full_tag("TYPE", 6, false, "needed")); //Print ROLE end fwrite($bf, end_tag("ROLE", 5, true)); } //End ROLES tag fwrite($bf, end_tag("ROLES", 4, true)); //Check if we have custom profile fields to backup if ($cpfields = get_records_sql("SELECT uif.shortname, uif.datatype, uidata.data\n FROM {$CFG->prefix}user_info_field uif,\n {$CFG->prefix}user_info_data uidata\n WHERE uif.id = uidata.fieldid\n AND uidata.userid = {$user->id}")) { //Start USER_CUSTOM_PROFILE_FIELDS tag fwrite($bf, start_tag("USER_CUSTOM_PROFILE_FIELDS", 4, true)); //Write custom profile fields foreach ($cpfields as $cpfield) { fwrite($bf, start_tag("USER_CUSTOM_PROFILE_FIELD", 5, true)); fwrite($bf, full_tag("FIELD_NAME", 6, false, $cpfield->shortname)); fwrite($bf, full_tag("FIELD_TYPE", 6, false, $cpfield->datatype)); fwrite($bf, full_tag("FIELD_DATA", 6, false, $cpfield->data)); fwrite($bf, end_tag("USER_CUSTOM_PROFILE_FIELD", 5, true)); } //End USER_CUSTOM_PROFILE_FIELDS tag fwrite($bf, end_tag("USER_CUSTOM_PROFILE_FIELDS", 4, true)); } //Check if we have user tags to backup if (!empty($CFG->usetags)) { if ($tags = tag_get_tags('user', $user->id)) { //This return them ordered by default //Start USER_TAGS tag fwrite($bf, start_tag("USER_TAGS", 4, true)); //Write user tags fields foreach ($tags as $tag) { fwrite($bf, start_tag("USER_TAG", 5, true)); fwrite($bf, full_tag("NAME", 6, false, $tag->name)); fwrite($bf, full_tag("RAWNAME", 6, false, $tag->rawname)); fwrite($bf, end_tag("USER_TAG", 5, true)); } //End USER_TAGS tag fwrite($bf, end_tag("USER_TAGS", 4, true)); } } //Check if we have user_preferences to backup if ($preferences_data = get_records("user_preferences", "userid", $user->old_id)) { //Start USER_PREFERENCES tag fwrite($bf, start_tag("USER_PREFERENCES", 4, true)); //Write each user_preference foreach ($preferences_data as $user_preference) { fwrite($bf, start_tag("USER_PREFERENCE", 5, true)); fwrite($bf, full_tag("NAME", 6, false, $user_preference->name)); fwrite($bf, full_tag("VALUE", 6, false, $user_preference->value)); fwrite($bf, end_tag("USER_PREFERENCE", 5, true)); } //End USER_PREFERENCES tag fwrite($bf, end_tag("USER_PREFERENCES", 4, true)); } $context = get_context_instance(CONTEXT_USER, $user->old_id); write_role_overrides_xml($bf, $context, 4); /// write role_assign code here write_role_assignments_xml($bf, $preferences, $context, 4); //End User tag fwrite($bf, end_tag("USER", 3, true)); //Do some output $counter++; if ($counter % 10 == 0) { echo "."; if ($counter % 200 == 0) { echo "<br />"; } backup_flush(300); } } //End Users tag fwrite($bf, end_tag("USERS", 2, true)); } else { // There aren't any users. $status = true; } if ($users) { rs_close($users); } return $status; }
/** * Returns tags related to a tag * * Related tags of a tag come from two sources: * - manually added related tags, which are tag_instance entries for that tag * - correlated tags, which are a calculated * * @param string $tag_name_or_id is a single **normalized** tag name or the id * of a tag * @param int $type the function will return either manually * (TAG_RELATED_MANUAL) related tags or correlated (TAG_RELATED_CORRELATED) * tags. Default is TAG_RELATED_ALL, which returns everything. * @param int $limitnum return a subset comprising this many records (optional, * default is 10) * @return array an array of tag objects */ function tag_get_related_tags($tagid, $type = TAG_RELATED_ALL, $limitnum = 10) { $related_tags = array(); if ($type == TAG_RELATED_ALL || $type == TAG_RELATED_MANUAL) { //gets the manually added related tags $related_tags = tag_get_tags('tag', $tagid); } if ($type == TAG_RELATED_ALL || $type == TAG_RELATED_CORRELATED) { //gets the correlated tags $automatic_related_tags = tag_get_correlated($tagid, $limitnum); if (is_array($automatic_related_tags)) { $related_tags = array_merge($related_tags, $automatic_related_tags); } } return array_slice(object_array_unique($related_tags), 0, $limitnum); }
/** * Test for function tag_compute_correlations() that is part of tag cron */ public function test_correlations() { global $DB; $task = new \core\task\tag_cron_task(); $tags = array_map(function ($t) { return $t->id; }, $this->prepare_correlated()); $task->compute_correlations(); $this->assertEquals($tags['cats'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['cat']))); $this->assertEquals($tags['cat'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['cats']))); $this->assertEquals($tags['dogs'] . ',' . $tags['puppy'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['dog']))); $this->assertEquals($tags['dog'] . ',' . $tags['puppy'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['dogs']))); $this->assertEquals($tags['dog'] . ',' . $tags['dogs'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['puppy']))); // Make sure get_correlated_tags() returns 'cats' as the only correlated tag to the 'cat'. $correlatedtags = array_values(core_tag_tag::get($tags['cat'])->get_correlated_tags(true)); $this->assertCount(3, $correlatedtags); // This will return all existing instances but they all point to the same tag. $this->assertEquals('cats', $correlatedtags[0]->rawname); $this->assertEquals('cats', $correlatedtags[1]->rawname); $this->assertEquals('cats', $correlatedtags[2]->rawname); $correlatedtags = array_values(core_tag_tag::get($tags['cat'])->get_correlated_tags()); $this->assertCount(1, $correlatedtags); // Duplicates are filtered out here. $this->assertEquals('cats', $correlatedtags[0]->rawname); // Make sure tag_get_correlated() returns 'dogs' and 'puppy' as the correlated tags to the 'dog'. $correlatedtags = core_tag_tag::get($tags['dog'])->get_correlated_tags(true); $this->assertCount(6, $correlatedtags); // 2 tags times 3 instances. $correlatedtags = array_values(core_tag_tag::get($tags['dog'])->get_correlated_tags()); $this->assertCount(2, $correlatedtags); $this->assertEquals('dogs', $correlatedtags[0]->rawname); $this->assertEquals('puppy', $correlatedtags[1]->rawname); // Function get_related_tags() will return both related and correlated tags. $relatedtags = array_values(core_tag_tag::get($tags['cat'])->get_related_tags()); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // Also test deprecated method tag_get_related_tags() and tag_get_correlated(). $correlatedtags = array_values(tag_get_correlated($tags['cat'])); $this->assertDebuggingCalled(); $this->assertCount(3, $correlatedtags); // This will return all existing instances but they all point to the same tag. $this->assertEquals('cats', $correlatedtags[0]->rawname); $this->assertEquals('cats', $correlatedtags[1]->rawname); $this->assertEquals('cats', $correlatedtags[2]->rawname); $correlatedtags = array_values(tag_get_related_tags($tags['cat'], TAG_RELATED_CORRELATED)); $this->assertDebuggingCalled(); $this->assertCount(1, $correlatedtags); // Duplicates are filtered out here. $this->assertEquals('cats', $correlatedtags[0]->rawname); $correlatedtags = array_values(tag_get_correlated($tags['dog'])); $this->assertDebuggingCalled(); $this->assertCount(6, $correlatedtags); // 2 tags times 3 instances. $correlatedtags = array_values(tag_get_related_tags($tags['dog'], TAG_RELATED_CORRELATED)); $this->assertDebuggingCalled(); $this->assertCount(2, $correlatedtags); $this->assertEquals('dogs', $correlatedtags[0]->rawname); $this->assertEquals('puppy', $correlatedtags[1]->rawname); $relatedtags = array_values(tag_get_related_tags($tags['cat'])); $this->assertDebuggingCalled(); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // End of testing deprecated methods. // If we then manually set 'cat' and 'cats' as related, get_related_tags() will filter out duplicates. core_tag_tag::get($tags['cat'])->set_related_tags(array('kitten', 'cats')); $relatedtags = array_values(core_tag_tag::get($tags['cat'])->get_related_tags()); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // Make sure core_tag_tag::get_item_tags(), core_tag_tag::get_correlated_tags() return the same set of fields. $relatedtags = core_tag_tag::get_item_tags('core', 'tag', $tags['cat']); $relatedtag = reset($relatedtags); $correlatedtags = core_tag_tag::get($tags['cat'])->get_correlated_tags(); $correlatedtag = reset($correlatedtags); $this->assertEquals(array_keys((array) $relatedtag->to_object()), array_keys((array) $correlatedtag->to_object())); // Make sure tag_get_correlated() and tag_get_tags() return the same set of fields. // Both functions were deprecated in 3.1. $relatedtags = tag_get_tags('tag', $tags['cat']); $this->assertDebuggingCalled(); $relatedtag = reset($relatedtags); $correlatedtags = tag_get_correlated($tags['cat']); $this->assertDebuggingCalled(); $correlatedtag = reset($correlatedtags); $this->assertEquals(array_keys((array) $relatedtag), array_keys((array) $correlatedtag)); }
/** * Test for function tag_compute_correlations() that is part of tag cron */ public function test_correlations() { global $DB; $task = new \core\task\tag_cron_task(); $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $user6 = $this->getDataGenerator()->create_user(); // Several records have both 'cat' and 'cats' tags attached to them. // This will make those tags automatically correlated. // Same with 'dog', 'dogs' and 'puppy. core_tag_tag::set_item_tags('core', 'user', $user1->id, context_user::instance($user1->id), array('cat', 'cats')); core_tag_tag::set_item_tags('core', 'user', $user2->id, context_user::instance($user2->id), array('cat', 'cats', 'kitten')); core_tag_tag::set_item_tags('core', 'user', $user3->id, context_user::instance($user3->id), array('cat', 'cats')); core_tag_tag::set_item_tags('core', 'user', $user4->id, context_user::instance($user4->id), array('dog', 'dogs', 'puppy')); core_tag_tag::set_item_tags('core', 'user', $user5->id, context_user::instance($user5->id), array('dog', 'dogs', 'puppy')); core_tag_tag::set_item_tags('core', 'user', $user6->id, context_user::instance($user6->id), array('dog', 'dogs', 'puppy')); $tags = core_tag_tag::get_by_name_bulk(core_tag_collection::get_default(), array('cat', 'cats', 'dog', 'dogs', 'kitten', 'puppy')); $tags = array_map(function ($t) { return $t->id; }, $tags); // Add manual relation between tags 'cat' and 'kitten'. core_tag_tag::get($tags['cat'])->set_related_tags(array('kitten')); $task->compute_correlations(); $this->assertEquals($tags['cats'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['cat']))); $this->assertEquals($tags['cat'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['cats']))); $this->assertEquals($tags['dogs'] . ',' . $tags['puppy'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['dog']))); $this->assertEquals($tags['dog'] . ',' . $tags['puppy'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['dogs']))); $this->assertEquals($tags['dog'] . ',' . $tags['dogs'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['puppy']))); // Make sure get_correlated_tags() returns 'cats' as the only correlated tag to the 'cat'. $correlatedtags = array_values(core_tag_tag::get($tags['cat'])->get_correlated_tags(true)); $this->assertCount(3, $correlatedtags); // This will return all existing instances but they all point to the same tag. $this->assertEquals('cats', $correlatedtags[0]->rawname); $this->assertEquals('cats', $correlatedtags[1]->rawname); $this->assertEquals('cats', $correlatedtags[2]->rawname); $correlatedtags = array_values(core_tag_tag::get($tags['cat'])->get_correlated_tags()); $this->assertCount(1, $correlatedtags); // Duplicates are filtered out here. $this->assertEquals('cats', $correlatedtags[0]->rawname); // Make sure tag_get_correlated() returns 'dogs' and 'puppy' as the correlated tags to the 'dog'. $correlatedtags = core_tag_tag::get($tags['dog'])->get_correlated_tags(true); $this->assertCount(6, $correlatedtags); // 2 tags times 3 instances. $correlatedtags = array_values(core_tag_tag::get($tags['dog'])->get_correlated_tags()); $this->assertCount(2, $correlatedtags); $this->assertEquals('dogs', $correlatedtags[0]->rawname); $this->assertEquals('puppy', $correlatedtags[1]->rawname); // Function get_related_tags() will return both related and correlated tags. $relatedtags = array_values(core_tag_tag::get($tags['cat'])->get_related_tags()); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // Also test deprecated method tag_get_related_tags() and tag_get_correlated(). $correlatedtags = array_values(tag_get_correlated($tags['cat'])); $this->assertDebuggingCalled(); $this->assertCount(3, $correlatedtags); // This will return all existing instances but they all point to the same tag. $this->assertEquals('cats', $correlatedtags[0]->rawname); $this->assertEquals('cats', $correlatedtags[1]->rawname); $this->assertEquals('cats', $correlatedtags[2]->rawname); $correlatedtags = array_values(tag_get_related_tags($tags['cat'], TAG_RELATED_CORRELATED)); $this->assertDebuggingCalled(); $this->assertCount(1, $correlatedtags); // Duplicates are filtered out here. $this->assertEquals('cats', $correlatedtags[0]->rawname); $correlatedtags = array_values(tag_get_correlated($tags['dog'])); $this->assertDebuggingCalled(); $this->assertCount(6, $correlatedtags); // 2 tags times 3 instances. $correlatedtags = array_values(tag_get_related_tags($tags['dog'], TAG_RELATED_CORRELATED)); $this->assertDebuggingCalled(); $this->assertCount(2, $correlatedtags); $this->assertEquals('dogs', $correlatedtags[0]->rawname); $this->assertEquals('puppy', $correlatedtags[1]->rawname); $relatedtags = array_values(tag_get_related_tags($tags['cat'])); $this->assertDebuggingCalled(); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // End of testing deprecated methods. // If we then manually set 'cat' and 'cats' as related, get_related_tags() will filter out duplicates. core_tag_tag::get($tags['cat'])->set_related_tags(array('kitten', 'cats')); $relatedtags = array_values(core_tag_tag::get($tags['cat'])->get_related_tags()); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // Make sure core_tag_tag::get_item_tags(), core_tag_tag::get_correlated_tags() return the same set of fields. $relatedtags = core_tag_tag::get_item_tags('core', 'tag', $tags['cat']); $relatedtag = reset($relatedtags); $correlatedtags = core_tag_tag::get($tags['cat'])->get_correlated_tags(); $correlatedtag = reset($correlatedtags); $this->assertEquals(array_keys((array) $relatedtag->to_object()), array_keys((array) $correlatedtag->to_object())); // Make sure tag_get_correlated() and tag_get_tags() return the same set of fields. // Both functions were deprecated in 3.1. $relatedtags = tag_get_tags('tag', $tags['cat']); $this->assertDebuggingCalled(); $relatedtag = reset($relatedtags); $correlatedtags = tag_get_correlated($tags['cat']); $this->assertDebuggingCalled(); $correlatedtag = reset($correlatedtags); $this->assertEquals(array_keys((array) $relatedtag), array_keys((array) $correlatedtag)); }
function backup_blog($bf, $blogid, $level) { global $DB; $blog = $DB->get_record('post', array('module' => 'blog', 'id' => $blogid)); /// start blog fwrite($bf, start_tag("BLOG", $level, true)); /// blog body fwrite($bf, full_tag("ID", $level + 1, false, $blog->id)); fwrite($bf, full_tag("MODULE", $level + 1, false, $blog->module)); fwrite($bf, full_tag("USERID", $level + 1, false, $blog->userid)); fwrite($bf, full_tag("COURSEID", $level + 1, false, $blog->courseid)); fwrite($bf, full_tag("GROUPID", $level + 1, false, $blog->groupid)); fwrite($bf, full_tag("MODULEID", $level + 1, false, $blog->moduleid)); fwrite($bf, full_tag("COURSEMODULEID", $level + 1, false, $blog->coursemoduleid)); fwrite($bf, full_tag("SUBJECT", $level + 1, false, $blog->subject)); fwrite($bf, full_tag("SUMMARY", $level + 1, false, $blog->summary)); fwrite($bf, full_tag("CONTENT", $level + 1, false, $blog->content)); fwrite($bf, full_tag("UNIQUEHASH", $level + 1, false, $blog->uniquehash)); fwrite($bf, full_tag("RATING", $level + 1, false, $blog->rating)); fwrite($bf, full_tag("FORMAT", $level + 1, false, $blog->format)); fwrite($bf, full_tag("ATTACHMENT", $level + 1, false, $blog->attachment)); fwrite($bf, full_tag("PUBLISHSTATE", $level + 1, false, $blog->publishstate)); fwrite($bf, full_tag("LASTMODIFIED", $level + 1, false, $blog->lastmodified)); fwrite($bf, full_tag("CREATED", $level + 1, false, $blog->created)); fwrite($bf, full_tag("USERMODIFIED", $level + 1, false, $blog->usermodified)); /// Blog tags /// Check if we have blog tags to backup if (!empty($CFG->usetags)) { if ($tags = tag_get_tags('post', $blog->id)) { //This return them ordered by default /// Start BLOG_TAGS tag fwrite($bf, start_tag("BLOG_TAGS", $level + 1, true)); /// Write blog tags fields foreach ($tags as $tag) { fwrite($bf, start_tag("BLOG_TAG", $level + 2, true)); fwrite($bf, full_tag("NAME", $level + 3, false, $tag->name)); fwrite($bf, full_tag("RAWNAME", $level + 3, false, $tag->rawname)); fwrite($bf, end_tag("BLOG_TAG", $level + 2, true)); } /// End BLOG_TAGS tag fwrite($bf, end_tag("BLOG_TAGS", $level + 1, true)); } } /// end blog fwrite($bf, end_tag("BLOG", $level, true)); }
/** * * * */ function bookmarks_get_item_tags($id) { return tag_get_tags('bookmark', $id); }
/** * Returns tags related to a tag * * Related tags of a tag come from two sources: * - manually added related tags, which are tag_instance entries for that tag * - correlated tags, which are calculated * * @package core_tag * @category tag * @access public * @param string $tagid is a single **normalized** tag name or the id of a tag * @param int $type the function will return either manually (TAG_RELATED_MANUAL) related tags or correlated * (TAG_RELATED_CORRELATED) tags. Default is TAG_RELATED_ALL, which returns everything. * @param int $limitnum (optional) return a subset comprising this many records, the default is 10 * @return array an array of tag objects */ function tag_get_related_tags($tagid, $type = TAG_RELATED_ALL, $limitnum = 10) { $related_tags = array(); if ($type == TAG_RELATED_ALL || $type == TAG_RELATED_MANUAL) { //gets the manually added related tags $related_tags = tag_get_tags('tag', $tagid); } if ($type == TAG_RELATED_ALL || $type == TAG_RELATED_CORRELATED) { //gets the correlated tags $automatic_related_tags = tag_get_correlated($tagid); $related_tags = array_merge($related_tags, $automatic_related_tags); } // Remove duplicated tags (multiple instances of the same tag). $seen = array(); foreach ($related_tags as $instance => $tag) { if (isset($seen[$tag->id])) { unset($related_tags[$instance]); } else { $seen[$tag->id] = 1; } } return array_slice($related_tags, 0, $limitnum); }
/** * Test for function tag_compute_correlations() that is part of tag cron */ public function test_correlations() { global $DB; $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $user6 = $this->getDataGenerator()->create_user(); // Several records have both 'cat' and 'cats' tags attached to them. // This will make those tags automatically correlated. // Same with 'dog', 'dogs' and 'puppy. tag_set('user', $user1->id, array('cat', 'cats'), 'core', context_user::instance($user1->id)->id); tag_set('user', $user2->id, array('cat', 'cats', 'kitten'), 'core', context_user::instance($user2->id)->id); tag_set('user', $user3->id, array('cat', 'cats'), 'core', context_user::instance($user3->id)->id); tag_set('user', $user4->id, array('dog', 'dogs', 'puppy'), 'core', context_user::instance($user4->id)->id); tag_set('user', $user5->id, array('dog', 'dogs', 'puppy'), 'core', context_user::instance($user5->id)->id); tag_set('user', $user6->id, array('dog', 'dogs', 'puppy'), 'core', context_user::instance($user6->id)->id); $tags = tag_get_id(array('cat', 'cats', 'dog', 'dogs', 'kitten', 'puppy')); // Add manual relation between tags 'cat' and 'kitten'. tag_set('tag', $tags['cat'], array('kitten'), 'core', context_system::instance()->id); tag_compute_correlations(); $this->assertEquals($tags['cats'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['cat']))); $this->assertEquals($tags['cat'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['cats']))); $this->assertEquals($tags['dogs'] . ',' . $tags['puppy'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['dog']))); $this->assertEquals($tags['dog'] . ',' . $tags['puppy'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['dogs']))); $this->assertEquals($tags['dog'] . ',' . $tags['dogs'], $DB->get_field_select('tag_correlation', 'correlatedtags', 'tagid = ?', array($tags['puppy']))); // Make sure tag_get_correlated() returns 'cats' as the only correlated tag to the 'cat'. $correlatedtags = array_values(tag_get_correlated($tags['cat'])); $this->assertCount(3, $correlatedtags); // This will return all existing instances but they all point to the same tag. $this->assertEquals('cats', $correlatedtags[0]->rawname); $this->assertEquals('cats', $correlatedtags[1]->rawname); $this->assertEquals('cats', $correlatedtags[2]->rawname); $correlatedtags = array_values(tag_get_related_tags($tags['cat'], TAG_RELATED_CORRELATED)); $this->assertCount(1, $correlatedtags); // Duplicates are filtered out here. $this->assertEquals('cats', $correlatedtags[0]->rawname); // Make sure tag_get_correlated() returns 'dogs' and 'puppy' as the correlated tags to the 'dog'. $correlatedtags = array_values(tag_get_correlated($tags['dog'])); $this->assertCount(6, $correlatedtags); // 2 tags times 3 instances. $correlatedtags = array_values(tag_get_related_tags($tags['dog'], TAG_RELATED_CORRELATED)); $this->assertCount(2, $correlatedtags); $this->assertEquals('dogs', $correlatedtags[0]->rawname); $this->assertEquals('puppy', $correlatedtags[1]->rawname); // Function tag_get_related_tags() with default argument will return both related and correlated tags. $relatedtags = array_values(tag_get_related_tags($tags['cat'])); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // If we then manually set 'cat' and 'cats' as related, tag_get_related_tags() will filter out duplicates. tag_set('tag', $tags['cat'], array('kitten', 'cats'), 'core', context_system::instance()->id); $relatedtags = array_values(tag_get_related_tags($tags['cat'])); $this->assertCount(2, $relatedtags); $this->assertEquals('kitten', $relatedtags[0]->rawname); $this->assertEquals('cats', $relatedtags[1]->rawname); // Make sure tag_get_correlated() and tag_get_tags() return the same set of fields. $relatedtags = tag_get_tags('tag', $tags['cat']); $relatedtag = reset($relatedtags); $correlatedtags = tag_get_correlated($tags['cat']); $correlatedtag = reset($correlatedtags); $this->assertEquals(array_keys((array) $relatedtag), array_keys((array) $correlatedtag)); }
/** * Returns a XHTML string with comma-separated wikitags links * * @param Object $WS WikiStorage */ function wiki_tags_get_tag_links($WS) { global $CFG, $COURSE; $page = wiki_page_last_version($WS->page, $WS); if (!$page) { return null; } $tags = tag_get_tags('wiki', $page->created); if (!empty($tags)) { $tags = array_values($tags); $links = ''; $size = count($tags); for ($i = 0; $i < $size; $i++) { $tag = $tags[$i]; $links .= '<a href="' . $CFG->wwwroot . '/mod/wiki/tags/view.php?cid=' . $COURSE->id . '&cmid=' . $WS->cm->id . '&tagid=' . $tag->id . '">' . tag_display_name($tag) . '</a>'; /* * // when moodle-core (2.0?) allow to add custom items to * // taglist page we can enable this. *$links .= '<a href="'.$CFG->wwwroot.'/tag/index.php?'. * '&id='.$tag->id.'">'.$tag->name.'</a>'; */ if ($i != $size - 1) { $links .= ', '; } } return $links; } return ''; }