Skip to content

Commit

Permalink
New Feature: Ability to add Google Maps as a question
Browse files Browse the repository at this point in the history
Dev Extension of the Short Question Type to allow Google Maps questions to be displayed. Users can drag and drop the marker, and the Latitude Longitude and other important info are stored.

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev@9610 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
Kshitij Parajulik committed Dec 9, 2010
1 parent 356e602 commit ac44785
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 10 deletions.
6 changes: 5 additions & 1 deletion admin/globalsettings.php
Expand Up @@ -92,6 +92,7 @@ function globalsettingssave()
$sessionlifetimetemp=(int)($_POST['sessionlifetime']);
if ($sessionlifetimetemp==0) $sessionlifetimetemp=3600;
setGlobalSetting('sessionlifetime',$sessionlifetimetemp);
setGlobalSetting('ipInfoDbAPIKey',$_POST['ipInfoDbAPIKey']);
setGlobalSetting('force_ssl',$_POST['force_ssl']);
setGlobalSetting('surveyPreview_require_Auth',strip_tags($_POST['surveyPreview_require_Auth']));
$savetime=trim(strip_tags((float) $_POST['timeadjust']).' hours'); //makes sure it is a number, at least 0
Expand Down Expand Up @@ -254,7 +255,10 @@ function globalsettingsdisplay()
$editsurvey .= ">".$clang->gT("Off")."</option>\n"
. "</select>\n</li>\n"
. "<li><label for='sessionlifetime'>".$clang->gT("Session lifetime (seconds):")."</label>\n"
. "<input type='text' size='10' id='sessionlifetime' name='sessionlifetime' value=\"".htmlspecialchars(getGlobalSetting('sessionlifetime'))."\" /></li>";
. "<input type='text' size='10' id='sessionlifetime' name='sessionlifetime' value=\"".htmlspecialchars(getGlobalSetting('sessionlifetime'))."\" /></li>"
. "<li><label for='ipInfoDbAPIKey'>".$clang->gT("IP Info DB API Key:")."</label>\n"
. "<input type='text' size='35' id='ipInfoDbAPIKey' name='ipInfoDbAPIKey' value=\"".htmlspecialchars(getGlobalSetting('ipInfoDbAPIKey'))."\" /></li>";


// End General TAB

Expand Down
118 changes: 115 additions & 3 deletions common_functions.php
Expand Up @@ -23,6 +23,7 @@
* If you want to generally add a new permission just add it here.
*
*/

function aGetBaseSurveyPermissions()
{
global $clang;
Expand Down Expand Up @@ -144,6 +145,11 @@ function getqtypelist($SelectedCode = "T", $ReturnType = "selector")
'subquestions'=>0,
'assessable'=>0,
'answerscales'=>0),
"J"=>array('description'=>$clang->gT("Location"),
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"K"=>array('description'=>$clang->gT("Multiple Numerical Input"),
'hasdefaultvalues'=>0,
'subquestions'=>1,
Expand Down Expand Up @@ -1514,6 +1520,7 @@ function fixsortorderAnswers($qid) //Function rewrites the sortorder for a group
global $dbprefix, $connect, $surveyid;
$qid=sanitize_int($qid);
$baselang = GetBaseLanguageFromSurveyID($surveyid);

$cdresult = db_execute_num("SELECT qid, code, sortorder FROM ".db_table_name('answers')." WHERE qid={$qid} and language='{$baselang}' ORDER BY sortorder"); //Checked
$position=0;
while ($cdrow=$cdresult->FetchRow())
Expand Down Expand Up @@ -2302,7 +2309,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi

// Types "L", "!" , "O", "D", "G", "N", "X", "Y", "5","S","T","U"

if ($qtypes[$arow['type']]['subquestions']==0 && $arow['type'] != "R" && $arow['type'] != "|")
if ($qtypes[$arow['type']]['subquestions']==0 && $arow['type'] != "R" && $arow['type'] != "|")
{
$fieldname="{$arow['sid']}X{$arow['gid']}X{$arow['qid']}";
$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>"{$arow['type']}", 'sid'=>$surveyid, "gid"=>$arow['gid'], "qid"=>$arow['qid'], "aid"=>"");
Expand Down Expand Up @@ -2598,7 +2605,6 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
}
}
}

if (isset($fieldmap)) {
$globalfieldmap[$surveyid][$style][$clang->langcode] = $fieldmap;
return $fieldmap;
Expand Down Expand Up @@ -3700,6 +3706,109 @@ function questionAttributes($returnByName=false)
"help"=>$clang->gT('Excludes all other options if a certain answer is selected - just enter the answer code(s) seperated with a semikolon.'),
"caption"=>$clang->gT('Exclusive option'));

// Map Options

