Exemple #1
0
/**
 * Calculates and stores the correlated tags of all tags. The correlations are stored in the 'tag_correlation' table.
 *
 * Two tags are correlated if they appear together a lot. Ex.: Users tagged with "computers" will probably also be tagged with "algorithms".
 *
 * The rationale for the 'tag_correlation' table is performance. It works as a cache for a potentially heavy load query done at the
 * 'tag_instance' table. So, the 'tag_correlation' table stores redundant information derived from the 'tag_instance' table.
 *
 * @package core_tag
 * @deprecated since 3.1
 * @param   int      $mincorrelation Only tags with more than $mincorrelation correlations will be identified.
 */
function tag_compute_correlations($mincorrelation = 2)
{
    debugging('Method tag_compute_correlations() is deprecated, ' . 'use \\core\\task\\tag_cron_task::compute_correlations()', DEBUG_DEVELOPER);
    $task = new \core\task\tag_cron_task();
    return $task->compute_correlations($mincorrelation);
}
Exemple #2
0
 /**
  * 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));
 }
Exemple #3
0
 /**
  * Testing function core_tag_tag::combine_tags() when correlated tags are present.
  */
 public function test_combine_tags_with_correlated()
 {
     $task = new \core\task\tag_cron_task();
     $tags = $this->prepare_correlated();
     $task->compute_correlations();
     // Now 'cat' is correlated with 'cats'.
     // Also 'dog', 'dogs' and 'puppy' are correlated.
     // There is a manual relation between 'cat' and 'kitten'.
     // See function test_correlations() for assertions.
     // Combine tags 'dog' and 'kitten' into 'cat' and make sure that cat is now correlated with dogs and puppy.
     $tags['cat']->combine_tags(array($tags['dog'], $tags['kitten']));
     $correlatedtags = $this->get_correlated_tags_names($tags['cat']);
     $this->assertEquals(['cats', 'dogs', 'puppy'], $correlatedtags);
     $correlatedtags = $this->get_correlated_tags_names($tags['dogs']);
     $this->assertEquals(['cat', 'puppy'], $correlatedtags);
     $correlatedtags = $this->get_correlated_tags_names($tags['puppy']);
     $this->assertEquals(['cat', 'dogs'], $correlatedtags);
     // Add tag that does not have any correlations.
     $user7 = $this->getDataGenerator()->create_user();
     core_tag_tag::set_item_tags('core', 'user', $user7->id, context_user::instance($user7->id), array('hippo'));
     $tags['hippo'] = core_tag_tag::get_by_name(core_tag_collection::get_default(), 'hippo', '*');
     // Combine tag 'cat' into 'hippo'. Now 'hippo' should have the same correlations 'cat' used to have and also
     // tags 'dogs' and 'puppy' should have 'hippo' in correlations.
     $tags['hippo']->combine_tags(array($tags['cat']));
     $correlatedtags = $this->get_correlated_tags_names($tags['hippo']);
     $this->assertEquals(['cats', 'dogs', 'puppy'], $correlatedtags);
     $correlatedtags = $this->get_correlated_tags_names($tags['dogs']);
     $this->assertEquals(['hippo', 'puppy'], $correlatedtags);
     $correlatedtags = $this->get_correlated_tags_names($tags['puppy']);
     $this->assertEquals(['dogs', 'hippo'], $correlatedtags);
 }