-
Notifications
You must be signed in to change notification settings - Fork 9
/
gpx.php
163 lines (150 loc) · 5.66 KB
/
gpx.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
<?php
// This file is part of Treasurehunt for Moodle
//
// 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/>.
/**
* GPX tracks exporter
*
* @package mod_treasurehunt
* @copyright Juan Pablo de Castro <jpdecastro@tel.uva.es>
* @author Juan Pablo de Castro <jpdecastro@tel.uva.es>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
/** @var $DB database_manager Database.*/
global $DB;
global $USER;
$id = required_param('id', PARAM_INT);
$userid = required_param('userid', PARAM_SEQUENCE);
$userids = explode(',', $userid);
list ($course, $cm) = get_course_and_cm_from_cmid($id, 'treasurehunt');
$treasurehunt = $DB->get_record('treasurehunt', array('id' => $cm->instance), '*', MUST_EXIST);
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
if (count($userids) > 1 || $USER->id != $userid) {
require_capability('mod/treasurehunt:viewusershistoricalattempts', $context);
}
date_default_timezone_set("UTC");
foreach ($userids as $userid) {
$trackpoints = $DB->get_records('treasurehunt_track', ['userid' => $userid, 'treasurehuntid' => $treasurehunt->id]);
$description = "Track for user:" . fullname($DB->get_record('user', ['id' => $userid]));
$segment = makesegment($trackpoints);
$segments[] = $segment;
$tracks[] = maketrack($description, $segments);
}
$gpx = makegpx($tracks);
header('Content-type: application/gpx');
header('Content-Disposition: attachment; filename="treasure_track.gpx"');
echo $gpx;
die;
/**
*
* @param array $trackpoint from table treasurehunt_track
* @return \stdClass $segment
*/
function makesegment($trackpoints) {
$activity = new stdClass();
$activity->trackPoints = [];
foreach ($trackpoints as $trackpoint) {
$gpxtrackpoint = new stdClass();
$gpxtrackpoint->time = $trackpoint->timestamp;
$coords = explode(' ', substr($trackpoint->location, 6, -1));
$gpxtrackpoint->lon = $coords[0];
$gpxtrackpoint->lat = $coords[1];
$gpxtrackpoint->type = 'trackpoint';
$activity->trackPoints[] = $gpxtrackpoint;
}
$segment = new stdClass();
$segment->activities = [$activity];
$segment->type = 'tracks';
return $segment;
}
/**
*
* @param array(string) $tracks tracks xml sections
* @return string xml format for gpx
*/
function makegpx($tracks) {
$xml = <<<GPX
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="TreasureHuntTrackExporter"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<link href="https://github.com/juacas/moodle-mod_treasurehunt">
<text>Treasurehunt track</text>
</link>
</metadata>
GPX;
$xml .= "\n";
$xml .= join("\n", $tracks);
$xml .= "\n";
$xml .= '</gpx>';
return $xml;
}
/**
*
* @param string $description
* @param array(\stdClass) array of $segments
* @return string xmlsection for a track. From <trk> to </trk>
*/
function maketrack($description, $segments) {
$xml = '<trk>
<name>Treasurehunt trace</name>
<desc>' . $description . '</desc>';
foreach ($segments as $segment) {
$xml .= "\n";
$xml .= "<trkseg>";
if ($segment->type == 'place') {
$xml .= maketrackpoint($segment);
} else {
foreach ($segment->activities as $activity) {
if ($activity->trackPoints) {
foreach ($activity->trackPoints as $point) {
$xml .= "\n";
$xml .= maketrackpoint($point);
}
}
}
}
$xml .= "\n";
$xml .= "</trkseg>";
}
$xml .= '</trk>';
return $xml;
}
function getisotime($date) {
$dateobj = new DateTime();
$dateobj->setTimestamp($date);
return $dateobj->format('c');
}
function maketrackpoint(&$data) {
$return = '';
if ($data->type == 'place') {
$starttime = getisotime($data->startTime);
$endtime = getisotime($data->endTime);
$return .= "<trkpt lat=\"" . $data->place->location->lat . "\" lon=\"" . $data->place->location->lon
. "\"><time>$starttime</time><location>" . $data->place->name . "</location></trkpt>";
$return .= "<trkpt lat=\"" . $data->place->location->lat . "\" lon=\"" . $data->place->location->lon
. "\"><time>$endtime</time><location>" . $data->place->name . "</location></trkpt>";
} else {
$time = getisotime($data->time);
$return .= "<trkpt lat=\"$data->lat\" lon=\"$data->lon\"><time>$time</time>";
$return .= "</trkpt>";
}
return $return;
}