/** * Custom form validation * * @param array $data * @param array $files * @return array */ public function validation($data, $files) { $errors = parent::validation($data, $files); if (isset($data['rawname'])) { $newname = core_text::strtolower($data['rawname']); $tag = $this->_customdata['tag']; if ($tag->name != $newname) { // The name has changed, let's make sure it's not another existing tag. if (core_tag_tag::get_by_name($tag->tagcollid, $newname)) { // Something exists already, so flag an error. $errors['rawname'] = get_string('namesalreadybeeingused', 'tag'); } } } return $errors; }
public function test_move_tags_with_related() { global $DB; list($collid1, $collid2, $user1, $user2, $blogpost) = $this->prepare_move_tags(); // Set Tag1 to be related to Tag2 and Tag4 (in collection 1). core_tag_tag::get_by_name($collid1, 'Tag1')->set_related_tags(array('Tag2', 'Tag4')); // Set collection for 'post' tag area to be collection 2 and add some tags there. $tagareablog = $DB->get_record('tag_area', array('itemtype' => 'post', 'component' => 'core')); core_tag_area::update($tagareablog, array('tagcollid' => $collid2)); core_tag_tag::set_item_tags('core', 'post', $blogpost->id, context_system::instance(), array('TAG1', 'Tag3')); // Move 'user' area from collection 1 to collection 2, make sure tags were moved completely. $tagarea = $DB->get_record('tag_area', array('itemtype' => 'user', 'component' => 'core')); core_tag_area::update($tagarea, array('tagcollid' => $collid2)); $this->assertEquals(array('Tag1', 'Tag2', 'Tag4'), $DB->get_fieldset_select('tag', 'rawname', 'tagcollid = ? ORDER BY name', array($collid1))); $this->assertEquals(array('TAG1', 'Tag2', 'Tag3', 'Tag4', 'Tag5'), $DB->get_fieldset_select('tag', 'rawname', 'tagcollid = ? ORDER BY name', array($collid2))); $this->assertEquals(array('TAG1', 'Tag2'), array_values(core_tag_tag::get_item_tags_array('core', 'user', $user1->id))); $this->assertEquals(array('Tag2', 'Tag3'), array_values(core_tag_tag::get_item_tags_array('core', 'user', $user2->id))); $tag11 = core_tag_tag::get_by_name($collid1, 'Tag1'); $related11 = tag_get_related_tags($tag11->id, TAG_RELATED_MANUAL); $this->assertDebuggingCalled(); $related11 = array_map('core_tag_tag::make_display_name', $related11); sort($related11); // Order of related tags may be random. $this->assertEquals('Tag2, Tag4', join(', ', $related11)); $tag21 = core_tag_tag::get_by_name($collid2, 'TAG1'); $related21 = tag_get_related_tags($tag21->id, TAG_RELATED_MANUAL); $this->assertDebuggingCalled(); $related21 = array_map('core_tag_tag::make_display_name', $related21); sort($related21); // Order of related tags may be random. $this->assertEquals('Tag2, Tag4', join(', ', $related21)); }
/** * Simple function to just return a single tag object when you know the name or something * * See also {@link core_tag_tag::get()} and {@link core_tag_tag::get_by_name()} * * @package core_tag * @deprecated since 3.1 * @param string $field which field do we use to identify the tag: id, name or rawname * @param string $value the required value of the aforementioned field * @param string $returnfields which fields do we want returned. This is a comma seperated string containing any combination of * 'id', 'name', 'rawname' or '*' to include all fields. * @return mixed tag object */ function tag_get($field, $value, $returnfields = 'id, name, rawname, tagcollid') { global $DB; debugging('Function tag_get() is deprecated. Use ' . ' core_tag_tag::get() or core_tag_tag::get_by_name()', DEBUG_DEVELOPER); if ($field === 'id') { $tag = core_tag_tag::get((int) $value, $returnfields); } else { if ($field === 'name') { $tag = core_tag_tag::get_by_name(0, $value, $returnfields); } else { $params = array($field => $value); return $DB->get_record('tag', $params, $returnfields); } } if ($tag) { return $tag->to_object(); } return null; }
/** * Testing function course_get_tagged_course_modules - search tagged course modules */ public function test_course_get_tagged_course_modules() { global $DB; $this->resetAfterTest(); $course3 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $course1 = $this->getDataGenerator()->create_course(); $cm11 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id, 'tags' => 'Cat, Dog')); $cm12 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'tags' => 'Cat, Mouse', 'visible' => 0)); $cm13 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'tags' => 'Cat, Mouse, Dog')); $cm21 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id, 'tags' => 'Cat, Mouse')); $cm31 = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id, 'tags' => 'Cat, Mouse')); // Admin is able to view everything. $this->setAdminUser(); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, 0, 0, 1, 0); $this->assertRegExp('/' . $cm11->name . '/', $res->content); $this->assertRegExp('/' . $cm12->name . '/', $res->content); $this->assertRegExp('/' . $cm13->name . '/', $res->content); $this->assertRegExp('/' . $cm21->name . '/', $res->content); $this->assertRegExp('/' . $cm31->name . '/', $res->content); // Results from course1 are returned before results from course2. $this->assertTrue(strpos($res->content, $cm11->name) < strpos($res->content, $cm21->name)); // Ordinary user is not able to see anything. $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, 0, 0, 1, 0); $this->assertNull($res); // Enrol user as student in course1 and course2. $roleids = $DB->get_records_menu('role', null, '', 'shortname, id'); $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['student']); $this->getDataGenerator()->enrol_user($user->id, $course2->id, $roleids['student']); core_tag_index_builder::reset_caches(); // Searching in the course context returns visible modules in this course. $context = context_course::instance($course1->id); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, 0, $context->id, 1, 0); $this->assertRegExp('/' . $cm11->name . '/', $res->content); $this->assertNotRegExp('/' . $cm12->name . '/', $res->content); $this->assertRegExp('/' . $cm13->name . '/', $res->content); $this->assertNotRegExp('/' . $cm21->name . '/', $res->content); $this->assertNotRegExp('/' . $cm31->name . '/', $res->content); // Searching FROM the course context returns visible modules in all courses. $context = context_course::instance($course2->id); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, $context->id, 0, 1, 0); $this->assertRegExp('/' . $cm11->name . '/', $res->content); $this->assertNotRegExp('/' . $cm12->name . '/', $res->content); $this->assertRegExp('/' . $cm13->name . '/', $res->content); $this->assertRegExp('/' . $cm21->name . '/', $res->content); $this->assertNotRegExp('/' . $cm31->name . '/', $res->content); // No access to course3. // Results from course2 are returned before results from course1. $this->assertTrue(strpos($res->content, $cm21->name) < strpos($res->content, $cm11->name)); // Enrol user in course1 as a teacher - now he should be able to see hidden module. $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['editingteacher']); get_fast_modinfo(0, 0, true); $context = context_course::instance($course1->id); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, $context->id, 0, 1, 0); $this->assertRegExp('/' . $cm12->name . '/', $res->content); // Create more modules and try pagination. $cm14 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id, 'tags' => 'Cat, Dog')); $cm15 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'tags' => 'Cat, Mouse', 'visible' => 0)); $cm16 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'tags' => 'Cat, Mouse, Dog')); $context = context_course::instance($course1->id); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, 0, $context->id, 1, 0); $this->assertRegExp('/' . $cm11->name . '/', $res->content); $this->assertRegExp('/' . $cm12->name . '/', $res->content); $this->assertRegExp('/' . $cm13->name . '/', $res->content); $this->assertNotRegExp('/' . $cm21->name . '/', $res->content); $this->assertRegExp('/' . $cm14->name . '/', $res->content); $this->assertRegExp('/' . $cm15->name . '/', $res->content); $this->assertNotRegExp('/' . $cm16->name . '/', $res->content); $this->assertNotRegExp('/' . $cm31->name . '/', $res->content); // No access to course3. $this->assertEmpty($res->prevpageurl); $this->assertNotEmpty($res->nextpageurl); $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'), false, 0, $context->id, 1, 1); $this->assertNotRegExp('/' . $cm11->name . '/', $res->content); $this->assertNotRegExp('/' . $cm12->name . '/', $res->content); $this->assertNotRegExp('/' . $cm13->name . '/', $res->content); $this->assertNotRegExp('/' . $cm21->name . '/', $res->content); $this->assertNotRegExp('/' . $cm14->name . '/', $res->content); $this->assertNotRegExp('/' . $cm15->name . '/', $res->content); $this->assertRegExp('/' . $cm16->name . '/', $res->content); $this->assertNotRegExp('/' . $cm31->name . '/', $res->content); // No access to course3. $this->assertNotEmpty($res->prevpageurl); $this->assertEmpty($res->nextpageurl); }
/** * Get tags by their ids * * @param array $params */ public static function get_tagindex($params) { global $PAGE; // Validate and normalize parameters. $tagindex = self::validate_parameters(self::get_tagindex_parameters(), array('tagindex' => $params)); $params = $tagindex['tagindex'] + array('excl' => 0, 'from' => 0, 'ctx' => 0, 'rec' => 1, 'page' => 0); // Login to the course / module if applicable. $context = $params['ctx'] ? context::instance_by_id($params['ctx']) : context_system::instance(); require_login(null, false, null, false, true); self::validate_context($context); $tag = core_tag_tag::get_by_name($params['tc'], $params['tag'], '*', MUST_EXIST); $tagareas = core_tag_collection::get_areas($params['tc']); $tagindex = $tag->get_tag_index($tagareas[$params['ta']], $params['excl'], $params['from'], $params['ctx'], $params['rec'], $params['page']); $renderer = $PAGE->get_renderer('core'); return $tagindex->export_for_template($renderer); }
/** * 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); }
public function test_mod_wiki_get_tagged_pages() { global $DB; $this->resetAfterTest(); $this->setAdminUser(); // Setup test data. $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); $course3 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $course1 = $this->getDataGenerator()->create_course(); $wiki1 = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id)); $wiki2 = $this->getDataGenerator()->create_module('wiki', array('course' => $course2->id)); $wiki3 = $this->getDataGenerator()->create_module('wiki', array('course' => $course3->id)); $page11 = $wikigenerator->create_content($wiki1, array('tags' => array('Cats', 'Dogs'))); $page12 = $wikigenerator->create_content($wiki1, array('tags' => array('Cats', 'mice'))); $page13 = $wikigenerator->create_content($wiki1, array('tags' => array('Cats'))); $page14 = $wikigenerator->create_content($wiki1); $page15 = $wikigenerator->create_content($wiki1, array('tags' => array('Cats'))); $page21 = $wikigenerator->create_content($wiki2, array('tags' => array('Cats'))); $page22 = $wikigenerator->create_content($wiki2, array('tags' => array('Cats', 'Dogs'))); $page23 = $wikigenerator->create_content($wiki2, array('tags' => array('mice', 'Cats'))); $page31 = $wikigenerator->create_content($wiki3, array('tags' => array('mice', 'Cats'))); $tag = core_tag_tag::get_by_name(0, 'Cats'); // Admin can see everything. $res = mod_wiki_get_tagged_pages($tag, false, 0, 0, 1, 0); $this->assertRegExp('/' . $page11->title . '/', $res->content); $this->assertRegExp('/' . $page12->title . '/', $res->content); $this->assertRegExp('/' . $page13->title . '/', $res->content); $this->assertNotRegExp('/' . $page14->title . '/', $res->content); $this->assertRegExp('/' . $page15->title . '/', $res->content); $this->assertRegExp('/' . $page21->title . '/', $res->content); $this->assertNotRegExp('/' . $page22->title . '/', $res->content); $this->assertNotRegExp('/' . $page23->title . '/', $res->content); $this->assertNotRegExp('/' . $page31->title . '/', $res->content); $this->assertEmpty($res->prevpageurl); $this->assertNotEmpty($res->nextpageurl); $res = mod_wiki_get_tagged_pages($tag, false, 0, 0, 1, 1); $this->assertNotRegExp('/' . $page11->title . '/', $res->content); $this->assertNotRegExp('/' . $page12->title . '/', $res->content); $this->assertNotRegExp('/' . $page13->title . '/', $res->content); $this->assertNotRegExp('/' . $page14->title . '/', $res->content); $this->assertNotRegExp('/' . $page15->title . '/', $res->content); $this->assertNotRegExp('/' . $page21->title . '/', $res->content); $this->assertRegExp('/' . $page22->title . '/', $res->content); $this->assertRegExp('/' . $page23->title . '/', $res->content); $this->assertRegExp('/' . $page31->title . '/', $res->content); $this->assertNotEmpty($res->prevpageurl); $this->assertEmpty($res->nextpageurl); // Create and enrol a user. $student = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual'); $this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual'); $this->setUser($student); core_tag_index_builder::reset_caches(); // User can not see pages in course 3 because he is not enrolled. $res = mod_wiki_get_tagged_pages($tag, false, 0, 0, 1, 1); $this->assertRegExp('/' . $page22->title . '/', $res->content); $this->assertRegExp('/' . $page23->title . '/', $res->content); $this->assertNotRegExp('/' . $page31->title . '/', $res->content); // User can search wiki pages inside a course. $coursecontext = context_course::instance($course1->id); $res = mod_wiki_get_tagged_pages($tag, false, 0, $coursecontext->id, 1, 0); $this->assertRegExp('/' . $page11->title . '/', $res->content); $this->assertRegExp('/' . $page12->title . '/', $res->content); $this->assertRegExp('/' . $page13->title . '/', $res->content); $this->assertNotRegExp('/' . $page14->title . '/', $res->content); $this->assertRegExp('/' . $page15->title . '/', $res->content); $this->assertNotRegExp('/' . $page21->title . '/', $res->content); $this->assertNotRegExp('/' . $page22->title . '/', $res->content); $this->assertNotRegExp('/' . $page23->title . '/', $res->content); $this->assertEmpty($res->nextpageurl); }
function get_content() { global $CFG, $USER; //note: do NOT include files at the top of this file require_once $CFG->libdir . '/filelib.php'; if ($this->content !== NULL) { return $this->content; } $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility $tag = optional_param('tag', '', PARAM_TAG); // tag $tc = optional_param('tc', 0, PARAM_INT); // Tag collection id. if ($tagid) { $tagobject = core_tag_tag::get($tagid); } else { if ($tag) { $tagobject = core_tag_tag::get_by_name($tc, $tag); } } if (empty($tagobject)) { $this->content = new stdClass(); $this->content->text = ''; $this->content->footer = ''; return $this->content; } //include related tags in the photo query ? $tagscsv = $tagobject->name; if (!empty($this->config->includerelatedtags)) { foreach ($tagobject->get_related_tags() as $t) { $tagscsv .= ',' . $t->get_display_name(false); } } $tagscsv = urlencode($tagscsv); //number of photos to display $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS; if (!empty($this->config->numberofphotos)) { $numberofphotos = $this->config->numberofphotos; } //sort search results by $sortby = 'relevance'; if (!empty($this->config->sortby)) { $sortby = $this->config->sortby; } //pull photos from a specific photoset if (!empty($this->config->photoset)) { $request = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos'; $request .= '&api_key=' . FLICKR_DEV_KEY; $request .= '&photoset_id=' . $this->config->photoset; $request .= '&per_page=' . $numberofphotos; $request .= '&format=php_serial'; $response = $this->fetch_request($request); $search = unserialize($response); foreach ($search['photoset']['photo'] as $p) { $p['owner'] = $search['photoset']['owner']; } $photos = array_values($search['photoset']['photo']); } else { $request = 'https://api.flickr.com/services/rest/?method=flickr.photos.search'; $request .= '&api_key=' . FLICKR_DEV_KEY; $request .= '&tags=' . $tagscsv; $request .= '&per_page=' . $numberofphotos; $request .= '&sort=' . $sortby; $request .= '&format=php_serial'; $response = $this->fetch_request($request); $search = unserialize($response); $photos = array_values($search['photos']['photo']); } if (strcmp($search['stat'], 'ok') != 0) { return; } //if no results were returned, exit... //Accessibility: render the list of photos $text = '<ul class="inline-list">'; foreach ($photos as $photo) { $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="' . s($photo['title']) . '">'; $text .= '<img alt="' . s($photo['title']) . '" class="flickr-photos" src="' . $this->build_photo_url($photo, 'square') . "\" /></a></li>\n"; } $text .= "</ul>\n"; $this->content = new stdClass(); $this->content->text = $text; $this->content->footer = ''; return $this->content; }
require_capability('moodle/tag:edit', $systemcontext); if ($tagname) { $tagcollid = optional_param('tc', 0, PARAM_INT); if (!$tagcollid) { // Tag name specified but tag collection was not. Try to guess it. $tags = core_tag_tag::guess_by_name($tagname, '*'); if (count($tags) > 1) { // This tag was found in more than one collection, redirect to search. redirect(new moodle_url('/tag/search.php', array('tag' => $tagname))); } else { if (count($tags) == 1) { $tag = reset($tags); } } } else { if (!($tag = core_tag_tag::get_by_name($tagcollid, $tagname, '*'))) { redirect(new moodle_url('/tag/search.php', array('tagcollid' => $tagcollid))); } } } else { if ($tagid) { $tag = core_tag_tag::get($tagid, '*'); } } if (empty($tag)) { redirect(new moodle_url('/tag/search.php')); } $PAGE->set_url($tag->get_view_url()); $PAGE->set_subpage($tag->id); $PAGE->set_context($systemcontext); $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
function get_videos_by_tag_and_category() { if (!($service = $this->get_service())) { return $this->get_error_message(); } $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility $tag = optional_param('tag', '', PARAM_TAG); // tag $tc = optional_param('tc', 0, PARAM_INT); // Tag collection id. if ($tagid) { $tagobject = core_tag_tag::get($tagid); } else { if ($tag) { $tagobject = core_tag_tag::get_by_name($tc, $tag); } } if (empty($tagobject)) { return ''; } $querytag = urlencode($tagobject->name); $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS; if (!empty($this->config->numberofvideos)) { $numberofvideos = $this->config->numberofvideos; } try { $response = $service->search->listSearch('id,snippet', array('q' => $querytag, 'type' => 'video', 'maxResults' => $numberofvideos, 'videoCategoryId' => $this->config->category)); } catch (Google_Service_Exception $e) { debugging('Google service exception: ' . $e->getMessage(), DEBUG_DEVELOPER); return $this->get_error_message(get_string('requesterror', 'block_tag_youtube')); } return $this->render_items($response); }
} else { $tag = array_shift($tags); } $tag->combine_tags($tags); \core\notification::success(get_string('combined', 'core_tag')); } redirect($PAGE->url); } } break; case 'renamecombine': // Allows to rename the tag and if the tag with the new name already exists these tags will be combined. if ($tagid && ($newname = required_param('newname', PARAM_TAG))) { require_sesskey(); $tag = core_tag_tag::get($tagid, '*', MUST_EXIST); $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*'); if ($targettag) { $targettag->combine_tags(array($tag)); \core\notification::success(get_string('combined', 'core_tag')); } else { $tag->update(array('rawname' => $newname)); \core\notification::success(get_string('changessaved', 'core_tag')); } } redirect($PAGE->url); break; case 'addstandardtag': require_sesskey(); $tagobjects = array(); if ($tagcoll) { $tagslist = optional_param('tagslist', '', PARAM_RAW);