Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  // A lot of this initial stuff is copied from mod/data/view.php
  19  
  20  require_once('../../../../config.php');
  21  require_once ('../../lib.php');
  22  
  23  // Optional params: row id "rid" - if set then export just one, otherwise export all
  24  
  25  $d       = required_param('d', PARAM_INT);   // database id
  26  $fieldid = required_param('fieldid', PARAM_INT);   // field id
  27  $rid     = optional_param('rid', 0, PARAM_INT);    //record id
  28  
  29  $url = new moodle_url('/mod/data/field/latlong/kml.php', array('d'=>$d, 'fieldid'=>$fieldid));
  30  if ($rid !== 0) {
  31      $url->param('rid', $rid);
  32  }
  33  $PAGE->set_url($url);
  34  
  35  if ($rid) {
  36      if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
  37          throw new \moodle_exception('invalidrecord', 'data');
  38      }
  39      if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
  40          throw new \moodle_exception('invalidid', 'data');
  41      }
  42      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  43          throw new \moodle_exception('coursemisconf');
  44      }
  45      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  46          throw new \moodle_exception('invalidcoursemodule');
  47      }
  48      if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
  49          throw new \moodle_exception('invalidfieldid', 'data');
  50      }
  51      if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
  52          throw new \moodle_exception('invalidfieldtype', 'data');
  53      }
  54      if (! $content = $DB->get_record('data_content', array('fieldid'=>$fieldid, 'recordid'=>$rid))) {
  55          throw new \moodle_exception('nofieldcontent', 'data');
  56      }
  57  } else {   // We must have $d
  58      if (! $data = $DB->get_record('data', array('id'=>$d))) {
  59          throw new \moodle_exception('invalidid', 'data');
  60      }
  61      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  62          throw new \moodle_exception('coursemisconf');
  63      }
  64      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  65          throw new \moodle_exception('invalidcoursemodule');
  66      }
  67      if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
  68          throw new \moodle_exception('invalidfieldid', 'data');
  69      }
  70      if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
  71          throw new \moodle_exception('invalidfieldtype', 'data');
  72      }
  73      $record = NULL;
  74  }
  75  
  76  require_course_login($course, true, $cm);
  77  
  78  $context = context_module::instance($cm->id);
  79  // If we have an empty Database then redirect because this page is useless without data.
  80  if (has_capability('mod/data:managetemplates', $context)) {
  81      if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
  82          redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
  83      }
  84  }
  85  
  86  
  87  
  88  
  89  //header('Content-type: text/plain'); // This is handy for debug purposes to look at the KML in the browser
  90  header('Content-type: application/vnd.google-earth.kml+xml kml');
  91  header('Content-Disposition: attachment; filename="moodleearth-'.$d.'-'.$rid.'-'.$fieldid.'.kml"');
  92  
  93  
  94  echo data_latlong_kml_top();
  95  
  96  if($rid) { // List one single item
  97      $pm = new stdClass();
  98      $pm->name = data_latlong_kml_get_item_name($content, $field);
  99      $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";
 100      $pm->long = $content->content1;
 101      $pm->lat = $content->content;
 102      echo data_latlong_kml_placemark($pm);
 103  } else {   // List all items in turn
 104  
 105      $contents = $DB->get_records('data_content', array('fieldid'=>$fieldid));
 106  
 107      echo '<Document>';
 108  
 109      foreach($contents as $content) {
 110          $pm->name = data_latlong_kml_get_item_name($content, $field);
 111          $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";
 112          $pm->long = $content->content1;
 113          $pm->lat = $content->content;
 114          echo data_latlong_kml_placemark($pm);
 115      }
 116  
 117      echo '</Document>';
 118  
 119  }
 120  
 121  echo data_latlong_kml_bottom();
 122  
 123  
 124  
 125  
 126  function data_latlong_kml_top() {
 127      return '<?xml version="1.0" encoding="UTF-8"?>
 128  <kml xmlns="http://earth.google.com/kml/2.0">
 129  
 130  ';
 131  }
 132  
 133  function data_latlong_kml_placemark($pm) {
 134      return '<Placemark>
 135    <description>'.$pm->description.'</description>
 136    <name>'.$pm->name.'</name>
 137    <LookAt>
 138      <longitude>'.$pm->long.'</longitude>
 139      <latitude>'.$pm->lat.'</latitude>
 140      <range>30500.8880792294568</range>
 141      <tilt>46.72425699662645</tilt>
 142      <heading>0.0</heading>
 143    </LookAt>
 144    <visibility>0</visibility>
 145    <Point>
 146      <extrude>1</extrude>
 147      <altitudeMode>relativeToGround</altitudeMode>
 148      <coordinates>'.$pm->long.','.$pm->lat.',50</coordinates>
 149    </Point>
 150  </Placemark>
 151  ';
 152  }
 153  
 154  function data_latlong_kml_bottom() {
 155      return '</kml>';
 156  }
 157  
 158  function data_latlong_kml_get_item_name($content, $field) {
 159      global $DB;
 160  
 161      // $field->param2 contains the user-specified labelling method
 162  
 163      $name = '';
 164  
 165      if($field->param2 > 0) {
 166          $name = htmlspecialchars($DB->get_field('data_content', 'content', array('fieldid'=>$field->param2, 'recordid'=>$content->recordid)), ENT_COMPAT);
 167      }elseif($field->param2 == -2) {
 168          $name = $content->content . ', ' . $content->content1;
 169      }
 170      if($name=='') { // Done this way so that "item #" is the default that catches any problems
 171          $name = get_string('entry', 'data') . " #$content->recordid";
 172      }
 173  
 174  
 175      return $name;
 176  }