Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / data / field / latlong / field.class.php
blobc5ef25a62ea74cb9be4b79f5071700670bc2f323
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
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 data_field_latlong($field=0, $data=0) {
47 parent::data_field_base($field, $data);
50 function display_add_field($recordid=0) {
51 global $CFG;
52 $lat = '';
53 $long = '';
54 if ($recordid) {
55 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
56 $lat = $content->content;
57 $long = $content->content1;
60 $str = '<div title="'.s($this->field->description).'">';
61 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
62 $str .= '<table><tr><td align="right">';
63 $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>';
64 $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>';
65 $str .= '</table>';
66 $str .= '</fieldset>';
67 $str .= '</div>';
68 return $str;
71 function display_search_field($value = '') {
72 global $CFG;
73 $lats = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content');
74 $longs = get_records_sql_menu('SELECT id, content1 from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content');
75 $options = array();
76 if(!empty($lats) && !empty($longs)) {
77 $options[''] = '';
78 // Make first index blank.
79 foreach($lats as $key => $temp) {
80 $options[$temp.','.$longs[$key]] = $temp.','.$longs[$key];
83 return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true);
86 function parse_search_field() {
87 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
90 function generate_sql($tablealias, $value) {
91 $latlong[0] = '';
92 $latlong[1] = '';
93 $latlong = explode (',', $value, 2);
94 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$latlong[0]' AND {$tablealias}.content1 = '$latlong[1]') ";
97 function display_browse_field($recordid, $template) {
98 global $CFG;
99 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
100 $lat = $content->content;
101 if (strlen($lat) < 1) {
102 return false;
104 $long = $content->content1;
105 if (strlen($long) < 1) {
106 return false;
108 if($lat < 0) {
109 $compasslat = sprintf('%01.4f', -$lat) . '°S';
110 } else {
111 $compasslat = sprintf('%01.4f', $lat) . '°N';
113 if($long < 0) {
114 $compasslong = sprintf('%01.4f', -$long) . '°W';
115 } else {
116 $compasslong = sprintf('%01.4f', $long) . '°E';
118 $str = '<form style="display:inline;">';
120 // Now let's create the jump-to-services link
121 $servicesshown = explode(',', $this->field->param1);
123 // These are the different things that can be magically inserted into URL schemes
124 $urlreplacements = array(
125 '@lat@'=> $lat,
126 '@long@'=> $long,
127 '@wwwroot@'=> $CFG->wwwroot,
128 '@contentid@'=> $content->id,
129 '@dataid@'=> $this->data->id,
130 '@courseid@'=> $this->data->course,
131 '@fieldid@'=> $content->fieldid,
132 '@recordid@'=> $content->recordid,
135 if(sizeof($servicesshown)==1 && $servicesshown[0]) {
136 $str .= " <a href='"
137 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicesshown[0]])
138 ."' title='$servicesshown[0]'>$compasslat, $compasslong</a>";
139 } elseif (sizeof($servicesshown)>1) {
140 $str .= "$compasslat, $compasslong\n<select name='jumpto'>";
141 foreach($servicesshown as $servicename){
142 // Add a link to a service
143 $str .= "\n <option value='"
144 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicename])
145 . "'>".htmlspecialchars($servicename)."</option>";
147 // NB! If you are editing this, make sure you don't break the javascript reference "previousSibling"
148 // which allows the "Go" button to refer to the drop-down selector.
149 $str .= "\n</select><input type='button' value='" . get_string('go') . "' onclick='if(previousSibling.value){self.location=previousSibling.value}'/>";
150 } else {
151 $str.= "$compasslat, $compasslong";
153 $str.= '</form>';
154 return $str;
156 return false;
159 function update_content($recordid, $value, $name='') {
160 $content = new object;
161 $content->fieldid = $this->field->id;
162 $content->recordid = $recordid;
163 $value = trim($value);
164 if (strlen($value) > 0) {
165 $value = floatval($value);
166 } else {
167 $value = null;
169 $names = explode('_', $name);
170 switch ($names[2]) {
171 case 0:
172 // update lat
173 $content->content = $value;
174 break;
175 case 1:
176 // update long
177 $content->content1 = $value;
178 break;
179 default:
180 break;
182 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
183 $content->id = $oldcontent->id;
184 return update_record('data_content', $content);
185 } else {
186 return insert_record('data_content', $content);
190 function get_sort_sql($fieldname) {
191 global $CFG;
192 switch ($CFG->dbfamily) {
193 case 'mysql':
194 // string in an arithmetic operation is converted to a floating-point number
195 return '('.$fieldname.'+0.0)';
196 case 'postgres':
197 //cast is for PG
198 return 'CAST('.$fieldname.' AS REAL)';
199 default:
200 //Return just the fieldname. TODO: Look behaviour under MSSQL and Oracle
201 return $fieldname;
205 function export_text_value($record) {
206 return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1);