示例#1
0
 /**
  * Test functions core_tag_tag::create_if_missing() and core_tag_tag::get_by_name_bulk().
  */
 public function test_create_get()
 {
     $tagset = array('Cat', ' Dog  ', '<Mouse', '<>', 'mouse', 'Dog');
     $collid = core_tag_collection::get_default();
     $tags = core_tag_tag::create_if_missing($collid, $tagset);
     $this->assertEquals(array('cat', 'dog', 'mouse'), array_keys($tags));
     $this->assertEquals('Dog', $tags['dog']->rawname);
     $this->assertEquals('mouse', $tags['mouse']->rawname);
     // Case of the last tag wins.
     $tags2 = core_tag_tag::create_if_missing($collid, array('CAT', 'Elephant'));
     $this->assertEquals(array('cat', 'elephant'), array_keys($tags2));
     $this->assertEquals('Cat', $tags2['cat']->rawname);
     $this->assertEquals('Elephant', $tags2['elephant']->rawname);
     $this->assertEquals($tags['cat']->id, $tags2['cat']->id);
     // Tag 'cat' already existed and was not created again.
     $tags3 = core_tag_tag::get_by_name_bulk($collid, $tagset);
     $this->assertEquals(array('cat', 'dog', 'mouse'), array_keys($tags3));
     $this->assertEquals('Dog', $tags3['dog']->rawname);
     $this->assertEquals('mouse', $tags3['mouse']->rawname);
 }
示例#2
0
/**
 * Adds one or more tag in the database.  This function should not be called directly : you should
 * use tag_set.
 *
 * @package core_tag
 * @deprecated since 3.1
 * @param   mixed    $tags     one tag, or an array of tags, to be created
 * @param   string   $type     type of tag to be created ("default" is the default value and "official" is the only other supported
 *                             value at this time). An official tag is kept even if there are no records tagged with it.
 * @return array     $tags ids indexed by their lowercase normalized names. Any boolean false in the array indicates an error while
 *                             adding the tag.
 */
function tag_add($tags, $type = "default")
{
    debugging('Function tag_add() is deprecated. You can use core_tag_tag::create_if_missing(), however it should not be necessary ' . 'since tags are created automatically when assigned to items', DEBUG_DEVELOPER);
    if (!is_array($tags)) {
        $tags = array($tags);
    }
    $objects = core_tag_tag::create_if_missing(core_tag_collection::get_default(), $tags, $type === 'official');
    // New function returns the tags in different format, for BC we keep the format that this function used to have.
    $rv = array();
    foreach ($objects as $name => $tagobject) {
        if (isset($tagobject->id)) {
            $rv[$tagobject->name] = $tagobject->id;
        } else {
            $rv[$name] = false;
        }
    }
    return $rv;
}
示例#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);
 }
