Due to changes in Quiz which allows commenting and manual grading of all question...
[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
f3ea6dfa 7class block_blog_tags extends block_base {
8 function init() {
f3ea6dfa 9 $this->version = 2006032000;
b51434e7 10 $this->title = get_string('blogtags', 'blog');
f3ea6dfa 11 }
12
7d61cf65 13 function instance_allow_multiple() {
14 return true;
15 }
16
17 function has_config() {
a2d09dee 18 return false;
7d61cf65 19 }
20
b51434e7 21 function applicable_formats() {
22 return array('all' => true, 'my' => false);
23 }
24
7d61cf65 25 function instance_allow_config() {
26 return true;
27 }
28
b51434e7 29 function specialization() {
30
31 // load userdefined title and make sure it's never empty
32 if (empty($this->config->title)) {
33 $this->title = get_string('blocktagstitle','blog');
34 } else {
35 $this->title = $this->config->title;
36 }
37 }
38
39
f3ea6dfa 40 function get_content() {
41
42 global $CFG;
43
b51434e7 44 if (empty($this->config->timewithin)) {
45 $this->config->timewithin = BLOGDEFAULTTIMEWITHIN;
46 }
47 if (empty($this->config->numberoftags)) {
48 $this->config->numberoftags = BLOGDEFAULTNUMBEROFTAGS;
49 }
50 if (empty($this->config->sort)) {
51 $this->config->sort = BLOGDEFAULTSORT;
52 }
f3ea6dfa 53
54 if ($this->content !== NULL) {
55 return $this->content;
56 }
57
58 if (empty($this->instance)) {
59 $this->content = '';
60 return $this->content;
61 }
62
63 $this->content = new stdClass;
64 $this->content->text = '';
65 $this->content->footer = '';
66
67
68 /// Get a list of tags
b51434e7 69
70 $timewithin = $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
f3ea6dfa 71
72 $sql = 'SELECT t.*, COUNT(DISTINCT bt.id) as ct ';
56c59187 73 $sql .= "FROM {$CFG->prefix}tags as t, {$CFG->prefix}blog_tag_instance as bt, {$CFG->prefix}post as p ";
f3ea6dfa 74 $sql .= 'WHERE t.id = bt.tagid ';
56c59187 75 $sql .= 'AND p.id = bt.entryid ';
76 $sql .= 'AND (p.publishstate = "site" or p.publishstate="public") ';
b51434e7 77 $sql .= "AND bt.timemodified > {$timewithin} ";
f3ea6dfa 78 $sql .= 'GROUP BY bt.tagid ';
79 $sql .= 'ORDER BY ct DESC, t.text ASC ';
b51434e7 80 $sql .= "LIMIT {$this->config->numberoftags} ";
f3ea6dfa 81
7d61cf65 82 if ($tags = get_records_sql($sql)) {
83
e8964639 84 /// There are 2 things to do:
85 /// 1. tags with the same count should have the same size class
86 /// 2. however many tags we have should be spread evenly over the
87 /// 20 size classes
88
89 $totaltags = count($tags);
90 $currenttag = 0;
91
92 $size = 20;
93 $lasttagct = -1;
94
7d61cf65 95 $etags = array();
96 foreach ($tags as $tag) {
e8964639 97
98 $currenttag++;
99
100 if ($currenttag == 1) {
101 $lasttagct = $tag->ct;
102 $size = 20;
103 } else if ($tag->ct != $lasttagct) {
104 $lasttagct = $tag->ct;
105 $size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
106 }
107
7d61cf65 108 $tag->class = "$tag->type s$size";
109 $etags[] = $tag;
f3ea6dfa 110
7d61cf65 111 }
112
e8964639 113 /// Now we sort the tag display order
b51434e7 114 $CFG->tagsort = $this->config->sort;
7d61cf65 115 usort($etags, "blog_tags_sort");
f3ea6dfa 116
e8964639 117 /// Finally we create the output
7d61cf65 118 foreach ($etags as $tag) {
e8964639 119 $link = $CFG->wwwroot.'/blog/index.php?courseid='.
315e4e83 120 $this->instance->pageid.'&amp;filtertype=site&amp;tagid='.$tag->id;
b51434e7 121 $this->content->text .= '<a href="'.$link.'" '.
122 'class="'.$tag->class.'" '.
123 'title="'.get_string('numberofentries','blog',$tag->ct).'">'.
124 $tag->text.'</a> ';
f3ea6dfa 125 }
f3ea6dfa 126
7d61cf65 127 }
f3ea6dfa 128 return $this->content;
129 }
130
b51434e7 131 function instance_config_print() {
132 global $CFG;
133
134 /// set up the numberoftags select field
135 $numberoftags = array();
136 for($i=1;$i<=50;$i++) $numberoftags[$i] = $i;
137
138 //// set up the timewithin select field
139 $timewithin = array();
140 $timewithin[10] = get_string('numdays', '', 10);
141 $timewithin[30] = get_string('numdays', '', 30);
142 $timewithin[60] = get_string('numdays', '', 60);
143 $timewithin[90] = get_string('numdays', '', 90);
144 $timewithin[120] = get_string('numdays', '', 120);
145 $timewithin[240] = get_string('numdays', '', 240);
146 $timewithin[365] = get_string('numdays', '', 365);
147
148 /// set up sort select field
149 $sort = array();
150 $sort['text'] = get_string('tagtext', 'blog');
151 $sort['id'] = get_string('tagdatelastused', 'blog');
152
153
154 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
155 print_simple_box_start('center', '', '', 5, 'blockconfigglobal');
156 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
157 print_simple_box_end();
158 } else {
159 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
160 }
161
f3ea6dfa 162 }
7d61cf65 163
164}
165
166function blog_tags_sort($a, $b) {
b51434e7 167 global $CFG;
168
169 if (empty($CFG->tagsort)) {
170 return 0;
171 } else {
172 $tagsort = $CFG->tagsort;
173 }
174
175 if (is_numeric($a->$tagsort)) {
176 return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
177 } elseif (is_string($a->$tagsort)) {
178 return strcmp($a->$tagsort, $b->$tagsort);
179 } else {
180 return 0;
181 }
f3ea6dfa 182}
7d61cf65 183
f3ea6dfa 184?>