/** * Implements callback inplace_editable() allowing to edit values in-place * * @param string $itemtype * @param int $itemid * @param mixed $newvalue * @return \core\output\inplace_editable */ function core_tag_inplace_editable($itemtype, $itemid, $newvalue) { if ($itemtype === 'tagname') { require_capability('moodle/tag:manage', context_system::instance()); $tag = core_tag_tag::get($itemid, '*', MUST_EXIST); $tag->update(array('rawname' => $newvalue)); return new \core_tag\output\tagname($tag); } }
public function test_related_tags() { global $DB; $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $tagcollid = core_tag_collection::get_default(); $tag = $this->getDataGenerator()->create_tag(array('$tagcollid' => $tagcollid, 'rawname' => 'My tag')); $tag = core_tag_tag::get($tag->id, '*'); $tag->set_related_tags(array('Synonym 1', 'Synonym 2')); $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $tag->id)); $this->assertEquals(2, count($relatedtags)); $this->assertEquals('Synonym 1', $relatedtags[0]->rawname); $this->assertEquals('Synonym 2', $relatedtags[1]->rawname); $t1 = core_tag_tag::get_by_name($tagcollid, 'Synonym 1', '*'); $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $t1->id)); $this->assertEquals(1, count($relatedtags)); $this->assertEquals('My tag', $relatedtags[0]->rawname); $t2 = core_tag_tag::get_by_name($tagcollid, 'Synonym 2', '*'); $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $t2->id)); $this->assertEquals(1, count($relatedtags)); $this->assertEquals('My tag', $relatedtags[0]->rawname); $tag->set_related_tags(array('Synonym 3', 'Synonym 2', 'Synonym 1')); $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $tag->id)); $this->assertEquals(3, count($relatedtags)); $this->assertEquals('Synonym 1', $relatedtags[0]->rawname); $this->assertEquals('Synonym 2', $relatedtags[1]->rawname); $this->assertEquals('Synonym 3', $relatedtags[2]->rawname); $t3 = core_tag_tag::get_by_name($tagcollid, 'Synonym 3', '*'); $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $t3->id)); $this->assertEquals(1, count($relatedtags)); $this->assertEquals('My tag', $relatedtags[0]->rawname); $tag->set_related_tags(array('Synonym 3', 'Synonym 2')); $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $tag->id)); $this->assertEquals(2, count($relatedtags)); $this->assertEquals('Synonym 2', $relatedtags[0]->rawname); $this->assertEquals('Synonym 3', $relatedtags[1]->rawname); // Assert "Synonym 1" no longer links but is still present (will be removed by cron). $relatedtags = array_values(core_tag_tag::get_item_tags('core', 'tag', $t1->id)); $this->assertEquals(0, count($relatedtags)); }
/** * Returns the correlated tags of a tag, retrieved from the tag_correlation table. Make sure cron runs, otherwise the table will be * empty and this function won't return anything. * * Correlated tags are calculated in cron based on existing tag instances. * * @package core_tag * @deprecated since 3.1 * @param int $tagid is a single tag id * @param int $notused this argument is no longer used * @return array an array of tag objects or an empty if no correlated tags are found */ function tag_get_correlated($tagid, $notused = null) { debugging('Method tag_get_correlated() is deprecated, ' . 'use core_tag_tag::get_correlated_tags()', DEBUG_DEVELOPER); $result = array(); if ($tag = core_tag_tag::get($tagid)) { $tags = $tag->get_correlated_tags(true); // Convert to objects for backward-compatibility. foreach ($tags as $id => $tag) { $result[$id] = $tag->to_object(); } } return $result; }
/** * Handle showing/processing the submission from the block editing form. * @return boolean true if the form was submitted and the new config saved. Does not * return if the editing form was displayed. False otherwise. */ public function process_url_edit() { global $CFG, $DB, $PAGE, $OUTPUT; $blockid = optional_param('bui_editid', null, PARAM_INT); if (!$blockid) { return false; } require_sesskey(); require_once $CFG->dirroot . '/blocks/edit_form.php'; $block = $this->find_instance($blockid); if (!$block->user_can_edit() && !$this->page->user_can_edit_blocks()) { throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('editblock')); } $editpage = new moodle_page(); $editpage->set_pagelayout('admin'); $editpage->set_course($this->page->course); //$editpage->set_context($block->context); $editpage->set_context($this->page->context); if ($this->page->cm) { $editpage->set_cm($this->page->cm); } $editurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring()); $editurlparams = $this->page->url->params(); $editurlparams['bui_editid'] = $blockid; $editpage->set_url($editurlbase, $editurlparams); $editpage->set_block_actions_done(); // At this point we are either going to redirect, or display the form, so // overwrite global $PAGE ready for this. (Formslib refers to it.) $PAGE = $editpage; //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that to $output = $editpage->get_renderer('core'); $OUTPUT = $output; $formfile = $CFG->dirroot . '/blocks/' . $block->name() . '/edit_form.php'; if (is_readable($formfile)) { require_once $formfile; $classname = 'block_' . $block->name() . '_edit_form'; if (!class_exists($classname)) { $classname = 'block_edit_form'; } } else { $classname = 'block_edit_form'; } $mform = new $classname($editpage->url, $block, $this->page); $mform->set_data($block->instance); if ($mform->is_cancelled()) { redirect($this->page->url); } else { if ($data = $mform->get_data()) { $bi = new stdClass(); $bi->id = $block->instance->id; // This may get overwritten by the special case handling below. $bi->pagetypepattern = $data->bui_pagetypepattern; $bi->showinsubcontexts = (bool) $data->bui_contexts; if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') { $bi->subpagepattern = null; } else { $bi->subpagepattern = $data->bui_subpagepattern; } $systemcontext = context_system::instance(); $frontpagecontext = context_course::instance(SITEID); $parentcontext = context::instance_by_id($data->bui_parentcontextid); // Updating stickiness and contexts. See MDL-21375 for details. if (has_capability('moodle/site:manageblocks', $parentcontext)) { // Check permissions in destination // Explicitly set the default context $bi->parentcontextid = $parentcontext->id; if ($data->bui_editingatfrontpage) { // The block is being edited on the front page // The interface here is a special case because the pagetype pattern is // totally derived from the context menu. Here are the excpetions. MDL-30340 switch ($data->bui_contexts) { case BUI_CONTEXTS_ENTIRE_SITE: // The user wants to show the block across the entire site $bi->parentcontextid = $systemcontext->id; $bi->showinsubcontexts = true; $bi->pagetypepattern = '*'; break; case BUI_CONTEXTS_FRONTPAGE_SUBS: // The user wants the block shown on the front page and all subcontexts $bi->parentcontextid = $frontpagecontext->id; $bi->showinsubcontexts = true; $bi->pagetypepattern = '*'; break; case BUI_CONTEXTS_FRONTPAGE_ONLY: // The user want to show the front page on the frontpage only $bi->parentcontextid = $frontpagecontext->id; $bi->showinsubcontexts = false; $bi->pagetypepattern = 'site-index'; // This is the only relevant page type anyway but we'll set it explicitly just // in case the front page grows site-index-* subpages of its own later break; } } } $bits = explode('-', $bi->pagetypepattern); // hacks for some contexts if ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid != SITEID) { // For course context // is page type pattern is mod-*, change showinsubcontext to 1 if ($bits[0] == 'mod' || $bi->pagetypepattern == '*') { $bi->showinsubcontexts = 1; } else { $bi->showinsubcontexts = 0; } } else { if ($parentcontext->contextlevel == CONTEXT_USER) { // for user context // subpagepattern should be null if ($bits[0] == 'user' or $bits[0] == 'my') { // we don't need subpagepattern in usercontext $bi->subpagepattern = null; } } } $bi->defaultregion = $data->bui_defaultregion; $bi->defaultweight = $data->bui_defaultweight; $DB->update_record('block_instances', $bi); if (!empty($block->config)) { $config = clone $block->config; } else { $config = new stdClass(); } foreach ($data as $configfield => $value) { if (strpos($configfield, 'config_') !== 0) { continue; } $field = substr($configfield, 7); $config->{$field} = $value; } $block->instance_config_save($config); $bp = new stdClass(); $bp->visible = $data->bui_visible; $bp->region = $data->bui_region; $bp->weight = $data->bui_weight; $needbprecord = !$data->bui_visible || $data->bui_region != $data->bui_defaultregion || $data->bui_weight != $data->bui_defaultweight; if ($block->instance->blockpositionid && !$needbprecord) { $DB->delete_records('block_positions', array('id' => $block->instance->blockpositionid)); } else { if ($block->instance->blockpositionid && $needbprecord) { $bp->id = $block->instance->blockpositionid; $DB->update_record('block_positions', $bp); } else { if ($needbprecord) { $bp->blockinstanceid = $block->instance->id; $bp->contextid = $this->page->context->id; $bp->pagetype = $this->page->pagetype; if ($this->page->subpage) { $bp->subpage = $this->page->subpage; } else { $bp->subpage = ''; } $DB->insert_record('block_positions', $bp); } } } redirect($this->page->url); } else { $strheading = get_string('blockconfiga', 'moodle', $block->get_title()); $editpage->set_title($strheading); $editpage->set_heading($strheading); $bits = explode('-', $this->page->pagetype); if ($bits[0] == 'tag' && !empty($this->page->subpage)) { // better navbar for tag pages $editpage->navbar->add(get_string('tags'), new moodle_url('/tag/')); $tag = core_tag_tag::get($this->page->subpage); // tag search page doesn't have subpageid if ($tag) { $editpage->navbar->add($tag->get_display_name(), $tag->get_view_url()); } } $editpage->navbar->add($block->get_title()); $editpage->navbar->add(get_string('configuration')); echo $output->header(); echo $output->heading($strheading, 2); $mform->display(); echo $output->footer(); exit; } } }
/** * Update tags * * @param array $tags */ public static function update_tags($tags) { global $CFG, $PAGE, $DB; // Validate and normalize parameters. $tags = self::validate_parameters(self::update_tags_parameters(), array('tags' => $tags)); $systemcontext = context_system::instance(); $canmanage = has_capability('moodle/tag:manage', $systemcontext); $canedit = has_capability('moodle/tag:edit', $systemcontext); $warnings = array(); if (empty($CFG->usetags)) { throw new moodle_exception('tagsaredisabled', 'tag'); } $renderer = $PAGE->get_renderer('core'); foreach ($tags['tags'] as $tag) { $tag = (array) $tag; if (array_key_exists('rawname', $tag)) { $tag['rawname'] = clean_param($tag['rawname'], PARAM_TAG); if (empty($tag['rawname'])) { unset($tag['rawname']); } } if (!$canmanage) { // User without manage capability can not change any fields except for descriptions. $tag = array_intersect_key($tag, array('id' => 1, 'description' => 1, 'descriptionformat' => 1)); } if (!$canedit) { // User without edit capability can not change description. $tag = array_diff_key($tag, array('description' => 1, 'descriptionformat' => 1)); } if (count($tag) <= 1) { $warnings[] = array('item' => $tag['id'], 'warningcode' => 'nothingtoupdate', 'message' => get_string('nothingtoupdate', 'core_tag')); continue; } if (!($tagobject = core_tag_tag::get($tag['id'], '*'))) { $warnings[] = array('item' => $tag['id'], 'warningcode' => 'tagnotfound', 'message' => get_string('tagnotfound', 'error')); continue; } // First check if new tag name is allowed. if (!empty($tag['rawname']) && ($existing = core_tag_tag::get_by_name($tagobject->tagcollid, $tag['rawname']))) { if ($existing->id != $tag['id']) { $warnings[] = array('item' => $tag['id'], 'warningcode' => 'namesalreadybeeingused', 'message' => get_string('namesalreadybeeingused', 'core_tag')); continue; } } if (array_key_exists('official', $tag)) { // Parameter 'official' deprecated and replaced with 'isstandard'. $tag['isstandard'] = $tag['official'] ? 1 : 0; unset($tag['official']); } if (isset($tag['flag'])) { if ($tag['flag']) { $tagobject->flag(); } else { $tagobject->reset_flag(); } unset($tag['flag']); } unset($tag['id']); if (count($tag)) { $tagobject->update($tag); } } return array('warnings' => $warnings); }
$tagcollid = optional_param('tc', 0, PARAM_INT); $tagareaid = optional_param('ta', null, PARAM_INT); $params = array(); if ($perpage != DEFAULT_PAGE_SIZE) { $params['perpage'] = $perpage; } if ($page > 0) { $params['page'] = $page; } admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report')); if (empty($CFG->usetags)) { print_error('tagsaredisabled', 'tag'); } $tagobject = null; if ($tagid) { $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST); $tagcollid = $tagobject->tagcollid; } $tagcoll = core_tag_collection::get_by_id($tagcollid); $tagarea = core_tag_area::get_by_id($tagareaid); $manageurl = new moodle_url('/tag/manage.php'); if ($tagcoll) { // We are inside a tag collection - add it to the page url and the breadcrumb. $PAGE->set_url(new moodle_url($PAGE->url, array('tc' => $tagcoll->id))); $PAGE->navbar->add(core_tag_collection::display_name($tagcoll), new moodle_url($manageurl, array('tc' => $tagcoll->id))); } $PAGE->set_blocks_editing_capability('moodle/tag:editblocks'); switch ($action) { case 'colladd': case 'colledit': if ($action === 'colladd' || $action === 'colledit' && $tagcoll && empty($tagcoll->component)) {
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; }
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'); $PAGE->set_pagelayout('standard'); $tagname = $tag->get_display_name(); $tagcollid = $tag->tagcollid; // set the relatedtags field of the $tag object that will be passed to the form $data = $tag->to_object(); $data->relatedtags = core_tag_tag::get_item_tags_array('core', 'tag', $tag->id);
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); }
$tag = $tags[$maintag]; } 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) {