Skip to content

Commit

Permalink
adds error checking to responses and prompts with alternate text the …
Browse files Browse the repository at this point in the history
…second time. Beginning to add counties as options
  • Loading branch information
aaronpk committed Dec 5, 2010
1 parent ca95fd7 commit 07f81b3
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 57 deletions.
66 changes: 63 additions & 3 deletions include/include.php
@@ -1,7 +1,7 @@
<?php
include('config.php');

function askQuestion($question)
function askQuestion($question, $secondTry=FALSE)
{
global $tropo, $voice;

Expand Down Expand Up @@ -35,12 +35,13 @@ function askQuestion($question)
$tropo[] = array(
'record' => array(
'say' => array(
'value' => $question['prompt'],
'value' => $question[($secondTry && array_key_exists('prompt2', $question) ? 'prompt2' : 'prompt')],
),
'voice' => $voice,
'name' => $question['key'],
'maxSilence' => 2, // if they stop talking for 2 seconds it will end recording and move on
'beep' => FALSE,
'format' => 'audio/mp3',
'url' => 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . $question['key'] . '.json?record=1&session_id=' . session_id()
)
);
Expand All @@ -50,7 +51,7 @@ function askQuestion($question)
$tropo[] = array(
'record' => array(
'say' => array(
'value' => $question['prompt'],
'value' => $question[($secondTry && array_key_exists('prompt2', $question) ? 'prompt2' : 'prompt')],
),
'voice' => $voice,
'bargein' => FALSE,
Expand All @@ -60,12 +61,67 @@ function askQuestion($question)
'value' => $choices
),
'beep' => FALSE,
'format' => 'audio/mp3',
'url' => 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . $question['key'] . '.json?record=1&session_id=' . session_id()
)
);
}
}

function storeSurveyResponse($key, $value)
{
$_SESSION['responses'][$key] = $value;

// Attempt to update the record in the DB
$query = db()->prepare('UPDATE `responses` SET `value` = :value WHERE `callID` = :callID AND `key` = :key');
$query->bindValue(':callID', $_SESSION['callID']);
$query->bindValue(':key', $key);
$query->bindValue(':value', $value);
$query->execute();

// If no rows were updated, that means there wasn't already a row for this key, so insert it now
if($query->rowCount() == 0)
{
$query = db()->prepare('INSERT INTO `responses` (`callID`, `key`, `value`) VALUES(:callID, :key, :value)');
$query->bindValue(':callID', $_SESSION['callID']);
$query->bindValue(':key', $key);
$query->bindValue(':value', $value);
$query->execute();
}
}

function getCountiesForZipcode($zip)
{
switch($zip)
{
case 98683:
return array(53011=>'Clark');
case 98112:
return array(53033=>'King');
case 98111:
return array(53033=>'King', 53053=>'Pierce');
default:
return array(53033=>'King');
}
}

function getCitiesForZipcode($zip)
{
switch($zip)
{
case 98683:
return array('Vancouver');
case 98112:
return array('Seattle');
case 98111:
return array('Seattle', 'Bellevue');
default:
return array('Seattle');
}
}



