You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
5.5 KiB
JavaScript
190 lines
5.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import api from '../axiosPostulante'
|
|
|
|
export const useExamenStore = defineStore('examenStore', {
|
|
state: () => ({
|
|
procesos: [],
|
|
areas: [],
|
|
examenActual: null,
|
|
preguntas: [],
|
|
cargando: false,
|
|
calificando: false,
|
|
resultado: null,
|
|
error: null,
|
|
}),
|
|
|
|
actions: {
|
|
async fetchProcesos() {
|
|
try {
|
|
this.cargando = true
|
|
const { data } = await api.get('/examen/procesos')
|
|
|
|
this.procesos = (data || []).map(p => ({
|
|
...p,
|
|
requiere_pago: p.requiere_pago === 1 || p.requiere_pago === '1' || p.requiere_pago === true
|
|
}))
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
} finally {
|
|
this.cargando = false
|
|
}
|
|
},
|
|
|
|
async fetchAreas(proceso_id) {
|
|
try {
|
|
this.cargando = true
|
|
const { data } = await api.get('/examen/areas', { params: { proceso_id } })
|
|
this.areas = data
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
} finally {
|
|
this.cargando = false
|
|
}
|
|
},
|
|
|
|
async crearExamen(area_proceso_id, pago = null) {
|
|
try {
|
|
this.cargando = true
|
|
const payload = { area_proceso_id, ...pago }
|
|
const { data } = await api.post('/examen/crear', payload)
|
|
this.examenActual = { id: data.examen_id }
|
|
return data
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
return { success: false, message: this.error }
|
|
} finally {
|
|
this.cargando = false
|
|
}
|
|
},
|
|
|
|
async fetchExamenActual() {
|
|
try {
|
|
this.cargando = true
|
|
const { data } = await api.get('/examen/actual')
|
|
this.examenActual = data.examen
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
} finally {
|
|
this.cargando = false
|
|
}
|
|
},
|
|
|
|
async generarPreguntas(examenId) {
|
|
try {
|
|
this.cargando = true
|
|
const { data } = await api.post(`/examen/${examenId}/generar-preguntas`)
|
|
return data
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
return { success: false, message: this.error }
|
|
} finally {
|
|
this.cargando = false
|
|
}
|
|
},
|
|
|
|
async iniciarExamen(examenId) {
|
|
try {
|
|
this.cargando = true
|
|
const { data } = await api.post('/examen/iniciar', { examen_id: examenId })
|
|
this.examenActual = data.examen
|
|
this.preguntas = data.preguntas
|
|
return data
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
return { success: false, message: this.error }
|
|
} finally {
|
|
this.cargando = false
|
|
}
|
|
},
|
|
|
|
async responderPregunta(preguntaId, respuesta) {
|
|
try {
|
|
const { data } = await api.post(`/examen/pregunta/${preguntaId}/responder`, { respuesta })
|
|
|
|
const index = this.preguntas.findIndex(p => p.id === preguntaId)
|
|
|
|
if (index !== -1 && data.success) {
|
|
this.preguntas[index].respuesta = respuesta
|
|
this.preguntas[index].es_correcta = data.correcta
|
|
this.preguntas[index].puntaje = data.puntaje
|
|
}
|
|
|
|
return data
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
return { success: false, message: this.error }
|
|
}
|
|
},
|
|
|
|
async calificarExamen(examenId) {
|
|
try {
|
|
this.error = null
|
|
this.calificando = true
|
|
|
|
const { data } = await api.post(`/examen/${examenId}/calificar`)
|
|
|
|
if (data?.success) {
|
|
this.resultado = {
|
|
examen_id: data.examen_id,
|
|
proceso_id: data.proceso_id,
|
|
total_puntos: data.total_puntos,
|
|
total_correctas: data.total_correctas,
|
|
total_incorrectas: data.total_incorrectas,
|
|
total_nulas: data.total_nulas,
|
|
porcentaje_correctas: data.porcentaje_correctas,
|
|
calificacion_sobre_20: data.calificacion_sobre_20,
|
|
orden_merito: data.orden_merito,
|
|
correctas_por_curso: data.correctas_por_curso,
|
|
incorrectas_por_curso: data.incorrectas_por_curso,
|
|
preguntas_totales_por_curso: data.preguntas_totales_por_curso,
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
this.error = data?.mensaje || data?.message || 'No se pudo calificar el examen.'
|
|
return { success: false, message: this.error }
|
|
} catch (e) {
|
|
const msg = e.response?.data?.mensaje || e.response?.data?.message || e.message
|
|
this.error = msg
|
|
return { success: false, message: msg, status: e.response?.status }
|
|
} finally {
|
|
this.calificando = false
|
|
}
|
|
},
|
|
|
|
async finalizarExamen(examenId) {
|
|
try {
|
|
const { data } = await api.post(`/examen/${examenId}/finalizar`)
|
|
|
|
if (data?.success) {
|
|
if (this.examenActual) {
|
|
this.examenActual.estado = 'finalizado'
|
|
this.examenActual.hora_fin = new Date().toISOString()
|
|
}
|
|
|
|
this.examenActual = null
|
|
this.preguntas = []
|
|
this.error = null
|
|
}
|
|
|
|
return data
|
|
} catch (e) {
|
|
this.error = e.response?.data?.message || e.message
|
|
return { success: false, message: this.error }
|
|
}
|
|
},
|
|
|
|
resetStore() {
|
|
this.procesos = []
|
|
this.areas = []
|
|
this.examenActual = null
|
|
this.preguntas = []
|
|
this.cargando = false
|
|
this.calificando = false
|
|
this.resultado = null
|
|
this.error = null
|
|
}
|
|
}
|
|
})
|