Added some new strings for blog tags block
[moodle.git] / blocks / blog_tags / block_blog_tags.php
CommitLineData
f3ea6dfa 1<?PHP //$Id$
2
3class block_blog_tags extends block_base {
4 function init() {
6479f657 5 $this->title = get_string('blogtags', 'blog');
f3ea6dfa 6 $this->version = 2006032000;
7 }
8
7d61cf65 9 function instance_allow_multiple() {
10 return true;
11 }
12
13 function has_config() {
14 return true;
15 }
16
17 function instance_allow_config() {
18 return true;
19 }
20
f3ea6dfa 21 function get_content() {
22
23 global $CFG;
24
25 $timewithin = time() - 7776000; // last 90 days
26 $topentries = 20; // get the 20 most popular tags
f3ea6dfa 27
28 if ($this->content !== NULL) {
29 return $this->content;
30 }
31
32 if (empty($this->instance)) {
33 $this->content = '';
34 return $this->content;
35 }
36
37 $this->content = new stdClass;
38 $this->content->text = '';
39 $this->content->footer = '';
40
41
42 /// Get a list of tags
43
44 $sql = 'SELECT t.*, COUNT(DISTINCT bt.id) as ct ';
45 $sql .= "FROM {$CFG->prefix}tags as t, {$CFG->prefix}blog_tag_instance as bt ";
46 $sql .= 'WHERE t.id = bt.tagid ';
47 $sql .= "AND bt.timemodified > $timewithin ";
48 $sql .= 'GROUP BY bt.tagid ';
49 $sql .= 'ORDER BY ct DESC, t.text ASC ';
50 $sql .= "LIMIT $topentries ";
51
7d61cf65 52 if ($tags = get_records_sql($sql)) {
53
e8964639 54 /// There are 2 things to do:
55 /// 1. tags with the same count should have the same size class
56 /// 2. however many tags we have should be spread evenly over the
57 /// 20 size classes
58
59 $totaltags = count($tags);
60 $currenttag = 0;
61
62 $size = 20;
63 $lasttagct = -1;
64
7d61cf65 65 $etags = array();
66 foreach ($tags as $tag) {
e8964639 67
68 $currenttag++;
69
70 if ($currenttag == 1) {
71 $lasttagct = $tag->ct;
72 $size = 20;
73 } else if ($tag->ct != $lasttagct) {
74 $lasttagct = $tag->ct;
75 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
76 }
77
7d61cf65 78 $tag->class = "$tag->type s$size";
79 $etags[] = $tag;
f3ea6dfa 80
7d61cf65 81 }
82
e8964639 83 /// Now we sort the tag display order
7d61cf65 84 usort($etags, "blog_tags_sort");
f3ea6dfa 85
e8964639 86 /// Finally we create the output
7d61cf65 87 foreach ($etags as $tag) {
e8964639 88 $link = $CFG->wwwroot.'/blog/index.php?courseid='.
89 $this->instance->pageid.'&filtertype=site&tagid='.$tag->id;
90 $this->content->text .= '<a href="'.$link.'" class="'.
91 $tag->class.'">'.$tag->text.'</a> ';
f3ea6dfa 92 }
f3ea6dfa 93
7d61cf65 94 }
f3ea6dfa 95 return $this->content;
96 }
97
98 function applicable_formats() {
99 return array('all' => true, 'my' => false);
100 }
7d61cf65 101
102}
103
104function blog_tags_sort($a, $b) {
105 return strcmp($a->text, $b->text);
f3ea6dfa 106}
7d61cf65 107
f3ea6dfa 108?>