Skip to content

Commit a9a2a04

Browse files
committed
added home_location field to user/user(s) methods; closes #369
1 parent fc8e0e7 commit a9a2a04

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

okapi/services/users/user.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<li><b>caches_notfound</b> - number of "Didn't find it" log entries,</li>
3939
<li><b>caches_hidden</b> - number of caches owned,</li>
4040
<li><b>rcmds_given</b> - number of recommendations given.</li>
41+
<li><p><b>home_location</b> - home location of the user in the "lat|lon" format
42+
(lat and lon are in full degrees with a dot as a decimal point);
43+
<b>null</b> if no home location is given in the user's Opencaching profile.</p>
44+
<p>This value can be accessed only with <b>Level 3</b> Authentication
45+
and only for the user of your Access Token. For all other reads,
46+
home_location will equal <b>null</b>.</p></li>
4147
</ul>
4248

4349
<p>If given user does not exist, the method will respond with an HTTP 400 error.</p>

okapi/services/users/users.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function options()
2121
}
2222

2323
private static $valid_field_names = array('uuid', 'username', 'profile_url', 'internal_id', 'is_admin',
24-
'caches_found', 'caches_notfound', 'caches_hidden', 'rcmds_given');
24+
'caches_found', 'caches_notfound', 'caches_hidden', 'rcmds_given', 'home_location');
2525

2626
public static function call(OkapiRequest $request)
2727
{
@@ -39,7 +39,7 @@ public static function call(OkapiRequest $request)
3939
if (!in_array($field, self::$valid_field_names))
4040
throw new InvalidParam('fields', "'$field' is not a valid field code.");
4141
$rs = Db::query("
42-
select user_id, uuid, username, admin
42+
select user_id, uuid, username, admin, latitude, longitude
4343
from user
4444
where uuid in ('".implode("','", array_map('mysql_real_escape_string', $user_uuids))."')
4545
");
@@ -72,6 +72,20 @@ public static function call(OkapiRequest $request)
7272
case 'caches_notfound': /* handled separately */ break;
7373
case 'caches_hidden': /* handled separately */ break;
7474
case 'rcmds_given': /* handled separately */ break;
75+
case 'home_location':
76+
if (!$request->token) {
77+
$entry['home_location'] = null;
78+
} elseif ($request->token->user_id != $row['user_id']) {
79+
$entry['home_location'] = null;
80+
} elseif (!$row['latitude'] && !$row['longitude']) {
81+
# OCPL sets NULL/NULL for unknown location, OCDE sets 0/0.
82+
# It is safe to return null also for OCPL 0/0, as this value
83+
# does not make sense.
84+
$entry['home_location'] = null;
85+
} else {
86+
$entry['home_location'] = round($row['latitude'], 6)."|".round($row['longitude'], 6);
87+
}
88+
break;
7589
default: throw new Exception("Missing field case: ".$field);
7690
}
7791
}

0 commit comments

Comments
 (0)