Merge branch 'm25_MDL-41410_Move_Comment_To_Appropriate_Place' of https://github...
[moodle.git] / blocks / blog_tags / block_blog_tags.php
blob9fb8b3a1bad373054b8ace340e287d9be6c3088f
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/>.
17 /**
18 * Blog tags block.
20 * @package block
21 * @subpackage blog_tags
22 * @copyright 2006 Shane Elliott
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 define('BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN', 90);
29 define('BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS', 20);
30 define('BLOCK_BLOG_TAGS_DEFAULTSORT', 'name');
32 class block_blog_tags extends block_base {
33 function init() {
34 $this->title = get_string('pluginname', 'block_blog_tags');
37 function instance_allow_multiple() {
38 return true;
41 function has_config() {
42 return false;
45 function applicable_formats() {
46 return array('all' => true, 'my' => false, 'tag' => false);
49 function instance_allow_config() {
50 return true;
53 function specialization() {
55 // load userdefined title and make sure it's never empty
56 if (empty($this->config->title)) {
57 $this->title = get_string('pluginname', 'block_blog_tags');
58 } else {
59 $this->title = $this->config->title;
63 function get_content() {
64 global $CFG, $SITE, $USER, $DB, $OUTPUT;
66 if ($this->content !== NULL) {
67 return $this->content;
70 // make sure blog and tags are actually enabled
71 if (empty($CFG->bloglevel)) {
72 $this->content = new stdClass();
73 $this->content->text = '';
74 if ($this->page->user_is_editing()) {
75 $this->content->text = get_string('blogdisable', 'blog');
77 return $this->content;
79 } else if (empty($CFG->usetags)) {
80 $this->content = new stdClass();
81 $this->content->text = '';
82 if ($this->page->user_is_editing()) {
83 $this->content->text = get_string('tagsaredisabled', 'tag');
85 return $this->content;
87 } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) {
88 $this->content = new stdClass();
89 $this->content->text = '';
90 return $this->content;
93 // require the libs and do the work
94 require_once($CFG->dirroot .'/blog/lib.php');
96 if (empty($this->config)) {
97 $this->config = new stdClass();
100 if (empty($this->config->timewithin)) {
101 $this->config->timewithin = BLOCK_BLOG_TAGS_DEFAULTTIMEWITHIN;
103 if (empty($this->config->numberoftags)) {
104 $this->config->numberoftags = BLOCK_BLOG_TAGS_DEFAULTNUMBEROFTAGS;
106 if (empty($this->config->sort)) {
107 $this->config->sort = BLOCK_BLOG_TAGS_DEFAULTSORT;
110 $this->content = new stdClass();
111 $this->content->text = '';
112 $this->content->footer = '';
114 /// Get a list of tags
115 $timewithin = time() - $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
117 $context = $this->page->context;
119 // admins should be able to read all tags
120 $type = '';
121 if (!has_capability('moodle/user:readuserblogs', context_system::instance())) {
122 $type = " AND (p.publishstate = 'site' or p.publishstate='public')";
125 $sql = "SELECT t.id, t.tagtype, t.rawname, t.name, COUNT(DISTINCT ti.id) AS ct
126 FROM {tag} t, {tag_instance} ti, {post} p, {blog_association} ba
127 WHERE t.id = ti.tagid AND p.id = ti.itemid
128 $type
129 AND ti.itemtype = 'post'
130 AND ti.timemodified > $timewithin";
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 ";
138 $sql .= "
139 GROUP BY t.id, t.tagtype, t.name, t.rawname
140 ORDER BY ct DESC, t.name ASC";
142 if ($tags = $DB->get_records_sql($sql, null, 0, $this->config->numberoftags)) {
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
149 $totaltags = count($tags);
150 $currenttag = 0;
152 $size = 20;
153 $lasttagct = -1;
155 $etags = array();
156 foreach ($tags as $tag) {
158 $currenttag++;
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) );
168 $tag->class = "$tag->tagtype s$size";
169 $etags[] = $tag;
173 /// Now we sort the tag display order
174 $CFG->tagsort = $this->config->sort;
175 usort($etags, "block_blog_tags_sort");
177 /// Finally we create the output
178 /// Accessibility: markup as a list.
179 $this->content->text .= "\n<ul class='inline-list'>\n";
180 foreach ($etags as $tag) {
181 $blogurl = new moodle_url('/blog/index.php');
183 switch ($CFG->bloglevel) {
184 case BLOG_USER_LEVEL:
185 $blogurl->param('userid', $USER->id);
186 break;
188 default:
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);
195 break;
198 $blogurl->param('tagid', $tag->id);
199 $link = html_writer::link($blogurl, tag_display_name($tag), array('class'=>$tag->class, 'title'=>get_string('numberofentries','blog',$tag->ct)));
200 $this->content->text .= '<li>' . $link . '</li> ';
202 $this->content->text .= "\n</ul>\n";
205 return $this->content;
209 function block_blog_tags_sort($a, $b) {
210 global $CFG;
212 if (empty($CFG->tagsort)) {
213 return 0;
214 } else {
215 $tagsort = $CFG->tagsort;
218 if (is_numeric($a->$tagsort)) {
219 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
220 } elseif (is_string($a->$tagsort)) {
221 return strcmp($a->$tagsort, $b->$tagsort); //TODO: this is not compatible with UTF-8!!
222 } else {
223 return 0;