function tropoInput()
{
if(get('test') == 1)
Expand Down Expand Up @@ -146,6 +202,10 @@ function post($k)

function surveyVal($k)
{
// If we're not in the middle of a survey, respond with TRUE so the survey.php file runs all blocks
if(defined('VIEW_MODE'))
return TRUE;

if(array_key_exists('responses', $_SESSION) && array_key_exists($k, $_SESSION['responses']))
return $_SESSION['responses'][$k];
else
Expand Down
78 changes: 47 additions & 31 deletions include/question.php
Expand Up @@ -2,71 +2,71 @@

$key = get('method');

ircdebug($key);

// Handle the successful response of the question

if(get('record') || get('hadRecording') || ($val=tropoInputValue()) !== FALSE)
if(get('record') || get('hadRecording') || (($responseValue = tropoInputValue()) !== FALSE))
{
if(get('record') == 1)
{
// Handle uploaded recordings
$filename = md5(microtime(TRUE)) . '.wav';
$fullPath = RECORDING_PATH . $filename;
move_uploaded_file($_FILES['filename']['tmp_name'], $filename);
$filename = md5(microtime(TRUE)) . '.mp3';
$fullpath = RECORDING_PATH . $filename;
move_uploaded_file($_FILES['filename']['tmp_name'], $fullpath);

$_SESSION['recordings'][$key] = $filename;

// Attempt to update the record in the DB
$query = db()->prepare('UPDATE `responses` SET `recording` = :recording WHERE `callID` = :callID AND `name` = :name');
$query = db()->prepare('UPDATE `responses` SET `recording` = :recording WHERE `callID` = :callID AND `key` = :key');
$query->bindValue(':callID', $_SESSION['callID']);
$query->bindValue(':name', $key);
$query->bindValue(':key', $key);
$query->bindValue(':recording', $filename);
$query->execute();

// If no rows were updated, that means there wasn't already a row for this key, so insert it now
if($query->rowCount() == 0)
{
$query = db()->prepare('INSERT INTO `responses` (`callID`, `name`, `recording`) VALUES(:callID, :name, :recording)');
$query = db()->prepare('INSERT INTO `responses` (`callID`, `key`, `recording`) VALUES(:callID, :key, :recording)');
$query->bindValue(':callID', $_SESSION['callID']);
$query->bindValue(':name', $key);
$query->bindValue(':key', $key);
$query->bindValue(':recording', $filename);
$query->execute();
}

die();
}
elseif(isset($val))
elseif(isset($responseValue))
{
$_SESSION['responses'][$key] = $val;

// Attempt to update the record in the DB
$query = db()->prepare('UPDATE `responses` SET `value` = :value WHERE `callID` = :callID AND `name` = :name');
$query->bindValue(':callID', $_SESSION['callID']);
$query->bindValue(':name', $key);
$query->bindValue(':value', $val);
$query->execute();

// If no rows were updated, that means there wasn't already a row for this key, so insert it now
if($query->rowCount() == 0)
{
$query = db()->prepare('INSERT INTO `responses` (`callID`, `name`, `value`) VALUES(:callID, :name, :value)');
$query->bindValue(':callID', $_SESSION['callID']);
$query->bindValue(':name', $key);
$query->bindValue(':value', $val);
$query->execute();
}
storeSurveyResponse($key, $responseValue);
}

// After processing the input, include the questions file so logic can happen in it based on the session values set above
include('survey.php');

// Find the next question and ask it
foreach($questions as $i=>$q)
{
if($q['key'] == $key)
{
$thisQuestion = $q;
if(array_key_exists($i+1, $questions))
askQuestion($questions[$i+1]);
$nextQuestion = $questions[$i+1];
else
complete();
$nextQuestion = FALSE;
}
}

// If we got a valid response from the user, continue, otherwise ask them the same question again
if(get('hadRecording') || isset($responseValue))
{
if($nextQuestion)
askQuestion($nextQuestion);
else
complete();
}
else
{
askQuestion($thisQuestion, TRUE);
}
}
else
{
Expand All @@ -78,6 +78,17 @@



function getQuestionName($key)
{
global $questions;

foreach($questions as $q)
if($q['key'] == $key)
return $q['name'];
else
return FALSE;
}


function complete()
{
Expand All @@ -87,6 +98,11 @@ function complete()
'say' => array('value'=>'The survey has been completed. Thanks.'),
'voice' => $voice
);

$query = db()->prepare('UPDATE `calls` SET `dateFinished` = :date WHERE `sessionID` :id');
$query->bindParam(':id', session_id());
$query->bindParam(':date', date('Y-m-d H:i:s'));
$query->execute();
}

?>
6 changes: 6 additions & 0 deletions index.php
Expand Up @@ -31,7 +31,12 @@

switch(get('method'))
{
case '':
include('view.php');
die();
break;
case 'incoming':
define('SURVEY_MODE', TRUE);
include('survey.php');

$query = db()->prepare('INSERT INTO `calls` (`date`, `callerID`, `sessionID`) VALUES(:date, :callerID, :sessionID)');
Expand All @@ -52,6 +57,7 @@

break;
default:
define('SURVEY_MODE', TRUE);
include('include/question.php');
break;
}
Expand Down
93 changes: 76 additions & 17 deletions survey.php
@@ -1,54 +1,113 @@
<?php

$firstPrompt = 'Thanks for calling the Mobile Assessment of Damage hotline.';
/*
$questions[] = array(
'key' => 'name',
'name' => 'Name',
'prompt' => 'What is your full name?',
'prompt2' => didntUnderstand() . 'What is your name?',
'choices' => '[RECORD]'
);
*/

$questions[] = array(
'key' => 'postcode',
'name' => 'Zip Code',
'prompt' => 'What is the zip code of the property?',
'prompt2' => didntUnderstand() . 'Speak each digit of the zip code slowly.',
'choices' => '[5 DIGITS]'
);

if(defined('SURVEY_MODE') && ($zip = surveyVal('postcode')) !== FALSE)
{
// Look up the counties for this zip code
$counties = getCountiesForZipcode($zip);

if(count($counties) > 1)
{
$countyChoices = array();
foreach($counties as $fips=>$c)
$countyChoices[] = $fips . '(' . $c . ')';

$questions[] = array(
'key' => 'county',
'name' => 'County',
'prompt' => 'Which county is the property in?',
'prompt2' => didntUnderstand() . 'What county is the property in?',
'choices' => implode(', ', $countyChoices)
);
}
else
{
storeSurveyResponse('county', array_pop($counties));
}
}
else
{
// define the key/name pair for the web view, and optionally a way to look up foreign keys in the database
$questions[] = array(
'key' => 'county',
'name' => 'County',
'lookup' => array('table'=>'counties', 'key'=>'fips', 'value'=>'countyName')
);
}

$questions[] = array(
'key' => 'insurance',
'name' => 'Has Insurance',
'prompt' => 'Do you have insurance?',
'prompt2' => didntUnderstand() . 'Do you have insurance? Yes or no.',
'choices' => '[BOOLEAN]'
);

if(surveyVal('insurance'))
{
$questions[] = array(
'key' => 'provider',
'name' => 'Insurance Provider',
'prompt' => 'Who is your insurance provider?',
'prompt2' => didntUnderstand() . 'Who is your provides your insurance?',
'choices' => '[RECORD]'
);
}


$questions[] = array(
'key' => 'owner',
'name' => 'Owner?',
'prompt' => 'Are you an owner or a renter?',
'prompt2' => didntUnderstand() . 'Do you own or rent?',
'choices' => '1(owner, own, 1, yes), 0(renter, rent, 2)'
);
$questions[] = array(
'key' => 'estpredistfmv',
'prompt' => 'What is the estimated fair market value of the structure before the disaster in dollars?',
'choices' => '[CURRENCY]'
);
$questions[] = array(
'key' => 'eststructloss',
'name' => 'Est. Structural Damage',
'prompt' => 'What is the estimated dollar amount of the structural loss?',
'prompt2' => didntUnderstand() . 'Say the dollar amount slowly',
'choices' => '[CURRENCY]'
);
$questions[] = array(
'key' => 'estperproploss',
'name' => 'Est. Personal Loss',
'prompt' => 'What is the estimated dollar amount of the personal property loss?',
'prompt2' => didntUnderstand() . 'Please say the dollar amount slowly',
'choices' => '[CURRENCY]'
);
/*
$questions[] = array(
'key' => 'insurance',
'prompt' => 'Do you have insurance?',
'choices' => '[BOOLEAN]'
);
$questions[] = array(
'key' => 'provider',
'prompt' => 'Who is your insurance provider?',
'choices' => '[RECORD]'
);
*/





function didntUnderstand()
{
$res[] = 'Sorry, I didn\'t understand that.';
$res[] = 'Sorry, I didn\'t get that.';
$res[] = 'I\'m sorry, I didn\'t get that.';
$res[] = 'Sorry, I\'m having trouble understanding you.';
$res[] = 'Can you please repeat that?';
$res[] = 'Can you say that again?';
return $res[array_rand($res)] . ' ';
}

?>

0 comments on commit 07f81b3

Please sign in to comment.