Files
Guna_Preventa/B4A/C_Cuestionario.bas
2025-05-22 12:03:46 -06:00

417 lines
18 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.2
@EndOfDesignText@
Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private bAceptarPregunta As Button
Dim encuestaIniciada As Boolean = False
Private Root2 As B4XView
Private EventName As String 'ignore
Private CallBack As Object 'ignore
Private vPreguntaActual As String
Private db As SQL
' Private clienteAnt As String = ""
Dim panelSombra As Panel 'Panel de sombra.
Dim cb_respuestas As String = ""
Dim panelActual As Panel
Dim bringToFrontCont As Int = 0
End Sub
'You can add more parameters here.
Public Sub Initialize (vCallback As Object, vEventName As String, vRoot As B4XView, skmt As SQL) As Object
Root2 = vRoot
EventName = vEventName
CallBack = vCallback
db = skmt
vPreguntaActual = 0
db.ExecNonQuery("CREATE TABLE IF NOT EXISTS CUESTIONARIO (Q_RUTA TEXT, Q_ALMACEN TEXT, Q_IDCLIENTE TEXT, Q_IDPREGUNTA TEXT, Q_PREGUNTA TEXT, Q_IDRESPUESTA TEXT, Q_RESPUESTA TEXT, Q_FECHA TEXT, Q_ENVIO_OK INTEGER DEFAULT 0)")
agregaColumna("CUESTIONARIO", "Q_RUTA", "TEXT")
agregaColumna("CUESTIONARIO", "Q_ALMACEN", "TEXT")
Return Me
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
'load the layout to Root
End Sub
'Regresa verdadero o falso dependiendo de si ya se inició la encuesta.
'Sub encuestaIniciada As Boolean
' Return vEncuestaIniciada
'End Sub
'Regresa el Id de la pregunta actual (read-only).
Sub preguntaActual As String 'ignore
Return vPreguntaActual
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
'Muestra una pregunta con sus posibles respuestas.
' La lista de respuestas puede ser directamente un array p. ej. Array As String("value1", "value2")
' Crear un Sub_Click con el nombre del evento para que reciba un mapa con la respuesta p. ej.: Sub questionario_Click
' Regresa un mapa con:
' idPregunta - El Id especificado de la pregunta.
' pregunta - El texto de la pregunta.
' idRespuesta - Un numero consecutivo empezando en 1 por cada pregunta.
' respuesta - El texto de la respuesta.
' panel - El panel del popup para poder ocultarlo al contestar la pregunta. ( p. ej.: m.Get("panel").As(Panel).Visible = false )
'###########################################
' Sub questionario_Click
' if m.get("idPregunta") = "1" ..Then .. Else con nuevas preguntas
' End Sub
'###########################################
Sub agregaPreguntaCheckbox(id As String, pregunta As String, respuestas As List) 'ignore
encuestaIniciada = True
vPreguntaActual = id
bAceptarPregunta.Initialize("bAceptarPregunta")
bAceptarPregunta.Enabled = False
Private su As StringUtils
panelSombra.Initialize("pSombra")
Private cd As ColorDrawable
cd.Initialize(Colors.ARGB(125, 98, 98, 98), 0)
panelSombra.Background = cd
Private panelX As Panel 'Panel de la pregunta.
panelX.Initialize("pQuest")
panelActual = panelX
Private lbl As Label 'Etiqueta de la pregunta.
lbl.Initialize("")
lbl.Text = pregunta
lbl.TextSize = 16
lbl.TextColor = Colors.Black
cd.Initialize2(Colors.white, 20, 1, Colors.Gray) 'Borde y esquinas redondeadas del panel de la pregunta.
panelX.Background = cd
panelSombra.AddView(panelX, 10dip, 0dip, 80%x, 200dip)
Root2.AddView(panelSombra, 0, 0, 100%x, 100%y) 'add the panel to the layout
panelX.AddView(lbl, 20dip, 20dip, (panelX.Width * 0.9), 40dip) 'Agregamos la etiqueta al panel.
lbl.Height = su.MeasureMultilineTextHeight(lbl, lbl.Text)
Private cb(respuestas.Size) As CheckBox
For p = 0 To respuestas.Size - 1
cb(p).Initialize("cb")
cb(p).Text = respuestas.Get(p)
cb(p).Tag = CreateMap("id":id, "pregunta":pregunta, "panel":panelSombra, "idRespuesta":p + 1)
panelX.AddView(cb(p), 10dip, (70 * (p + 1)), (panelX.Width * 0.9), 10) 'Agegamos el radio al panel.
cb(p).Height = su.MeasureMultilineTextHeight(cb(p), cb(p).Text) + 1 'Calculamos el alto del radio de acuerdo al largo del texto.
Private newTop As Int = lbl.top + lbl.Height + 10 'Si es el primer radio, lo ponemos en top = alto de la "pregunta" + 10.
If p <> 0 Then newTop =cb(p - 1).Top + cb(p - 1).Height + 10 'Calculamos el Top del radio de acuerdo al top y alto del radio anterior.
cb(p).Top = newTop
Next
bAceptarPregunta.Text = "Continuar"
' panelX.AddView(bAceptarPregunta, 10, newTop + r(respuestas.Size - 1).Height + 20, 150dip, 50dip) 'Ponemos el boton de continuar despues del ultimo radio.
panelX.AddView(bAceptarPregunta, 10, newTop + cb(respuestas.Size - 1).Height + 20, 150dip, 50dip) 'Ponemos el boton de continuar despues del ultimo radio.
bAceptarPregunta.Left = (panelX.Width / 2) - (bAceptarPregunta.Width / 2)
panelX.Height = bAceptarPregunta.Top + bAceptarPregunta.Height + 15dip 'Calculamos el alto del panel de acuerdo al Top del boton.
panelX.left = (Root2.Width / 2) - (panelX.Width / 2)
panelX.top = (Root2.Height / 3) - (panelX.Height / 3)
panelSombra.Width = Root2.Width
panelSombra.Height = Root2.Height
panelSombra.Elevation = 100
panelSombra.BringToFront
' panelSombra.Elevation = 0
End Sub
Sub agregaPreguntaRadio(id As String, pregunta As String, respuestas As List) 'ignore
encuestaIniciada = True
vPreguntaActual = id
bAceptarPregunta.Initialize("bAceptarPregunta")
Private su As StringUtils
panelSombra.Initialize("pSombra")
Private cd As ColorDrawable
cd.Initialize(Colors.ARGB(125, 98, 98, 98), 0)
panelSombra.Background = cd
Private panelX As Panel 'Panel de la pregunta.
panelX.Initialize("pQuest")
panelActual = panelX
Private lbl As Label 'Etiqueta de la pregunta.
lbl.Initialize("")
lbl.Text = pregunta
lbl.TextSize = 16
lbl.TextColor = Colors.Black
cd.Initialize2(Colors.white, 20, 1, Colors.Gray) 'Borde y esquinas redondeadas del panel de la pregunta.
panelX.Background = cd
panelSombra.AddView(panelX, 10dip, 0dip, 80%x, 200dip)
Root2.AddView(panelSombra, 0, 0, 100%x, 100%y) 'add the panel to the layout
panelX.AddView(lbl, 20dip, 20dip, (panelX.Width * 0.9), 40dip) 'Agregamos la etiqueta al panel.
lbl.Height = su.MeasureMultilineTextHeight(lbl, lbl.Text)
Private r(respuestas.Size) As RadioButton
For p = 0 To respuestas.Size - 1
r(p).Initialize("r")
r(p).Text = respuestas.Get(p)
r(p).Tag = CreateMap("id":id, "pregunta":pregunta, "panel":panelSombra, "idRespuesta":p + 1)
panelX.AddView(r(p), 10dip, (70 * (p + 1)), (panelX.Width * 0.9), 10) 'Agegamos el radio al panel.
r(p).Height = su.MeasureMultilineTextHeight(r(p), r(p).Text) + 25 'Calculamos el alto del radio de acuerdo al largo del texto.
Private newTop As Int = lbl.top + lbl.Height + 10 'Si es el primer radio, lo ponemos en top = alto de la "pregunta" + 10.
If p <> 0 Then newTop = r(p - 1).Top + r(p - 1).Height + 5 'Calculamos el Top del radio de acuerdo al top y alto del radio anterior.
r(p).Top = newTop
Next
r(0).Checked = True
bAceptarPregunta.Text = "Continuar"
' panelX.AddView(bAceptarPregunta, 10, newTop + r(respuestas.Size - 1).Height + 20, 150dip, 50dip) 'Ponemos el boton de continuar despues del ultimo radio.
panelX.AddView(bAceptarPregunta, 10, newTop + r(respuestas.Size - 1).Height + 20, 150dip, 50dip) 'Ponemos el boton de continuar despues del ultimo radio.
bAceptarPregunta.Left = (panelX.Width / 2) - (bAceptarPregunta.Width / 2)
panelX.Height = bAceptarPregunta.Top + bAceptarPregunta.Height + 15dip 'Calculamos el alto del panel de acuerdo al Top del boton.
panelX.left = (Root2.Width / 2) - (panelX.Width / 2)
panelX.top = (Root2.Height / 3) - (panelX.Height / 2)
panelSombra.Width = Root2.Width
panelSombra.Height = Root2.Height
panelSombra.Elevation = 100
panelSombra.BringToFront
End Sub
'Agrega preguntas de respuesta abierta, los ids y texto de las preguntas deben de ser listas.
Sub agregaPreguntaAbierta(id As String, pregunta As String, ids As List, respuestas As List) 'ignore
Log("Iniciamos PreguntaAbierta")
Log(ids)
Log(respuestas)
Private sv_cuest As ScrollView
sv_cuest.Initialize(500dip)
encuestaIniciada = True
vPreguntaActual = id
bAceptarPregunta.Initialize("bAceptarPregunta")
Private su As StringUtils
panelSombra.Initialize("pSombra")
Private cd As ColorDrawable
cd.Initialize(Colors.ARGB(125, 98, 98, 98), 0)
panelSombra.Background = cd
Private panelX As Panel 'Panel de la pregunta.
panelX.Initialize("pQuest")
Private sv_cuest As ScrollView
sv_cuest.Initialize(100dip)
Private panel2 As Panel 'Panel de la pregunta.
panel2.Initialize("panel2")
panel2.Color = Colors.Yellow
panelActual = panelX
Private lbl As Label 'Etiqueta de la pregunta.
lbl.Initialize("")
lbl.Text = pregunta
lbl.TextSize = 16
lbl.TextColor = Colors.Black
cd.Initialize2(Colors.white, 20, 1, Colors.Gray) 'Borde y esquinas redondeadas del panel de la pregunta.
panelX.Background = cd
panelSombra.AddView(panelX, 10dip, 0dip, 80%x, 200dip)
Root2.AddView(panelSombra, 0, 0, 100%x, 100%y) 'add the panel to the layout
panelX.AddView(lbl, 20dip, 20dip, (panelX.Width * 0.9), 40dip) 'Agregamos la etiqueta al panel.
lbl.Height = su.MeasureMultilineTextHeight(lbl, lbl.Text)
Private et(respuestas.Size) As EditText
Private labelX(respuestas.Size) As Label
For p = 0 To respuestas.Size - 1
et(p).Initialize("et")
labelX(p).Initialize("labelX")
labelX(p).Height = 60dip
' et(p).Color = Colors.red
' labelX(p).Color = Colors.Blue
et(p).As(B4XView).SetTextAlignment("CENTER", "CENTER")
labelX(p).As(B4XView).SetTextAlignment("CENTER", "CENTER")
et(p).SetTextSizeAnimated(0, 10)
et(p).Tag = CreateMap("id":id, "pregunta":pregunta, "panel":panelSombra, "idRespuesta":ids.Get(p))
labelX(p).Text = respuestas.Get(p)
Private lx As Label = labelX(p)
et(p).Text = ""
Log($"Agregamos ET ${labelX(p).text} - ${(70*p)}"$)
' Private labelXHeight As String =
Log(labelX(p).Height)
sv_cuest.panel.AddView(labelX(p), 0, (70 * p), (panelX.Width * 0.72), 60) 'Agregamos la etiqueta al panel.
sv_cuest.panel.AddView(et(p), (labelX(p).Width + 5), (70 * p), (panelX.Width * 0.2), 115) 'Agregamos el edittext al panel.
Private newTop As Int = 0 'lbl.top + lbl.Height + 20 'Si es el primer radio, lo ponemos en top = alto de la "pregunta" + 10.
If p <> 0 Then newTop = et(p - 1).Top + et(p - 1).Height + 5 'Calculamos el Top del radio de acuerdo al top y alto del radio anterior.
et(p).Top = newTop
labelX(p).Top = newTop
Next
bAceptarPregunta.Text = "Continuar"
panelX.AddView(sv_cuest, 10, lbl.top + lbl.Height + 20, (panelX.Width * 0.95), 300dip)
sv_cuest.panel.Height = newTop + 40dip
panelX.Height = newTop + 80dip + 35dip 'Calculamos el alto del panel de acuerdo al Top del boton.
panelX.Height = 150dip + (respuestas.Size * 80) 'Calculamos el alto del panel de acuerdo al Top del boton.
If panelX.Height > (Root2.Height * 0.7) Then ' Si el panel es mayor al 70%, lo regresamos al 70%.
panelX.Height = Root2.Height * 0.7
End If
panelX.AddView(bAceptarPregunta, 10, (panelX.Height - 55dip), 150dip, 50dip) 'Ponemos el boton de continuar despues del ultimo radio.
bAceptarPregunta.Left = (panelX.Width / 2) - (bAceptarPregunta.Width / 2)
bAceptarPregunta.Enabled = False
sv_cuest.Height = panelX.Height - (lbl.Top + lbl.Height) - 55dip
panelX.left = (Root2.Width / 2) - (panelX.Width / 2)
panelX.top = (Root2.Height / 2) - (panelX.Height / 2) 'Centramos verticalmente el panel.
If respuestas.Size < 10 Then panelX.top = (Root2.Height / 3) - (panelX.Height / 2) ' Si hay mas de 9 productos en la lista, ponemos el panel a 1/3 de altura
panelSombra.Width = Root2.Width
panelSombra.Height = Root2.Height
panelSombra.Elevation = 100dip
panelSombra.BringToFront
bringToFrontCont = 0
End Sub
private Sub preguntaContestada(Success As Map) 'ignore
If SubExists(CallBack, EventName & "_preguntaContestada") Then
CallSubDelayed2(CallBack, EventName & "_preguntaContestada", Success)
End If
End Sub
private Sub pSombra_Click
End Sub
private Sub r_CheckedChange(Checked As Boolean)
bAceptarPregunta.tag = CreateMap("idPregunta":Sender.As(RadioButton).tag.As(Map).Get("id").As(String), "pregunta":Sender.As(RadioButton).tag.As(Map).Get("pregunta"), "idRespuesta":Sender.As(RadioButton).tag.As(Map).Get("idRespuesta").As(String), "respuesta":Sender.As(RadioButton).text, "panel":Sender.As(RadioButton).tag.As(Map).Get("panel"))
End Sub
private Sub cb_CheckedChange(Checked As Boolean)
Private elCB As CheckBox = Sender.As(CheckBox)
Private elCBMap As Map = elCB.tag.As(Map)
Private cuantosChks As Int = 0
' Log(panelActual.NumberOfViews)
If elCB.Text = "Ninguno de los anteriores" And elCB.Checked = True Then
For Each v As View In panelActual.GetAllViewsRecursive
If getViewType(v) = "CheckBox" Then
cuantosChks = cuantosChks + 1
v.As(CheckBox).Checked = False
v.As(CheckBox).Enabled = False
' Log(v.As(CheckBox).Text)
' Log(v.As(CheckBox).Checked)
If v.As(CheckBox).Text = "Ninguno de los anteriores" Then
v.As(CheckBox).Enabled = True
v.As(CheckBox).Checked = True
End If
If v.As(CheckBox).Checked Then cb_respuestas = cb_respuestas &"|" & v.As(CheckBox).Text
End If
Next
else if elCB.Text = "Ninguno de los anteriores" And elCB.Checked = False Then
For Each v As View In panelActual.GetAllViewsRecursive
If getViewType(v) = "CheckBox" Then
v.As(CheckBox).Enabled = True
End If
Next
End If
If cuantosChks = 0 Then bAceptarPregunta.Enabled = False ' Si no hay opciones palomeadas, deshabilitamos el boton.
cb_respuestas = ""
Private cb_idRespuestas As String= ""
For Each v As View In panelActual.GetAllViewsRecursive
' Log(getViewType(v))
If getViewType(v) = "CheckBox" Then
' Log(v.As(CheckBox).Text)
' Log(v.As(CheckBox).Checked)
If v.As(CheckBox).Checked Then
' Log(v.As(CheckBox).Tag.As(Map))
If cb_respuestas = "" Then
cb_respuestas = v.As(CheckBox).Text
cb_idRespuestas = v.As(CheckBox).Tag.As(Map).Get("idRespuesta")
Else
cb_respuestas = cb_respuestas &"|" & v.As(CheckBox).Text
cb_idRespuestas = cb_idRespuestas &"|" & v.As(CheckBox).Tag.As(Map).Get("idRespuesta")
End If
bAceptarPregunta.Enabled = True ' Si hay opciones palomeadas, habilitamos el boton.
End If
End If
Next
' Log(cb_idRespuestas)
' Log(elCBMap)
' Log(cb_idRespuestas)
bAceptarPregunta.tag = CreateMap("idPregunta":elCBMap.Get("id").As(String), "pregunta":elCBMap.Get("pregunta"), "idRespuesta":cb_idRespuestas, "respuesta":cb_respuestas, "panel":elCBMap.Get("panel"))
End Sub
Private Sub et_TextChanged(Old As String, New As String)
Log($"${Old}, ${New}"$)
Private etTotales As Int = 0
Private etCont As Int = 0
Private etResps As String = ""
Private etIdResps As String = ""
For Each v As View In panelActual.GetAllViewsRecursive
' Log(getViewType(v))
If getViewType(v) = "EditText" Then
etTotales = etTotales + 1
' Log($"Este ET: ${v.As(EditText).text}"$)
If v.As(EditText).text <> "" Then
etCont = etCont + 1
If etResps = "" Then
etResps = v.As(EditText).Text
etIdResps = v.As(EditText).Tag.As(Map).Get("idRespuesta")
Else
etResps = etResps &"|" & v.As(EditText).Text
etIdResps = etIdResps &"|" & v.As(EditText).Tag.As(Map).Get("idRespuesta")
End If
End If
End If
Next
' Log($"${etResps}, ${etIdResps}"$)
Log($"${etCont}, ${etTotales}"$)
bAceptarPregunta.Tag = CreateMap("idPregunta":Sender.As(EditText).tag.As(Map).Get("id").As(String), "pregunta":Sender.As(EditText).tag.As(Map).Get("pregunta"), "idRespuesta":etIdResps, "respuesta":etResps, "panel":Sender.As(EditText).tag.As(Map).Get("panel"))
If etCont = etTotales Then bAceptarPregunta.Enabled = True Else bAceptarPregunta.Enabled = False
End Sub
Private Sub bAceptarPregunta_Click
encuestaIniciada = True
Private m As Map = Sender.As(Button).tag.As(Map) 'Recibimos el id de la pregunta, el id de la respuesta, el texto de la respuesta y el objeto del panelSombra para poder ocultarlo.
preguntaContestada(m)
'' m.Get("panel").As(Panel).Visible = False 'Ocultamos el panel de la pregunta.
' m.Get("panel").As(Panel).RemoveView
' If m.Get("idPregunta") = "1" And m.Get("respuesta") = "No, está enrejado o no se tiene acceso" Then
' Log("Tiendajon / Ventana / Kiosko")
' else If m.Get("idPregunta") = "1" And m.Get("respuesta") = "Si se pude acceder" Then
' agregaPregunta("2", "¿Tiene al menos 2 pasillos con acceso directo a la mercancía?", Array As String("Si tiene al menos 2 pasillos con acceso directo a la mercancía", "No tiene pasillos o solo uno central"))
' End If
' If m.Get("idPregunta") = "2" And m.Get("respuesta") = "Si tiene al menos 2 pasillos con acceso directo a la mercancía" Then
' Log("Mini-Super")
' else If m.Get("idPregunta") = "2" And m.Get("respuesta") = "No tiene pasillos o solo uno central" Then
' agregaPregunta("3", "¿Cuenta con enfriador horizontal para venta de perecederos como queso, jamon, crema, etc.?", Array As String("Si", "No tiene enfriador horizontal. ¿El local esta enfocado a venta especializada?"))
' End If
End Sub
'Regresa verdadero si el cliente dado tiene cuestionario contestado.
Sub clienteConCuestionario(idCliente As String) As Boolean 'ignore
Private r As Boolean = False
Private c As Cursor = db.ExecQuery($"select count(*) as q from CUESTIONARIO where Q_IDCLIENTE = '${idCliente}'"$)
c.Position = 0
If c.GetInt("q") > 0 Then r = True
Return r
End Sub
'Oculta el anel de la pregunta.
Sub ocultPanelPregunta
If panelSombra.IsInitialized Then panelSombra.RemoveView
End Sub
'Agrega una columna a la tabla especificada.
'Hay que indicar el "tipo" de la columna (TEXT, INTEGER, ETC)
'Ej. agregaColumna("TABLA", "COLUMNA", "TIPO")
Sub agregaColumna(tabla As String, columna As String, tipo As String) 'ignore
Try 'Intentamos usar "pragma_table_info" para revisar si existe la columna en la tabla
Private c As Cursor = db.ExecQuery($"SELECT COUNT(*) AS fCol FROM pragma_table_info('${tabla}') WHERE name='${columna}'"$)
c.Position = 0
If c.GetString("fCol") = 0 Then 'Si no esta la columna la agregamos
db.ExecNonQuery($"ALTER TABLE ${tabla} ADD COLUMN ${columna} ${tipo}"$)
Log($"Columna "${columna} ${tipo}", agregada a "${tabla}"."$)
End If
Catch 'Si no funciona "pragma_table_info" lo hacemos con try/catch
Try
db.ExecNonQuery($"ALTER TABLE ${tabla} ADD COLUMN ${columna} ${tipo}"$)
Log($"Columna "${columna} ${tipo}", agregada a "${tabla}".."$)
Catch
Log(LastException)
End Try
End Try
End Sub
Sub getViewType (v As View) As String 'ignore
Private t As String = GetType(v)
' Log(t)
Private t2() As String = Regex.Split("\.", t)
' Log(t2.Length)
If t2.Length = 3 Then
t = t2(2)
End If
Return t
End Sub
'Regresa una lista de una cadena (separada por pipes) dada.
Sub strToList(str As String) As List 'ignore
Private resps() As String = Regex.Split("\|", str)
Private l As List
l.Initialize
For r = 0 To resps.Length - 1
l.Add(resps(r))
Next
Return l
End Sub