Added option to see all users on map

Added an option to see last position of each user at the same time.
This commit is contained in:
Jon Uriarte Bilbao 2019-02-02 19:45:43 +01:00 committed by Bartek Fabiszewski
parent 33a9e30625
commit 25c1b24c49
5 changed files with 141 additions and 8 deletions

View File

@ -202,6 +202,57 @@
return $position; return $position;
} }
/**
* Get last position data from database
* (for all users)
*
* @return array|bool Array of uPosition positions, false on error
*/
public static function getLastAllUsers() {
$query = "SELECT
p.id,
UNIX_TIMESTAMP(p.time) AS tstamp,
p.user_id,
p.track_id,
p.latitude,
p.longitude,
p.altitude,
p.speed,
p.bearing,
p.accuracy,
p.provider,
p.comment,
p.image_id,
u.login
FROM
" . self::db()->table('positions') . " p
LEFT JOIN " . self::db()->table('users') . " u
ON ( p.user_id = u.id )
WHERE p.id = (
SELECT
p2.id
FROM
" . self::db()->table('positions') . " p2
WHERE
p2.user_id = p.user_id
ORDER BY
p2.time DESC,
p2.id DESC
LIMIT 1
)";
$result = self::db()->query($query);
if ($result === false) {
return false;
}
$positionsArr = [];
while ($row = $result->fetch_assoc()) {
$positionsArr[] = self::rowToObject($row);
}
$result->close();
return $positionsArr;
}
/** /**
* Get array of all positions * Get array of all positions
* *
@ -329,4 +380,4 @@
} }
} }
?> ?>

View File

@ -150,7 +150,7 @@
<input id="latest" type="checkbox" onchange="toggleLatest();"> <?= $lang["latest"] ?><br> <input id="latest" type="checkbox" onchange="toggleLatest();"> <?= $lang["latest"] ?><br>
<input type="checkbox" onchange="autoReload();"> <?= $lang["autoreload"] ?> (<a href="javascript:void(0);" onclick="setTime();"><span id="auto"><?= uConfig::$interval ?></span></a> s)<br> <input type="checkbox" onchange="autoReload();"> <?= $lang["autoreload"] ?> (<a href="javascript:void(0);" onclick="setTime();"><span id="auto"><?= uConfig::$interval ?></span></a> s)<br>
</form> </form>
<a href="javascript:void(0);" onclick="loadTrack(userid, trackid, 0);"> <?= $lang["reload"] ?></a><br> <a href="javascript:void(0);" onclick="reload(userid, trackid);"> <?= $lang["reload"] ?></a><br>
</div> </div>
<div id="summary"></div> <div id="summary"></div>

View File

@ -134,6 +134,16 @@ function getXHR() {
return xmlhttp; return xmlhttp;
} }
function reload(userid, trackid){
var usersSelect = document.getElementsByName('user')[0];
if (usersSelect[usersSelect.selectedIndex].text == lang['allusers']) {
loadLastPositionAllUsers();
}
else{
loadTrack(userid, trackid, 0);
}
}
function loadTrack(userid, trackid, update) { function loadTrack(userid, trackid, update) {
var title = document.getElementById('track').getElementsByClassName('menutitle')[0]; var title = document.getElementById('track').getElementsByClassName('menutitle')[0];
if (trackid < 0) { return; } if (trackid < 0) { return; }
@ -154,11 +164,34 @@ function loadTrack(userid, trackid, update) {
removeLoader(title); removeLoader(title);
} }
} }
xhr.open('GET', 'utils/getpositions.php?trackid=' + trackid + '&userid=' + userid, true); xhr.open('GET', 'utils/getpositions.php?trackid=' + trackid + '&userid=' + userid + '&last=' + latest, true);
xhr.send(); xhr.send();
setLoader(title); setLoader(title);
} }
function loadLastPositionAllUsers() {
if (latest == 1) { trackid = 0; }
var xhr = getXHR();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
clearMap();
var xml = xhr.responseXML;
var positions = xml.getElementsByTagName('position');
var posLen = positions.length;
for (var i = 0; i < posLen; i++) {
var p = parsePosition(positions[i], i);
// set marker
setMarker(p, i, posLen);
}
}
xhr = null;
}
}
xhr.open('GET', 'utils/getpositions.php?trackid=' + trackid + '&userid=' + userid + '&last=' + latest, true);
xhr.send();
}
function parsePosition(p, id) { function parsePosition(p, id) {
// read data // read data
var latitude = parseFloat(getNode(p, 'latitude')); var latitude = parseFloat(getNode(p, 'latitude'));
@ -231,6 +264,15 @@ function getPopupHtml(p, i, count) {
'<img class="icon" alt="' + lang['tdistance'] + '" title="' + lang['tdistance'] + '" src="images/distance_blue.svg"> ' + '<img class="icon" alt="' + lang['tdistance'] + '" title="' + lang['tdistance'] + '" src="images/distance_blue.svg"> ' +
(p.totalMeters.toKm() * factor_km).toFixed(2) + ' ' + unit_km + '<br>' + '</div>'; (p.totalMeters.toKm() * factor_km).toFixed(2) + ' ' + unit_km + '<br>' + '</div>';
} }
if (p.username == null){
p.username = lang["nousername"];
}
if (p.trackname == null){
p.trackname = lang["notrackname"];
}
if (p.comments == null){
p.comments = lang["nocomment"];
}
var popup = var popup =
'<div id="popup">' + '<div id="popup">' +
'<div id="pheader">' + '<div id="pheader">' +
@ -367,13 +409,25 @@ Number.prototype.toKmH = function() {
// negate value // negate value
function toggleLatest() { function toggleLatest() {
var usersSelect = document.getElementsByName('user')[0];
if (latest == 0) { if (latest == 0) {
if (usersSelect.options[usersSelect.length-1].text != lang['allusers']){
var option = document.createElement("option");
option.text = lang['allusers'];
if (usersSelect.length >= 2){
usersSelect.add(option);
}
}
latest = 1; latest = 1;
loadTrack(userid, 0, 1); loadTrack(userid, 0, 1);
} }
else { else {
if (usersSelect.options[usersSelect.length-1].text == lang['allusers']){
usersSelect.remove(usersSelect.length-1);
}
latest = 0; latest = 0;
loadTrack(userid, trackid, 1); loadTrack(userid, trackid, 1);
} }
} }
@ -388,15 +442,26 @@ function selectTrack(f) {
trackid = 0; trackid = 0;
} }
document.getElementById('latest').checked = false; document.getElementById('latest').checked = false;
var usersSelect = document.getElementsByName('user')[0];
if (latest == 1) { toggleLatest(); } if (latest == 1) { toggleLatest(); }
loadTrack(userid, trackid, 1); loadTrack(userid, trackid, 1);
} }
function selectUser(f) { function selectUser(f) {
userid = f.options[f.selectedIndex].value; userid = f.options[f.selectedIndex].value;
document.getElementById('latest').checked = false;
if (latest == 1) { toggleLatest(); } if (f.options[f.selectedIndex].text == lang['allusers']){
getTracks(userid); var trackSelect = document.getElementsByName('track')[0];
var length = trackSelect.options.length;
for (i = 0; i < length; i++) {
trackSelect.options[i] = null;
}
loadLastPositionAllUsers();
}
else{
document.getElementById('latest').checked = false;
getTracks(userid);
}
} }
function getTracks(userid, trackid) { function getTracks(userid, trackid) {
@ -452,7 +517,13 @@ function clearOptions(el) {
function autoReload() { function autoReload() {
if (live == 0) { if (live == 0) {
live = 1; live = 1;
auto = setInterval(function () { loadTrack(userid, trackid, 0); }, interval * 1000); var usersSelect = document.getElementsByName('user')[0];
if (usersSelect[usersSelect.selectedIndex].text == lang['allusers']) {
auto = setInterval(function () { loadLastPositionAllUsers(); }, interval * 1000);
}
else{
auto = setInterval(function () { loadTrack(userid, trackid, 0); }, interval * 1000);
}
} }
else { else {
live = 0; live = 0;

View File

@ -121,4 +121,9 @@ $lang["iparsefailure"] = "Parsing failed";
$lang["idatafailure"] = "No track data in imported file"; $lang["idatafailure"] = "No track data in imported file";
$lang["isizefailure"] = "The uploaded file size should not exceed %d bytes"; // substitutes number of bytes $lang["isizefailure"] = "The uploaded file size should not exceed %d bytes"; // substitutes number of bytes
$lang["imultiple"] = "Notice, multiple tracks imported (%d)"; // substitutes number of imported tracks $lang["imultiple"] = "Notice, multiple tracks imported (%d)"; // substitutes number of imported tracks
$lang["allusers"] = "All Users";
$lang["notrackname"] = "No Track";
$lang["nousername"] = "No User";
$lang["nocomment"] = "No Comment";
?> ?>

View File

@ -25,6 +25,7 @@ $auth = new uAuth();
$userId = uUtils::getInt('userid'); $userId = uUtils::getInt('userid');
$trackId = uUtils::getInt('trackid'); $trackId = uUtils::getInt('trackid');
$last = uUtils::getInt('last');
$positionsArr = []; $positionsArr = [];
if ($userId) { if ($userId) {
@ -42,6 +43,11 @@ if ($userId) {
} }
} }
} }
else{
if ($last) {
$positionsArr = uPosition::getLastAllUsers();
}
}
header("Content-type: text/xml"); header("Content-type: text/xml");
$xml = new XMLWriter(); $xml = new XMLWriter();
@ -78,4 +84,4 @@ $xml->endElement();
$xml->endDocument(); $xml->endDocument();
$xml->flush(); $xml->flush();
?> ?>