This repository was archived by the owner on Jan 23, 2025. It is now read-only.
forked from telabotanica/moodle-block_usersmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
293 lines (268 loc) · 12.8 KB
/
lib.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?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/>.
/**
* A Moodle block that shows a map of users
*
* English and french versions included / versions anglaise et française incluses.
*
* @package block_usersmap
* @category blocks
* @copyright 2016 Mathias Chouet, Tela Botanica
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Generates the block contents : map and / or text
*/
function usersmap_generate_content($config) {
global $DB;
global $CFG;
global $COURSE;
global $PAGE;
$PAGE->requires->css('/blocks/usersmap/css/leaflet.css');
$PAGE->requires->css('/blocks/usersmap/css/MarkerCluster.css');
$PAGE->requires->css('/blocks/usersmap/css/MarkerCluster.Default.css');
$PAGE->requires->css('/blocks/usersmap/css/Control.FullScreen.css');
$PAGE->requires->css('/blocks/usersmap/css/usersmap.css');
$PAGE->requires->js('/blocks/usersmap/js/leaflet.js', true);
$PAGE->requires->js('/blocks/usersmap/js/leaflet.markercluster.js', true);
$PAGE->requires->js('/blocks/usersmap/js/Control.FullScreen.js', true);
$content = '';
// Leaflet Map.
$content .= '<div id="usersmap-map" class="">';
$content .= '</div>';
// Inline Javascript code because declaration order is important.
$jsinitmapcode = '';
$jsinitmapcode .= '<script type="text/javascript">' . PHP_EOL;
// Build base layer based on settings.
$tileservermode = get_config('usersmap', 'Tileserver');
switch ($tileservermode) {
case 'osm':
$jsinitmapcode .= "baseLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',"
. "{maxZoom: 20, attribution: 'Map data © <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors'});";
break;
case 'gstreets':
$jsinitmapcode .= "baseLayer = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',"
. "{maxZoom: 20, subdomains:['mt0','mt1','mt2','mt3']});";
break;
case 'gsatellite':
$jsinitmapcode .= "baseLayer = L.tileLayer('http://{s}.google.com/vt/lyrs=s,h&x={x}&y={y}&z={z}',"
. "{maxZoom: 20, subdomains:['mt0','mt1','mt2','mt3']});";
break;
case 'custom':
default:
$layerurl = get_config('usersmap', 'Tileserver_Url_Scheme');
if (empty($layerurl)) {
throw new Exception(get_string('error_tilserver_url_not_set', 'block_usersmap'));
}
$layermaxzoom = get_config('usersmap', 'Tileserver_Max_Zoom');
if (empty($layermaxzoom)) {
$layermaxzoom = 20;
}
$layerattribution = get_config('usersmap', 'Tileserver_Attribution');
$jsinitmapcode .= "baseLayer = L.tileLayer('$layerurl',"
. "{maxZoom: $layermaxzoom, attribution: '$layerattribution'});";
break;
}
$jsinitmapcode .= "var usersmap = L.map('usersmap-map', {scrollWheelZoom: false, fullscreenControl: true,
fullscreenControlOptions: {position: 'topleft'}});" . PHP_EOL;
$jsinitmapcode .= "baseLayer.addTo(usersmap);" . PHP_EOL;
// Clustering layer.
$jsinitmapcode .= "usersLayer = new L.MarkerClusterGroup({disableClusteringAtZoom : 10});";
$jsinitmapcode .= "usersLayer.addTo(usersmap);" . PHP_EOL;
// Change default icon because generated path is not understood by Moodle.
$markerurl = new moodle_url('/blocks/usersmap/js/images/marker-icon.png');
$jsinitmapcode .= "var newDefaultMarkerIcon = L.icon({iconUrl: '$markerurl', iconSize: [24,36], iconAnchor: [12,36]});";
$jsinitmapcode .= "L.Marker.mergeOptions({icon: newDefaultMarkerIcon});";
$jsinitmapcode .= '</script>' . PHP_EOL;
// Append inline JS to content (this is a useless comment).
$content .= $jsinitmapcode;
// Get all available users locations.
// @TODO Load GeoJSON directly from the database ? Which performance ?
// Select users according to the instance config.
$whattodisplay = 'aeu';
if (isset($config->whattodisplay)) {
$whattodisplay = $config->whattodisplay;
}
$onlineusersonly = in_array($whattodisplay, array('omu', 'oeu'));
$enrolledusersonly = in_array($whattodisplay, array('aeu', 'oeu'));
$r0 = "SELECT bu.id as id, bu.lat as lat, bu.lon as lon, bu.city as city, count(*) as nb "
. "FROM " . $CFG->prefix . "block_usersmap bu ";
$join = " LEFT JOIN user u ON u.id = bu.userid";
$where = array("bu.lat IS NOT NULL", "bu.lon IS NOT NULL", "u.deleted = 0", "u.suspended = 0", "u.confirmed = 1");
if ($onlineusersonly) {
$where[] = "UNIX_TIMESTAMP() - u.lastaccess < 900"; // 15 mn.
}
if ($enrolledusersonly && ($COURSE->id > 1)) { // Course n°1 is platform home.
$join .= " LEFT JOIN user_enrolments ue ON ue.userid = bu.userid LEFT JOIN enrol e ON e.id = ue.enrolid";
$where[] = "e.courseid = " . $COURSE->id;
}
$r0 .= $join
. " WHERE " . implode(' AND ', $where)
. " GROUP BY bu.lat, bu.lon"; // City should always be the same for a given lat,lon pair.
$res = $DB->get_records_sql($r0, array());
$jsmarkerscode = '<script type="text/javascript">' . PHP_EOL;
if ($res) {
// Generate JS code for markers.
$markerid = 1;
foreach ($res as $r) {
// Add multiple instance of the marker so that the clustering
// plugin shows the right number of users in its icons.
$realnbusers = $r->nb;
for ($i = 0; $i < $realnbusers; $i++) {
$jsmarkerscode .= 'var marker_' . $markerid . ' = L.marker([' . $r->lat . ', ' . $r->lon . ']);' . PHP_EOL;
$jsmarkerscode .= 'usersLayer.addLayer(marker_' . $markerid . ');' . PHP_EOL;
$jsmarkerscode .= 'marker_' . $markerid . '.bindPopup("' . $r->city . ' : ' . $r->nb . '").openPopup();';
$markerid++;
}
}
$jsmarkerscode .= 'usersmap.fitBounds(usersLayer.getBounds());' . PHP_EOL;
$jsmarkerscode .= 'usersmap.setZoom(1);' . PHP_EOL;
} else {
$jsmarkerscode .= "usersmap.setView({lat:43.614203, lng:3.860752}, 1);" . PHP_EOL; // Center on Montpellier, best city in the world.
}
$jsmarkerscode .= '</script>' . PHP_EOL;
$content .= $jsmarkerscode;
// Count all active users in Moodle.
$displaynbmoodleusers = false;
if (isset($config->displaynbmoodleusers)) {
$displaynbmoodleusers = $config->displaynbmoodleusers;
}
if ($displaynbmoodleusers) {
$r1 = "SELECT count(*) as nb FROM " . $CFG->prefix . "user WHERE confirmed = 1 AND deleted = 0 AND suspended = 0";
$res = $DB->get_records_sql($r1, array());
$singleresult = array_shift($res);
$nbusers = $singleresult->nb;
$stringformat = get_string('config_display_nb_moodle_users_format_default', 'block_usersmap');
if (isset($config->displaynbmoodleusers_format)) {
$stringformat = $config->displaynbmoodleusers_format;
}
$message = str_replace('{nb}', $nbusers, $stringformat);
$content .= "<p>$message</p>";
}
// Count all users enrolled in the current course.
$displaynbenrolledusers = false;
if (isset($config->displaynbenrolledusers)) {
$displaynbenrolledusers = $config->displaynbenrolledusers;
}
if ($displaynbenrolledusers) {
if ($COURSE->id != 1) { // Course n°1 is platform home.
$r2 = "SELECT count(DISTINCT userid) as nb "
. "FROM " . $CFG->prefix . "user_enrolments ue "
. "LEFT JOIN " . $CFG->prefix . "enrol e ON e.id = ue.enrolid "
. "WHERE e.courseid = " . $COURSE->id;
$res = $DB->get_records_sql($r2, array());
$singleresult = array_shift($res);
$nbenrolledusers = $singleresult->nb;
$stringformat = get_string('config_display_nb_enrolled_users_format_default', 'block_usersmap');
if (isset($config->displaynbenrolledusers_format)) {
$stringformat = $config->displaynbenrolledusers_format;
}
$message = str_replace('{nb}', $nbenrolledusers, $stringformat);
$content .= "<p>$message</p>";
}
}
return $content;
}
/**
* Called by the cron (scheduled task) to retrieve geolocation data based on
* users cities
*/
function usersmap_update_geolocation($updateeveryone=false) {
global $DB;
global $CFG;
if ($updateeveryone) { // Clear data once in a while.
$DB->delete_records("block_usersmap");
}
// Query all users having a city set in their profile.
$q = "SELECT id, city, country FROM " . $CFG->prefix . "user WHERE city != '' AND deleted = 0";
if (! $updateeveryone) { // Only update users having no geolocation yet.
$q .= " AND id NOT IN (SELECT userid FROM " . $CFG->prefix . "block_usersmap)";
}
$q .= " LIMIT 100"; // 100 at a time to spare the geolocation server's life.
$res = $DB->get_records_sql($q, array());
// Setup the geolocation service befor the loop.
$geolocationservice = get_config('usersmap', 'Geolocation_Server');
$baseurl = '';
$latfield = 'lat';
$lonfield = 'lon';
switch($geolocationservice) {
case 'geonames':
$baseurl = "http://api.geonames.org/searchJSON?maxRows=1";
$geonamesusername = get_config('usersmap', 'Geonames_Username');
if (empty($geonamesusername)) {
echo "ERROR Geolocation_Server is set to 'Geonames' but Geonames_Username is not set";
return false;
}
$baseurl .= "&username=$geonamesusername";
break;
case 'custom':
default:
$baseurl = get_config('usersmap', 'Geolocation_Url_Scheme');
if (empty($baseurl)) {
echo "ERROR Geolocation_Server is set to 'custom' but Geolocation_Url_Scheme is not set";
return false;
}
if (get_config('usersmap', 'Geolocation_Lat_Field') != '') {
$latfield = get_config('usersmap', 'Geolocation_Lat_Field');
}
if (get_config('usersmap', 'Geolocation_Lon_Field') != '') {
$lonfield = get_config('usersmap', 'Geolocation_Lon_Field');
}
break;
}
// Get geolocation data for each user.
if ($res) {
foreach ($res as $r) {
$newrecord = new StdClass();
$newrecord->userid = $r->id;
$newrecord->city = $r->city;
switch($geolocationservice) {
case 'geonames':
$url = $baseurl;
$url .= "&q=" . urlencode(trim($r->city));
$url .= "&country=" . urlencode(trim($r->country));
$info = file_get_contents($url);
if ($info) {
$info = json_decode($info, true);
if (isset($info['geonames']) && isset($info['geonames'][0])) {
$newrecord->lat = $info['geonames'][0]['lat'];
$newrecord->lon = $info['geonames'][0]['lng'];
}
}
break;
case 'custom':
default:
$url = str_replace('{city}', urlencode(trim($r->city)), $baseurl);
$url = str_replace('{country}', urlencode(trim($r->country)), $url);
$info = file_get_contents($url);
if ($info) {
$info = json_decode($info, true);
$newrecord->lat = $info[$latfield];
$newrecord->lon = $info[$lonfield];
}
break;
}
// Inserting one at a time because of quotes issue.
$DB->insert_record("block_usersmap", $newrecord);
/* Locations not found are also inserted. To get the percentage of
* geolocation, run this query :
* SELECT a.nb, b.nb, a.nb/b.nb as pourcentage FROM
* (SELECT count(*) as nb FROM block_usersmap WHERE lat IS NOT NULL AND lon IS NOT NULL) as a,
* (SELECT count(*) as nb FROM block_usersmap) as b;
*/
}
}
}