Merge branch 'wip-MDL-37356-master' of git://github.com/abgreeve/moodle
[moodle.git] / mod / data / field / latlong / field.class.php
CommitLineData
1adbd2c3 1<?php
8161ac98 2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
9// Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25class data_field_latlong extends data_field_base {
8161ac98 26 var $type = 'latlong';
aab98aaf 27
8161ac98 28 // This is an array of URL schemes for linking out to services, using the float values of lat and long.
29 // In each scheme, the special markers @lat@ and @long@ will be replaced by the float values.
30 // The config options for the field store each service name that should be displayed, in a comma-separated
31 // field. Therefore please DO NOT include commas in the service names if you are adding extra services.
2858af42 32
33 // Parameter data used:
34 // "param1" is a comma-separated list of the linkout service names that are enabled for this instance
35 // "param2" indicates the label that will be used in generating Google Earth KML files: -1 for item #, -2 for lat/long, positive number for the (text) field to use.
36
8161ac98 37 var $linkoutservices = array(
38 "Google Maps" => "http://maps.google.com/maps?q=@lat@,+@long@&iwloc=A&hl=en",
39 "Google Earth" => "@wwwroot@/mod/data/field/latlong/kml.php?d=@dataid@&fieldid=@fieldid@&rid=@recordid@",
40 "Geabios" => "http://www.geabios.com/html/services/maps/PublicMap.htm?lat=@lat@&lon=@long@&fov=0.3&title=Moodle%20data%20item",
41 "OpenStreetMap" => "http://www.openstreetmap.org/index.html?lat=@lat@&lon=@long@&zoom=11",
42 "Multimap" => "http://www.multimap.com/map/browse.cgi?scale=200000&lon=@long@&lat=@lat@&icon=x"
2858af42 43 );
8161ac98 44 // Other map sources listed at http://kvaleberg.com/extensions/mapsources/index.php?params=51_30.4167_N_0_7.65_W_region:earth
45
2858af42 46 function display_add_field($recordid=0) {
a656d951 47 global $CFG, $DB;
48
8161ac98 49 $lat = '';
50 $long = '';
2858af42 51 if ($recordid) {
a656d951 52 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
8161ac98 53 $lat = $content->content;
54 $long = $content->content1;
55 }
56 }
e357c206 57 $str = '<div title="'.s($this->field->description).'">';
aab98aaf 58 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
61240489 59 $str .= '<table><tr><td align="right">';
e357c206 60 $str .= '<label for="field_'.$this->field->id.'_0">' . get_string('latitude', 'data') . '</label></td><td><input type="text" name="field_'.$this->field->id.'_0" id="field_'.$this->field->id.'_0" value="'.s($lat).'" size="10" />°N</td></tr>';
61 $str .= '<tr><td align="right"><label for="field_'.$this->field->id.'_1">' . get_string('longitude', 'data') . '</label></td><td><input type="text" name="field_'.$this->field->id.'_1" id="field_'.$this->field->id.'_1" value="'.s($long).'" size="10" />°E</td></tr>';
61240489 62 $str .= '</table>';
aab98aaf 63 $str .= '</fieldset>';
8161ac98 64 $str .= '</div>';
8161ac98 65 return $str;
66 }
2858af42 67
7900ecb0 68 function display_search_field($value = '') {
eca89126
EL
69 global $CFG, $DB;
70
71 $varcharlat = $DB->sql_compare_text('content');
72 $varcharlong= $DB->sql_compare_text('content1');
73 $latlongsrs = $DB->get_recordset_sql(
74 "SELECT DISTINCT $varcharlat AS la, $varcharlong AS lo
75 FROM {data_content}
76 WHERE fieldid = ?
77 ORDER BY $varcharlat, $varcharlong", array($this->field->id));
78
7900ecb0 79 $options = array();
eca89126 80 foreach ($latlongsrs as $latlong) {
cffc08a6
AG
81 $latitude = format_float($latlong->la, 4);
82 $longitude = format_float($latlong->lo, 4);
83 $options[$latlong->la . ',' . $latlong->lo] = $latitude . ' ' . $longitude;
7900ecb0 84 }
eca89126
EL
85 $latlongsrs->close();
86
01f8e80d
RW
87 $return = html_writer::label(get_string('latlong', 'data'), 'menuf_'.$this->field->id, false, array('class' => 'accesshide'));
88 $return .= html_writer::select($options, 'f_'.$this->field->id, $value);
89 return $return;
7900ecb0 90 }
2858af42 91
7900ecb0 92 function parse_search_field() {
93 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
94 }
2858af42 95
7900ecb0 96 function generate_sql($tablealias, $value) {
be4d5a92
EL
97 global $DB;
98
e3487936 99 static $i=0;
100 $i++;
101 $name1 = "df_latlong1_$i";
102 $name2 = "df_latlong2_$i";
be4d5a92
EL
103 $varcharlat = $DB->sql_compare_text("{$tablealias}.content");
104 $varcharlong= $DB->sql_compare_text("{$tablealias}.content1");
105
e3487936 106
7900ecb0 107 $latlong[0] = '';
108 $latlong[1] = '';
109 $latlong = explode (',', $value, 2);
be4d5a92 110 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharlat = :$name1 AND $varcharlong = :$name2) ",
e3487936 111 array($name1=>$latlong[0], $name2=>$latlong[1]));
7900ecb0 112 }
8161ac98 113
114 function display_browse_field($recordid, $template) {
a656d951 115 global $CFG, $DB;
a656d951 116 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
30f54a9c 117 $lat = $content->content;
118 if (strlen($lat) < 1) {
119 return false;
120 }
121 $long = $content->content1;
122 if (strlen($long) < 1) {
123 return false;
8161ac98 124 }
cffc08a6 125 // We use format_float to display in the regional format.
2858af42 126 if($lat < 0) {
cffc08a6 127 $compasslat = format_float(-$lat, 4) . '°S';
2858af42 128 } else {
cffc08a6 129 $compasslat = format_float($lat, 4) . '°N';
2858af42 130 }
131 if($long < 0) {
cffc08a6 132 $compasslong = format_float(-$long, 4) . '°W';
2858af42 133 } else {
cffc08a6 134 $compasslong = format_float($long, 4) . '°E';
2858af42 135 }
aab98aaf 136
8161ac98 137 // Now let's create the jump-to-services link
138 $servicesshown = explode(',', $this->field->param1);
139
140 // These are the different things that can be magically inserted into URL schemes
141 $urlreplacements = array(
142 '@lat@'=> $lat,
143 '@long@'=> $long,
144 '@wwwroot@'=> $CFG->wwwroot,
145 '@contentid@'=> $content->id,
146 '@dataid@'=> $this->data->id,
147 '@courseid@'=> $this->data->course,
148 '@fieldid@'=> $content->fieldid,
149 '@recordid@'=> $content->recordid,
2858af42 150 );
8161ac98 151
152 if(sizeof($servicesshown)==1 && $servicesshown[0]) {
649ac85d 153 $str = " <a href='"
8161ac98 154 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicesshown[0]])
cffc08a6 155 ."' title='$servicesshown[0]'>$compasslat $compasslong</a>";
8161ac98 156 } elseif (sizeof($servicesshown)>1) {
649ac85d 157 $str = '<form id="latlongfieldbrowse">';
01f8e80d
RW
158 $str .= "$compasslat, $compasslong\n";
159 $str .= "<label class='accesshide' for='jumpto'>". get_string('jumpto') ."</label>";
160 $str .= "<select id='jumpto' name='jumpto'>";
61240489 161 foreach($servicesshown as $servicename){
162 // Add a link to a service
163 $str .= "\n <option value='"
164 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicename])
165 . "'>".htmlspecialchars($servicename)."</option>";
166 }
167 // NB! If you are editing this, make sure you don't break the javascript reference "previousSibling"
168 // which allows the "Go" button to refer to the drop-down selector.
169 $str .= "\n</select><input type='button' value='" . get_string('go') . "' onclick='if(previousSibling.value){self.location=previousSibling.value}'/>";
649ac85d 170 $str .= '</form>';
9c1ae6fc 171 } else {
649ac85d 172 $str = "$compasslat, $compasslong";
8161ac98 173 }
649ac85d 174
8161ac98 175 return $str;
176 }
177 return false;
178 }
179
180 function update_content($recordid, $value, $name='') {
a656d951 181 global $DB;
182
39790bd8 183 $content = new stdClass();
8161ac98 184 $content->fieldid = $this->field->id;
185 $content->recordid = $recordid;
cffc08a6
AG
186 // When updating these values (which might be region formatted) we should format
187 // the float to allow for a consistent float format in the database.
188 $value = unformat_float($value);
30f54a9c 189 $value = trim($value);
190 if (strlen($value) > 0) {
191 $value = floatval($value);
192 } else {
193 $value = null;
194 }
8161ac98 195 $names = explode('_', $name);
2858af42 196 switch ($names[2]) {
197 case 0:
198 // update lat
30f54a9c 199 $content->content = $value;
8161ac98 200 break;
2858af42 201 case 1:
202 // update long
30f54a9c 203 $content->content1 = $value;
8161ac98 204 break;
205 default:
206 break;
207 }
a656d951 208 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
8161ac98 209 $content->id = $oldcontent->id;
a656d951 210 return $DB->update_record('data_content', $content);
8161ac98 211 } else {
a656d951 212 return $DB->insert_record('data_content', $content);
8161ac98 213 }
214 }
215
216 function get_sort_sql($fieldname) {
cbc08f3b 217 global $DB;
6e6878b5 218 return $DB->sql_cast_char2real($fieldname, true);
8161ac98 219 }
2858af42 220
221 function export_text_value($record) {
cffc08a6 222 // The content here is from the database and does not require location formating.
0721309f 223 return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1);
2858af42 224 }
225
8161ac98 226}
227
1adbd2c3 228