MDL-50851 block_tags: tag collections settings
[moodle.git] / blocks / blog_tags / block_blog_tags.php
CommitLineData
4ca6cfbf 1<?php
fd85ceac
PS
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 * Blog tags block.
19 *
f25a6839 20 * @package block_blog_tags
fd85ceac
PS
21 * @copyright 2006 Shane Elliott
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27define('BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN', 90);
28define('BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS', 20);
29define('BLOCK_BLOG_TAGS_DEFAULTSORT', 'name');
6279c33f 30
f3ea6dfa 31class block_blog_tags extends block_base {
32 function init() {
aaa5c766 33 $this->title = get_string('pluginname', 'block_blog_tags');
f3ea6dfa 34 }
35
7d61cf65 36 function instance_allow_multiple() {
37 return true;
38 }
39
40 function has_config() {
a2d09dee 41 return false;
7d61cf65 42 }
43
b51434e7 44 function applicable_formats() {
9591bc3c 45 return array('all' => true, 'my' => false, 'tag' => false);
b51434e7 46 }
47
7d61cf65 48 function instance_allow_config() {
49 return true;
50 }
51
b51434e7 52 function specialization() {
53
54 // load userdefined title and make sure it's never empty
55 if (empty($this->config->title)) {
aaa5c766 56 $this->title = get_string('pluginname', 'block_blog_tags');
b51434e7 57 } else {
58 $this->title = $this->config->title;
59 }
60 }
61
f3ea6dfa 62 function get_content() {
b0e90a0c 63 global $CFG, $SITE, $USER, $DB, $OUTPUT;
f3ea6dfa 64
fd85ceac
PS
65 if ($this->content !== NULL) {
66 return $this->content;
67 }
68
69 // make sure blog and tags are actually enabled
70 if (empty($CFG->bloglevel)) {
71 $this->content = new stdClass();
72 $this->content->text = '';
73 if ($this->page->user_is_editing()) {
74 $this->content->text = get_string('blogdisable', 'blog');
75 }
76 return $this->content;
77
78 } else if (empty($CFG->usetags)) {
79 $this->content = new stdClass();
ab2f17b0 80 $this->content->text = '';
730d28c5
NC
81 if ($this->page->user_is_editing()) {
82 $this->content->text = get_string('tagsaredisabled', 'tag');
83 }
ab2f17b0 84 return $this->content;
fd85ceac
PS
85
86 } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) {
87 $this->content = new stdClass();
88 $this->content->text = '';
89 return $this->content;
ab2f17b0 90 }
91
fd85ceac
PS
92 // require the libs and do the work
93 require_once($CFG->dirroot .'/blog/lib.php');
94
7f3b68a8
DP
95 if (empty($this->config)) {
96 $this->config = new stdClass();
97 }
98
b51434e7 99 if (empty($this->config->timewithin)) {
fd85ceac 100 $this->config->timewithin = BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN;
b51434e7 101 }
102 if (empty($this->config->numberoftags)) {
fd85ceac 103 $this->config->numberoftags = BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS;
b51434e7 104 }
105 if (empty($this->config->sort)) {
fd85ceac 106 $this->config->sort = BLOCK_BLOG_TAGS_DEFAULTSORT;
f3ea6dfa 107 }
108
fd85ceac 109 $this->content = new stdClass();
f3ea6dfa 110 $this->content->text = '';
111 $this->content->footer = '';
112
4ca6cfbf 113 /// Get a list of tags
84895c53 114 $timewithin = time() - $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
4ca6cfbf 115
8eaf1ba1 116 $context = $this->page->context;
e96f2a77 117
45df7de3 118 // admins should be able to read all tags
119 $type = '';
fe2fdd11 120 if (!has_capability('moodle/user:readuserblogs', context_system::instance())) {
45df7de3 121 $type = " AND (p.publishstate = 'site' or p.publishstate='public')";
81c07f5e 122 }
f3ea6dfa 123
219f652b 124 $sql = "SELECT t.id, t.tagtype, t.rawname, t.name, COUNT(DISTINCT ti.id) AS ct
730d28c5 125 FROM {tag} t, {tag_instance} ti, {post} p, {blog_association} ba
219f652b 126 WHERE t.id = ti.tagid AND p.id = ti.itemid
127 $type
39356e18 128 AND ti.itemtype = 'post'
730d28c5
NC
129 AND ti.timemodified > $timewithin";
130
8eaf1ba1
SH
131 if ($context->contextlevel == CONTEXT_MODULE) {
132 $sql .= " AND ba.contextid = $context->id AND p.id = ba.blogid ";
133 } else if ($context->contextlevel == CONTEXT_COURSE) {
134 $sql .= " AND ba.contextid = $context->id AND p.id = ba.blogid ";
730d28c5
NC
135 }
136
137 $sql .= "
219f652b 138 GROUP BY t.id, t.tagtype, t.name, t.rawname
139 ORDER BY ct DESC, t.name ASC";
140
98891b93 141 if ($tags = $DB->get_records_sql($sql, null, 0, $this->config->numberoftags)) {
7d61cf65 142
e8964639 143 /// There are 2 things to do:
144 /// 1. tags with the same count should have the same size class
145 /// 2. however many tags we have should be spread evenly over the
146 /// 20 size classes
e96f2a77 147
e8964639 148 $totaltags = count($tags);
149 $currenttag = 0;
150
151 $size = 20;
152 $lasttagct = -1;
e96f2a77 153
7d61cf65 154 $etags = array();
155 foreach ($tags as $tag) {
e96f2a77 156
e8964639 157 $currenttag++;
158
159 if ($currenttag == 1) {
160 $lasttagct = $tag->ct;
161 $size = 20;
162 } else if ($tag->ct != $lasttagct) {
163 $lasttagct = $tag->ct;
164 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
165 }
e96f2a77 166
bd1f4559 167 $tag->class = "$tag->tagtype s$size";
7d61cf65 168 $etags[] = $tag;
f3ea6dfa 169
7d61cf65 170 }
171
e8964639 172 /// Now we sort the tag display order
b51434e7 173 $CFG->tagsort = $this->config->sort;
fd85ceac 174 usort($etags, "block_blog_tags_sort");
e96f2a77 175
e8964639 176 /// Finally we create the output
5489fce7 177 /// Accessibility: markup as a list.
178 $this->content->text .= "\n<ul class='inline-list'>\n";
7d61cf65 179 foreach ($etags as $tag) {
a6855934 180 $blogurl = new moodle_url('/blog/index.php');
730d28c5 181
6279c33f 182 switch ($CFG->bloglevel) {
183 case BLOG_USER_LEVEL:
730d28c5 184 $blogurl->param('userid', $USER->id);
6279c33f 185 break;
186
6279c33f 187 default:
8eaf1ba1
SH
188 if ($context->contextlevel == CONTEXT_MODULE) {
189 $blogurl->param('modid', $context->instanceid);
190 } else if ($context->contextlevel == CONTEXT_COURSE) {
191 $blogurl->param('courseid', $context->instanceid);
25fb38d4 192 }
4ca6cfbf 193
6279c33f 194 break;
195 }
196
730d28c5 197 $blogurl->param('tagid', $tag->id);
0f4c64b7
PS
198 $link = html_writer::link($blogurl, tag_display_name($tag), array('class'=>$tag->class, 'title'=>get_string('numberofentries','blog',$tag->ct)));
199 $this->content->text .= '<li>' . $link . '</li> ';
f3ea6dfa 200 }
5489fce7 201 $this->content->text .= "\n</ul>\n";
f3ea6dfa 202
7d61cf65 203 }
f3ea6dfa 204 return $this->content;
205 }
7d61cf65 206}
207
fd85ceac 208function block_blog_tags_sort($a, $b) {
b51434e7 209 global $CFG;
210
211 if (empty($CFG->tagsort)) {
212 return 0;
213 } else {
214 $tagsort = $CFG->tagsort;
215 }
216
217 if (is_numeric($a->$tagsort)) {
218 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
219 } elseif (is_string($a->$tagsort)) {
fd85ceac 220 return strcmp($a->$tagsort, $b->$tagsort); //TODO: this is not compatible with UTF-8!!
b51434e7 221 } else {
222 return 0;
223 }
f3ea6dfa 224}
7d61cf65 225
4ca6cfbf 226