3 // A lot of this initial stuff is copied from mod/data/view.php
5 require_once('../../../../config.php');
6 require_once('../../lib.php');
8 // Optional params: row id "rid" - if set then export just one, otherwise export all
10 $d = required_param('d', PARAM_INT
); // database id
11 $fieldid = required_param('fieldid', PARAM_INT
); // field id
12 $rid = optional_param('rid', 0, PARAM_INT
); //record id
16 if (! $record = get_record('data_records', 'id', $rid)) {
17 error('Record ID is incorrect');
19 if (! $data = get_record('data', 'id', $record->dataid
)) {
20 error('Data ID is incorrect');
22 if (! $course = get_record('course', 'id', $data->course
)) {
23 error('Course is misconfigured');
25 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
26 error('Course Module ID was incorrect');
28 if (! $field = get_record('data_fields', 'id', $fieldid)) {
29 error('Field ID is incorrect');
31 if (! $field->type
== 'latlong') { // Make sure we're looking at a latlong data type!
32 error('Field ID is incorrect');
34 if (! $content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $rid)) {
35 error('Field content not found');
37 } else { // We must have $d
38 if (! $data = get_record('data', 'id', $d)) {
39 error('Data ID is incorrect');
41 if (! $course = get_record('course', 'id', $data->course
)) {
42 error('Course is misconfigured');
44 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
45 error('Course Module ID was incorrect');
47 if (! $field = get_record('data_fields', 'id', $fieldid)) {
48 error('Field ID is incorrect');
50 if (! $field->type
== 'latlong') { // Make sure we're looking at a latlong data type!
51 error('Field ID is incorrect');
56 require_course_login($course, true, $cm);
58 /// If it's hidden then it's don't show anything. :)
59 if (empty($cm->visible
) and !has_capability('moodle/course:viewhiddenactivities',get_context_instance(CONTEXT_MODULE
, $cm->id
))) {
61 $navlinks[] = array('name' => $strdatabases, 'link' => "index.php?id=$course->id", 'type' => 'activity');
62 $navlinks[] = array('name' => format_string($data->name
), 'link' => '', 'type' => 'activityinstance');
63 $navigation = build_navigation($navlinks);
65 print_header_simple(format_string($data->name
), "", $navigation,
66 "", "", true, '', navmenu($course, $cm));
67 notice(get_string("activityiscurrentlyhidden"));
70 /// If we have an empty Database then redirect because this page is useless without data
71 if (has_capability('mod/data:managetemplates', $context)) {
72 if (!record_exists('data_fields','dataid',$data->id
)) { // Brand new database!
73 redirect($CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
); // Redirect to field entry
80 //header('Content-type: text/plain'); // This is handy for debug purposes to look at the KML in the browser
81 header('Content-type: application/vnd.google-earth.kml+xml kml');
82 header('Content-Disposition: attachment; filename="moodleearth-'.$d.'-'.$rid.'-'.$fieldid.'.kml"');
85 echo data_latlong_kml_top();
87 if($rid) { // List one single item
88 $pm->name
= data_latlong_kml_get_item_name($content, $field);
89 $pm->description
= "<a href='$CFG->wwwroot/mod/data/view.php?d=$d&rid=$rid'>Item #$rid</a> in Moodle data activity";
90 $pm->long
= $content->content1
;
91 $pm->lat
= $content->content
;
92 echo data_latlong_kml_placemark($pm);
93 } else { // List all items in turn
95 $contents = get_records('data_content', 'fieldid', $fieldid);
99 foreach($contents as $content) {
100 $pm->name
= data_latlong_kml_get_item_name($content, $field);
101 $pm->description
= "<a href='$CFG->wwwroot/mod/data/view.php?d=$d&rid=$content->recordid'>Item #$content->recordid</a> in Moodle data activity";
102 $pm->long
= $content->content1
;
103 $pm->lat
= $content->content
;
104 echo data_latlong_kml_placemark($pm);
111 echo data_latlong_kml_bottom();
116 function data_latlong_kml_top() {
117 return '<?xml version="1.0" encoding="UTF-8"?>
118 <kml xmlns="http://earth.google.com/kml/2.0">
123 function data_latlong_kml_placemark($pm) {
125 <description>'.$pm->description
.'</description>
126 <name>'.$pm->name
.'</name>
128 <longitude>'.$pm->long
.'</longitude>
129 <latitude>'.$pm->lat
.'</latitude>
130 <range>30500.8880792294568</range>
131 <tilt>46.72425699662645</tilt>
132 <heading>0.0</heading>
134 <visibility>0</visibility>
137 <altitudeMode>relativeToGround</altitudeMode>
138 <coordinates>'.$pm->long
.','.$pm->lat
.',50</coordinates>
144 function data_latlong_kml_bottom() {
148 function data_latlong_kml_get_item_name($content, $field) {
149 // $field->param2 contains the user-specified labelling method
153 if($field->param2
> 0) {
154 $name = htmlspecialchars(get_field('data_content', 'content', 'fieldid', $field->param2
, 'recordid', $content->recordid
));
155 }elseif($field->param2
== -2) {
156 $name = $content->content
. ', ' . $content->content1
;
158 if($name=='') { // Done this way so that "item #" is the default that catches any problems
159 $name = get_string('entry', 'data') . " #$content->recordid";