2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * @package block_blog_tags
21 * @copyright 2006 Shane Elliott
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 define('BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN', 90);
28 define('BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS', 20);
29 define('BLOCK_BLOG_TAGS_DEFAULTSORT', 'name');
31 class block_blog_tags extends block_base {
33 $this->title = get_string('pluginname', 'block_blog_tags');
36 function instance_allow_multiple() {
40 function has_config() {
44 function applicable_formats() {
45 return array('all' => true, 'my' => false, 'tag' => false);
48 function instance_allow_config() {
52 function specialization() {
54 // load userdefined title and make sure it's never empty
55 if (empty($this->config->title)) {
56 $this->title = get_string('pluginname', 'block_blog_tags');
58 $this->title = $this->config->title;
62 function get_content() {
63 global $CFG, $SITE, $USER, $DB, $OUTPUT;
65 if ($this->content !== NULL) {
66 return $this->content;
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');
76 return $this->content;
78 } else if (empty($CFG->usetags)) {
79 $this->content = new stdClass();
80 $this->content->text = '';
81 if ($this->page->user_is_editing()) {
82 $this->content->text = get_string('tagsaredisabled', 'tag');
84 return $this->content;
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;
92 // require the libs and do the work
93 require_once($CFG->dirroot .'/blog/lib.php');
95 if (empty($this->config)) {
96 $this->config = new stdClass();
99 if (empty($this->config->timewithin)) {
100 $this->config->timewithin = BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN;
102 if (empty($this->config->numberoftags)) {
103 $this->config->numberoftags = BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS;
105 if (empty($this->config->sort)) {
106 $this->config->sort = BLOCK_BLOG_TAGS_DEFAULTSORT;
109 $this->content = new stdClass();
110 $this->content->text = '';
111 $this->content->footer = '';
113 /// Get a list of tags
114 $timewithin = time() - $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
116 $context = $this->page->context;
118 // admins should be able to read all tags
120 if (!has_capability('moodle/user:readuserblogs', context_system::instance())) {
121 $type = " AND (p.publishstate = 'site' or p.publishstate='public')";
124 $sql = "SELECT t.id, t.tagtype, t.rawname, t.name, COUNT(DISTINCT ti.id) AS ct
125 FROM {tag} t, {tag_instance} ti, {post} p, {blog_association} ba
126 WHERE t.id = ti.tagid AND p.id = ti.itemid
128 AND ti.itemtype = 'post'
129 AND ti.timemodified > $timewithin";
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 ";
138 GROUP BY t.id, t.tagtype, t.name, t.rawname
139 ORDER BY ct DESC, t.name ASC";
141 if ($tags = $DB->get_records_sql($sql, null, 0, $this->config->numberoftags)) {
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
148 $totaltags = count($tags);
155 foreach ($tags as $tag) {
159 if ($currenttag == 1) {
160 $lasttagct = $tag->ct;
162 } else if ($tag->ct != $lasttagct) {
163 $lasttagct = $tag->ct;
164 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
167 $tag->class = "$tag->tagtype s$size";
172 /// Now we sort the tag display order
173 $CFG->tagsort = $this->config->sort;
174 usort($etags, "block_blog_tags_sort");
176 /// Finally we create the output
177 /// Accessibility: markup as a list.
178 $this->content->text .= "\n<ul class='inline-list'>\n";
179 foreach ($etags as $tag) {
180 $blogurl = new moodle_url('/blog/index.php');
182 switch ($CFG->bloglevel) {
183 case BLOG_USER_LEVEL:
184 $blogurl->param('userid', $USER->id);
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);
197 $blogurl->param('tagid', $tag->id);
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> ';
201 $this->content->text .= "\n</ul>\n";
204 return $this->content;
208 function block_blog_tags_sort($a, $b) {
211 if (empty($CFG->tagsort)) {
214 $tagsort = $CFG->tagsort;
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)) {
220 return strcmp($a->$tagsort, $b->$tagsort); //TODO: this is not compatible with UTF-8!!