MDL-31496 mod_data Replacing inline styles for CSS classes in templates and browse...
[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
EL
80 foreach ($latlongsrs as $latlong) {
81 $options[$latlong->la . ',' . $latlong->lo] = $latlong->la . ',' . $latlong->lo;
7900ecb0 82 }
eca89126
EL
83 $latlongsrs->close();
84
01f8e80d
RW
85 $return = html_writer::label(get_string('latlong', 'data'), 'menuf_'.$this->field->id, false, array('class' => 'accesshide'));
86 $return .= html_writer::select($options, 'f_'.$this->field->id, $value);
87 return $return;
7900ecb0 88 }
2858af42 89
7900ecb0 90 function parse_search_field() {
91 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
92 }
2858af42 93
7900ecb0 94 function generate_sql($tablealias, $value) {
be4d5a92
EL
95 global $DB;
96
e3487936 97 static $i=0;
98 $i++;
99 $name1 = "df_latlong1_$i";
100 $name2 = "df_latlong2_$i";
be4d5a92
EL
101 $varcharlat = $DB->sql_compare_text("{$tablealias}.content");
102 $varcharlong= $DB->sql_compare_text("{$tablealias}.content1");
103
e3487936 104
7900ecb0 105 $latlong[0] = '';
106 $latlong[1] = '';
107 $latlong = explode (',', $value, 2);
be4d5a92 108 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharlat = :$name1 AND $varcharlong = :$name2) ",
e3487936 109 array($name1=>$latlong[0], $name2=>$latlong[1]));
7900ecb0 110 }
8161ac98 111
112 function display_browse_field($recordid, $template) {
a656d951 113 global $CFG, $DB;
a656d951 114 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
30f54a9c 115 $lat = $content->content;
116 if (strlen($lat) < 1) {
117 return false;
118 }
119 $long = $content->content1;
120 if (strlen($long) < 1) {
121 return false;
8161ac98 122 }
2858af42 123 if($lat < 0) {
0721309f 124 $compasslat = sprintf('%01.4f', -$lat) . '°S';
2858af42 125 } else {
0721309f 126 $compasslat = sprintf('%01.4f', $lat) . '°N';
2858af42 127 }
128 if($long < 0) {
0721309f 129 $compasslong = sprintf('%01.4f', -$long) . '°W';
2858af42 130 } else {
0721309f 131 $compasslong = sprintf('%01.4f', $long) . '°E';
2858af42 132 }
aab98aaf 133
8161ac98 134 // Now let's create the jump-to-services link
135 $servicesshown = explode(',', $this->field->param1);
136
137 // These are the different things that can be magically inserted into URL schemes
138 $urlreplacements = array(
139 '@lat@'=> $lat,
140 '@long@'=> $long,
141 '@wwwroot@'=> $CFG->wwwroot,
142 '@contentid@'=> $content->id,
143 '@dataid@'=> $this->data->id,
144 '@courseid@'=> $this->data->course,
145 '@fieldid@'=> $content->fieldid,
146 '@recordid@'=> $content->recordid,
2858af42 147 );
8161ac98 148
149 if(sizeof($servicesshown)==1 && $servicesshown[0]) {
649ac85d 150 $str = " <a href='"
8161ac98 151 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicesshown[0]])
9c1ae6fc 152 ."' title='$servicesshown[0]'>$compasslat, $compasslong</a>";
8161ac98 153 } elseif (sizeof($servicesshown)>1) {
649ac85d 154 $str = '<form id="latlongfieldbrowse">';
01f8e80d
RW
155 $str .= "$compasslat, $compasslong\n";
156 $str .= "<label class='accesshide' for='jumpto'>". get_string('jumpto') ."</label>";
157 $str .= "<select id='jumpto' name='jumpto'>";
61240489 158 foreach($servicesshown as $servicename){
159 // Add a link to a service
160 $str .= "\n <option value='"
161 . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicename])
162 . "'>".htmlspecialchars($servicename)."</option>";
163 }
164 // NB! If you are editing this, make sure you don't break the javascript reference "previousSibling"
165 // which allows the "Go" button to refer to the drop-down selector.
166 $str .= "\n</select><input type='button' value='" . get_string('go') . "' onclick='if(previousSibling.value){self.location=previousSibling.value}'/>";
649ac85d 167 $str .= '</form>';
9c1ae6fc 168 } else {
649ac85d 169 $str = "$compasslat, $compasslong";
8161ac98 170 }
649ac85d 171
8161ac98 172 return $str;
173 }
174 return false;
175 }
176
177 function update_content($recordid, $value, $name='') {
a656d951 178 global $DB;
179
39790bd8 180 $content = new stdClass();
8161ac98 181 $content->fieldid = $this->field->id;
182 $content->recordid = $recordid;
30f54a9c 183 $value = trim($value);
184 if (strlen($value) > 0) {
185 $value = floatval($value);
186 } else {
187 $value = null;
188 }
8161ac98 189 $names = explode('_', $name);
2858af42 190 switch ($names[2]) {
191 case 0:
192 // update lat
30f54a9c 193 $content->content = $value;
8161ac98 194 break;
2858af42 195 case 1:
196 // update long
30f54a9c 197 $content->content1 = $value;
8161ac98 198 break;
199 default:
200 break;
201 }
a656d951 202 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
8161ac98 203 $content->id = $oldcontent->id;
a656d951 204 return $DB->update_record('data_content', $content);
8161ac98 205 } else {
a656d951 206 return $DB->insert_record('data_content', $content);
8161ac98 207 }
208 }
209
210 function get_sort_sql($fieldname) {
cbc08f3b 211 global $DB;
6e6878b5 212 return $DB->sql_cast_char2real($fieldname, true);
8161ac98 213 }
2858af42 214
215 function export_text_value($record) {
0721309f 216 return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1);
2858af42 217 }
218
8161ac98 219}
220
1adbd2c3 221