[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/data/field/latlong/ -> field.class.php (source)

   1  <?php
   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  
  25  class data_field_latlong extends data_field_base {
  26      var $type = 'latlong';
  27  
  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.
  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  
  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"
  43      );
  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  
  46      function display_add_field($recordid=0) {
  47          global $CFG, $DB;
  48  
  49          $lat = '';
  50          $long = '';
  51          if ($recordid) {
  52              if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  53                  $lat  = $content->content;
  54                  $long = $content->content1;
  55              }
  56          }
  57          $str = '<div title="'.s($this->field->description).'">';
  58          $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
  59          $str .= '<table><tr><td align="right">';
  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>';
  62          $str .= '</table>';
  63          $str .= '</fieldset>';
  64          $str .= '</div>';
  65          return $str;
  66      }
  67  
  68      function display_search_field($value = '') {
  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  
  79          $options = array();
  80          foreach ($latlongsrs as $latlong) {
  81              $latitude = format_float($latlong->la, 4);
  82              $longitude = format_float($latlong->lo, 4);
  83              $options[$latlong->la . ',' . $latlong->lo] = $latitude . ' ' . $longitude;
  84          }
  85          $latlongsrs->close();
  86  
  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;
  90      }
  91  
  92      function parse_search_field() {
  93          return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
  94      }
  95  
  96      function generate_sql($tablealias, $value) {
  97          global $DB;
  98  
  99          static $i=0;
 100          $i++;
 101          $name1 = "df_latlong1_$i";
 102          $name2 = "df_latlong2_$i";
 103          $varcharlat = $DB->sql_compare_text("{$tablealias}.content");
 104          $varcharlong= $DB->sql_compare_text("{$tablealias}.content1");
 105  
 106  
 107          $latlong[0] = '';
 108          $latlong[1] = '';
 109          $latlong = explode (',', $value, 2);
 110          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharlat = :$name1 AND $varcharlong = :$name2) ",
 111                       array($name1=>$latlong[0], $name2=>$latlong[1]));
 112      }
 113  
 114      function display_browse_field($recordid, $template) {
 115          global $CFG, $DB;
 116          if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 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;
 124              }
 125              // We use format_float to display in the regional format.
 126              if($lat < 0) {
 127                  $compasslat = format_float(-$lat, 4) . '°S';
 128              } else {
 129                  $compasslat = format_float($lat, 4) . '°N';
 130              }
 131              if($long < 0) {
 132                  $compasslong = format_float(-$long, 4) . '°W';
 133              } else {
 134                  $compasslong = format_float($long, 4) . '°E';
 135              }
 136  
 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,
 150              );
 151  
 152              if(sizeof($servicesshown)==1 && $servicesshown[0]) {
 153                  $str = " <a href='"
 154                            . str_replace(array_keys($urlreplacements), array_values($urlreplacements), $this->linkoutservices[$servicesshown[0]])
 155                            ."' title='$servicesshown[0]'>$compasslat $compasslong</a>";
 156              } elseif (sizeof($servicesshown)>1) {
 157                  $str = '<form id="latlongfieldbrowse">';
 158                  $str .= "$compasslat, $compasslong\n";
 159                  $str .= "<label class='accesshide' for='jumpto'>". get_string('jumpto') ."</label>";
 160                  $str .= "<select id='jumpto' name='jumpto'>";
 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}'/>";
 170                  $str .= '</form>';
 171              } else {
 172                  $str = "$compasslat, $compasslong";
 173              }
 174  
 175              return $str;
 176          }
 177          return false;
 178      }
 179  
 180      function update_content($recordid, $value, $name='') {
 181          global $DB;
 182  
 183          $content = new stdClass();
 184          $content->fieldid = $this->field->id;
 185          $content->recordid = $recordid;
 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);
 189          $value = trim($value);
 190          if (strlen($value) > 0) {
 191              $value = floatval($value);
 192          } else {
 193              $value = null;
 194          }
 195          $names = explode('_', $name);
 196          switch ($names[2]) {
 197              case 0:
 198                  // update lat
 199                  $content->content = $value;
 200                  break;
 201              case 1:
 202                  // update long
 203                  $content->content1 = $value;
 204                  break;
 205              default:
 206                  break;
 207          }
 208          if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 209              $content->id = $oldcontent->id;
 210              return $DB->update_record('data_content', $content);
 211          } else {
 212              return $DB->insert_record('data_content', $content);
 213          }
 214      }
 215  
 216      function get_sort_sql($fieldname) {
 217          global $DB;
 218          return $DB->sql_cast_char2real($fieldname, true);
 219      }
 220  
 221      function export_text_value($record) {
 222          // The content here is from the database and does not require location formating.
 223          return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1);
 224      }
 225  
 226  }
 227  
 228  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1