示例#4
0
文件: area.php 项目: bewanyk/moodle
 /**
  * Update the database to contain a list of tagged areas for a component.
  * The list of tagged areas is read from [plugindir]/db/tag.php
  *
  * @param string $componentname - The frankenstyle component name.
  */
 public static function reset_definitions_for_component($componentname)
 {
     global $DB;
     $dir = core_component::get_component_directory($componentname);
     $file = $dir . '/db/tag.php';
     $tagareas = null;
     if (file_exists($file)) {
         require_once $file;
     }
     list($a, $b) = core_component::normalize_component($componentname);
     $component = $b ? $a . '_' . $b : $a;
     list($existingareas, $existingcolls) = self::get_definitions_for_component($componentname);
     $itemtypes = array();
     $collections = array();
     $needcleanup = false;
     if ($tagareas) {
         foreach ($tagareas as $tagarea) {
             $record = (object) $tagarea;
             if ($component !== 'core' || empty($record->component)) {
                 if (isset($record->component) && $record->component !== $component) {
                     debugging("Item type {$record->itemtype} has illegal component {$record->component}", DEBUG_DEVELOPER);
                 }
                 $record->component = $component;
             }
             unset($record->tagcollid);
             if (!empty($record->collection)) {
                 // Create collection if it does not exist, or update 'searchable' and/or 'customurl' if needed.
                 $key = $record->collection . ':' . $record->component;
                 $collectiondata = array_intersect_key((array) $record, array('component' => 1, 'searchable' => 1, 'customurl' => 1));
                 $collectiondata['name'] = $record->collection;
                 if (!array_key_exists($key, $existingcolls)) {
                     $existingcolls[$key] = core_tag_collection::create($collectiondata);
                 } else {
                     core_tag_collection::update($existingcolls[$key], $collectiondata);
                 }
                 $record->tagcollid = $existingcolls[$key]->id;
                 $collections[$key] = $existingcolls[$key];
                 unset($record->collection);
             }
             unset($record->searchable);
             unset($record->customurl);
             if (!isset($record->callback)) {
                 $record->callback = null;
             }
             if (!isset($record->callbackfile)) {
                 $record->callbackfile = null;
             }
             $itemtypes[$record->itemtype . ':' . $record->component] = $record;
         }
     }
     $todeletearea = array_diff_key($existingareas, $itemtypes);
     $todeletecoll = array_diff_key($existingcolls, $collections);
     // Delete tag areas that are no longer needed.
     foreach ($todeletearea as $key => $record) {
         self::delete($record);
     }
     // Update tag areas if changed.
     $toupdatearea = array_intersect_key($existingareas, $itemtypes);
     foreach ($toupdatearea as $key => $tagarea) {
         if (!isset($itemtypes[$key]->tagcollid)) {
             foreach ($todeletecoll as $tagcoll) {
                 if ($tagcoll->id == $tagarea->tagcollid) {
                     $itemtypes[$key]->tagcollid = core_tag_collection::get_default();
                 }
             }
         }
         self::update($tagarea, $itemtypes[$key]);
     }
     // Create new tag areas.
     $toaddarea = array_diff_key($itemtypes, $existingareas);
     foreach ($toaddarea as $record) {
         self::create($record);
     }
     // Delete tag collections that are no longer needed.
     foreach ($todeletecoll as $key => $tagcoll) {
         core_tag_collection::delete($tagcoll);
     }
 }
示例#5
0
文件: tags.php 项目: rushi963/moodle
 /**
  * Finds the tag collection to use for standard tag selector
  *
  * @return int
  */
 protected function get_tag_collection()
 {
     if (empty($this->tagsoptions['tagcollid']) && (empty($this->tagsoptions['itemtype']) || empty($this->tagsoptions['component']))) {
         debugging('You need to specify \'itemtype\' and \'component\' of the tagged ' . 'area in the tags form element options', DEBUG_DEVELOPER);
     }
     if (!empty($this->tagsoptions['tagcollid'])) {
         return $this->tagsoptions['tagcollid'];
     }
     if ($this->tagsoptions['itemtype']) {
         $this->tagsoptions['tagcollid'] = core_tag_area::get_collection($this->tagsoptions['component'], $this->tagsoptions['itemtype']);
     } else {
         $this->tagsoptions['tagcollid'] = core_tag_collection::get_default();
     }
     return $this->tagsoptions['tagcollid'];
 }
示例#6
0
 /**
  * Create a tag.
  *
  * @param array|stdClass $record
  * @return stdClass the tag record
  */
 public function create_tag($record = null)
 {
     global $DB, $USER;
     $this->tagcount++;
     $i = $this->tagcount;
     $record = (array) $record;
     if (!isset($record['userid'])) {
         $record['userid'] = $USER->id;
     }
     if (!isset($record['rawname'])) {
         if (isset($record['name'])) {
             $record['rawname'] = $record['name'];
         } else {
             $record['rawname'] = 'Tag name ' . $i;
         }
     }
     // Attribute 'name' should be a lowercase version of 'rawname', if not set.
     if (!isset($record['name'])) {
         $record['name'] = core_text::strtolower($record['rawname']);
     } else {
         $record['name'] = core_text::strtolower($record['name']);
     }
     if (!isset($record['tagcollid'])) {
         $record['tagcollid'] = core_tag_collection::get_default();
     }
     if (!isset($record['description'])) {
         $record['description'] = 'Tag description';
     }
     if (!isset($record['descriptionformat'])) {
         $record['descriptionformat'] = FORMAT_MOODLE;
     }
     if (!isset($record['flag'])) {
         $record['flag'] = 0;
     }
     if (!isset($record['timemodified'])) {
         $record['timemodified'] = time();
     }
     $id = $DB->insert_record('tag', $record);
     return $DB->get_record('tag', array('id' => $id), '*', MUST_EXIST);
 }