Skip to content

Instantly share code, notes, and snippets.

@camilosampedro
Created May 31, 2017 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilosampedro/347d290a53552f84bee4d9b198e87cdd to your computer and use it in GitHub Desktop.
Save camilosampedro/347d290a53552f84bee4d9b198e87cdd to your computer and use it in GitHub Desktop.
AGI voz IP
#!/usr/bin/php -q
<?php
set_time_limit(30);
$param_error_log = '/tmp/notas.log';
$param_debug_on = 1;
require('phpagi.php');
require('yandex.php');
require("definiciones.inc");
$agi = new AGI();
$agi->answer();
sleep(1);
// Mensaje de bienvenida
text2wav($agi,"Bienvenido a Crisper Soluciones limitadas.");
text2wav($agi,"Se le mostrar� informaci�n sobre lugares de inter�s.");
$usuario = "";
$ciudad = "";
// Conexión a MySQ
$conexion = mysql_connect(MAQUINA, USUARIO, CLAVE) or die("Error: El servidor no puede conectar con la base de datos");
// Referenciamos el cursor a la base de datos
mysql_select_db("sitios_de_interes", $conexion);
menu($agi);
function menu($agi)
{
global $usuario;
do {
menuLogin($agi);
} while ($usuario !== "");
menuUsuario($agi);
do {
if ($usuario !== '') {
menuUsuario($agi);
} else {
menuLogin($agi);
}
} while ($usuario === "");
menuUsuario($agi);
}
function menuLogin($agi)
{
do {
text2wav($agi,"Por favor ingrese su ID de usuario despu�s del beep");
text2wav($agi,"Presione * si desea listar los usuarios:");
$todo_el_resultado = $agi->get_data('beep', 5000, 20);
$tecla = $todo_el_resultado['result'];
if ($tecla == '*') {
mostrarTodosLosUsuarios($agi);
}
} while ($tecla === '*');
loguearUsuario($agi, $tecla);
}
function mostrarTodosLosUsuarios($agi)
{
text2wav($agi,"Los usuarios que existen en la base de datos son:");
$query="SELECT id, nombre FROM usuario";
$resultado_query=mysql_query($query);
while ($row=mysql_fetch_array($resultado_query)) {
text2wav($agi,"id ".$row[0]);
text2wav($agi,"nombre ".$row[1]);
}
}
function loguearUsuario($agi, $tecla)
{
global $usuario;
$query = "SELECT * FROM usuario WHERE id=".$tecla;
$resultado_query = mysql_query($query);
if (mysql_num_rows($resultado_query) != 0) {
$row = mysql_fetch_array($resultado_query);
$usuario = $row[1];
text2wav($agi,"Bienvenido, ". $usuario);
} else {
text2wav($agi,"No existe este usuario, intentelo de nuevo.");
}
}
function menuUsuario($agi)
{
text2wav($agi,"Para seleccionar una ciudad, presione 1");
text2wav($agi,"Para ver el top de lugares globalmente, presione 2");
text2wav($agi,"Para salir, presione 0");
$todo_el_resultado = $agi->get_data('beep', 5000, 20);
$tecla = $todo_el_resultado['result'];
switch ($tecla) {
case '0':
$agi->hangup();
case '1':
mostrarCiudades($agi);
break;
case '2':
verTop($agi);
break;
default:
text2wav($agi,"Acci�n no reconocida. Por favor intentelo de nuevo.");
break;
}
}
function mostrarCiudades($agi)
{
global $ciudad;
do {
text2wav($agi,"Ingrese el identificador de la ciudad:");
text2wav($agi,"Para listar las ciudades, presione asterisco");
$todo_el_resultado = $agi->get_data('beep', 5000, 20);
$tecla = $todo_el_resultado['result'];
if ($tecla === '*') {
$query = "SELECT id, nombre FROM ciudad";
$resultado_query = mysql_query($query);
while ($row = mysql_fetch_array($resultado_query)) {
text2wav($agi,"Nombre: " . $row[1]);
text2wav($agi,"Identificador: " . $row[0]);
}
} else {
$query = "SELECT * FROM ciudad WHERE id=".$tecla;
$resultado_query = mysql_query($query);
if (mysql_num_rows($resultado_query) <= 0) {
text2wav($agi,"La ciudad no existe en nuestra base de datos");
text2wav($agi,"Intentelo de nuevo");
} else {
$ciudad = $tecla;
}
}
} while ($ciudad === '');
menuCiudad($agi);
}
function menuCiudad($agi)
{
global $ciudad;
$sitio = "";
do {
text2wav($agi,"Ingrese el identificador del sitio de inter�s");
text2wav($agi,"Para listar los sitios, presione asterisco");
$todo_el_resultado = $agi->get_data('beep', 5000, 20);
$tecla = $todo_el_resultado['result'];
if ($tecla === '*') {
verSitiosPorCiudad($agi);
} else {
$sitio = seleccionarSitio($agi, $tecla);
}
} while ($sitio === '');
menuCiudad($agi);
}
function verSitiosPorCiudad($agi)
{
global $ciudad;
$query = "SELECT sitio_de_interes.nombre, COUNT(calificacion.valor), AVG(calificacion.valor) FROM calificacion ";
$query .= "INNER JOIN sitio_de_interes ON calificacion.sitio_de_interes_id = sitio_de_interes.id ";
$query .= "INNER JOIN ciudad ON sitio_de_interes.ciudad = ciudad.id ";
$query .= "WHERE ciudad.id = ".$ciudad;
$query .= "GROUP BY sitio_de_interes.nombre ";
$query .= "ORDER BY AVG(calificacion.valor) DESC ";
$query .= "LIMIT 5";
$resultado_query = mysql_query($query);
text2wav($agi,"La lista de sitios para la ciudad son");
while ($row = mysql_fetch_array($resultado_query)) {
text2wav($agi,"Nombre: " . $row[0]);
text2wav($agi,"Puntuaci�n: " . $row[2]);
text2wav($agi,"Calificado por ".$row[1]." usuarios");
}
}
function seleccionarSitio($agi, $tecla)
{
$sitio = "";
if (validarSitio($agi, $tecla)) {
text2wav($agi,"Para calificar el sitio, presione 1");
text2wav($agi,"Para registrar una visita en el sitio, presione 2");
text2wav($agi,"Para escoger otro sitio, presione 3");
text2wav($agi,"Para salir, presione 0");
$todo_el_resultado = $agi->get_data('beep', 5000, 20);
$tecla = $todo_el_resultado['result'];
switch ($tecla) {
case '0':
$agi->hangup();
break;
case '1':
calificarSitio($agi, $tecla);
break;
case '2':
registrarVisita($agi, $tecla);
break;
default:
text2wav($agi,"Accion no reconocida, intente de nuevo");
break;
}
}
return $sitio;
}
function validarSitio($agi, $tecla)
{
$query = "SELECT * FROM sitio_de_interes WHERE id=".$tecla;
$resultado_query = mysql_query($query);
if (mysql_num_rows($resultado_query) <= 0) {
text2wav($agi,'El sitio no existe en nuestra base de datos');
text2wav($agi,'Intentelo de nuevo');
return false;
} else {
return true;
}
}
function calificarSitio($agi, $sitio)
{
global $usuario;
do {
text2wav($agi,"Ingrese la valoraci�n del sitio, de uno a cinco");
$todo_el_resultado = $agi->get_data('beep', 5000, 20);
$tecla = $todo_el_resultado['result'];
$valor = $tecla;
} while ($tecla < 1 || $tecla > 5);
text2wav($agi,"Registrando la calificaci�n");
$query="INSERT INTO calificacion(sitio_de_interes_id, usuario_id, valor) VALUES('$sitio', '$usuario', $valor)";
$resultado_query = mysql_query($query);
text2wav($agi,"Registrado �xitosamente");
}
function verTop($agi)
{
$query = "SELECT sitio_de_interes.nombre, COUNT(calificacion.valor), AVG(calificacion.valor) FROM calificacion ";
$query .= "INNER JOIN sitio_de_interes ON calificacion.sitio_de_interes_id = sitio_de_interes.id ";
$query .= "GROUP BY sitio_de_interes.nombre ";
$query .= "ORDER BY AVG(calificacion.valor) DESC ";
$query .= "LIMIT 5";
$resultado_query = mysql_query($query);
while ($row = mysql_fetch_array($resultado_query)) {
text2wav($agi,"Nombre: " . $row[0]);
text2wav($agi,"Puntuaci�n: " . $row[2]);
text2wav($agi,"Calificado por ".$row[1]." usuarios");
}
}
function mostrarLugares($agi)
{
$query = "SELECT id, nombre_de_sitio FROM sitio_de_interes";
$resultado_query = mysql_query("SELECT id, nombre_de_sitio FROM sitio_de_interes");
if (mysql_num_rows($resultado_query) > 0) {
text2wav($agi,"Hay " . mysql_num_rows($resultado_query) . ' resultados.');
while ($row = mysql_fetch_array($resultado_query)) {
text2wav($agi,$row[0] . " " . $row[1]);
}
detallesLugares($agi);
mysql_close($conexion);
} else {
text2wav($agi,"El codigo que ha ingresado es incorrecto");
$agi->hangup();
}
}
function detallesLugares($agi)
{
text2wav($agi,"Ingrese el nombre del lugar que desea conocer m�s detalles");
// Tecla
$todo_el_resultado = $agi->get_data('beep', 3000, 20);
$tecla = $todo_el_resultado['result'];
$query = "SELECT s.nombre_de_sitio, s.descripcion, c.nombre FROM sitio_de_interes AS s INNER JOIN ciudad AS c ON s.ciudad = c.id WHERE s.id = " . $tecla;
$resultado_query = mysql_query($query);
if (mysql_num_rows($resultado_query) > 0) {
text2wav($agi,"Hay " . mysql_num_rows($resultado_query) . " resultados.");
while ($row = mysql_fetch_array($resultado_query)) {
text2wav($agi,$row[0]);
text2wav($agi,"Descripci�n: " . $row[1]);
text2wav($agi,"Ciudad: " . $row[2]);
}
} else {
text2wav($agi,"El codigo que ha ingresado es incorrecto");
$agi->hangup();
}
$query = "INSERT INTO consulta(fecha) VALUES (CURRENT_TIMESTAMP)";
$resultado_query = mysql_query($query);
}
$agi->hangup();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment