File size: 12,013 Bytes
111157d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba0d9d4
 
 
 
 
 
 
 
 
111157d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33ae98b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111157d
33ae98b
111157d
 
33ae98b
 
 
 
 
 
 
 
 
 
111157d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efccf51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import streamlit as st
import logging
from datetime import datetime, timezone
from dateutil.parser import parse

# Configuraci贸n del logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

#Importaciones locales.

from ..utils.widget_utils import generate_unique_key
from session_state import initialize_session_state, logout

from translations import get_translations

from ..auth.auth import authenticate_user, authenticate_student, authenticate_admin

from ..admin.admin_ui import admin_page

from ..chatbot import display_sidebar_chat

# Students activities
from ..studentact.student_activities_v2 import display_student_activities
from ..studentact.current_situation_interface import display_current_situation_interface
from ..studentact.current_situation_analysis import analyze_text_dimensions


##Importaciones desde la configuraci贸n de bases datos #######

from ..database.sql_db import (
    get_user,
    get_admin_user,
    get_student_user,
    get_teacher_user,
    create_user,
    create_student_user,
    create_teacher_user,
    create_admin_user,
    update_student_user,  # Agregada
    delete_student_user,  # Agregada
    record_login,
    record_logout,
    get_recent_sessions,
    get_user_total_time, 
    store_application_request,
    store_student_feedback
)

from ..database.mongo_db import (
    get_collection,
    insert_document,
    find_documents,
    update_document,
    delete_document
)

from ..database.semantic_mongo_db import (
    store_student_semantic_result,
    get_student_semantic_analysis,
    update_student_semantic_analysis,
    delete_student_semantic_analysis,
    get_student_semantic_data
)



from ..database.chat_mongo_db import store_chat_history, get_chat_history

##Importaciones desde los an谩lisis #######
from ..semantic.semantic_interface import (
    display_semantic_interface, 
    display_semantic_results
)

from ..discourse.discourse_interface import (    # Agregar esta importaci贸n
    display_discourse_interface,
    display_discourse_results
)


