/** * Returns the list of areas indexed by itemtype and component * * @param int $tagcollid return only areas in this tag collection * @param bool $enabledonly return only enabled tag areas * @return array itemtype=>component=>tagarea object */ public static function get_areas($tagcollid = null, $enabledonly = false) { global $DB; $cache = cache::make('core', 'tags'); if (($itemtypes = $cache->get('tag_area')) === false) { $colls = core_tag_collection::get_collections(); $defaultcoll = reset($colls); $itemtypes = array(); $areas = $DB->get_records('tag_area', array(), 'component,itemtype'); foreach ($areas as $area) { if ($colls[$area->tagcollid]->component) { $area->locked = true; } $itemtypes[$area->itemtype][$area->component] = $area; } $cache->set('tag_area', $itemtypes); } if ($tagcollid || $enabledonly) { $rv = array(); foreach ($itemtypes as $itemtype => $it) { foreach ($it as $component => $v) { if (($v->tagcollid == $tagcollid || !$tagcollid) && (!$enabledonly || $v->enabled)) { $rv[$itemtype][$component] = $v; } } } return $rv; } return $itemtypes; }
/** * Constructor * @param string|moodle_url $pageurl */ public function __construct($pageurl) { global $OUTPUT; parent::__construct(); $this->attributes['class'] = 'generaltable tag-collections-table'; $this->head = array(get_string('name'), get_string('component', 'tag'), get_string('tagareas', 'tag'), get_string('searchable', 'tag') . $OUTPUT->help_icon('searchable', 'tag'), ''); $this->data = array(); $tagcolls = core_tag_collection::get_collections(); $idx = 0; foreach ($tagcolls as $tagcoll) { $actions = ''; $name = core_tag_collection::display_name($tagcoll); $url = new moodle_url($pageurl, array('sesskey' => sesskey(), 'tc' => $tagcoll->id)); if (!$tagcoll->isdefault) { // Move up. if ($idx > 1) { $url->param('action', 'collmoveup'); $actions .= $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')), null, array('class' => 'action-icon action_moveup')); } // Move down. if ($idx < count($tagcolls) - 1) { $url->param('action', 'collmovedown'); $actions .= $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')), null, array('class' => 'action-icon action_movedown')); } } if (!$tagcoll->isdefault && empty($tagcoll->component)) { // Delete. $url->param('action', 'colldelete'); $actions .= $OUTPUT->action_icon('#', new pix_icon('t/delete', get_string('delete')), null, array('data-url' => $url->out(false), 'data-collname' => $name, 'class' => 'action-icon action_delete')); } $component = ''; if ($tagcoll->component) { $component = $tagcoll->component === 'core' || preg_match('/^core_/', $tagcoll->component) ? get_string('coresystem') : get_string('pluginname', $tagcoll->component); } $allareas = core_tag_collection::get_areas_names(null, false); $validareas = core_tag_collection::get_areas_names($tagcoll->id); $areaslist = array_map(function ($key) use($allareas, $validareas) { return "<li data-areaid=\"{$key}\" " . (array_key_exists($key, $validareas) ? "" : "style=\"display:none;\"") . ">{$allareas[$key]}</li>"; }, array_keys($allareas)); $displayname = new \core_tag\output\tagcollname($tagcoll); $searchable = new \core_tag\output\tagcollsearchable($tagcoll); $this->data[] = array($displayname->render($OUTPUT), $component, "<ul data-collectionid=\"{$tagcoll->id}\">" . join('', $areaslist) . '</ul>', $searchable->render($OUTPUT), $actions); $idx++; } }
/** * Constructor * @param string|moodle_url $pageurl */ public function __construct($pageurl) { global $OUTPUT; parent::__construct(); $this->attributes['class'] = 'generaltable tag-collections-table'; $this->head = array(get_string('name'), get_string('component', 'tag'), get_string('tagareas', 'tag'), get_string('searchable', 'tag') . $OUTPUT->help_icon('searchable', 'tag'), ''); $this->data = array(); $tagcolls = core_tag_collection::get_collections(); $idx = 0; foreach ($tagcolls as $tagcoll) { $actions = ''; $name = core_tag_collection::display_name($tagcoll); $url = new moodle_url($pageurl, array('sesskey' => sesskey(), 'tc' => $tagcoll->id)); if (!$tagcoll->isdefault) { // Move up. if ($idx > 1) { $url->param('action', 'collmoveup'); $actions .= $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup'))); } // Move down. if ($idx < count($tagcolls) - 1) { $url->param('action', 'collmovedown'); $actions .= $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))); } } if (empty($tagcoll->component)) { // Edit. $url->param('action', 'colledit'); $actions .= $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('edittagcoll', 'tag', $name))); } if (!$tagcoll->isdefault && empty($tagcoll->component)) { // Delete. $url->param('action', 'colldelete'); $actions .= $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete'))); } $manageurl = new moodle_url('/tag/manage.php', array('tc' => $tagcoll->id)); $component = ''; if ($tagcoll->component) { $component = $tagcoll->component === 'core' || preg_match('/^core_/', $tagcoll->component) ? get_string('coresystem') : get_string('pluginname', $tagcoll->component); } $this->data[] = array(html_writer::link($manageurl, $name), $component, join(', ', core_tag_collection::get_areas_names($tagcoll->id)), $tagcoll->searchable ? get_string('yes') : '-', $actions); $idx++; } }
/** * Very basic test for create/move/update/delete actions, without any itemtype movements. */ public function test_tag_coll_basic() { global $DB; // Make sure there is one and only one tag coll that is marked as default. $tagcolls = core_tag_collection::get_collections(); $this->assertEquals(1, count($DB->get_records('tag_coll', array('isdefault' => 1)))); $defaulttagcoll = core_tag_collection::get_default(); // Create a new tag coll to store user tags and something else. $data = (object) array('name' => 'new tag coll'); $tagcollid1 = core_tag_collection::create($data)->id; $tagcolls = core_tag_collection::get_collections(); $this->assertEquals('new tag coll', $tagcolls[$tagcollid1]->name); // Create a new tag coll to store post tags. $data = (object) array('name' => 'posts'); $tagcollid2 = core_tag_collection::create($data)->id; $tagcolls = core_tag_collection::get_collections(); $this->assertEquals('posts', $tagcolls[$tagcollid2]->name); $this->assertEquals($tagcolls[$tagcollid1]->sortorder + 1, $tagcolls[$tagcollid2]->sortorder); // Illegal tag colls sortorder changing. $this->assertFalse(core_tag_collection::change_sortorder($tagcolls[$defaulttagcoll], 1)); $this->assertFalse(core_tag_collection::change_sortorder($tagcolls[$defaulttagcoll], -1)); $this->assertFalse(core_tag_collection::change_sortorder($tagcolls[$tagcollid2], 1)); // Move the very last tag coll one position up. $this->assertTrue(core_tag_collection::change_sortorder($tagcolls[$tagcollid2], -1)); $tagcolls = core_tag_collection::get_collections(); $this->assertEquals($tagcolls[$tagcollid2]->sortorder + 1, $tagcolls[$tagcollid1]->sortorder); // Move the second last tag coll one position down. $this->assertTrue(core_tag_collection::change_sortorder($tagcolls[$tagcollid2], 1)); $tagcolls = core_tag_collection::get_collections(); $this->assertEquals($tagcolls[$tagcollid1]->sortorder + 1, $tagcolls[$tagcollid2]->sortorder); // Edit tag coll. $this->assertTrue(core_tag_collection::update($tagcolls[$tagcollid2], (object) array('name' => 'posts2'))); $tagcolls = core_tag_collection::get_collections(); $this->assertEquals('posts2', $tagcolls[$tagcollid2]->name); // Delete tag coll. $count = $DB->count_records('tag_coll'); $this->assertFalse(core_tag_collection::delete($tagcolls[$defaulttagcoll])); $this->assertTrue(core_tag_collection::delete($tagcolls[$tagcollid1])); $this->assertEquals($count - 1, $DB->count_records('tag_coll')); }
/** * Search for tags with names that match some text * * @package core_tag * @deprecated since 3.1 * @param string $text escaped string that the tag names will be matched against * @param bool $ordered If true, tags are ordered by their popularity. If false, no ordering. * @param int/string $limitfrom (optional, required if $limitnum is set) return a subset of records, starting at this point. * @param int/string $limitnum (optional, required if $limitfrom is set) return a subset comprising this many records. * @param int $tagcollid * @return array/boolean an array of objects, or false if no records were found or an error occured. */ function tag_find_tags($text, $ordered = true, $limitfrom = '', $limitnum = '', $tagcollid = null) { debugging('Method tag_find_tags() is deprecated without replacement', DEBUG_DEVELOPER); global $DB; $text = core_text::strtolower(clean_param($text, PARAM_TAG)); list($sql, $params) = $DB->get_in_or_equal($tagcollid ? array($tagcollid) : array_keys(core_tag_collection::get_collections(true))); array_unshift($params, "%{$text}%"); if ($ordered) { $query = "SELECT tg.id, tg.name, tg.rawname, tg.tagcollid, COUNT(ti.id) AS count\n FROM {tag} tg LEFT JOIN {tag_instance} ti ON tg.id = ti.tagid\n WHERE tg.name LIKE ? AND tg.tagcollid {$sql}\n GROUP BY tg.id, tg.name, tg.rawname\n ORDER BY count DESC"; } else { $query = "SELECT tg.id, tg.name, tg.rawname, tg.tagcollid\n FROM {tag} tg\n WHERE tg.name LIKE ? AND tg.tagcollid {$sql}"; } return $DB->get_records_sql($query, $params, $limitfrom, $limitnum); }
/** * Looking in all tag collections for the tag with the given name * * @param string $name tag name * @param string $returnfields * @return array array of core_tag_tag instances */ public static function guess_by_name($name, $returnfields = 'id, name, rawname, tagcollid') { global $DB; if (empty($name)) { return array(); } $tagcolls = core_tag_collection::get_collections(); list($sql, $params) = $DB->get_in_or_equal(array_keys($tagcolls), SQL_PARAMS_NAMED); $params['name'] = core_text::strtolower($name); $tags = $DB->get_records_select('tag', 'name = :name AND tagcollid ' . $sql, $params, '', $returnfields); if (count($tags) > 1) { // Sort in the same order as tag collections. uasort($tags, create_function('$a,$b', '$tagcolls = core_tag_collection::get_collections(); ' . 'return $tagcolls[$a->tagcollid]->sortorder < $tagcolls[$b->tagcollid]->sortorder ? -1 : 1;')); } $rv = array(); foreach ($tags as $id => $tag) { $rv[$id] = new static($tag); } return $rv; }