$qattributes["location_city"]=array(
"types"=>"S",
'readonly_when_active'=>true,
'category'=>$clang->gT('Location'),
'sortorder'=>100,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('Yes'),
1=>$clang->gT('No')),
"help"=>$clang->gT("Store the city of the user?"),
"caption"=>$clang->gT("City"));

$qattributes["location_state"]=array(
"types"=>"S",
'readonly_when_active'=>true,
'category'=>$clang->gT('Location'),
'sortorder'=>100,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('Yes'),
1=>$clang->gT('No')),
"help"=>$clang->gT("Store the state of the user?"),
"caption"=>$clang->gT("State"));

$qattributes["location_postal"]=array(
"types"=>"S",
'readonly_when_active'=>true,
'category'=>$clang->gT('Location'),
'sortorder'=>100,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('Yes'),
1=>$clang->gT('No')),
"help"=>$clang->gT("Store the postal code of the user?"),
"caption"=>$clang->gT("Postal Code"));

$qattributes["location_country"]=array(
"types"=>"S",
'readonly_when_active'=>true,
'category'=>$clang->gT('Location'),
'sortorder'=>100,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('Yes'),
1=>$clang->gT('No')),
"help"=>$clang->gT("Store the country of the user?"),
"caption"=>$clang->gT("Country"));

$qattributes["location_mapservice"]=array(
"types"=>"S",
'category'=>$clang->gT('Location'),
'sortorder'=>90,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('Off'),
1=>$clang->gT('Google Maps')),
"help"=>$clang->gT("Which mapping service to use?"),
"caption"=>$clang->gT("Mapping Service"));

$qattributes["location_mapwidth"]=array(
"types"=>"S",
'category'=>$clang->gT('Location'),
'sortorder'=>102,
'inputtype'=>'text',
'default'=>'500',
"help"=>$clang->gT("Width of the Map Holder"),
"caption"=>$clang->gT("Width"));

$qattributes["location_mapheight"]=array(
"types"=>"S",
'category'=>$clang->gT('Location'),
'sortorder'=>103,
'inputtype'=>'text',
'default'=>'300',
"help"=>$clang->gT("Height of the Map Holder"),
"caption"=>$clang->gT("Height"));

$qattributes["location_nodefaultfromip"]=array(
"types"=>"S",
'category'=>$clang->gT('Location'),
'sortorder'=>91,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('Yes'),
1=>$clang->gT('No')),
"help"=>$clang->gT("Get the default location using the user's IP address?"),
"caption"=>$clang->gT("IP as default location"));

$qattributes["location_defaultcoordinates"]=array(
"types"=>"S",
'category'=>$clang->gT('Location'),
'sortorder'=>101,
'inputtype'=>'text',
"help"=>$clang->gT('Default coordinates of the map when the page first loads. Format: latitude [space] longtitude'),
"caption"=>$clang->gT('Default Position'));

$qattributes["location_mapzoom"]=array(
"types"=>"S",
'category'=>$clang->gT('Location'),
'sortorder'=>101,
'inputtype'=>'text',
'default'=>'11',
"help"=>$clang->gT("Maps Zoom Level"),
"caption"=>$clang->gT("Zoom"));

// End Map Options

