Merge branch 'MDL-77272-main' of https://github.com/SergioComeron/moodle
[moodle.git] / tag / index.php
blob6ef8fc7b0460817e2a2aec82ba98cc27a85fae49
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.
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/>.
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
25 require_once('../config.php');
26 require_once($CFG->dirroot . '/lib/weblib.php');
27 require_once($CFG->dirroot . '/blog/lib.php');
29 require_login();
31 if (empty($CFG->usetags)) {
32 throw new \moodle_exception('tagsaredisabled', 'tag');
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);
44 $edit = optional_param('edit', -1, PARAM_BOOL);
46 $systemcontext = context_system::instance();
48 if ($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);
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)));
64 } else if ($tagid) {
65 $tag = core_tag_tag::get($tagid, '*');
67 unset($tagid);
68 if (empty($tag)) {
69 redirect(new moodle_url('/tag/search.php'));
72 if ($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 $PAGE->set_secondary_navigation(false);
76 } else {
77 $PAGE->set_context($systemcontext);
80 $tagcollid = $tag->tagcollid;
82 $pageurl = $tag->get_view_url($exclusivemode, $fromctx, $ctx, $rec);
83 $PAGE->set_url($pageurl);
84 $PAGE->set_subpage($tag->id);
85 $tagnode = $PAGE->navigation->find('tags', null);
86 $tagnode->make_active();
87 $PAGE->set_pagelayout('standard');
88 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
90 $buttons = '';
91 if (has_capability('moodle/tag:manage', context_system::instance())) {
92 $buttons .= $OUTPUT->single_button(new moodle_url('/tag/manage.php'),
93 get_string('managetags', 'tag'), 'GET');
95 if ($PAGE->user_allowed_editing()) {
96 if ($edit != -1) {
97 $USER->editing = $edit;
99 $buttons .= $OUTPUT->edit_button(clone($PAGE->url));
102 $PAGE->navbar->add($tagname, $pageurl);
103 $PAGE->set_title(get_string('tag', 'tag') .' - '. $tag->get_display_name());
104 $PAGE->set_heading($COURSE->fullname);
105 $PAGE->set_button($buttons);
107 // Find all areas in this collection and their items tagged with this tag.
108 $tagareas = core_tag_collection::get_areas($tagcollid);
109 if ($tagareaid) {
110 $tagareas = array_intersect_key($tagareas, array($tagareaid => 1));
112 if (!$tagareaid && count($tagareas) == 1) {
113 // Automatically set "exclusive" mode for tag collection with one tag area only.
114 $exclusivemode = 1;
116 $entities = array();
117 foreach ($tagareas as $ta) {
118 $entities[] = $tag->get_tag_index($ta, $exclusivemode, $fromctx, $ctx, $rec, $page);
120 $entities = array_filter($entities);
122 $tagrenderer = $PAGE->get_renderer('core', 'tag');
123 $pagecontents = $tagrenderer->tag_index_page($tag, array_filter($entities), $tagareaid,
124 $exclusivemode, $fromctx, $ctx, $rec, $page);
126 echo $OUTPUT->header();
127 echo $pagecontents;
128 echo $OUTPUT->footer();