2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_latlong extends data_field_base {
26 var $type = 'latlong';
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.
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.
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"
44 // Other map sources listed at http://kvaleberg.com/extensions/mapsources/index.php?params=51_30.4167_N_0_7.65_W_region:earth
46 function display_add_field($recordid = 0, $formdata = null) {
47 global $CFG, $DB, $OUTPUT;
52 $fieldname = 'field_' . $this->field->id . '_0';
53 $lat = $formdata->$fieldname;
54 $fieldname = 'field_' . $this->field->id . '_1';
55 $long = $formdata->$fieldname;
56 } else if ($recordid) {
57 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
58 $lat = $content->content;
59 $long = $content->content1;
62 $str = '<div title="'.s($this->field->description).'">';
63 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
64 $str .= '<table><tr><td align="right">';
65 $str .= '<label for="field_'.$this->field->id.'_0" class="mod-data-input">' . get_string('latitude', 'data');
66 if ($this->field->required) {
67 $str .= html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
68 array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
70 $str .= '</label></td><td><input type="text" name="field_'.$this->field->id.'_0" id="field_'.$this->field->id.'_0" value="';
71 $str .= s($lat).'" size="10" />°N</td></tr>';
72 $str .= '<tr><td align="right"><label for="field_'.$this->field->id.'_1" class="mod-data-input">';
73 $str .= get_string('longitude', 'data');
74 if ($this->field->required) {
75 $str .= html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
76 array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
78 $str .= '</label></td><td><input type="text" name="field_'.$this->field->id.'_1" id="field_'.$this->field->id.'_1" value="';
79 $str .= s($long).'" size="10" />°E</td>';
82 $str .= '</fieldset>';
87 function display_search_field($value = '') {
90 $varcharlat = $DB->sql_compare_text('content');
91 $varcharlong= $DB->sql_compare_text('content1');
92 $latlongsrs = $DB->get_recordset_sql(
93 "SELECT DISTINCT $varcharlat AS la, $varcharlong AS lo
96 ORDER BY $varcharlat, $varcharlong", array($this->field->id));
99 foreach ($latlongsrs as $latlong) {
100 $latitude = format_float($latlong->la, 4);
101 $longitude = format_float($latlong->lo, 4);
102 $options[$latlong->la . ',' . $latlong->lo] = $latitude . ' ' . $longitude;
104 $latlongsrs->close();
106 $return = html_writer::label(get_string('latlong', 'data'), 'menuf_'.$this->field->id, false, array('class' => 'accesshide'));
107 $return .= html_writer::select($options, 'f_'.$this->field->id, $value);
111 function parse_search_field() {
112 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
115 function generate_sql($tablealias, $value) {
120 $name1 = "df_latlong1_$i";
121 $name2 = "df_latlong2_$i";
122 $varcharlat = $DB->sql_compare_text("{$tablealias}.content");
123 $varcharlong= $DB->sql_compare_text("{$tablealias}.content1");
128 $latlong = explode (',', $value, 2);
129 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharlat = :$name1 AND $varcharlong = :$name2) ",
130 array($name1=>$latlong[0], $name2=>$latlong[1]));
133 function display_browse_field($recordid, $template) {
135 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
136 $lat = $content->content;
137 if (strlen($lat) < 1) {
140 $long = $content->content1;
141 if (strlen($long) < 1) {
144 // We use format_float to display in the regional format.
146 $compasslat = format_float(-$lat, 4) . '°S';
148 $compasslat = format_float($lat, 4) . '°N';
151 $compasslong = format_float(-$long, 4) . '°W';
153 $compasslong = format_float($long, 4) . '°E';
156 // Now let's create the jump-to-services link
157 $servicesshown = explode(',', $this->field->param1);
159 // These are the different things that can be magically inserted into URL schemes
160 $urlreplacements = array(
163 '@wwwroot@'=> $CFG->wwwroot,
164 '@contentid@'=> $content->id,
165 '@dataid@'=> $this->data->id,
166 '@courseid@'=> $this->data->course,
167 '@fieldid@'=> $content->fieldid,
168 '@recordid@'=> $content->recordid,
171 if(sizeof($servicesshown)==1 && $servicesshown[0]) {
173 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicesshown[0]])
174 ."' title='$servicesshown[0]'>$compasslat $compasslong</a>";
175 } elseif (sizeof($servicesshown)>1) {
176 $str = '<form id="latlongfieldbrowse">';
177 $str .= "$compasslat, $compasslong\n";
178 $str .= "<label class='accesshide' for='jumpto'>". get_string('jumpto') ."</label>";
179 $str .= "<select id='jumpto' name='jumpto'>";
180 foreach($servicesshown as $servicename){
181 // Add a link to a service
182 $str .= "\n <option value='"
183 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicename])
184 . "'>".htmlspecialchars($servicename)."</option>";
186 // NB! If you are editing this, make sure you don't break the javascript reference "previousSibling"
187 // which allows the "Go" button to refer to the drop-down selector.
188 $str .= "\n</select><input type='button' value='" . get_string('go') . "' onclick='if(previousSibling.value){self.location=previousSibling.value}'/>";
191 $str = "$compasslat, $compasslong";
199 function update_content($recordid, $value, $name='') {
202 $content = new stdClass();
203 $content->fieldid = $this->field->id;
204 $content->recordid = $recordid;
205 // When updating these values (which might be region formatted) we should format
206 // the float to allow for a consistent float format in the database.
207 $value = unformat_float($value);
208 $value = trim($value);
209 if (strlen($value) > 0) {
210 $value = floatval($value);
214 $names = explode('_', $name);
218 $content->content = $value;
222 $content->content1 = $value;
227 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
228 $content->id = $oldcontent->id;
229 return $DB->update_record('data_content', $content);
231 return $DB->insert_record('data_content', $content);
235 function get_sort_sql($fieldname) {
237 return $DB->sql_cast_char2real($fieldname, true);
240 function export_text_value($record) {
241 // The content here is from the database and does not require location formating.
242 return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1);
246 * Check if a field from an add form is empty
248 * @param mixed $value
252 function notemptyfield($value, $name) {
253 return isset($value) && !($value == '');
257 * Validate values for this field.
258 * Both the Latitude and the Longitude fields need to be filled in.
260 * @param array $values The entered values for the lat. and long.
261 * @return string|bool Error message or false.
263 public function field_validation($values) {
265 // The lat long class has two values that need to be checked.
266 foreach ($values as $value) {
267 if (isset($value->value) && !($value->value == '')) {
271 // If we have nothing filled in or both filled in then everything is okay.
272 if ($valuecount == 0 || $valuecount == 2) {
275 // If we get here then only one field has been filled in.
276 return get_string('latlongboth', 'data');