MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / blocks / blog_tags / block_blog_tags.php
CommitLineData
f3ea6dfa 1<?PHP //$Id$
2
b51434e7 3define('BLOGDEFAULTTIMEWITHIN', 90);
4define('BLOGDEFAULTNUMBEROFTAGS', 20);
bd1f4559 5define('BLOGDEFAULTSORT', 'name');
b51434e7 6
6caa2c52 7require_once($CFG->dirroot .'/blog/lib.php');
6279c33f 8
f3ea6dfa 9class block_blog_tags extends block_base {
10 function init() {
433c242f 11 $this->version = 2007101509;
332135ef 12 $this->title = get_string('blocktagstitle', 'blog');
f3ea6dfa 13 }
14
7d61cf65 15 function instance_allow_multiple() {
16 return true;
17 }
18
19 function has_config() {
a2d09dee 20 return false;
7d61cf65 21 }
22
b51434e7 23 function applicable_formats() {
9591bc3c 24 return array('all' => true, 'my' => false, 'tag' => false);
b51434e7 25 }
26
7d61cf65 27 function instance_allow_config() {
28 return true;
29 }
30
b51434e7 31 function specialization() {
32
33 // load userdefined title and make sure it's never empty
34 if (empty($this->config->title)) {
35 $this->title = get_string('blocktagstitle','blog');
36 } else {
37 $this->title = $this->config->title;
38 }
39 }
40
f3ea6dfa 41 function get_content() {
b0e90a0c 42 global $CFG, $SITE, $USER, $DB, $OUTPUT;
f3ea6dfa 43
93bbdd50 44 if (empty($CFG->usetags) || empty($CFG->bloglevel)) {
ab2f17b0 45 $this->content->text = '';
730d28c5
NC
46 if ($this->page->user_is_editing()) {
47 $this->content->text = get_string('tagsaredisabled', 'tag');
48 }
ab2f17b0 49 return $this->content;
50 }
51
b51434e7 52 if (empty($this->config->timewithin)) {
53 $this->config->timewithin = BLOGDEFAULTTIMEWITHIN;
54 }
55 if (empty($this->config->numberoftags)) {
56 $this->config->numberoftags = BLOGDEFAULTNUMBEROFTAGS;
57 }
58 if (empty($this->config->sort)) {
59 $this->config->sort = BLOGDEFAULTSORT;
60 }
f3ea6dfa 61
62 if ($this->content !== NULL) {
63 return $this->content;
64 }
65
f3ea6dfa 66 $this->content = new stdClass;
67 $this->content->text = '';
68 $this->content->footer = '';
69
730d28c5 70 /// Get a list of tags
84895c53 71 $timewithin = time() - $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
730d28c5
NC
72
73 $blogheaders = blog_get_headers();
e96f2a77 74
45df7de3 75 // admins should be able to read all tags
76 $type = '';
957f6fc9 77 if (!has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_SYSTEM))) {
45df7de3 78 $type = " AND (p.publishstate = 'site' or p.publishstate='public')";
81c07f5e 79 }
f3ea6dfa 80
219f652b 81 $sql = "SELECT t.id, t.tagtype, t.rawname, t.name, COUNT(DISTINCT ti.id) AS ct
730d28c5 82 FROM {tag} t, {tag_instance} ti, {post} p, {blog_association} ba
219f652b 83 WHERE t.id = ti.tagid AND p.id = ti.itemid
84 $type
39356e18 85 AND ti.itemtype = 'post'
730d28c5
NC
86 AND ti.timemodified > $timewithin";
87
88 if (!empty($blogheaders['filters']['module'])) {
89 $modulecontext = get_context_instance(CONTEXT_MODULE, $blogheaders['filters']['module']);
90 $sql .= " AND ba.contextid = $modulecontext->id AND p.id = ba.blogid ";
91 } else if (!empty($blogheaders['filters']['course'])) {
92 $coursecontext = get_context_instance(CONTEXT_COURSE, $blogheaders['filters']['course']);
93 $sql .= " AND ba.contextid = $coursecontext->id AND p.id = ba.blogid ";
94 }
95
96 $sql .= "
219f652b 97 GROUP BY t.id, t.tagtype, t.name, t.rawname
98 ORDER BY ct DESC, t.name ASC";
99
98891b93 100 if ($tags = $DB->get_records_sql($sql, null, 0, $this->config->numberoftags)) {
7d61cf65 101
e8964639 102 /// There are 2 things to do:
103 /// 1. tags with the same count should have the same size class
104 /// 2. however many tags we have should be spread evenly over the
105 /// 20 size classes
e96f2a77 106
e8964639 107 $totaltags = count($tags);
108 $currenttag = 0;
109
110 $size = 20;
111 $lasttagct = -1;
e96f2a77 112
7d61cf65 113 $etags = array();
114 foreach ($tags as $tag) {
e96f2a77 115
e8964639 116 $currenttag++;
117
118 if ($currenttag == 1) {
119 $lasttagct = $tag->ct;
120 $size = 20;
121 } else if ($tag->ct != $lasttagct) {
122 $lasttagct = $tag->ct;
123 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
124 }
e96f2a77 125
bd1f4559 126 $tag->class = "$tag->tagtype s$size";
7d61cf65 127 $etags[] = $tag;
f3ea6dfa 128
7d61cf65 129 }
130
e8964639 131 /// Now we sort the tag display order
b51434e7 132 $CFG->tagsort = $this->config->sort;
7d61cf65 133 usort($etags, "blog_tags_sort");
e96f2a77 134
e8964639 135 /// Finally we create the output
5489fce7 136 /// Accessibility: markup as a list.
137 $this->content->text .= "\n<ul class='inline-list'>\n";
7d61cf65 138 foreach ($etags as $tag) {
730d28c5
NC
139 $blogurl = new moodle_url($CFG->wwwroot.'/blog/index.php');
140
6279c33f 141 switch ($CFG->bloglevel) {
142 case BLOG_USER_LEVEL:
730d28c5 143 $blogurl->param('userid', $USER->id);
6279c33f 144 break;
145
6279c33f 146 default:
730d28c5
NC
147 if (!empty($blogheaders['filters']['module'])) {
148 $blogurl->param('modid', $blogheaders['filters']['module']);
149 } else if (!empty($blogheaders['filters']['course'])) {
150 $blogurl->param('courseid', $blogheaders['filters']['course']);
25fb38d4 151 }
730d28c5 152
6279c33f 153 break;
154 }
155
730d28c5
NC
156 $blogurl->param('tagid', $tag->id);
157 $link = html_link::make($blogurl, tag_display_name($tag));
b0e90a0c 158 $link->add_class($tag->class);
159 $link->title = get_string('numberofentries','blog',$tag->ct);
160 $this->content->text .= '<li>' . $OUTPUT->link($link) . '</li> ';
f3ea6dfa 161 }
5489fce7 162 $this->content->text .= "\n</ul>\n";
f3ea6dfa 163
7d61cf65 164 }
f3ea6dfa 165 return $this->content;
166 }
7d61cf65 167}
168
169function blog_tags_sort($a, $b) {
b51434e7 170 global $CFG;
171
172 if (empty($CFG->tagsort)) {
173 return 0;
174 } else {
175 $tagsort = $CFG->tagsort;
176 }
177
178 if (is_numeric($a->$tagsort)) {
179 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
180 } elseif (is_string($a->$tagsort)) {
181 return strcmp($a->$tagsort, $b->$tagsort);
182 } else {
183 return 0;
184 }
f3ea6dfa 185}
7d61cf65 186
f3ea6dfa 187?>