[ Index ]

PHP Cross Reference of Unnamed Project

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, $formdata = null) {
  47          global $CFG, $DB, $OUTPUT;
  48  
  49          $lat = '';
  50          $long = '';
  51          if ($formdata) {
  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;
  60              }
  61          }
  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')));
  69          }
  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')));
  77          }
  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>';
  80          $str .= '</tr>';
  81          $str .= '</table>';
  82          $str .= '</fieldset>';
  83          $str .= '</div>';
  84          return $str;
  85      }
  86  
  87      function display_search_field($value = '') {
  88          global $CFG, $DB;
  89  
  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
  94                 FROM {data_content}
  95                WHERE fieldid = ?
  96               ORDER BY $varcharlat, $varcharlong", array($this->field->id));
  97  
  98          $options = array();
  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;
 103          }
 104          $latlongsrs->close();
 105  
 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);
 108         return $return;
 109      }
 110  
 111      function parse_search_field() {
 112          return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
 113      }
 114  
 115      function generate_sql($tablealias, $value) {
 116          global $DB;
 117  
 118          static $i=0;
 119          $i++;
 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");
 124  
 125  
 126          $latlong[0] = '';
 127          $latlong[1] = '';
 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]));
 131      }
 132  
 133      function display_browse_field($recordid, $template) {
 134          global $CFG, $DB;
 135          if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 136              $lat = $content->content;
 137              if (strlen($lat) < 1) {
 138                  return false;
 139              }
 140              $long = $content->content1;
 141              if (strlen($long) < 1) {
 142                  return false;
 143              }
 144              // We use format_float to display in the regional format.
 145              if($lat < 0) {
 146                  $compasslat = format_float(-$lat, 4) . '°S';
 147              } else {
 148                  $compasslat = format_float($lat, 4) . '°N';
 149              }
 150              if($long < 0) {
 151                  $compasslong = format_float(-$long, 4) . '°W';
 152              } else {
 153                  $compasslong = format_float($long, 4) . '°E';
 154              }
 155  
 156              // Now let's create the jump-to-services link
 157              $servicesshown = explode(',', $this->field->param1);
 158  
 159              // These are the different things that can be magically inserted into URL schemes
 160              $urlreplacements = array(
 161                  '@lat@'=> $lat,
 162                  '@long@'=> $long,
 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,
 169              );
 170  
 171              if(sizeof($servicesshown)==1 && $servicesshown[0]) {
 172                  $str = " <a href='"
 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>";
 185                  }
 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}'/>";
 189                  $str .= '</form>';
 190              } else {
 191                  $str = "$compasslat, $compasslong";
 192              }
 193  
 194              return $str;
 195          }
 196          return false;
 197      }
 198  
 199      function update_content($recordid, $value, $name='') {
 200          global $DB;
 201  
 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);
 211          } else {
 212              $value = null;
 213          }
 214          $names = explode('_', $name);
 215          switch ($names[2]) {
 216              case 0:
 217                  // update lat
 218                  $content->content = $value;
 219                  break;
 220              case 1:
 221                  // update long
 222                  $content->content1 = $value;
 223                  break;
 224              default:
 225                  break;
 226          }
 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);
 230          } else {
 231              return $DB->insert_record('data_content', $content);
 232          }
 233      }
 234  
 235      function get_sort_sql($fieldname) {
 236          global $DB;
 237          return $DB->sql_cast_char2real($fieldname, true);
 238      }
 239  
 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);
 243      }
 244  
 245      /**
 246       * Check if a field from an add form is empty
 247       *
 248       * @param mixed $value
 249       * @param mixed $name
 250       * @return bool
 251       */
 252      function notemptyfield($value, $name) {
 253          return isset($value) && !($value == '');
 254      }
 255  
 256      /**
 257       * Validate values for this field.
 258       * Both the Latitude and the Longitude fields need to be filled in.
 259       *
 260       * @param array $values The entered values for the lat. and long.
 261       * @return string|bool Error message or false.
 262       */
 263      public function field_validation($values) {
 264          $valuecount = 0;
 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 == '')) {
 268                  $valuecount++;
 269              }
 270          }
 271          // If we have nothing filled in or both filled in then everything is okay.
 272          if ($valuecount == 0 || $valuecount == 2) {
 273              return false;
 274          }
 275          // If we get here then only one field has been filled in.
 276          return get_string('latlongboth', 'data');
 277      }
 278  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1