####################################################################################
def user_page(lang_code, t):
    logger.info(f"Entrando en user_page para el estudiante: {st.session_state.username}")

    # Inicializar el tab seleccionado si no existe
    if 'selected_tab' not in st.session_state:
        st.session_state.selected_tab = 0

    # Manejar la carga inicial de datos del usuario
    if 'user_data' not in st.session_state:
        with st.spinner(t.get('loading_data', "Cargando tus datos...")):
            try:
                # Obtener datos sem谩nticos
                semantic_data = get_student_semantic_data(st.session_state.username)
                
                # Verificar si la operaci贸n fue exitosa
                if semantic_data.get('status') == 'error':
                    raise Exception(semantic_data.get('error', 'Error desconocido al obtener datos'))
                
                # Almacenar datos en session_state
                st.session_state.user_data = {
                    'semantic_analyses': semantic_data.get('entries', []),
                    'analysis_count': semantic_data.get('count', 0),
                    'last_analysis': semantic_data['entries'][0] if semantic_data['entries'] else None,
                    'username': st.session_state.username,
                    'loaded_at': datetime.now(timezone.utc).isoformat()
                }
                
                st.session_state.last_data_fetch = datetime.now(timezone.utc).isoformat()
                
            except Exception as e:
                logger.error(f"Error al obtener datos del usuario: {str(e)}")
                # Crear estructura vac铆a para evitar errores
                st.session_state.user_data = {
                    'semantic_analyses': [],
                    'analysis_count': 0,
                    'last_analysis': None,
                    'username': st.session_state.username,
                    'error': str(e)
                }
                st.error(t.get('data_load_error', "Hubo un problema al cargar tus datos. Algunas funciones pueden estar limitadas."))
                # No hacer return aqu铆 para permitir que la aplicaci贸n contin煤e

    logger.info(f"Idioma actual: {st.session_state.lang_code}")
    logger.info(f"Modelos NLP cargados: {'nlp_models' in st.session_state}")

    # Configuraci贸n de idiomas disponibles
    languages = {'Espa帽ol': 'es', 'English': 'en', 'Fran莽ais': 'fr', 'Portugu锚s': 'pt'}

    # Estilos CSS personalizados
    st.markdown("""
    <style>
    .stSelectbox > div > div {
        padding-top: 0px;
    }
    .stButton > button {
        padding-top: 2px;
        margin-top: 0px;
    }
    div[data-testid="stHorizontalBlock"] > div:nth-child(3) {
        display: flex;
        justify-content: flex-end;
        align-items: center;
    }
    </style>
    """, unsafe_allow_html=True)

    # Barra superior con informaci贸n del usuario y controles
    with st.container():
        col1, col2, col3 = st.columns([2, 2, 1])
        with col1:
            st.markdown(f"<h3 style='margin-bottom: 0; padding-top: 10px;'>{t['welcome']}, {st.session_state.username}</h3>", 
                       unsafe_allow_html=True)
        with col2:
            selected_lang = st.selectbox(
                t['select_language'],
                list(languages.keys()),
                index=list(languages.values()).index(st.session_state.lang_code),
                key=f"language_selector_{st.session_state.username}_{st.session_state.lang_code}"
            )
            new_lang_code = languages[selected_lang]
            if st.session_state.lang_code != new_lang_code:
                st.session_state.lang_code = new_lang_code
                st.rerun()
        with col3:
            if st.button(t['logout'], 
                        key=f"logout_button_{st.session_state.username}_{st.session_state.lang_code}"):
                st.session_state.clear()
                st.rerun()

    st.markdown("---")

    # Asegurarse de que tenemos las traducciones del chatbot
    chatbot_t = t.get('CHATBOT_TRANSLATIONS', {}).get(lang_code, {})
    
    # Mostrar chatbot en sidebar
    display_sidebar_chat(lang_code, chatbot_t)

    # Inicializar estados para todos los tabs
    if 'tab_states' not in st.session_state:
        st.session_state.tab_states = {
            'semantic_active': False,
            'discourse_active': False,
            'activities_active': False,
            'feedback_active': False
        }
    
    # Sistema de tabs
    tab_names = [
        t.get('semantic_tab', 'An谩lisis Sem谩ntico'),
        t.get('discourse_tab', 'An谩lisis comparado de textos'),
        t.get('activities_tab', 'Registro de mis actividades'),
        t.get('feedback_tab', 'Formulario de Comentarios')
    ]
    
    tabs = st.tabs(tab_names)

    # Manejar el contenido de cada tab
    for index, tab in enumerate(tabs):
        with tab:
            try:
                # Actualizar el tab seleccionado solo si no hay un an谩lisis activo
                if tab.selected and st.session_state.selected_tab != index:
                    can_switch = True
                    for state_key in st.session_state.tab_states.keys():
                        if st.session_state.tab_states[state_key] and index != get_tab_index(state_key):
                            can_switch = False
                            break
                    if can_switch:
                        st.session_state.selected_tab = index

                if index == 0:  # Sem谩ntico
                    st.session_state.tab_states['semantic_active'] = True
                    display_semantic_interface(
                        st.session_state.lang_code,
                        st.session_state.nlp_models,
                        t  # Pasamos todo el diccionario de traducciones
                    )
                
                elif index == 1:  # Discurso
                    st.session_state.tab_states['discourse_active'] = True
                    display_discourse_interface(
                        st.session_state.lang_code,
                        st.session_state.nlp_models,
                        t  # Pasamos todo el diccionario de traducciones
                    )

                elif index == 2:  # Actividades
                    st.session_state.tab_states['activities_active'] = True
                    display_student_activities(
                        username=st.session_state.username,
                        lang_code=st.session_state.lang_code,
                        t=t  # Pasamos todo el diccionario de traducciones
                    )

                elif index == 3:  # Feedback
                    st.session_state.tab_states['feedback_active'] = True
                    display_feedback_form(
                        st.session_state.lang_code,
                        t  # Ya estaba recibiendo el diccionario completo
                    )

            except Exception as e:
                # Desactivar el estado en caso de error
                state_key = get_state_key_for_index(index)
                if state_key:
                    st.session_state.tab_states[state_key] = False
                logger.error(f"Error en tab {index}: {str(e)}")
                st.error(t.get('tab_error', 'Error al cargar esta secci贸n'))

    # Panel de depuraci贸n (solo visible en desarrollo)
    if st.session_state.get('debug_mode', False):
        with st.expander("Debug Info"):
            st.write(f"P谩gina actual: {st.session_state.page}")
            st.write(f"Usuario: {st.session_state.get('username', 'No logueado')}")
            st.write(f"Rol: {st.session_state.get('role', 'No definido')}")
            st.write(f"Idioma: {st.session_state.lang_code}")
            st.write(f"Tab seleccionado: {st.session_state.selected_tab}")
            st.write(f"脷ltima actualizaci贸n de datos: {st.session_state.get('last_data_fetch', 'Nunca')}")
            st.write(f"Traducciones disponibles: {list(t.keys())}")


def get_tab_index(state_key):
    """Obtiene el 铆ndice del tab basado en la clave de estado"""
    index_map = {
        'semantic_active': 0,
        'discourse_active': 1,
        'activities_active': 2,
        'feedback_active': 3
    }
    return index_map.get(state_key, -1)

def get_state_key_for_index(index):
    """Obtiene la clave de estado basada en el 铆ndice del tab"""
    state_map = {
        0: 'semantic_active',
        1: 'discourse_active',
        2: 'activities_active',
        3: 'feedback_active'
    }
    return state_map.get(index)

def display_feedback_form(lang_code, t):
    """
    Muestra el formulario de retroalimentaci贸n
    Args:
        lang_code: C贸digo de idioma
        t: Diccionario de traducciones
    """
    logging.info(f"display_feedback_form called with lang_code: {lang_code}")

    # Obtener traducciones espec铆ficas para el formulario de feedback
    feedback_t = t.get('FEEDBACK', {})
    
    # Si no hay traducciones espec铆ficas, usar el diccionario general
    if not feedback_t:
        feedback_t = t

    #st.header(feedback_t.get('feedback_title', 'Formulario de Opini贸n'))

    name = st.text_input(feedback_t.get('name', 'Nombre'))
    email = st.text_input(feedback_t.get('email', 'Correo electr贸nico'))
    feedback = st.text_area(feedback_t.get('feedback', 'Retroalimentaci贸n'))

    if st.button(feedback_t.get('submit', 'Enviar')):
        if name and email and feedback:
            if store_student_feedback(st.session_state.username, name, email, feedback):
                st.success(feedback_t.get('feedback_success', 'Gracias por tu respuesta'))
            else:
                st.error(feedback_t.get('feedback_error', 'Hubo un problema al enviar el formulario. Por favor, intenta de nuevo.'))
        else:
            st.warning(feedback_t.get('complete_all_fields', 'Por favor, completa todos los campos'))