$qattributes["hide_tip"]=array(
"types"=>"!KLMNOPRWZ",
'category'=>$clang->gT('Display'),
Expand Down Expand Up @@ -4429,7 +4538,10 @@ function getHeader($meta = false)
$js_header = ''; $css_header='';
foreach ($js_header_includes as $jsinclude)
{
$js_header .= "<script type=\"text/javascript\" src=\"".$rooturl."$jsinclude\"></script>\n";
if (substr($jsinclude,0,4) == 'http')
$js_header .= "<script type=\"text/javascript\" src=\"$jsinclude\"></script>\n";
else
$js_header .= "<script type=\"text/javascript\" src=\"".$rooturl."$jsinclude\"></script>\n";
}

foreach ($css_header_includes as $cssinclude)
Expand Down
5 changes: 3 additions & 2 deletions config-defaults.php
Expand Up @@ -493,7 +493,10 @@
$ssl_emergency_override = false;


// Get your IP Info DB key from http://ipinfodb.com/
// If you have the API key, you can use it to get the approximate location of the user initially.

$ipInfoDbAPIKey = '';


//DO NOT EVER CHANGE THE FOLLOWING 5 LINES ---------------
Expand Down Expand Up @@ -553,5 +556,3 @@
{
// commandline installation, no relativeurl needed
}


91 changes: 88 additions & 3 deletions qanda.php
Expand Up @@ -56,7 +56,6 @@
{
define('SHOW_NO_ANSWER',0);
};

function retrieveConditionInfo($ia)
{
//This function returns an array containing all related conditions
Expand Down Expand Up @@ -774,7 +773,6 @@ function retrieveAnswers($ia, $notanswered=null, $notvalidated=null, $filenotval
case '1': //Array (Flexible Labels) dual scale
$values=do_array_dual($ia);
break;

} //End Switch

if (isset($values)) //Break apart $values array returned from switch
Expand Down Expand Up @@ -4125,7 +4123,6 @@ function textLimit(field, maxlen) {
}



// ---------------------------------------------------------------
function do_multiplenumeric($ia)
{
Expand Down Expand Up @@ -4825,6 +4822,80 @@ function textLimit(field, maxlen) {
if ($_SESSION[$ia[1]]) {$answer .= str_replace("\\", "", $_SESSION[$ia[1]]);}

$answer .= "</textarea>\n";
}
elseif((int)($qidattributes['location_mapservice'])!=0){

$mapservice = $qidattributes['location_mapservice'];
$currentLocation = $_SESSION[$ia[1]];
$currentLatLong = null;

$floatLat = 0;
$floatLng = 0;

// Get the latitude/longtitude for the point that needs to be displayed by default
if (strlen($currentLocation) > 2){
$currentLatLong = explode(';',$currentLocation);
$currentLatLong = array($currentLatLong[0],$currentLatLong[1]);
}
else{
if ((int)($qidattributes['location_nodefaultfromip'])==0)
$currentLatLong = getLatLongFromIp($_SERVER['REMOTE_ADDR']);
if (!isset($currentLatLong) || $currentLatLong==false){
$floatLat = 0;
$floatLng = 0;
$LatLong = explode(" ",trim($qidattributes['location_defaultcoordinates']));

if (isset($LatLong[0]) && isset($LatLong[1])){
$floatLat = $LatLong[0];
$floatLng = $LatLong[1];
}

$currentLatLong = array($floatLat,$floatLng);
}
}
// 2 - city; 3 - state; 4 - country; 5 - postal
$strBuild = "";
if ($qidattributes['location_city'])
$strBuild .= "2";
if ($qidattributes['location_state'])
$strBuild .= "3";
if ($qidattributes['location_country'])
$strBuild .= "4";
if ($qidattributes['location_postal'])
$strBuild .= "5";

$currentLocation = $currentLatLong[0] . " " . $currentLatLong[1];
$answer = "
<script type=\"text/javascript\">
zoom['$ia[1]'] = {$qidattributes['location_mapzoom']};
</script>
<p class=\"question\">
<input type=\"hidden\" name=\"$ia[1]\" id=\"answer$ia[1]\" value=\"{$_SESSION[$ia[1]]}\">
<input class=\"text location\" type=\"text\" size=\"20\" name=\"$ia[1]_c\"
id=\"answer$ia[1]_c\" value=\"$currentLocation\"
onkeyup=\"$checkconditionFunction(this.value, this.name, this.type)\" />
</p>
<input type=\"hidden\" name=\"boycott_$ia[1]\" id=\"boycott_$ia[1]\"
value = \"{$strBuild}\" >
<input type=\"hidden\" name=\"mapservice_$ia[1]\" id=\"mapservice_$ia[1]\"
class=\"mapservice\" value = \"{$qidattributes['location_mapservice']}\" >
<div id=\"gmap_canvas_$ia[1]_c\" style=\"width: {$qidattributes['location_mapwidth']}px; height: {$qidattributes['location_mapheight']}px\"></div>";

if ($qidattributes['location_mapservice']==1)
$js_header_includes[] = "http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA";
elseif ($qidattributes['location_mapservice']==2)
$js_header_includes[] = "http://www.openlayers.org/api/OpenLayers.js";

if (isset($qidattributes['hide_tip']) && $qidattributes['hide_tip']==0)
{
$answer .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Drag and drop the pin to the desired location. You may also right click on the map to move the pin.').'</span>';
$question_text['help'] = $clang->gT('Drag and drop the pin to the desired location. You may also right click on the map to move the pin.');
}


}
else
{
Expand All @@ -4833,6 +4904,8 @@ function textLimit(field, maxlen) {
.htmlspecialchars($_SESSION[$ia[1]],ENT_QUOTES,'UTF-8')
."\" maxlength=\"$maxsize\" onkeyup=\"$checkconditionFunction(this.value, this.name, this.type)\" $numbersonly />\n\t$suffix\n</p>\n";
}


if (trim($qidattributes['time_limit'])!='')
{
$js_header_includes[] = '/scripts/coookies.js';
Expand All @@ -4844,6 +4917,18 @@ function textLimit(field, maxlen) {

}

function getLatLongFromIp($ip){
global $ipInfoDbAPIKey;
$xml = simplexml_load_file("http://api.ipinfodb.com/v2/ip_query.php?key=$ipInfoDbAPIKey&timezone=false");
if ($xml->{'Status'} == "OK"){
$lat = (float)$xml->{'Latitude'};
$lng = (float)$xml->{'Longitude'};

return(array($lat,$lng));
}
else
return false;
}



Expand Down

0 comments on commit ac44785

Please sign in to comment.