fix undefined variable $context on line 135
[moodle.git] / blocks / blog_tags / block_blog_tags.php
CommitLineData
f3ea6dfa 1<?PHP //$Id$
2
b51434e7 3define('BLOGDEFAULTTIMEWITHIN', 90);
4define('BLOGDEFAULTNUMBEROFTAGS', 20);
5define('BLOGDEFAULTSORT', 'text');
6
6caa2c52 7require_once($CFG->dirroot .'/blog/lib.php');
6279c33f 8
f3ea6dfa 9class block_blog_tags extends block_base {
10 function init() {
f3ea6dfa 11 $this->version = 2006032000;
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() {
24 return array('all' => true, 'my' => false);
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
41
f3ea6dfa 42 function get_content() {
43
25fb38d4 44 global $CFG, $SITE, $COURSE;
f3ea6dfa 45
ab2f17b0 46 if (empty($CFG->bloglevel)) {
47 $this->content->text = '';
48 return $this->content;
49 }
50
b51434e7 51 if (empty($this->config->timewithin)) {
52 $this->config->timewithin = BLOGDEFAULTTIMEWITHIN;
53 }
54 if (empty($this->config->numberoftags)) {
55 $this->config->numberoftags = BLOGDEFAULTNUMBEROFTAGS;
56 }
57 if (empty($this->config->sort)) {
58 $this->config->sort = BLOGDEFAULTSORT;
59 }
f3ea6dfa 60
61 if ($this->content !== NULL) {
62 return $this->content;
63 }
64
65 if (empty($this->instance)) {
66 $this->content = '';
67 return $this->content;
68 }
69
70 $this->content = new stdClass;
71 $this->content->text = '';
72 $this->content->footer = '';
73
74
75 /// Get a list of tags
b51434e7 76
77 $timewithin = $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
f3ea6dfa 78
11c467d9 79 $sql = 'SELECT t.id, t.type, t.text, COUNT(DISTINCT bt.id) as ct ';
56c59187 80 $sql .= "FROM {$CFG->prefix}tags as t, {$CFG->prefix}blog_tag_instance as bt, {$CFG->prefix}post as p ";
f3ea6dfa 81 $sql .= 'WHERE t.id = bt.tagid ';
56c59187 82 $sql .= 'AND p.id = bt.entryid ';
30550795 83 $sql .= 'AND (p.publishstate = \'site\' or p.publishstate=\'public\') ';
b51434e7 84 $sql .= "AND bt.timemodified > {$timewithin} ";
11c467d9 85 $sql .= 'GROUP BY t.id, t.type, t.text ';
f3ea6dfa 86 $sql .= 'ORDER BY ct DESC, t.text ASC ';
b51434e7 87 $sql .= "LIMIT {$this->config->numberoftags} ";
f3ea6dfa 88
7d61cf65 89 if ($tags = get_records_sql($sql)) {
90
e8964639 91 /// There are 2 things to do:
92 /// 1. tags with the same count should have the same size class
93 /// 2. however many tags we have should be spread evenly over the
94 /// 20 size classes
95
96 $totaltags = count($tags);
97 $currenttag = 0;
98
99 $size = 20;
100 $lasttagct = -1;
101
7d61cf65 102 $etags = array();
103 foreach ($tags as $tag) {
e8964639 104
105 $currenttag++;
106
107 if ($currenttag == 1) {
108 $lasttagct = $tag->ct;
109 $size = 20;
110 } else if ($tag->ct != $lasttagct) {
111 $lasttagct = $tag->ct;
112 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
113 }
114
7d61cf65 115 $tag->class = "$tag->type s$size";
116 $etags[] = $tag;
f3ea6dfa 117
7d61cf65 118 }
119
e8964639 120 /// Now we sort the tag display order
b51434e7 121 $CFG->tagsort = $this->config->sort;
7d61cf65 122 usort($etags, "blog_tags_sort");
f3ea6dfa 123
e8964639 124 /// Finally we create the output
7d61cf65 125 foreach ($etags as $tag) {
6279c33f 126 switch ($CFG->bloglevel) {
127 case BLOG_USER_LEVEL:
128 $filtertype = 'user';
129 $filterselect = $USER->id;
130 break;
131
132 case BLOG_GROUP_LEVEL:
133 $filtertype = 'group';
134 $filterselect = get_current_group($this->instance->pageid);
135 break;
136
137 case BLOG_COURSE_LEVEL:
138 $filtertype = 'course';
25fb38d4 139 if (isset($COURSE->id)) {
140 $filterselect = $COURSE->id;
dd0863dd 141 } else {
142 $filterselect = $this->instance->pageid;
143 }
6279c33f 144 break;
145
146 default:
25fb38d4 147 if (isset($COURSE->id) && $COURSE->id != SITEID) {
148 $filtertype = 'course';
149 $filterselect = $COURSE->id;
150 } else {
151 $filtertype = 'site';
152 $filterselect = SITEID;
153 }
6279c33f 154 break;
155 }
156
157 $link = $CFG->wwwroot.'/blog/index.php?filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;tagid='.$tag->id;
b51434e7 158 $this->content->text .= '<a href="'.$link.'" '.
159 'class="'.$tag->class.'" '.
160 'title="'.get_string('numberofentries','blog',$tag->ct).'">'.
161 $tag->text.'</a> ';
f3ea6dfa 162 }
f3ea6dfa 163
7d61cf65 164 }
f3ea6dfa 165 return $this->content;
166 }
167
b51434e7 168 function instance_config_print() {
169 global $CFG;
170
171 /// set up the numberoftags select field
172 $numberoftags = array();
173 for($i=1;$i<=50;$i++) $numberoftags[$i] = $i;
174
175 //// set up the timewithin select field
176 $timewithin = array();
177 $timewithin[10] = get_string('numdays', '', 10);
178 $timewithin[30] = get_string('numdays', '', 30);
179 $timewithin[60] = get_string('numdays', '', 60);
180 $timewithin[90] = get_string('numdays', '', 90);
181 $timewithin[120] = get_string('numdays', '', 120);
182 $timewithin[240] = get_string('numdays', '', 240);
183 $timewithin[365] = get_string('numdays', '', 365);
184
185 /// set up sort select field
186 $sort = array();
187 $sort['text'] = get_string('tagtext', 'blog');
188 $sort['id'] = get_string('tagdatelastused', 'blog');
189
190
191 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
192 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
193 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
194 print_simple_box_end();
195 } else {
196 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
197 }
198
f3ea6dfa 199 }
7d61cf65 200
201}
202
203function blog_tags_sort($a, $b) {
b51434e7 204 global $CFG;
205
206 if (empty($CFG->tagsort)) {
207 return 0;
208 } else {
209 $tagsort = $CFG->tagsort;
210 }
211
212 if (is_numeric($a->$tagsort)) {
213 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
214 } elseif (is_string($a->$tagsort)) {
215 return strcmp($a->$tagsort, $b->$tagsort);
216 } else {
217 return 0;
218 }
f3ea6dfa 219}
7d61cf65 220
f3ea6dfa 221?>