Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/field/latlong/field.class.php
Go to the documentation of this file.
00001 <?php
00003 //                                                                       //
00004 // NOTICE OF COPYRIGHT                                                   //
00005 //                                                                       //
00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00007 //          http://moodle.org                                            //
00008 //                                                                       //
00009 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
00010 //                                                                       //
00011 // This program is free software; you can redistribute it and/or modify  //
00012 // it under the terms of the GNU General Public License as published by  //
00013 // the Free Software Foundation; either version 2 of the License, or     //
00014 // (at your option) any later version.                                   //
00015 //                                                                       //
00016 // This program is distributed in the hope that it will be useful,       //
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00019 // GNU General Public License for more details:                          //
00020 //                                                                       //
00021 //          http://www.gnu.org/copyleft/gpl.html                         //
00022 //                                                                       //
00024 
00025 class data_field_latlong extends data_field_base {
00026     var $type = 'latlong';
00027 
00028     // This is an array of URL schemes for linking out to services, using the float values of lat and long.
00029     // In each scheme, the special markers @lat@ and @long@ will be replaced by the float values.
00030     // The config options for the field store each service name that should be displayed, in a comma-separated
00031     // field. Therefore please DO NOT include commas in the service names if you are adding extra services.
00032 
00033     // Parameter data used:
00034     // "param1" is a comma-separated list of the linkout service names that are enabled for this instance
00035     // "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.
00036 
00037     var $linkoutservices = array(
00038           "Google Maps" => "http://maps.google.com/maps?q=@lat@,+@long@&iwloc=A&hl=en",
00039           "Google Earth" => "@wwwroot@/mod/data/field/latlong/kml.php?d=@dataid@&fieldid=@fieldid@&rid=@recordid@",
00040           "Geabios" => "http://www.geabios.com/html/services/maps/PublicMap.htm?lat=@lat@&lon=@long@&fov=0.3&title=Moodle%20data%20item",
00041           "OpenStreetMap" => "http://www.openstreetmap.org/index.html?lat=@lat@&lon=@long@&zoom=11",
00042           "Multimap" => "http://www.multimap.com/map/browse.cgi?scale=200000&lon=@long@&lat=@lat@&icon=x"
00043     );
00044     // Other map sources listed at http://kvaleberg.com/extensions/mapsources/index.php?params=51_30.4167_N_0_7.65_W_region:earth
00045 
00046     function display_add_field($recordid=0) {
00047         global $CFG, $DB;
00048 
00049         $lat = '';
00050         $long = '';
00051         if ($recordid) {
00052             if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00053                 $lat  = $content->content;
00054                 $long = $content->content1;
00055             }
00056         }
00057         $str = '<div title="'.s($this->field->description).'">';
00058         $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
00059         $str .= '<table><tr><td align="right">';
00060         $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>';
00061         $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>';
00062         $str .= '</table>';
00063         $str .= '</fieldset>';
00064         $str .= '</div>';
00065         return $str;
00066     }
00067 
00068     function display_search_field($value = '') {
00069         global $CFG, $DB;
00070 
00071         $varcharlat = $DB->sql_compare_text('content');
00072         $varcharlong= $DB->sql_compare_text('content1');
00073         $latlongsrs = $DB->get_recordset_sql(
00074             "SELECT DISTINCT $varcharlat AS la, $varcharlong AS lo
00075                FROM {data_content}
00076               WHERE fieldid = ?
00077              ORDER BY $varcharlat, $varcharlong", array($this->field->id));
00078 
00079         $options = array();
00080         foreach ($latlongsrs as $latlong) {
00081             $options[$latlong->la . ',' . $latlong->lo] = $latlong->la . ',' . $latlong->lo;
00082         }
00083         $latlongsrs->close();
00084 
00085        return html_writer::select($options, 'f_'.$this->field->id, $value);
00086     }
00087 
00088     function parse_search_field() {
00089         return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
00090     }
00091 
00092     function generate_sql($tablealias, $value) {
00093         global $DB;
00094 
00095         static $i=0;
00096         $i++;
00097         $name1 = "df_latlong1_$i";
00098         $name2 = "df_latlong2_$i";
00099         $varcharlat = $DB->sql_compare_text("{$tablealias}.content");
00100         $varcharlong= $DB->sql_compare_text("{$tablealias}.content1");
00101 
00102 
00103         $latlong[0] = '';
00104         $latlong[1] = '';
00105         $latlong = explode (',', $value, 2);
00106         return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharlat = :$name1 AND $varcharlong = :$name2) ",
00107                      array($name1=>$latlong[0], $name2=>$latlong[1]));
00108     }
00109 
00110     function display_browse_field($recordid, $template) {
00111         global $CFG, $DB;
00112         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00113             $lat = $content->content;
00114             if (strlen($lat) < 1) {
00115                 return false;
00116             }
00117             $long = $content->content1;
00118             if (strlen($long) < 1) {
00119                 return false;
00120             }
00121             if($lat < 0) {
00122                 $compasslat = sprintf('%01.4f', -$lat) . '°S';
00123             } else {
00124                 $compasslat = sprintf('%01.4f', $lat) . '°N';
00125             }
00126             if($long < 0) {
00127                 $compasslong = sprintf('%01.4f', -$long) . '°W';
00128             } else {
00129                 $compasslong = sprintf('%01.4f', $long) . '°E';
00130             }
00131             $str = '<form style="display:inline;">';
00132 
00133             // Now let's create the jump-to-services link
00134             $servicesshown = explode(',', $this->field->param1);
00135 
00136             // These are the different things that can be magically inserted into URL schemes
00137             $urlreplacements = array(
00138                 '@lat@'=> $lat,
00139                 '@long@'=> $long,
00140                 '@wwwroot@'=> $CFG->wwwroot,
00141                 '@contentid@'=> $content->id,
00142                 '@dataid@'=> $this->data->id,
00143                 '@courseid@'=> $this->data->course,
00144                 '@fieldid@'=> $content->fieldid,
00145                 '@recordid@'=> $content->recordid,
00146             );
00147 
00148             if(sizeof($servicesshown)==1 && $servicesshown[0]) {
00149                 $str .= " <a href='"
00150                           . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicesshown[0]])
00151                           ."' title='$servicesshown[0]'>$compasslat, $compasslong</a>";
00152             } elseif (sizeof($servicesshown)>1) {
00153                 $str .= "$compasslat, $compasslong\n<select name='jumpto'>";
00154                 foreach($servicesshown as $servicename){
00155                     // Add a link to a service
00156                     $str .= "\n  <option value='"
00157                                . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicename])
00158                                . "'>".htmlspecialchars($servicename)."</option>";
00159                 }
00160                 // NB! If you are editing this, make sure you don't break the javascript reference "previousSibling"
00161                 //   which allows the "Go" button to refer to the drop-down selector.
00162                 $str .= "\n</select><input type='button' value='" . get_string('go') . "' onclick='if(previousSibling.value){self.location=previousSibling.value}'/>";
00163             } else {
00164                 $str.= "$compasslat, $compasslong";
00165             }
00166             $str.= '</form>';
00167             return $str;
00168         }
00169         return false;
00170     }
00171 
00172     function update_content($recordid, $value, $name='') {
00173         global $DB;
00174 
00175         $content = new stdClass();
00176         $content->fieldid = $this->field->id;
00177         $content->recordid = $recordid;
00178         $value = trim($value);
00179         if (strlen($value) > 0) {
00180             $value = floatval($value);
00181         } else {
00182             $value = null;
00183         }
00184         $names = explode('_', $name);
00185         switch ($names[2]) {
00186             case 0:
00187                 // update lat
00188                 $content->content = $value;
00189                 break;
00190             case 1:
00191                 // update long
00192                 $content->content1 = $value;
00193                 break;
00194             default:
00195                 break;
00196         }
00197         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00198             $content->id = $oldcontent->id;
00199             return $DB->update_record('data_content', $content);
00200         } else {
00201             return $DB->insert_record('data_content', $content);
00202         }
00203     }
00204 
00205     function get_sort_sql($fieldname) {
00206         global $DB;
00207         return $DB->sql_cast_char2real($fieldname, true);
00208     }
00209 
00210     function export_text_value($record) {
00211         return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1);
00212     }
00213 
00214 }
00215 
00216 
 All Data Structures Namespaces Files Functions Variables Enumerations