Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / data / field / latlong / kml.php
blob7af26312b5e439d408a6dc8ac57dba11ba6b984a
1 <?php
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
15 if ($rid) {
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');
53 $record = NULL;
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))) {
60 $navigation = build_navigation('', $cm);
61 print_header_simple(format_string($data->name), "", $navigation,
62 "", "", true, '', navmenu($course, $cm));
63 notice(get_string("activityiscurrentlyhidden"));
66 /// If we have an empty Database then redirect because this page is useless without data
67 if (has_capability('mod/data:managetemplates', $context)) {
68 if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
69 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
76 //header('Content-type: text/plain'); // This is handy for debug purposes to look at the KML in the browser
77 header('Content-type: application/vnd.google-earth.kml+xml kml');
78 header('Content-Disposition: attachment; filename="moodleearth-'.$d.'-'.$rid.'-'.$fieldid.'.kml"');
81 echo data_latlong_kml_top();
83 if($rid) { // List one single item
84 $pm->name = data_latlong_kml_get_item_name($content, $field);
85 $pm->description = "&lt;a href='$CFG->wwwroot/mod/data/view.php?d=$d&amp;rid=$rid'&gt;Item #$rid&lt;/a&gt; in Moodle data activity";
86 $pm->long = $content->content1;
87 $pm->lat = $content->content;
88 echo data_latlong_kml_placemark($pm);
89 } else { // List all items in turn
91 $contents = get_records('data_content', 'fieldid', $fieldid);
93 echo '<Document>';
95 foreach($contents as $content) {
96 $pm->name = data_latlong_kml_get_item_name($content, $field);
97 $pm->description = "&lt;a href='$CFG->wwwroot/mod/data/view.php?d=$d&amp;rid=$content->recordid'&gt;Item #$content->recordid&lt;/a&gt; in Moodle data activity";
98 $pm->long = $content->content1;
99 $pm->lat = $content->content;
100 echo data_latlong_kml_placemark($pm);
103 echo '</Document>';
107 echo data_latlong_kml_bottom();
112 function data_latlong_kml_top() {
113 return '<?xml version="1.0" encoding="UTF-8"?>
114 <kml xmlns="http://earth.google.com/kml/2.0">
119 function data_latlong_kml_placemark($pm) {
120 return '<Placemark>
121 <description>'.$pm->description.'</description>
122 <name>'.$pm->name.'</name>
123 <LookAt>
124 <longitude>'.$pm->long.'</longitude>
125 <latitude>'.$pm->lat.'</latitude>
126 <range>30500.8880792294568</range>
127 <tilt>46.72425699662645</tilt>
128 <heading>0.0</heading>
129 </LookAt>
130 <visibility>0</visibility>
131 <Point>
132 <extrude>1</extrude>
133 <altitudeMode>relativeToGround</altitudeMode>
134 <coordinates>'.$pm->long.','.$pm->lat.',50</coordinates>
135 </Point>
136 </Placemark>
140 function data_latlong_kml_bottom() {
141 return '</kml>';
144 function data_latlong_kml_get_item_name($content, $field) {
145 // $field->param2 contains the user-specified labelling method
147 $name = '';
149 if($field->param2 > 0) {
150 $name = htmlspecialchars(get_field('data_content', 'content', 'fieldid', $field->param2, 'recordid', $content->recordid));
151 }elseif($field->param2 == -2) {
152 $name = $content->content . ', ' . $content->content1;
154 if($name=='') { // Done this way so that "item #" is the default that catches any problems
155 $name = get_string('entry', 'data') . " #$content->recordid";
159 return $name;