Browse files

Reorganizing filenames and code. Responses and recordings are stored …

…in the database, and every response now has audio recorded with it.
  • Loading branch information...
1 parent 03726e0 commit 5af2ae1bb1be0650e9108bd00a26bd1253a89602 @aaronpk aaronpk committed Dec 5, 2010
Showing with 148 additions and 104 deletions.
  1. +0 −16 app/incoming.php
  2. +0 −61 app/question.php
  3. +2 −0 include/config-template.php
  4. +19 −14 include/include.php
  5. +92 −0 include/question.php
  6. +33 −13 index.php
  7. +2 −0 include/questions.php → survey.php
View
16 app/incoming.php
@@ -1,16 +0,0 @@
-<?php
-
-session_start();
-
-include('include/questions.php');
-
-$tropo[] = array(
- 'say' => array(
- 'value' => '. . . Thanks for calling the Mobile Assessment of Damage hotline.',
- ),
- 'voice' => $voice
-);
-
-askQuestion(array_shift($questions));
-
-?>
View
61 app/question.php
@@ -1,61 +0,0 @@
-<?php
-
-$key = get('method');
-
-
-// Handle the successful response of the question
-
-if(get('record') || get('hadRecording') || ($val=tropoInputValue()) !== FALSE)
-{
- if(get('record') == 1)
- {
- // Tropo seems to not send the cookie with the recorded file
- session_name($_GET['session_id']);
- session_start();
- // Handle uploaded recordings
- $filename = '/web/sites/loqi.me/www/madpub/recordings/' . md5(microtime(TRUE)) . '.wav';
- move_uploaded_file($_FILES['filename']['tmp_name'], $filename);
- $_SESSION['recordings'][$key] = $filename;
- die();
- }
- elseif(isset($val))
- {
- session_start();
- $_SESSION['responses'][$key] = $val;
- }
- else
- {
- session_start();
- }
-
- // After processing the input, include the questions file so logic can happen in it based on the session values set above
- include('include/questions.php');
-
- // Find the next question and ask it
- foreach($questions as $i=>$q)
- if($q['key'] == $key)
- if(array_key_exists($i+1, $questions))
- askQuestion($questions[$i+1]);
- else
- complete();
-}
-else
-{
- $tropo[] = array(
- 'say' => array('value'=>'Sorry, there was an error. Goodbye.'),
- 'voice' => $voice
- );
-}
-
-
-function complete()
-{
- global $tropo, $voice;
-
- $tropo[] = array(
- 'say' => array('value'=>'The survey has been completed. Thanks.'),
- 'voice' => $voice
- );
-}
-
-?>
View
2 include/config-template.php
@@ -12,4 +12,6 @@
define('WEB_ROOT', '/madpub/');
+define('RECORDING_PATH', '/tmp/recordings/');
+
?>
View
33 include/include.php
@@ -39,15 +39,16 @@ function askQuestion($question)
),
'voice' => $voice,
'name' => $question['key'],
- 'maxSilence' => 3,
+ 'maxSilence' => 2, // if they stop talking for 2 seconds it will end recording and move on
+ 'beep' => FALSE,
'url' => 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . $question['key'] . '.json?record=1&session_id=' . session_id()
)
);
}
else
{
$tropo[] = array(
- 'ask' => array(
+ 'record' => array(
'say' => array(
'value' => $question['prompt'],
),
@@ -57,13 +58,14 @@ function askQuestion($question)
'name' => $question['key'],
'choices' => array(
'value' => $choices
- )
+ ),
+ 'beep' => FALSE,
+ 'url' => 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . $question['key'] . '.json?record=1&session_id=' . session_id()
)
);
}
}
-
function tropoInput()
{
if(get('test') == 1)
@@ -86,9 +88,7 @@ function tropoInput()
echo 'JSON input' . "\n";
print_r($input);
echo "\n";
- $fp = fopen('/tmp/tropo.txt', 'a');
- fwrite($fp, ob_get_clean());
- fclose($fp);
+ filedebug(ob_get_clean());
}
return $input;
}
@@ -120,8 +120,8 @@ function db()
try {
$db = new PDO(PDO_DSN, PDO_USER, PDO_PASS);
} catch (PDOException $e) {
- header('HTTP/1.1 500 Server Error');
- die('Connection failed: ' . $e->getMessage());
+ header('HTTP/1.1 500 Server Error');
+ die('Connection failed: ' . $e->getMessage());
}
}
@@ -164,11 +164,16 @@ function filedebug($obj)
if(!isset($fp))
$fp = fopen(TMP_LOGFILE, 'a');
-
- ob_start();
- print_r($obj);
- echo "\n";
- fwrite($fp, ob_get_clean());
+
+ if(is_array($obj) || is_object($obj))
+ {
+ ob_start();
+ print_r($obj);
+ echo "\n";
+ fwrite($fp, ob_get_clean());
+ }
+ else
+ fwrite($fp, $obj . "\n");
}
/**
View
92 include/question.php
@@ -0,0 +1,92 @@
+<?php
+
+$key = get('method');
+
+ircdebug($key);
+
+// Handle the successful response of the question
+
+if(get('record') || get('hadRecording') || ($val=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);
+ $_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->bindValue(':callID', $_SESSION['callID']);
+ $query->bindValue(':name', $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->bindValue(':callID', $_SESSION['callID']);
+ $query->bindValue(':name', $key);
+ $query->bindValue(':recording', $filename);
+ $query->execute();
+ }
+
+ die();
+ }
+ elseif(isset($val))
+ {
+ $_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();
+ }
+ }
+
+ // 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)
+ if(array_key_exists($i+1, $questions))
+ askQuestion($questions[$i+1]);
+ else
+ complete();
+}
+else
+{
+ $tropo[] = array(
+ 'say' => array('value'=>'Sorry, there was an error. Goodbye.'),
+ 'voice' => $voice
+ );
+}
+
+
+
+
+function complete()
+{
+ global $tropo, $voice;
+
+ $tropo[] = array(
+ 'say' => array('value'=>'The survey has been completed. Thanks.'),
+ 'voice' => $voice
+ );
+}
+
+?>
View
46 index.php
@@ -2,15 +2,20 @@
include('include/include.php');
$input = tropoInput();
-/*
+
+// Tropo seems to not send the cookie with the recorded file, so we pass it in a query string parameter
+if(get('session_id'))
+ session_id($_GET['session_id']);
+
+session_start();
+
if(is_object($input))
{
if(property_exists($input, 'session'))
{
- $sessionID = $input->session->id;
$_SESSION['session'] = $input->session;
$_SESSION['callerID'] = (@$input->session->from->id ? $input->session->from->id : '');
- ircLog('Incoming call from ' . $_SESSION['callerID']);
+ ircdebug('Incoming call from ' . $_SESSION['callerID']);
}
else
{
@@ -19,19 +24,35 @@
}
else
$sessionID = FALSE;
-*/
+
$tropo = array();
$voice = 'allison';
switch(get('method'))
{
case 'incoming':
- case 'firstName':
- include('app/' . get('method') . '.php');
+ include('survey.php');
+
+ $query = db()->prepare('INSERT INTO `calls` (`date`, `callerID`, `sessionID`) VALUES(:date, :callerID, :sessionID)');
+ $query->bindValue(':date', date('Y-m-d H:i:s'));
+ $query->bindValue(':callerID', $_SESSION['callerID']);
+ $query->bindValue(':sessionID', session_id());
+ $query->execute();
+ $_SESSION['callID'] = db()->lastInsertId();
+
+ $tropo[] = array(
+ 'say' => array(
+ 'value' => '. . . ' . $firstPrompt,
+ ),
+ 'voice' => $voice
+ );
+
+ askQuestion(array_shift($questions));
+
break;
default:
- include('app/question.php');
+ include('include/question.php');
break;
}
@@ -42,18 +63,17 @@
if(get('test'))
{
echo formatJSON($json);
-
- if(isset($_SESSION))
- {
- echo "\n\nSession:\n";
- print_r($_SESSION);
- }
}
else
{
echo $json;
}
+if(isset($_SESSION))
+{
+ filedebug("\n\nSession:\n");
+ filedebug($_SESSION);
+}
filedebug($json);
View
2 include/questions.php → survey.php
@@ -1,5 +1,7 @@
<?php
+$firstPrompt = 'Thanks for calling the Mobile Assessment of Damage hotline.';
+
$questions[] = array(
'key' => 'insurance',
'prompt' => 'Do you have insurance?',

0 comments on commit 5af2ae1

Please sign in to comment.