.
// A lot of this initial stuff is copied from mod/data/view.php
require_once('../../../../config.php');
require_once('../../lib.php');
// Optional params: row id "rid" - if set then export just one, otherwise export all
$d       = required_param('d', PARAM_INT);   // database id
$fieldid = required_param('fieldid', PARAM_INT);   // field id
$rid     = optional_param('rid', 0, PARAM_INT);    //record id
$url = new moodle_url('/mod/data/field/latlong/kml.php', array('d'=>$d, 'fieldid'=>$fieldid));
if ($rid !== 0) {
    $url->param('rid', $rid);
}
$PAGE->set_url($url);
if ($rid) {
    if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
        print_error('invalidrecord', 'data');
    }
    if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
        print_error('invalidid', 'data');
    }
    if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
        print_error('coursemisconf');
    }
    if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
        print_error('invalidcoursemodule');
    }
    if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
        print_error('invalidfieldid', 'data');
    }
    if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
        print_error('invalidfieldtype', 'data');
    }
    if (! $content = $DB->get_record('data_content', array('fieldid'=>$fieldid, 'recordid'=>$rid))) {
        print_error('nofieldcontent', 'data');
    }
} else {   // We must have $d
    if (! $data = $DB->get_record('data', array('id'=>$d))) {
        print_error('invalidid', 'data');
    }
    if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
        print_error('coursemisconf');
    }
    if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
        print_error('invalidcoursemodule');
    }
    if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
        print_error('invalidfieldid', 'data');
    }
    if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
        print_error('invalidfieldtype', 'data');
    }
    $record = NULL;
}
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
/// If it's hidden then it's don't show anything.  :)
if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
    $PAGE->set_title($data->name);
    echo $OUTPUT->header();
    notice(get_string("activityiscurrentlyhidden"));
}
/// If we have an empty Database then redirect because this page is useless without data
if (has_capability('mod/data:managetemplates', $context)) {
    if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
        redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
    }
}
//header('Content-type: text/plain'); // This is handy for debug purposes to look at the KML in the browser
header('Content-type: application/vnd.google-earth.kml+xml kml');
header('Content-Disposition: attachment; filename="moodleearth-'.$d.'-'.$rid.'-'.$fieldid.'.kml"');
echo data_latlong_kml_top();
if($rid) { // List one single item
    $pm = new stdClass();
    $pm->name = data_latlong_kml_get_item_name($content, $field);
    $pm->description = "<a href='$CFG->wwwroot/mod/data/view.php?d=$d&rid=$rid'>Item #$rid</a> in Moodle data activity";
    $pm->long = $content->content1;
    $pm->lat = $content->content;
    echo data_latlong_kml_placemark($pm);
} else {   // List all items in turn
    $contents = $DB->get_records('data_content', array('fieldid'=>$fieldid));
    echo '';
    foreach($contents as $content) {
        $pm->name = data_latlong_kml_get_item_name($content, $field);
        $pm->description = "<a href='$CFG->wwwroot/mod/data/view.php?d=$d&rid=$content->recordid'>Item #$content->recordid</a> in Moodle data activity";
        $pm->long = $content->content1;
        $pm->lat = $content->content;
        echo data_latlong_kml_placemark($pm);
    }
    echo '';
}
echo data_latlong_kml_bottom();
function data_latlong_kml_top() {
    return '
';
}
function data_latlong_kml_placemark($pm) {
    return '
  '.$pm->description.'
  '.$pm->name.'
  
    '.$pm->long.'
    '.$pm->lat.'
    30500.8880792294568
    46.72425699662645
    0.0
  
  0
  
    1
    relativeToGround
    '.$pm->long.','.$pm->lat.',50
  
';
}
function data_latlong_kml_bottom() {
    return '';
}
function data_latlong_kml_get_item_name($content, $field) {
    global $DB;
    // $field->param2 contains the user-specified labelling method
    $name = '';
    if($field->param2 > 0) {
        $name = htmlspecialchars($DB->get_field('data_content', 'content', array('fieldid'=>$field->param2, 'recordid'=>$content->recordid)));
    }elseif($field->param2 == -2) {
        $name = $content->content . ', ' . $content->content1;
    }
    if($name=='') { // Done this way so that "item #" is the default that catches any problems
        $name = get_string('entry', 'data') . " #$content->recordid";
    }
    return $name;
}