|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 // A lot of this initial stuff is copied from mod/data/view.php 00019 00020 require_once('../../../../config.php'); 00021 require_once('../../lib.php'); 00022 00023 // Optional params: row id "rid" - if set then export just one, otherwise export all 00024 00025 $d = required_param('d', PARAM_INT); // database id 00026 $fieldid = required_param('fieldid', PARAM_INT); // field id 00027 $rid = optional_param('rid', 0, PARAM_INT); //record id 00028 00029 $url = new moodle_url('/mod/data/field/latlong/kml.php', array('d'=>$d, 'fieldid'=>$fieldid)); 00030 if ($rid !== 0) { 00031 $url->param('rid', $rid); 00032 } 00033 $PAGE->set_url($url); 00034 00035 if ($rid) { 00036 if (! $record = $DB->get_record('data_records', array('id'=>$rid))) { 00037 print_error('invalidrecord', 'data'); 00038 } 00039 if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) { 00040 print_error('invalidid', 'data'); 00041 } 00042 if (! $course = $DB->get_record('course', array('id'=>$data->course))) { 00043 print_error('coursemisconf'); 00044 } 00045 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { 00046 print_error('invalidcoursemodule'); 00047 } 00048 if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) { 00049 print_error('invalidfieldid', 'data'); 00050 } 00051 if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type! 00052 print_error('invalidfieldtype', 'data'); 00053 } 00054 if (! $content = $DB->get_record('data_content', array('fieldid'=>$fieldid, 'recordid'=>$rid))) { 00055 print_error('nofieldcontent', 'data'); 00056 } 00057 } else { // We must have $d 00058 if (! $data = $DB->get_record('data', array('id'=>$d))) { 00059 print_error('invalidid', 'data'); 00060 } 00061 if (! $course = $DB->get_record('course', array('id'=>$data->course))) { 00062 print_error('coursemisconf'); 00063 } 00064 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { 00065 print_error('invalidcoursemodule'); 00066 } 00067 if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) { 00068 print_error('invalidfieldid', 'data'); 00069 } 00070 if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type! 00071 print_error('invalidfieldtype', 'data'); 00072 } 00073 $record = NULL; 00074 } 00075 00076 require_course_login($course, true, $cm); 00077 00079 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities',get_context_instance(CONTEXT_MODULE, $cm->id))) { 00080 $PAGE->set_title($data->name); 00081 echo $OUTPUT->header(); 00082 notice(get_string("activityiscurrentlyhidden")); 00083 } 00084 00086 if (has_capability('mod/data:managetemplates', $context)) { 00087 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database! 00088 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry 00089 } 00090 } 00091 00092 00093 00094 00095 //header('Content-type: text/plain'); // This is handy for debug purposes to look at the KML in the browser 00096 header('Content-type: application/vnd.google-earth.kml+xml kml'); 00097 header('Content-Disposition: attachment; filename="moodleearth-'.$d.'-'.$rid.'-'.$fieldid.'.kml"'); 00098 00099 00100 echo data_latlong_kml_top(); 00101 00102 if($rid) { // List one single item 00103 $pm = new stdClass(); 00104 $pm->name = data_latlong_kml_get_item_name($content, $field); 00105 $pm->description = "<a href='$CFG->wwwroot/mod/data/view.php?d=$d&rid=$rid'>Item #$rid</a> in Moodle data activity"; 00106 $pm->long = $content->content1; 00107 $pm->lat = $content->content; 00108 echo data_latlong_kml_placemark($pm); 00109 } else { // List all items in turn 00110 00111 $contents = $DB->get_records('data_content', array('fieldid'=>$fieldid)); 00112 00113 echo '<Document>'; 00114 00115 foreach($contents as $content) { 00116 $pm->name = data_latlong_kml_get_item_name($content, $field); 00117 $pm->description = "<a href='$CFG->wwwroot/mod/data/view.php?d=$d&rid=$content->recordid'>Item #$content->recordid</a> in Moodle data activity"; 00118 $pm->long = $content->content1; 00119 $pm->lat = $content->content; 00120 echo data_latlong_kml_placemark($pm); 00121 } 00122 00123 echo '</Document>'; 00124 00125 } 00126 00127 echo data_latlong_kml_bottom(); 00128 00129 00130 00131 00132 function data_latlong_kml_top() { 00133 return '<?xml version="1.0" encoding="UTF-8"?> 00134 <kml xmlns="http://earth.google.com/kml/2.0"> 00135 00136 '; 00137 } 00138 00139 function data_latlong_kml_placemark($pm) { 00140 return '<Placemark> 00141 <description>'.$pm->description.'</description> 00142 <name>'.$pm->name.'</name> 00143 <LookAt> 00144 <longitude>'.$pm->long.'</longitude> 00145 <latitude>'.$pm->lat.'</latitude> 00146 <range>30500.8880792294568</range> 00147 <tilt>46.72425699662645</tilt> 00148 <heading>0.0</heading> 00149 </LookAt> 00150 <visibility>0</visibility> 00151 <Point> 00152 <extrude>1</extrude> 00153 <altitudeMode>relativeToGround</altitudeMode> 00154 <coordinates>'.$pm->long.','.$pm->lat.',50</coordinates> 00155 </Point> 00156 </Placemark> 00157 '; 00158 } 00159 00160 function data_latlong_kml_bottom() { 00161 return '</kml>'; 00162 } 00163 00164 function data_latlong_kml_get_item_name($content, $field) { 00165 global $DB; 00166 00167 // $field->param2 contains the user-specified labelling method 00168 00169 $name = ''; 00170 00171 if($field->param2 > 0) { 00172 $name = htmlspecialchars($DB->get_field('data_content', 'content', array('fieldid'=>$field->param2, 'recordid'=>$content->recordid))); 00173 }elseif($field->param2 == -2) { 00174 $name = $content->content . ', ' . $content->content1; 00175 } 00176 if($name=='') { // Done this way so that "item #" is the default that catches any problems 00177 $name = get_string('entry', 'data') . " #$content->recordid"; 00178 } 00179 00180 00181 return $name; 00182 }