Merge branch 'MDL-58490-master' of git://github.com/lameze/moodle
[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
ac840cf4 78 } else if (!core_tag_tag::is_enabled('core', 'post')) {
fd85ceac 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
e11d7380 124 $sql = "SELECT t.id, t.isstandard, 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'
ac840cf4 129 AND ti.component = 'core'
730d28c5
NC
130 AND ti.timemodified > $timewithin";
131
8eaf1ba1
SH
132 if ($context->contextlevel == CONTEXT_MODULE) {
133 $sql .= " AND ba.contextid = $context->id AND p.id = ba.blogid ";
134 } else if ($context->contextlevel == CONTEXT_COURSE) {
135 $sql .= " AND ba.contextid = $context->id AND p.id = ba.blogid ";
730d28c5
NC
136 }
137
138 $sql .= "
e11d7380 139 GROUP BY t.id, t.isstandard, t.name, t.rawname
219f652b 140 ORDER BY ct DESC, t.name ASC";
141
98891b93 142 if ($tags = $DB->get_records_sql($sql, null, 0, $this->config->numberoftags)) {
7d61cf65 143
e8964639 144 /// There are 2 things to do:
145 /// 1. tags with the same count should have the same size class
146 /// 2. however many tags we have should be spread evenly over the
147 /// 20 size classes
e96f2a77 148
e8964639 149 $totaltags = count($tags);
150 $currenttag = 0;
151
152 $size = 20;
153 $lasttagct = -1;
e96f2a77 154
7d61cf65 155 $etags = array();
156 foreach ($tags as $tag) {
e96f2a77 157
e8964639 158 $currenttag++;
159
160 if ($currenttag == 1) {
161 $lasttagct = $tag->ct;
162 $size = 20;
163 } else if ($tag->ct != $lasttagct) {
164 $lasttagct = $tag->ct;
165 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
166 }
e96f2a77 167
e11d7380 168 $tag->class = ($tag->isstandard ? "standardtag " : "") . "s$size";
7d61cf65 169 $etags[] = $tag;
f3ea6dfa 170
7d61cf65 171 }
172
e8964639 173 /// Now we sort the tag display order
b51434e7 174 $CFG->tagsort = $this->config->sort;
fd85ceac 175 usort($etags, "block_blog_tags_sort");
e96f2a77 176
e8964639 177 /// Finally we create the output
5489fce7 178 /// Accessibility: markup as a list.
179 $this->content->text .= "\n<ul class='inline-list'>\n";
7d61cf65 180 foreach ($etags as $tag) {
a6855934 181 $blogurl = new moodle_url('/blog/index.php');
730d28c5 182
6279c33f 183 switch ($CFG->bloglevel) {
184 case BLOG_USER_LEVEL:
730d28c5 185 $blogurl->param('userid', $USER->id);
6279c33f 186 break;
187
6279c33f 188 default:
8eaf1ba1
SH
189 if ($context->contextlevel == CONTEXT_MODULE) {
190 $blogurl->param('modid', $context->instanceid);
191 } else if ($context->contextlevel == CONTEXT_COURSE) {
192 $blogurl->param('courseid', $context->instanceid);
25fb38d4 193 }
4ca6cfbf 194
6279c33f 195 break;
196 }
197
730d28c5 198 $blogurl->param('tagid', $tag->id);
ac840cf4
MG
199 $link = html_writer::link($blogurl, core_tag_tag::make_display_name($tag),
200 array('class' => $tag->class,
201 'title' => get_string('numberofentries', 'blog', $tag->ct)));
0f4c64b7 202 $this->content->text .= '<li>' . $link . '</li> ';
f3ea6dfa 203 }
5489fce7 204 $this->content->text .= "\n</ul>\n";
f3ea6dfa 205
7d61cf65 206 }
f3ea6dfa 207 return $this->content;
208 }
7d61cf65 209}
210
fd85ceac 211function block_blog_tags_sort($a, $b) {
b51434e7 212 global $CFG;
213
214 if (empty($CFG->tagsort)) {
215 return 0;
216 } else {
217 $tagsort = $CFG->tagsort;
218 }
219
220 if (is_numeric($a->$tagsort)) {
221 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
222 } elseif (is_string($a->$tagsort)) {
fd85ceac 223 return strcmp($a->$tagsort, $b->$tagsort); //TODO: this is not compatible with UTF-8!!
b51434e7 224 } else {
225 return 0;
226 }
f3ea6dfa 227}
7d61cf65 228
4ca6cfbf 229