Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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          print_error('invalidrecord', 'data');
  38      }
  39      if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
  40          print_error('invalidid', 'data');
  41      }
  42      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  43          print_error('coursemisconf');
  44      }
  45      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  46          print_error('invalidcoursemodule');
  47      }
  48      if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
  49          print_error('invalidfieldid', 'data');
  50      }
  51      if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
  52          print_error('invalidfieldtype', 'data');
  53      }
  54      if (! $content = $DB->get_record('data_content', array('fieldid'=>$fieldid, 'recordid'=>$rid))) {
  55          print_error('nofieldcontent', 'data');
  56      }
  57  } else {   // We must have $d
  58      if (! $data = $DB->get_record('data', array('id'=>$d))) {
  59          print_error('invalidid', 'data');
  60      }
  61      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  62          print_error('coursemisconf');
  63      }
  64      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  65          print_error('invalidcoursemodule');
  66      }
  67      if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
  68          print_error('invalidfieldid', 'data');
  69      }
  70      if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
  71          print_error('invalidfieldtype', 'data');
  72      }
  73      $record = NULL;
  74  }
  75  
  76  require_course_login($course, true, $cm);
  77  
  78  $context = context_module::instance($cm->id);
  79  
  80  /// If it's hidden then it's don't show anything.  :)
  81  if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
  82      $PAGE->set_title($data->name);
  83      echo $OUTPUT->header();
  84      notice(get_string("activityiscurrentlyhidden"));
  85  }
  86  
  87  /// If we have an empty Database then redirect because this page is useless without data
  88  if (has_capability('mod/data:managetemplates', $context)) {
  89      if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
  90          redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
  91      }
  92  }
  93  
  94  
  95  
  96  
  97  //header('Content-type: text/plain'); // This is handy for debug purposes to look at the KML in the browser
  98  header('Content-type: application/vnd.google-earth.kml+xml kml');
  99  header('Content-Disposition: attachment; filename="moodleearth-'.$d.'-'.$rid.'-'.$fieldid.'.kml"');
 100  
 101  
 102  echo data_latlong_kml_top();
 103  
 104  if($rid) { // List one single item
 105      $pm = new stdClass();
 106      $pm->name = data_latlong_kml_get_item_name($content, $field);
 107      $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";
 108      $pm->long = $content->content1;
 109      $pm->lat = $content->content;
 110      echo data_latlong_kml_placemark($pm);
 111  } else {   // List all items in turn
 112  
 113      $contents = $DB->get_records('data_content', array('fieldid'=>$fieldid));
 114  
 115      echo '<Document>';
 116  
 117      foreach($contents as $content) {
 118          $pm->name = data_latlong_kml_get_item_name($content, $field);
 119          $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";
 120          $pm->long = $content->content1;
 121          $pm->lat = $content->content;
 122          echo data_latlong_kml_placemark($pm);
 123      }
 124  
 125      echo '</Document>';
 126  
 127  }
 128  
 129  echo data_latlong_kml_bottom();
 130  
 131  
 132  
 133  
 134  function data_latlong_kml_top() {
 135      return '<?xml version="1.0" encoding="UTF-8"?>
 136  <kml xmlns="http://earth.google.com/kml/2.0">
 137  
 138  ';
 139  }
 140  
 141  function data_latlong_kml_placemark($pm) {
 142      return '<Placemark>
 143    <description>'.$pm->description.'</description>
 144    <name>'.$pm->name.'</name>
 145    <LookAt>
 146      <longitude>'.$pm->long.'</longitude>
 147      <latitude>'.$pm->lat.'</latitude>
 148      <range>30500.8880792294568</range>
 149      <tilt>46.72425699662645</tilt>
 150      <heading>0.0</heading>
 151    </LookAt>
 152    <visibility>0</visibility>
 153    <Point>
 154      <extrude>1</extrude>
 155      <altitudeMode>relativeToGround</altitudeMode>
 156      <coordinates>'.$pm->long.','.$pm->lat.',50</coordinates>
 157    </Point>
 158  </Placemark>
 159  ';
 160  }
 161  
 162  function data_latlong_kml_bottom() {
 163      return '</kml>';
 164  }
 165  
 166  function data_latlong_kml_get_item_name($content, $field) {
 167      global $DB;
 168  
 169      // $field->param2 contains the user-specified labelling method
 170  
 171      $name = '';
 172  
 173      if($field->param2 > 0) {
 174          $name = htmlspecialchars($DB->get_field('data_content', 'content', array('fieldid'=>$field->param2, 'recordid'=>$content->recordid)));
 175      }elseif($field->param2 == -2) {
 176          $name = $content->content . ', ' . $content->content1;
 177      }
 178      if($name=='') { // Done this way so that "item #" is the default that catches any problems
 179          $name = get_string('entry', 'data') . " #$content->recordid";
 180      }
 181  
 182  
 183      return $name;
 184  }