-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_databasetags.php
212 lines (174 loc) · 6.59 KB
/
block_databasetags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package block_databasetags
* @subpackage tag
* @copyright 2015 onwards Andrew Hancox (andrewdchancox@googlemail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_databasetags extends block_base {
public function init() {
$this->title = get_string('pluginname', 'block_databasetags');
}
public function instance_allow_multiple() {
return true;
}
public function has_config() {
return false;
}
public function applicable_formats() {
return array('all' => true);
}
public function instance_allow_config() {
return true;
}
public function specialization() {
// Load userdefined title and make sure it's never empty.
if (empty($this->config->title)) {
$this->title = get_string('pluginname', 'block_databasetags');
} else {
$this->title = $this->config->title;
}
}
public function get_content() {
if (!isset($this->config)) {
$this->config = new stdClass();
}
if (empty($this->config->numberofdatabasetags)) {
$this->config->numberofdatabasetags = 80;
}
if ($this->content !== null) {
return $this->content;
}
if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
$fieldids = $this->get_fieldids();
$tags = $this->get_tags($fieldids);
$this->content->text = $this->tag_print_cloud($tags, $this->config->numberofdatabasetags);
return $this->content;
}
private function get_fieldids() {
$config = (array)$this->config;
$fieldids = array();
foreach (array_keys($config) as $setting) {
if (strpos($setting, 'field_') !== false) {
$fieldids[] = substr($setting, 6);
}
}
return $fieldids;
}
private static function can_access_course($courseid) {
global $DB;
static $cache = array();
if (!array_key_exists($courseid, $cache)) {
$course = $DB->get_record('course', array('id' => $courseid));
$cache[$courseid] = can_access_course($course);
}
return $cache[$courseid];
}
public static function get_tags($fieldids) {
global $DB;
if (empty($fieldids)) {
return array();
}
list($insql, $params) = $DB->get_in_or_equal($fieldids);
$sql = "
SELECT dc.id, dc.content, df.name as fieldname, df.type as fieldtype, df.id as fieldid, df.dataid, d.course as courseid
FROM {data_content} dc
INNER JOIN {data_fields} df ON dc.fieldid = df.id
INNER JOIN {data} d ON d.id = df.dataid
WHERE dc.fieldid $insql
";
$rawtags = $DB->get_records_sql($sql, $params);
$splittags = array();
foreach ($rawtags as $rawtag) {
if (!self::can_access_course($rawtag->courseid)) {
continue;
}
if ($rawtag->fieldtype == 'tag') {
$seperator = ',';
} else if ($rawtag->fieldtype == 'checkbox' || $rawtag->fieldtype == 'linkedcheckbox' || $rawtag->fieldtype == 'multimenu') {
$seperator = '##';
}
if ($rawtag->fieldtype == 'linkedradiobutton' || $rawtag->fieldtype == 'menu') {
$tagsincontent = array($rawtag->content);
} else if (!empty($seperator)) {
$tagsincontent = explode($seperator, $rawtag->content);
} else {
$tagsincontent = [];
}
$keyablefieldname = str_replace(' ', '_', $rawtag->fieldname);
foreach ($tagsincontent as $tagincontent) {
$tagincontent = trim($tagincontent);
if (empty($tagincontent)) {
continue;
}
$key = $keyablefieldname . '_' . $tagincontent;
if (!array_key_exists($key, $splittags)) {
$tag = new stdClass();
$tag->key = $key;
$tag->name = $tagincontent;
$tag->count = 1;
$tag->tagtype = $keyablefieldname;
$tag->fieldtype = $rawtag->fieldtype;
$tag->fieldid = $rawtag->fieldid;
$tag->dataid = $rawtag->dataid;
$splittags[$key] = $tag;
} else {
$splittags[$key]->count += 1;
}
}
}
return $splittags;
}
public static function tag_print_cloud($tagsincloud, $nroftags=150) {
$maxcount = 0;
foreach ($tagsincloud as $tag) {
if ($tag->count > $maxcount) {
$maxcount = $tag->count;
}
}
ksort($tagsincloud, SORT_NATURAL);
$tagsincloud = array_slice($tagsincloud, 0, $nroftags);
foreach ($tagsincloud as $tag) {
$size = (int) (( $tag->count / $maxcount) * 20);
$tag->class = "$tag->tagtype s$size";
}
$output = '';
$output .= "\n<ul class='tag_cloud inline-list'>\n";
foreach ($tagsincloud as $tag) {
$fieldparam = "f_{$tag->fieldid}";
if ($tag->fieldtype == 'checkbox' || $tag->fieldtype == 'linkedcheckbox') {
$fieldparam .= '[]';
}
$params = array(
$fieldparam => $tag->name,
'd' => $tag->dataid,
'advanced' => 1
);
$url = new moodle_url('/mod/data/view.php', $params);
$link = html_writer::link($url, $tag->name, array('class' => $tag->class));
$output .= '<li>' . $link . '</li> ';
}
$output .= "\n</ul>\n";
return $output;
}
}