1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * @package    core_tag
20 * @category   tag
21 * @copyright  2007 Luiz Cruz <luiz.laydner@gmail.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once('../config.php');
26require_once($CFG->dirroot . '/lib/weblib.php');
27require_once($CFG->dirroot . '/blog/lib.php');
28
29require_login();
30
31if (empty($CFG->usetags)) {
32    print_error('tagsaredisabled', 'tag');
33}
34
35$tagid       = optional_param('id', 0, PARAM_INT); // tag id
36$tagname     = optional_param('tag', '', PARAM_TAG); // tag
37$tagareaid   = optional_param('ta', 0, PARAM_INT); // Tag area id.
38$exclusivemode = optional_param('excl', 0, PARAM_BOOL); // Exclusive mode (show entities in one tag area only).
39$page        = optional_param('page', 0, PARAM_INT); // Page to display.
40$fromctx     = optional_param('from', null, PARAM_INT);
41$ctx         = optional_param('ctx', null, PARAM_INT);
42$rec         = optional_param('rec', 1, PARAM_INT);
43
44$edit        = optional_param('edit', -1, PARAM_BOOL);
45
46$systemcontext   = context_system::instance();
47
48if ($tagname) {
49    $tagcollid = optional_param('tc', 0, PARAM_INT);
50    if (!$tagcollid) {
51        // Tag name specified but tag collection was not. Try to guess it.
52        $tags = core_tag_tag::guess_by_name($tagname, '*');
53        if (count($tags) > 1) {
54            // This tag was found in more than one collection, redirect to search.
55            redirect(new moodle_url('/tag/search.php', array('query' => $tagname)));
56        } else if (count($tags) == 1) {
57            $tag = reset($tags);
58        }
59    } else {
60        if (!$tag = core_tag_tag::get_by_name($tagcollid, $tagname, '*')) {
61            redirect(new moodle_url('/tag/search.php', array('tc' => $tagcollid, 'query' => $tagname)));
62        }
63    }
64} else if ($tagid) {
65    $tag = core_tag_tag::get($tagid, '*');
66}
67unset($tagid);
68if (empty($tag)) {
69    redirect(new moodle_url('/tag/search.php'));
70}
71
72if ($ctx && ($context = context::instance_by_id($ctx, IGNORE_MISSING)) && $context->contextlevel >= CONTEXT_COURSE) {
73    list($context, $course, $cm) = get_context_info_array($context->id);
74    require_login($course, false, $cm, false, true);
75} else {
76    $PAGE->set_context($systemcontext);
77}
78
79$tagcollid = $tag->tagcollid;
80
81$PAGE->set_url($tag->get_view_url($exclusivemode, $fromctx, $ctx, $rec));
82$PAGE->set_subpage($tag->id);
83$tagnode = $PAGE->navigation->find('tags', null);
84$tagnode->make_active();
85$PAGE->set_pagelayout('standard');
86$PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
87
88$buttons = '';
89if (has_capability('moodle/tag:manage', context_system::instance())) {
90    $buttons .= $OUTPUT->single_button(new moodle_url('/tag/manage.php'),
91            get_string('managetags', 'tag'), 'GET');
92}
93if ($PAGE->user_allowed_editing()) {
94    if ($edit != -1) {
95        $USER->editing = $edit;
96    }
97    $buttons .= $OUTPUT->edit_button(clone($PAGE->url));
98}
99
100$PAGE->navbar->add($tagname);
101$PAGE->set_title(get_string('tag', 'tag') .' - '. $tag->get_display_name());
102$PAGE->set_heading($COURSE->fullname);
103$PAGE->set_button($buttons);
104
105// Find all areas in this collection and their items tagged with this tag.
106$tagareas = core_tag_collection::get_areas($tagcollid);
107if ($tagareaid) {
108    $tagareas = array_intersect_key($tagareas, array($tagareaid => 1));
109}
110if (!$tagareaid && count($tagareas) == 1) {
111    // Automatically set "exclusive" mode for tag collection with one tag area only.
112    $exclusivemode = 1;
113}
114$entities = array();
115foreach ($tagareas as $ta) {
116    $entities[] = $tag->get_tag_index($ta, $exclusivemode, $fromctx, $ctx, $rec, $page);
117}
118$entities = array_filter($entities);
119
120$tagrenderer = $PAGE->get_renderer('core', 'tag');
121$pagecontents = $tagrenderer->tag_index_page($tag, array_filter($entities), $tagareaid,
122        $exclusivemode, $fromctx, $ctx, $rec, $page);
123
124echo $OUTPUT->header();
125echo $pagecontents;
126echo $OUTPUT->footer();
127