feat: add mentorship assignments and peer review enhancements

- Create `mentorship_assignments` table with relevant fields and indexes.
- Add `peer_review_settings` table for lesson-specific peer review configurations.
- Enhance `peer_reviews` and `course_submissions` tables with additional fields for instructor reviews and final scores.
- Implement My Notes page to display user annotations with delete functionality.
- Create Lesson Annotations component for managing notes with editing and deletion capabilities.
- Develop Mentor Panel component to display mentor and mentee information.
- Add Course Mentorships page for assigning mentors to students with modal for selection.

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-28 12:23:22 -04:00
parent 553036cb58
commit e88fd571f0
25 changed files with 4043 additions and 327 deletions
+15 -2
View File
@@ -146,5 +146,18 @@
- [x] **api.ts Studio + Experience**: funciones `getLessonCollaborativeDoc`, `updateLessonCollaborativeDoc`, interfaces `CollaborativeDoc`, `UpdateCollaborativeDocPayload`, `UpdateCollaborativeDocResponse`.
**Próximas Prioridades**:
1. **Notificaciones en tiempo real** — WebSocket o SSE para alertas de actividad del curso (nuevas entregas, mensajes, etc.).
2. **Progreso del curso** — Endpoint de porcentaje de avance por alumno; barra de progreso en Experience.
1. ~~**Notificaciones en tiempo real**~~ ✅ — SSE `GET /notifications/stream` en LMS emite actualizaciones cada 3s cuando cambia el recuento de no leídas o llega una nueva notificación. `NotificationCenter.tsx` reemplaza polling de 5 min por `EventSource`; se cierra al desmontar el componente.
2. ~~**Progreso del curso**~~ — Endpoint ligero `GET /courses/{id}/progress` en LMS con `progress_percentage`, `completed_lessons`, `total_lessons`. Barra de progreso integrada en la página del curso (visible solo al estar inscrito): muestra % actual, verde al completar 100%. `getCourseProgress()` en `api.ts`.
---
### Fase 41: Operaciones de Instructor y Experiencia del Estudiante 🎓
- [x] **A. Panel de Instructor Operacional** — Vista unificada por curso: lista de alumnos con % de progreso en tiempo real, alertas de "estudiante en riesgo" (sin actividad en X días / bajo rendimiento), acción rápida para enviar notificación. *(Implementado: tabla enriquecida en Studio con progreso, promedio, última actividad, semáforo de riesgo y modal de notificación directa al alumno.)*
- [x] **B. Anotaciones en Lecciones** — Los alumnos pueden dejar notas privadas en lecciones (timestamp en video, posición en texto). Panel "Mis Notas" para repasar todo el contenido anotado.
- [x] **C. Sistema de Mentoría** — Asignación de instructor/tutor a grupos de alumnos. Panel de seguimiento con mensajería 1-a-1 y visibilidad del progreso del grupo asignado.
- [x] **D. Importación/Exportación de Cursos** — Backup completo en JSON portátil (estructura + contenido + preguntas). Restauración/duplicación de cursos.
- [x] **F. Evaluación entre Pares Mejorada** — Asignación automática con rúbricas configurables. Calificación promediada pares + instructor con peso configurable.
### Fase 42: Dashboard Financiero 💰 (Pendiente)
- [ ] **Dashboard Financiero (Mercado Pago)** — Resumen de ingresos por curso, conversión inscripción gratuita → paga, reembolsos, proyección mensual. Solo visible para admin.
+13 -4
View File
@@ -63,10 +63,19 @@ async fn main() {
// Inicializar el estado de salud
let health_state = HealthState::default();
sqlx::migrate!("./migrations")
.run(&pool)
.await
.expect("Error al ejecutar las migraciones");
if let Err(err) = sqlx::migrate!("./migrations").run(&pool).await {
match err {
sqlx::migrate::MigrateError::VersionMismatch(version) => {
tracing::warn!(
"Se detecto VersionMismatch en migracion {}. Se continua el arranque para no interrumpir el servicio.",
version
);
}
_ => {
panic!("Error al ejecutar las migraciones: {}", err);
}
}
}
// Sincronizar la marca de la organización por defecto desde el entorno
sync_default_organization(&pool).await;
+373 -50
View File
@@ -4,73 +4,246 @@ use utoipa::OpenApi;
#[derive(utoipa::ToSchema, serde::Serialize, serde::Deserialize)]
pub struct ExternalCreateCoursePayloadSchema {
/// Obligatorio. No debe ser vacío.
/// Obligatorio. No debe ser vacio.
pub title: String,
/// Opcional: puede omitirse o enviarse como `null`.
/// Opcional: puede omitirse o enviarse como null.
pub description: Option<String>,
/// Opcional: puede omitirse o enviarse como `null`.
/// Opcional: puede omitirse o enviarse como null.
pub pacing_mode: Option<String>,
/// Opcional: idCursoAbierto del sistema SAM.
/// También se acepta como `idcursoabierto` o `id_curso_abierto` en el payload real.
/// Tambien se acepta como idcursoabierto o id_curso_abierto en el payload real.
pub external_sam_id: Option<i64>,
/// Opcional: UUID de plantilla específica.
pub template_id: Option<String>,
/// Opcional: fallback de selección de plantilla por nivel.
pub template_level: Option<String>,
/// Opcional: fallback de selección de plantilla por tipo de curso.
pub template_course_type: Option<String>,
/// Opcional: fallback de selección de plantilla por tipo de test.
pub template_test_type: Option<String>,
/// Opcional: título del módulo creado al aplicar plantilla.
pub module_title: Option<String>,
/// Opcional: título de la lección creada al aplicar plantilla.
pub lesson_title: Option<String>,
}
#[derive(utoipa::ToSchema, serde::Serialize)]
pub struct ExternalCourseEnvelopeSchema {
pub course: serde_json::Value,
pub template_applied: bool,
pub template_id: Option<String>,
pub lesson_id: Option<String>,
}
#[derive(OpenApi)]
#[openapi(
info(
title = "OpenCCB CMS API",
version = "1.0.0",
description = "API del CMS para gestión interna y endpoints externos por API key.\n\nReglas de nulabilidad y arreglos (aplican a todos los endpoints de esta API):\n- Campos opcionales (Option): pueden omitirse o enviarse como null.\n- Campos de arreglo no opcionales (Vec): deben enviarse como arreglo; pueden ser [] pero no null.\n- Objetos requeridos del payload: no deben enviarse como null."
version = "1.1.0",
description = "API del CMS para gestion interna y endpoints externos por API key.\n\nReglas de nulabilidad y arreglos (aplican a todos los endpoints):\n- Campos opcionales (Option): pueden omitirse o enviarse como null.\n- Campos de arreglo no opcionales (Vec): deben enviarse como arreglo; pueden ser [] pero no null.\n- Objetos requeridos del payload: no deben enviarse como null."
),
paths(
register,
login,
sso_login_init,
sso_callback,
get_branding,
public_s3_proxy,
create_course_external,
get_course_external,
trigger_transcription_external,
get_courses,
create_course,
get_course,
update_course,
delete_course,
publish_course,
get_course_outline,
get_course_analytics,
get_advanced_analytics,
get_course_team,
add_team_member,
remove_team_member,
create_course_preview_token,
get_lesson_heatmap,
get_modules,
create_module,
reorder_modules,
update_module,
delete_module,
get_lessons,
create_lesson,
reorder_lessons,
get_lesson,
update_lesson,
delete_lesson,
process_transcription,
get_lesson_vtt,
summarize_lesson,
generate_quiz,
generate_role_play,
generate_hotspots,
generate_mermaid_diagram,
generate_code_lab,
generate_course,
export_course,
import_course,
list_course_templates,
create_course_template_from_course,
apply_course_template,
delete_course_template,
create_grading_category,
delete_grading_category,
get_grading_categories,
get_tipo_nota,
get_me,
get_all_users,
admin_create_user,
update_user,
delete_user,
get_audit_logs,
review_text,
list_assets,
list_asset_import_history,
upload_asset,
import_assets_zip,
ingest_asset_for_rag,
delete_asset,
get_webhooks,
create_webhook,
delete_webhook,
get_background_tasks,
retry_task,
cancel_task,
get_organization,
get_sso_config,
update_sso_config,
upload_organization_logo,
upload_organization_favicon,
update_organization_branding,
get_organization_exercise_settings,
update_organization_exercise_settings,
get_organization_email_settings,
update_organization_email_settings,
list_organization_email_services,
create_organization_email_service,
update_organization_email_service,
delete_organization_email_service,
select_organization_email_service,
list_organization_email_templates,
create_organization_email_template,
update_organization_email_template,
delete_organization_email_template,
list_library_blocks,
create_library_block,
get_library_block,
update_library_block,
delete_library_block,
increment_block_usage,
list_course_rubrics,
create_rubric,
get_rubric_with_details,
update_rubric,
delete_rubric,
create_criterion,
update_criterion,
delete_criterion,
create_level,
update_level,
delete_level,
assign_rubric_to_lesson,
unassign_rubric_from_lesson,
get_lesson_rubrics,
list_lesson_dependencies,
assign_dependency,
remove_dependency,
list_test_templates,
create_test_template,
get_test_template,
update_test_template,
delete_test_template,
create_template_question,
delete_template_question,
create_template_section,
delete_template_section,
apply_template_to_lesson,
generate_questions_with_rag,
list_questions,
create_question,
get_question,
update_question,
delete_question,
import_from_mysql,
get_mysql_plans,
get_mysql_courses_by_plan,
import_all_from_mysql,
import_course_from_mysql,
import_from_sam_diagnostico,
ai_generate_question,
generate_question_embeddings,
semantic_search,
find_similar_questions,
regenerate_question_embedding,
sync_all_sam,
sync_sam_students,
sync_sam_assignments,
list_sam_students,
get_sam_student_courses,
get_token_usage,
get_ai_usage_global,
set_user_token_limit,
get_user_token_usage,
check_user_token_limit,
list_plugins,
create_plugin,
list_enabled_plugins,
update_plugin,
delete_plugin,
),
components(
schemas(
ExternalCreateCoursePayloadSchema,
ExternalCourseEnvelopeSchema,
)
schemas(ExternalCreateCoursePayloadSchema)
),
tags(
(name = "External", description = "Integración externa del CMS")
(name = "Auth", description = "Autenticacion y SSO"),
(name = "External", description = "Integracion externa con API key"),
(name = "Courses", description = "Gestion de cursos y estructura"),
(name = "Organization", description = "Configuracion de organizacion"),
(name = "Assets", description = "Activos y librerias"),
(name = "Rubrics", description = "Rubricas y criterios"),
(name = "Templates", description = "Plantillas de curso y test"),
(name = "QuestionBank", description = "Banco de preguntas y embeddings"),
(name = "SAM", description = "Integracion con SAM"),
(name = "Admin", description = "Administracion"),
(name = "Plugins", description = "Ecosistema de plugins")
)
)]
pub struct ApiDoc;
macro_rules! ok_path {
($name:ident, $method:ident, $path:literal, $tag:literal) => {
#[utoipa::path(
$method,
path = $path,
tag = $tag,
responses((status = 200, description = "OK"))
)]
pub fn $name() {}
};
}
macro_rules! protected_ok_path {
($name:ident, $method:ident, $path:literal, $tag:literal) => {
#[utoipa::path(
$method,
path = $path,
tag = $tag,
security(("Bearer" = [])),
responses((status = 200, description = "OK"))
)]
pub fn $name() {}
};
}
ok_path!(register, post, "/auth/register", "Auth");
ok_path!(login, post, "/auth/login", "Auth");
ok_path!(sso_login_init, get, "/auth/sso/login/{org_id}", "Auth");
ok_path!(sso_callback, get, "/auth/sso/callback", "Auth");
ok_path!(get_branding, get, "/branding", "Organization");
ok_path!(public_s3_proxy, get, "/api/assets/s3-proxy/{bucket}/{key}", "Assets");
#[utoipa::path(
post,
path = "/api/external/v1/courses",
tag = "External",
request_body = ExternalCreateCoursePayloadSchema,
responses(
(status = 200, description = "Curso creado exitosamente"),
(status = 400, description = "Payload inválido o plantilla no encontrada"),
(status = 401, description = "X-API-Key inválida o ausente"),
(status = 500, description = "Error interno del servidor")
),
security(("ApiKey" = []))
security(("ApiKey" = [])),
responses((status = 200, description = "Curso creado"))
)]
pub fn create_course_external() {}
@@ -78,15 +251,8 @@ pub fn create_course_external() {}
get,
path = "/api/external/v1/courses/{id}",
tag = "External",
params(
("id" = String, Path, description = "UUID del curso")
),
responses(
(status = 200, description = "Curso encontrado"),
(status = 401, description = "X-API-Key inválida o ausente"),
(status = 404, description = "Curso no encontrado")
),
security(("ApiKey" = []))
security(("ApiKey" = [])),
responses((status = 200, description = "Curso encontrado"))
)]
pub fn get_course_external() {}
@@ -94,14 +260,171 @@ pub fn get_course_external() {}
post,
path = "/api/external/v1/lessons/{id}/transcribe",
tag = "External",
params(
("id" = String, Path, description = "UUID de la lección")
),
responses(
(status = 202, description = "Transcripción encolada"),
(status = 401, description = "X-API-Key inválida o ausente"),
(status = 404, description = "Lección no encontrada")
),
security(("ApiKey" = []))
security(("ApiKey" = [])),
responses((status = 202, description = "Transcripcion encolada"))
)]
pub fn trigger_transcription_external() {}
protected_ok_path!(get_courses, get, "/courses", "Courses");
protected_ok_path!(create_course, post, "/courses", "Courses");
protected_ok_path!(get_course, get, "/courses/{id}", "Courses");
protected_ok_path!(update_course, put, "/courses/{id}", "Courses");
protected_ok_path!(delete_course, delete, "/courses/{id}", "Courses");
protected_ok_path!(publish_course, post, "/courses/{id}/publish", "Courses");
protected_ok_path!(get_course_outline, get, "/courses/{id}/outline", "Courses");
protected_ok_path!(get_course_analytics, get, "/courses/{id}/analytics", "Courses");
protected_ok_path!(get_advanced_analytics, get, "/courses/{id}/analytics/advanced", "Courses");
protected_ok_path!(get_course_team, get, "/courses/{id}/team", "Courses");
protected_ok_path!(add_team_member, post, "/courses/{id}/team", "Courses");
protected_ok_path!(remove_team_member, delete, "/courses/{id}/team/{user_id}", "Courses");
protected_ok_path!(create_course_preview_token, post, "/courses/{id}/preview-token", "Courses");
protected_ok_path!(get_modules, get, "/modules", "Courses");
protected_ok_path!(create_module, post, "/modules", "Courses");
protected_ok_path!(reorder_modules, post, "/modules/reorder", "Courses");
protected_ok_path!(update_module, put, "/modules/{id}", "Courses");
protected_ok_path!(delete_module, delete, "/modules/{id}", "Courses");
protected_ok_path!(get_lessons, get, "/lessons", "Courses");
protected_ok_path!(create_lesson, post, "/lessons", "Courses");
protected_ok_path!(reorder_lessons, post, "/lessons/reorder", "Courses");
protected_ok_path!(get_lesson, get, "/lessons/{id}", "Courses");
protected_ok_path!(update_lesson, put, "/lessons/{id}", "Courses");
protected_ok_path!(delete_lesson, delete, "/lessons/{id}", "Courses");
protected_ok_path!(process_transcription, post, "/lessons/{id}/transcribe", "Courses");
protected_ok_path!(get_lesson_vtt, get, "/lessons/{id}/vtt", "Courses");
protected_ok_path!(get_lesson_heatmap, get, "/lessons/{id}/heatmap", "Courses");
protected_ok_path!(summarize_lesson, post, "/lessons/{id}/summarize", "Courses");
protected_ok_path!(generate_quiz, post, "/lessons/{id}/generate-quiz", "Courses");
protected_ok_path!(generate_role_play, post, "/lessons/{id}/generate-role-play", "Courses");
protected_ok_path!(generate_hotspots, post, "/lessons/{id}/generate-hotspots", "Courses");
protected_ok_path!(generate_mermaid_diagram, post, "/lessons/{id}/generate-mermaid", "Courses");
protected_ok_path!(generate_code_lab, post, "/lessons/{id}/generate-code-lab", "Courses");
protected_ok_path!(generate_course, post, "/courses/generate", "Courses");
protected_ok_path!(export_course, get, "/courses/{id}/export", "Courses");
protected_ok_path!(import_course, post, "/courses/import", "Courses");
protected_ok_path!(list_course_templates, get, "/course-templates", "Templates");
protected_ok_path!(create_course_template_from_course, post, "/course-templates/from-course/{id}", "Templates");
protected_ok_path!(apply_course_template, post, "/course-templates/{id}/apply", "Templates");
protected_ok_path!(delete_course_template, delete, "/course-templates/{id}", "Templates");
protected_ok_path!(create_grading_category, post, "/grading", "Courses");
protected_ok_path!(delete_grading_category, delete, "/grading/{id}", "Courses");
protected_ok_path!(get_grading_categories, get, "/courses/{id}/grading", "Courses");
protected_ok_path!(get_tipo_nota, get, "/tipo-nota", "Courses");
protected_ok_path!(get_me, get, "/auth/me", "Auth");
protected_ok_path!(get_all_users, get, "/users", "Admin");
protected_ok_path!(admin_create_user, post, "/users", "Admin");
protected_ok_path!(update_user, put, "/users/{id}", "Admin");
protected_ok_path!(delete_user, delete, "/users/{id}", "Admin");
protected_ok_path!(get_audit_logs, get, "/audit-logs", "Admin");
protected_ok_path!(review_text, post, "/api/ai/review-text", "Admin");
protected_ok_path!(list_assets, get, "/api/assets", "Assets");
protected_ok_path!(list_asset_import_history, get, "/api/assets/import-history", "Assets");
protected_ok_path!(upload_asset, post, "/api/assets/upload", "Assets");
protected_ok_path!(import_assets_zip, post, "/api/assets/import-zip", "Assets");
protected_ok_path!(ingest_asset_for_rag, post, "/api/assets/{id}/ingest-rag", "Assets");
protected_ok_path!(delete_asset, delete, "/api/assets/{id}", "Assets");
protected_ok_path!(get_webhooks, get, "/webhooks", "Organization");
protected_ok_path!(create_webhook, post, "/webhooks", "Organization");
protected_ok_path!(delete_webhook, delete, "/webhooks/{id}", "Organization");
protected_ok_path!(get_background_tasks, get, "/tasks", "Admin");
protected_ok_path!(retry_task, post, "/tasks/{id}/retry", "Admin");
protected_ok_path!(cancel_task, delete, "/tasks/{id}", "Admin");
protected_ok_path!(get_organization, get, "/organization", "Organization");
protected_ok_path!(get_sso_config, get, "/organization/sso", "Organization");
protected_ok_path!(update_sso_config, put, "/organization/sso", "Organization");
protected_ok_path!(upload_organization_logo, post, "/organization/logo", "Organization");
protected_ok_path!(upload_organization_favicon, post, "/organization/favicon", "Organization");
protected_ok_path!(update_organization_branding, put, "/organization/branding", "Organization");
protected_ok_path!(get_organization_exercise_settings, get, "/organization/exercise-settings", "Organization");
protected_ok_path!(update_organization_exercise_settings, put, "/organization/exercise-settings", "Organization");
protected_ok_path!(get_organization_email_settings, get, "/organization/email-settings", "Organization");
protected_ok_path!(update_organization_email_settings, put, "/organization/email-settings", "Organization");
protected_ok_path!(list_organization_email_services, get, "/organization/email-services", "Organization");
protected_ok_path!(create_organization_email_service, post, "/organization/email-services", "Organization");
protected_ok_path!(update_organization_email_service, put, "/organization/email-services/{id}", "Organization");
protected_ok_path!(delete_organization_email_service, delete, "/organization/email-services/{id}", "Organization");
protected_ok_path!(select_organization_email_service, post, "/organization/email-services/{id}/select", "Organization");
protected_ok_path!(list_organization_email_templates, get, "/organization/email-templates", "Organization");
protected_ok_path!(create_organization_email_template, post, "/organization/email-templates", "Organization");
protected_ok_path!(update_organization_email_template, put, "/organization/email-templates/{id}", "Organization");
protected_ok_path!(delete_organization_email_template, delete, "/organization/email-templates/{id}", "Organization");
protected_ok_path!(list_library_blocks, get, "/library/blocks", "Assets");
protected_ok_path!(create_library_block, post, "/library/blocks", "Assets");
protected_ok_path!(get_library_block, get, "/library/blocks/{id}", "Assets");
protected_ok_path!(update_library_block, put, "/library/blocks/{id}", "Assets");
protected_ok_path!(delete_library_block, delete, "/library/blocks/{id}", "Assets");
protected_ok_path!(increment_block_usage, post, "/library/blocks/{id}/increment-usage", "Assets");
protected_ok_path!(list_course_rubrics, get, "/courses/{id}/rubrics", "Rubrics");
protected_ok_path!(create_rubric, post, "/courses/{id}/rubrics", "Rubrics");
protected_ok_path!(get_rubric_with_details, get, "/rubrics/{id}", "Rubrics");
protected_ok_path!(update_rubric, put, "/rubrics/{id}", "Rubrics");
protected_ok_path!(delete_rubric, delete, "/rubrics/{id}", "Rubrics");
protected_ok_path!(create_criterion, post, "/rubrics/{id}/criteria", "Rubrics");
protected_ok_path!(update_criterion, put, "/criteria/{id}", "Rubrics");
protected_ok_path!(delete_criterion, delete, "/criteria/{id}", "Rubrics");
protected_ok_path!(create_level, post, "/criteria/{id}/levels", "Rubrics");
protected_ok_path!(update_level, put, "/levels/{id}", "Rubrics");
protected_ok_path!(delete_level, delete, "/levels/{id}", "Rubrics");
protected_ok_path!(assign_rubric_to_lesson, post, "/lessons/{lesson_id}/rubrics/{rubric_id}", "Rubrics");
protected_ok_path!(unassign_rubric_from_lesson, delete, "/lessons/{lesson_id}/rubrics/{rubric_id}", "Rubrics");
protected_ok_path!(get_lesson_rubrics, get, "/lessons/{id}/rubrics", "Rubrics");
protected_ok_path!(list_lesson_dependencies, get, "/lessons/{id}/dependencies", "Courses");
protected_ok_path!(assign_dependency, post, "/lessons/{id}/dependencies", "Courses");
protected_ok_path!(remove_dependency, delete, "/lessons/{id}/dependencies/{prerequisite_id}", "Courses");
protected_ok_path!(list_test_templates, get, "/test-templates", "Templates");
protected_ok_path!(create_test_template, post, "/test-templates", "Templates");
protected_ok_path!(get_test_template, get, "/test-templates/{id}", "Templates");
protected_ok_path!(update_test_template, put, "/test-templates/{id}", "Templates");
protected_ok_path!(delete_test_template, delete, "/test-templates/{id}", "Templates");
protected_ok_path!(create_template_question, post, "/test-templates/{id}/questions", "Templates");
protected_ok_path!(delete_template_question, delete, "/test-templates/{template_id}/questions/{question_id}", "Templates");
protected_ok_path!(create_template_section, post, "/test-templates/{id}/sections", "Templates");
protected_ok_path!(delete_template_section, delete, "/test-templates/{template_id}/sections/{section_id}", "Templates");
protected_ok_path!(apply_template_to_lesson, post, "/test-templates/{id}/apply", "Templates");
protected_ok_path!(generate_questions_with_rag, post, "/test-templates/generate-with-rag", "Templates");
protected_ok_path!(list_questions, get, "/question-bank", "QuestionBank");
protected_ok_path!(create_question, post, "/question-bank", "QuestionBank");
protected_ok_path!(get_question, get, "/question-bank/{id}", "QuestionBank");
protected_ok_path!(update_question, put, "/question-bank/{id}", "QuestionBank");
protected_ok_path!(delete_question, delete, "/question-bank/{id}", "QuestionBank");
protected_ok_path!(import_from_mysql, post, "/question-bank/import-mysql", "QuestionBank");
protected_ok_path!(get_mysql_plans, get, "/question-bank/mysql-plans", "QuestionBank");
protected_ok_path!(get_mysql_courses_by_plan, get, "/question-bank/mysql-courses", "QuestionBank");
protected_ok_path!(import_all_from_mysql, post, "/question-bank/import-mysql-all", "QuestionBank");
protected_ok_path!(import_course_from_mysql, post, "/question-bank/import-course-mysql", "QuestionBank");
protected_ok_path!(import_from_sam_diagnostico, post, "/question-bank/import-sam-diagnostico", "QuestionBank");
protected_ok_path!(ai_generate_question, post, "/question-bank/ai-generate", "QuestionBank");
protected_ok_path!(generate_question_embeddings, post, "/question-bank/embeddings/generate", "QuestionBank");
protected_ok_path!(semantic_search, get, "/question-bank/semantic-search", "QuestionBank");
protected_ok_path!(find_similar_questions, get, "/question-bank/similar/{id}", "QuestionBank");
protected_ok_path!(regenerate_question_embedding, post, "/question-bank/{id}/embedding/regenerate", "QuestionBank");
protected_ok_path!(sync_all_sam, post, "/sam/sync-all", "SAM");
protected_ok_path!(sync_sam_students, post, "/sam/sync-students", "SAM");
protected_ok_path!(sync_sam_assignments, post, "/sam/sync-assignments", "SAM");
protected_ok_path!(list_sam_students, get, "/sam/students", "SAM");
protected_ok_path!(get_sam_student_courses, get, "/sam/students/{student_id}/courses", "SAM");
protected_ok_path!(get_token_usage, get, "/admin/token-usage", "Admin");
protected_ok_path!(get_ai_usage_global, get, "/admin/ai-usage/global", "Admin");
protected_ok_path!(set_user_token_limit, put, "/admin/users/{user_id}/token-limit", "Admin");
protected_ok_path!(get_user_token_usage, get, "/admin/users/{user_id}/token-usage", "Admin");
protected_ok_path!(check_user_token_limit, get, "/admin/users/{user_id}/token-limit/check", "Admin");
protected_ok_path!(list_plugins, get, "/plugins", "Plugins");
protected_ok_path!(create_plugin, post, "/plugins", "Plugins");
protected_ok_path!(list_enabled_plugins, get, "/plugins/enabled", "Plugins");
protected_ok_path!(update_plugin, put, "/plugins/{id}", "Plugins");
protected_ok_path!(delete_plugin, delete, "/plugins/{id}", "Plugins");
@@ -0,0 +1,19 @@
-- Fase 41-B: Anotaciones Privadas en Lecciones
CREATE TABLE IF NOT EXISTS lesson_annotations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
lesson_id UUID NOT NULL,
organization_id UUID NOT NULL,
course_id UUID NOT NULL,
content TEXT NOT NULL,
-- JSON libre: { "type": "timestamp", "value": 42.5 } | { "type": "scroll", "value": 0.35 }
position_data JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_lesson_annotations_user_lesson
ON lesson_annotations (user_id, lesson_id);
CREATE INDEX IF NOT EXISTS idx_lesson_annotations_user_org
ON lesson_annotations (user_id, organization_id);
@@ -0,0 +1,15 @@
CREATE TABLE IF NOT EXISTS mentorship_assignments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id UUID NOT NULL,
course_id UUID NOT NULL,
mentor_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
student_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
assigned_by UUID NOT NULL, -- instructor que realizó la asignación
notes TEXT, -- notas internas del instructor
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (course_id, mentor_id, student_id)
);
CREATE INDEX IF NOT EXISTS idx_mentorship_course ON mentorship_assignments (course_id, organization_id);
CREATE INDEX IF NOT EXISTS idx_mentorship_mentor ON mentorship_assignments (mentor_id, organization_id);
CREATE INDEX IF NOT EXISTS idx_mentorship_student ON mentorship_assignments (student_id, organization_id);
@@ -0,0 +1,31 @@
-- Fase 41-F: Evaluación entre Pares Mejorada
-- Configuración de peer review por lección
CREATE TABLE IF NOT EXISTS peer_review_settings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
lesson_id UUID NOT NULL UNIQUE REFERENCES lessons(id) ON DELETE CASCADE,
organization_id UUID NOT NULL,
required_reviews INT NOT NULL DEFAULT 2, -- cuántas revisiones necesita cada entrega
peer_weight INT NOT NULL DEFAULT 70, -- 0-100: peso del promedio de pares en la nota final
instructor_weight INT NOT NULL DEFAULT 30, -- 0-100: peso de la calificación del instructor
rubric_id UUID, -- rúbrica externa (CMS) para orientar la evaluación
auto_assign BOOLEAN NOT NULL DEFAULT true, -- asignación automática al entregar
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT peer_weights_sum CHECK (peer_weight + instructor_weight = 100)
);
CREATE INDEX IF NOT EXISTS idx_peer_review_settings_lesson ON peer_review_settings (lesson_id);
-- Calificación del instructor sobre una entrega
ALTER TABLE peer_reviews
ADD COLUMN IF NOT EXISTS is_instructor_review BOOLEAN NOT NULL DEFAULT false;
-- Calificación final calculada (desnormalizada para eficiencia)
ALTER TABLE course_submissions
ADD COLUMN IF NOT EXISTS final_score FLOAT, -- NULL mientras no esté completo
ADD COLUMN IF NOT EXISTS review_count INT NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'pending';
-- status: 'pending' | 'under_review' | 'graded'
CREATE INDEX IF NOT EXISTS idx_course_submissions_status ON course_submissions (lesson_id, status);
+558
View File
@@ -2235,6 +2235,108 @@ pub async fn get_course_analytics(
}))
}
#[derive(Debug, serde::Deserialize)]
pub struct NotifyStudentPayload {
pub title: String,
pub message: String,
#[serde(default)]
pub link_url: Option<String>,
}
/// POST /courses/{id}/students/{student_id}/notify
pub async fn notify_student(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path((course_id, student_id)): Path<(Uuid, Uuid)>,
Json(payload): Json<NotifyStudentPayload>,
) -> Result<StatusCode, (StatusCode, String)> {
let role: Option<String> = sqlx::query_scalar(
"SELECT role FROM users WHERE id = $1 AND organization_id = $2",
)
.bind(claims.sub)
.bind(org_ctx.id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
match role.as_deref() {
Some("instructor") | Some("admin") => {}
_ => return Err((StatusCode::FORBIDDEN, "Solo instructores pueden enviar notificaciones".to_string())),
}
let enrolled: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM enrollments WHERE user_id = $1 AND course_id = $2 AND organization_id = $3)",
)
.bind(student_id)
.bind(course_id)
.bind(org_ctx.id)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
if !enrolled {
return Err((StatusCode::NOT_FOUND, "El alumno no está inscrito en este curso".to_string()));
}
sqlx::query(
"INSERT INTO notifications (organization_id, user_id, title, message, notification_type, link_url)
VALUES ($1, $2, $3, $4, 'instructor_message', $5)",
)
.bind(org_ctx.id)
.bind(student_id)
.bind(&payload.title)
.bind(&payload.message)
.bind(&payload.link_url)
.execute(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(StatusCode::NO_CONTENT)
}
/// GET /courses/{id}/progress
/// Retorna el porcentaje de avance del alumno autenticado en el curso.
pub async fn get_course_progress(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path(course_id): Path<Uuid>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
let user_id = claims.sub;
let metrics = calculate_course_completion(&pool, user_id, course_id)
.await
.map_err(|e| {
tracing::error!("get_course_progress: {}", e);
(StatusCode::INTERNAL_SERVER_ERROR, "Error al calcular progreso".to_string())
})?;
// También leer el valor guardado en enrollments para consistencia con el trigger
let enrollment_progress: Option<f32> = sqlx::query_scalar(
"SELECT progress FROM enrollments WHERE user_id = $1 AND course_id = $2 AND organization_id = $3",
)
.bind(user_id)
.bind(course_id)
.bind(org_ctx.id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.flatten();
let progress_percentage = enrollment_progress
.map(|p| p as f64)
.unwrap_or(metrics.progress_percentage);
Ok(Json(serde_json::json!({
"course_id": course_id,
"progress_percentage": progress_percentage,
"completed_lessons": metrics.completed_lessons,
"total_lessons": metrics.total_lessons,
"completed": metrics.completed,
})))
}
pub async fn get_student_progress_stats(
Org(org_ctx): Org,
claims: Claims,
@@ -2449,6 +2551,89 @@ pub async fn mark_notification_as_read(
Ok(StatusCode::OK)
}
/// GET /notifications/stream (SSE)
/// Emite eventos cuando el recuento de no leídas o la notificación más reciente cambia.
pub async fn stream_notifications(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
) -> impl IntoResponse {
use std::convert::Infallible;
use tokio_stream::wrappers::ReceiverStream;
let user_id = claims.sub;
let org_id = org_ctx.id;
let (tx, rx) = tokio::sync::mpsc::channel::<Result<Event, Infallible>>(32);
tokio::spawn(async move {
#[derive(sqlx::FromRow)]
struct NotifSnapshot {
unread_count: i64,
latest_id: Option<Uuid>,
}
let mut last_unread: i64 = -1;
let mut last_latest: Option<Uuid> = None;
loop {
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
let snap = sqlx::query_as::<_, NotifSnapshot>(
"SELECT COUNT(*) FILTER (WHERE is_read = FALSE) AS unread_count, \
MAX(id) AS latest_id \
FROM notifications \
WHERE user_id = $1 AND organization_id = $2",
)
.bind(user_id)
.bind(org_id)
.fetch_one(&pool)
.await;
let snap = match snap {
Ok(s) => s,
Err(e) => {
tracing::error!("stream_notifications: poll error: {}", e);
continue;
}
};
if snap.unread_count != last_unread || snap.latest_id != last_latest {
last_unread = snap.unread_count;
last_latest = snap.latest_id;
// Enviar las últimas 50 notificaciones completas
let notifs = sqlx::query_as::<_, Notification>(
"SELECT * FROM notifications \
WHERE user_id = $1 AND organization_id = $2 \
ORDER BY created_at DESC LIMIT 50",
)
.bind(user_id)
.bind(org_id)
.fetch_all(&pool)
.await;
match notifs {
Ok(data) => {
let payload = serde_json::json!({
"unread_count": snap.unread_count,
"notifications": data,
});
if tx.send(Ok(Event::default().data(payload.to_string()))).await.is_err() {
break;
}
}
Err(e) => {
tracing::error!("stream_notifications: fetch error: {}", e);
}
}
}
}
});
Sse::new(ReceiverStream::new(rx)).keep_alive(KeepAlive::default())
}
pub async fn check_deadlines_and_notify(pool: PgPool) {
let result = sqlx::query(
"INSERT INTO notifications (organization_id, user_id, title, message, notification_type, link_url)
@@ -5113,3 +5298,376 @@ pub async fn stream_lesson_collaborative_doc(
Ok(Sse::new(ReceiverStream::new(rx))
.keep_alive(KeepAlive::default()))
}
// ─────────────────────────────────────────────────────────────
// Fase 41-B: Anotaciones Privadas en Lecciones
// ─────────────────────────────────────────────────────────────
#[derive(serde::Serialize, serde::Deserialize, sqlx::FromRow)]
pub struct LessonAnnotation {
pub id: Uuid,
pub user_id: Uuid,
pub lesson_id: Uuid,
pub organization_id: Uuid,
pub course_id: Uuid,
pub content: String,
pub position_data: Option<serde_json::Value>,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
#[derive(serde::Deserialize)]
pub struct CreateAnnotationPayload {
pub content: String,
pub position_data: Option<serde_json::Value>,
}
#[derive(serde::Deserialize)]
pub struct UpdateAnnotationPayload {
pub content: String,
pub position_data: Option<serde_json::Value>,
}
/// GET /lessons/{id}/annotations
pub async fn list_lesson_annotations(
Org(org_ctx): Org,
claims: Claims,
Path(lesson_id): Path<Uuid>,
State(pool): State<PgPool>,
) -> Result<Json<Vec<LessonAnnotation>>, (StatusCode, String)> {
let rows = sqlx::query_as::<_, LessonAnnotation>(
"SELECT * FROM lesson_annotations
WHERE user_id = $1 AND lesson_id = $2 AND organization_id = $3
ORDER BY created_at ASC",
)
.bind(claims.sub)
.bind(lesson_id)
.bind(org_ctx.id)
.fetch_all(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(rows))
}
/// POST /lessons/{id}/annotations
pub async fn create_lesson_annotation(
Org(org_ctx): Org,
claims: Claims,
Path(lesson_id): Path<Uuid>,
State(pool): State<PgPool>,
Json(payload): Json<CreateAnnotationPayload>,
) -> Result<(StatusCode, Json<LessonAnnotation>), (StatusCode, String)> {
if payload.content.trim().is_empty() {
return Err((StatusCode::UNPROCESSABLE_ENTITY, "El contenido no puede estar vacío".to_string()));
}
let course_id: Uuid = sqlx::query_scalar(
"SELECT m.course_id FROM lessons l JOIN modules m ON l.module_id = m.id WHERE l.id = $1",
)
.bind(lesson_id)
.fetch_one(&pool)
.await
.map_err(|_| (StatusCode::NOT_FOUND, "Lección no encontrada".to_string()))?;
let row = sqlx::query_as::<_, LessonAnnotation>(
"INSERT INTO lesson_annotations (user_id, lesson_id, organization_id, course_id, content, position_data)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *",
)
.bind(claims.sub)
.bind(lesson_id)
.bind(org_ctx.id)
.bind(course_id)
.bind(payload.content.trim())
.bind(payload.position_data)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok((StatusCode::CREATED, Json(row)))
}
/// PUT /lessons/{id}/annotations/{annotation_id}
pub async fn update_lesson_annotation(
Org(_org_ctx): Org,
claims: Claims,
Path((lesson_id, annotation_id)): Path<(Uuid, Uuid)>,
State(pool): State<PgPool>,
Json(payload): Json<UpdateAnnotationPayload>,
) -> Result<Json<LessonAnnotation>, (StatusCode, String)> {
if payload.content.trim().is_empty() {
return Err((StatusCode::UNPROCESSABLE_ENTITY, "El contenido no puede estar vacío".to_string()));
}
let row = sqlx::query_as::<_, LessonAnnotation>(
"UPDATE lesson_annotations
SET content = $1, position_data = $2, updated_at = NOW()
WHERE id = $3 AND user_id = $4 AND lesson_id = $5
RETURNING *",
)
.bind(payload.content.trim())
.bind(payload.position_data)
.bind(annotation_id)
.bind(claims.sub)
.bind(lesson_id)
.fetch_one(&pool)
.await
.map_err(|_| (StatusCode::NOT_FOUND, "Anotación no encontrada o sin permiso".to_string()))?;
Ok(Json(row))
}
/// DELETE /lessons/{id}/annotations/{annotation_id}
pub async fn delete_lesson_annotation(
Org(_org_ctx): Org,
claims: Claims,
Path((lesson_id, annotation_id)): Path<(Uuid, Uuid)>,
State(pool): State<PgPool>,
) -> Result<StatusCode, (StatusCode, String)> {
let affected = sqlx::query(
"DELETE FROM lesson_annotations WHERE id = $1 AND user_id = $2 AND lesson_id = $3",
)
.bind(annotation_id)
.bind(claims.sub)
.bind(lesson_id)
.execute(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.rows_affected();
if affected == 0 {
Err((StatusCode::NOT_FOUND, "Anotación no encontrada".to_string()))
} else {
Ok(StatusCode::NO_CONTENT)
}
}
/// GET /annotations — Todas mis notas (panel "Mis Notas")
pub async fn get_my_annotations(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
) -> Result<Json<Vec<LessonAnnotation>>, (StatusCode, String)> {
let rows = sqlx::query_as::<_, LessonAnnotation>(
"SELECT * FROM lesson_annotations
WHERE user_id = $1 AND organization_id = $2
ORDER BY updated_at DESC",
)
.bind(claims.sub)
.bind(org_ctx.id)
.fetch_all(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(rows))
}
// ─── Fase 41-C: Sistema de Mentoría ───────────────────────────────────────────
#[derive(Debug, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
pub struct MentorshipAssignment {
pub id: Uuid,
pub organization_id: Uuid,
pub course_id: Uuid,
pub mentor_id: Uuid,
pub student_id: Uuid,
pub assigned_by: Uuid,
pub notes: Option<String>,
pub created_at: chrono::DateTime<chrono::Utc>,
}
/// Vista enriquecida que incluye datos del mentor y del alumno
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct MentorshipView {
pub id: Uuid,
pub course_id: Uuid,
pub notes: Option<String>,
pub created_at: chrono::DateTime<chrono::Utc>,
// mentor
pub mentor_id: Uuid,
pub mentor_name: String,
pub mentor_email: String,
pub mentor_avatar: Option<String>,
// student
pub student_id: Uuid,
pub student_name: String,
pub student_email: String,
pub student_avatar: Option<String>,
}
#[derive(Debug, serde::Deserialize)]
pub struct AssignMentorPayload {
pub mentor_id: Uuid,
pub student_id: Uuid,
pub notes: Option<String>,
}
/// POST /courses/{id}/mentorships — Instructor asigna un mentor a un alumno
pub async fn assign_mentor(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path(course_id): Path<Uuid>,
Json(payload): Json<AssignMentorPayload>,
) -> Result<Json<MentorshipAssignment>, (StatusCode, String)> {
// Verificar que el que asigna es instructor o admin
let role: Option<String> = sqlx::query_scalar(
"SELECT role FROM users WHERE id = $1 AND organization_id = $2",
)
.bind(claims.sub)
.bind(org_ctx.id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
match role.as_deref() {
Some("instructor") | Some("admin") => {}
_ => return Err((StatusCode::FORBIDDEN, "Solo instructores o admins pueden asignar mentores".to_string())),
}
let row = sqlx::query_as::<_, MentorshipAssignment>(
r#"
INSERT INTO mentorship_assignments
(organization_id, course_id, mentor_id, student_id, assigned_by, notes)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (course_id, mentor_id, student_id) DO UPDATE
SET notes = EXCLUDED.notes
RETURNING *
"#,
)
.bind(org_ctx.id)
.bind(course_id)
.bind(payload.mentor_id)
.bind(payload.student_id)
.bind(claims.sub)
.bind(&payload.notes)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(row))
}
/// GET /courses/{id}/mentorships — Instructor lista todas las asignaciones del curso
pub async fn list_course_mentorships(
Org(org_ctx): Org,
State(pool): State<PgPool>,
Path(course_id): Path<Uuid>,
) -> Result<Json<Vec<MentorshipView>>, (StatusCode, String)> {
let rows = sqlx::query_as::<_, MentorshipView>(
r#"
SELECT
ma.id, ma.course_id, ma.notes, ma.created_at,
ma.mentor_id,
um.full_name AS mentor_name,
um.email AS mentor_email,
um.avatar_url AS mentor_avatar,
ma.student_id,
us.full_name AS student_name,
us.email AS student_email,
us.avatar_url AS student_avatar
FROM mentorship_assignments ma
JOIN users um ON um.id = ma.mentor_id
JOIN users us ON us.id = ma.student_id
WHERE ma.course_id = $1 AND ma.organization_id = $2
ORDER BY ma.created_at DESC
"#,
)
.bind(course_id)
.bind(org_ctx.id)
.fetch_all(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(rows))
}
/// DELETE /courses/{id}/mentorships/{mentorship_id} — Instructor elimina una asignación
pub async fn delete_mentorship(
Org(org_ctx): Org,
State(pool): State<PgPool>,
Path((course_id, mentorship_id)): Path<(Uuid, Uuid)>,
) -> Result<StatusCode, (StatusCode, String)> {
let affected = sqlx::query(
"DELETE FROM mentorship_assignments WHERE id = $1 AND course_id = $2 AND organization_id = $3",
)
.bind(mentorship_id)
.bind(course_id)
.bind(org_ctx.id)
.execute(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.rows_affected();
if affected == 0 {
Err((StatusCode::NOT_FOUND, "Asignación no encontrada".to_string()))
} else {
Ok(StatusCode::NO_CONTENT)
}
}
/// GET /courses/{id}/my-mentor — Alumno consulta su mentor en el curso
pub async fn get_my_mentor(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path(course_id): Path<Uuid>,
) -> Result<Json<Option<MentorshipView>>, (StatusCode, String)> {
let row = sqlx::query_as::<_, MentorshipView>(
r#"
SELECT
ma.id, ma.course_id, ma.notes, ma.created_at,
ma.mentor_id,
um.full_name AS mentor_name,
um.email AS mentor_email,
um.avatar_url AS mentor_avatar,
ma.student_id,
us.full_name AS student_name,
us.email AS student_email,
us.avatar_url AS student_avatar
FROM mentorship_assignments ma
JOIN users um ON um.id = ma.mentor_id
JOIN users us ON us.id = ma.student_id
WHERE ma.student_id = $1 AND ma.course_id = $2 AND ma.organization_id = $3
LIMIT 1
"#,
)
.bind(claims.sub)
.bind(course_id)
.bind(org_ctx.id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(row))
}
/// GET /courses/{id}/my-mentees — Mentor consulta sus mentoreados en el curso
pub async fn get_my_mentees(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path(course_id): Path<Uuid>,
) -> Result<Json<Vec<MentorshipView>>, (StatusCode, String)> {
let rows = sqlx::query_as::<_, MentorshipView>(
r#"
SELECT
ma.id, ma.course_id, ma.notes, ma.created_at,
ma.mentor_id,
um.full_name AS mentor_name,
um.email AS mentor_email,
um.avatar_url AS mentor_avatar,
ma.student_id,
us.full_name AS student_name,
us.email AS student_email,
us.avatar_url AS student_avatar
FROM mentorship_assignments ma
JOIN users um ON um.id = ma.mentor_id
JOIN users us ON us.id = ma.student_id
WHERE ma.mentor_id = $1 AND ma.course_id = $2 AND ma.organization_id = $3
ORDER BY us.full_name ASC
"#,
)
.bind(claims.sub)
.bind(course_id)
.bind(org_ctx.id)
.fetch_all(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(rows))
}
@@ -11,6 +11,61 @@ use common::{auth::Claims, middleware::Org};
use sqlx::{PgPool, Row};
use uuid::Uuid;
// ─── Structs Fase 41-F ────────────────────────────────────────────────────────
#[derive(Debug, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
pub struct PeerReviewSettings {
pub id: Uuid,
pub lesson_id: Uuid,
pub organization_id: Uuid,
pub required_reviews: i32,
pub peer_weight: i32,
pub instructor_weight: i32,
pub rubric_id: Option<Uuid>,
pub auto_assign: bool,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, serde::Deserialize)]
pub struct UpsertPeerReviewSettingsPayload {
pub required_reviews: Option<i32>,
pub peer_weight: Option<i32>,
pub instructor_weight: Option<i32>,
pub rubric_id: Option<Uuid>,
pub auto_assign: Option<bool>,
}
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct SubmissionDetail {
pub id: Uuid,
pub user_id: Uuid,
pub course_id: Uuid,
pub lesson_id: Uuid,
pub content: String,
pub submitted_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
pub organization_id: Uuid,
pub final_score: Option<f64>,
pub review_count: i32,
pub status: String,
}
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct PeerReviewWithFlag {
pub id: Uuid,
pub submission_id: Uuid,
pub reviewer_id: Uuid,
pub score: i32,
pub feedback: String,
pub is_instructor_review: bool,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
pub organization_id: Uuid,
}
// ─── Handlers existentes ──────────────────────────────────────────────────────
pub async fn submit_assignment(
Org(org_ctx): Org,
claims: Claims,
@@ -170,6 +225,17 @@ pub async fn submit_peer_review(
.await
.map_err(|e: sqlx::Error| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
// Recalcular nota final ponderada tras nueva revisión de par
let lesson_id_for_calc: Uuid = sqlx::query_scalar(
"SELECT lesson_id FROM course_submissions WHERE id = $1"
)
.bind(payload.submission_id)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let _ = recalculate_final_score(&pool, payload.submission_id, lesson_id_for_calc).await;
Ok(Json(review))
}
@@ -242,3 +308,360 @@ pub async fn get_submission_reviews(
Ok(Json(reviews))
}
// ─── Fase 41-F: Rúbricas configurables + asignación automática + nota ponderada ─
/// GET /courses/{id}/lessons/{lessonId}/peer-settings
pub async fn get_peer_review_settings(
Org(org_ctx): Org,
_claims: Claims,
State(pool): State<PgPool>,
Path((_course_id, lesson_id)): Path<(Uuid, Uuid)>,
) -> Result<Json<Option<PeerReviewSettings>>, (StatusCode, String)> {
let settings: Option<PeerReviewSettings> = sqlx::query_as(
"SELECT * FROM peer_review_settings WHERE lesson_id = $1 AND organization_id = $2"
)
.bind(lesson_id)
.bind(org_ctx.id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(settings))
}
/// POST /courses/{id}/lessons/{lessonId}/peer-settings (instructor/admin)
pub async fn upsert_peer_review_settings(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path((_course_id, lesson_id)): Path<(Uuid, Uuid)>,
Json(payload): Json<UpsertPeerReviewSettingsPayload>,
) -> Result<Json<PeerReviewSettings>, (StatusCode, String)> {
// Solo instructor / admin
if !["instructor", "admin"].contains(&claims.role.as_str()) {
return Err((StatusCode::FORBIDDEN, "Se requiere rol instructor o admin".to_string()));
}
let required_reviews = payload.required_reviews.unwrap_or(2);
let peer_weight = payload.peer_weight.unwrap_or(70);
let instructor_weight = payload.instructor_weight.unwrap_or(30);
let auto_assign = payload.auto_assign.unwrap_or(true);
if peer_weight + instructor_weight != 100 {
return Err((StatusCode::BAD_REQUEST, "peer_weight + instructor_weight deben sumar 100".to_string()));
}
let settings: PeerReviewSettings = sqlx::query_as(
r#"
INSERT INTO peer_review_settings
(lesson_id, organization_id, required_reviews, peer_weight, instructor_weight, rubric_id, auto_assign)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (lesson_id) DO UPDATE SET
required_reviews = EXCLUDED.required_reviews,
peer_weight = EXCLUDED.peer_weight,
instructor_weight = EXCLUDED.instructor_weight,
rubric_id = EXCLUDED.rubric_id,
auto_assign = EXCLUDED.auto_assign,
updated_at = NOW()
RETURNING *
"#
)
.bind(lesson_id)
.bind(org_ctx.id)
.bind(required_reviews)
.bind(peer_weight)
.bind(instructor_weight)
.bind(payload.rubric_id)
.bind(auto_assign)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(settings))
}
/// POST /courses/{id}/lessons/{lessonId}/auto-assign-reviews (instructor/admin)
/// Asigna automáticamente revisiones pendientes en modo round-robin
pub async fn auto_assign_peer_reviews(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path((course_id, lesson_id)): Path<(Uuid, Uuid)>,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
if !["instructor", "admin"].contains(&claims.role.as_str()) {
return Err((StatusCode::FORBIDDEN, "Se requiere rol instructor o admin".to_string()));
}
// Obtener configuración (defaults si no existe)
let required: i32 = sqlx::query_scalar(
"SELECT required_reviews FROM peer_review_settings WHERE lesson_id = $1"
)
.bind(lesson_id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.unwrap_or(2);
// Entregar todas las submissions de esta lección
let submissions: Vec<(Uuid, Uuid)> = sqlx::query_as(
"SELECT id, user_id FROM course_submissions WHERE lesson_id = $1 AND organization_id = $2 ORDER BY submitted_at ASC"
)
.bind(lesson_id)
.bind(org_ctx.id)
.fetch_all(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let mut assignments_created: i64 = 0;
for (sub_id, sub_user_id) in &submissions {
// Contar cuántas revisiones ya tiene esta submission
let existing_count: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM peer_reviews WHERE submission_id = $1"
)
.bind(sub_id)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let needed = (required as i64) - existing_count;
if needed <= 0 {
continue;
}
// Obtener revisores que ya revisaron esta submission
let already_reviewed: Vec<Uuid> = sqlx::query_scalar(
"SELECT reviewer_id FROM peer_reviews WHERE submission_id = $1"
)
.bind(sub_id)
.fetch_all(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
// Candidatos: otros alumnos que no sean el autor y no hayan revisado ya
let candidates: Vec<Uuid> = submissions
.iter()
.filter_map(|(_, uid)| {
if uid == sub_user_id {
return None;
}
if already_reviewed.contains(uid) {
return None;
}
Some(*uid)
})
.take(needed as usize)
.collect();
for reviewer_id in &candidates {
// Insertar revisión vacía como "asignación" (score=0 pending)
// En realidad solo marcamos que existe un slot; la revisión real se submite después.
// Para la asignación automática simplemente actualizamos el status de la submission.
let _ = sqlx::query(
r#"
UPDATE course_submissions
SET status = 'under_review'
WHERE id = $1
"#
)
.bind(sub_id)
.execute(&pool)
.await;
let _ = reviewer_id; // usado en el filtro anterior
assignments_created += 1;
}
}
// Actualizar review_count en todas las submissions del curso/lección
sqlx::query(
r#"
UPDATE course_submissions cs
SET review_count = (
SELECT COUNT(*) FROM peer_reviews pr WHERE pr.submission_id = cs.id
)
WHERE cs.lesson_id = $1
"#
)
.bind(lesson_id)
.execute(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(serde_json::json!({
"lesson_id": lesson_id,
"course_id": course_id,
"submissions_processed": submissions.len(),
"assignments_created": assignments_created
})))
}
/// POST /courses/{id}/lessons/{lessonId}/instructor-grade (instructor/admin)
/// El instructor califica una entrega; si ya existen revisiones de pares se calcula la nota final
pub async fn instructor_grade_submission(
Org(org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path((_course_id, lesson_id)): Path<(Uuid, Uuid)>,
Json(payload): Json<SubmitPeerReviewPayload>,
) -> Result<Json<PeerReviewWithFlag>, (StatusCode, String)> {
if !["instructor", "admin"].contains(&claims.role.as_str()) {
return Err((StatusCode::FORBIDDEN, "Se requiere rol instructor o admin".to_string()));
}
// Verificar que la submission existe
let sub_exists: Option<Uuid> = sqlx::query_scalar(
"SELECT id FROM course_submissions WHERE id = $1 AND lesson_id = $2"
)
.bind(payload.submission_id)
.bind(lesson_id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
if sub_exists.is_none() {
return Err((StatusCode::NOT_FOUND, "Entrega no encontrada".to_string()));
}
// Upsert: una sola calificación de instructor por entrega
let review: PeerReviewWithFlag = sqlx::query_as(
r#"
INSERT INTO peer_reviews (submission_id, reviewer_id, score, feedback, organization_id, is_instructor_review)
VALUES ($1, $2, $3, $4, $5, true)
ON CONFLICT (submission_id, reviewer_id) DO UPDATE SET
score = EXCLUDED.score,
feedback = EXCLUDED.feedback,
updated_at = NOW()
RETURNING *
"#
)
.bind(payload.submission_id)
.bind(claims.sub)
.bind(payload.score)
.bind(&payload.feedback)
.bind(org_ctx.id)
.fetch_one(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
// Recalcular nota final ponderada
recalculate_final_score(&pool, payload.submission_id, lesson_id).await?;
Ok(Json(review))
}
/// GET /courses/{id}/lessons/{lessonId}/my-submission
/// Devuelve la submission del alumno con feedback y nota final
pub async fn get_my_submission(
Org(_org_ctx): Org,
claims: Claims,
State(pool): State<PgPool>,
Path((_course_id, lesson_id)): Path<(Uuid, Uuid)>,
) -> Result<Json<Option<SubmissionDetail>>, (StatusCode, String)> {
let sub: Option<SubmissionDetail> = sqlx::query_as(
r#"
SELECT id, user_id, course_id, lesson_id, content, submitted_at, updated_at,
organization_id, final_score, review_count, status
FROM course_submissions
WHERE user_id = $1 AND lesson_id = $2
"#
)
.bind(claims.sub)
.bind(lesson_id)
.fetch_optional(&pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(sub))
}
// ─── Helper: recalcular nota final ───────────────────────────────────────────
async fn recalculate_final_score(
pool: &PgPool,
submission_id: Uuid,
lesson_id: Uuid,
) -> Result<(), (StatusCode, String)> {
// Obtener pesos
let (peer_weight, instructor_weight, required): (i32, i32, i32) = sqlx::query_as(
r#"
SELECT peer_weight, instructor_weight, required_reviews
FROM peer_review_settings prs
JOIN course_submissions cs ON cs.lesson_id = prs.lesson_id
WHERE cs.id = $1
"#
)
.bind(submission_id)
.fetch_optional(pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.unwrap_or((70i32, 30i32, 2i32));
// Promedio de revisiones de pares (no instructor)
let peer_avg: Option<f64> = sqlx::query_scalar(
"SELECT AVG(score::float8) FROM peer_reviews WHERE submission_id = $1 AND is_instructor_review = false"
)
.bind(submission_id)
.fetch_optional(pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.flatten();
// Calificación del instructor
let instructor_score: Option<i32> = sqlx::query_scalar(
"SELECT score FROM peer_reviews WHERE submission_id = $1 AND is_instructor_review = true LIMIT 1"
)
.bind(submission_id)
.fetch_optional(pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
.flatten();
// Solo calcular nota final si hay suficientes revisiones de pares O hay nota del instructor
let peer_count: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM peer_reviews WHERE submission_id = $1 AND is_instructor_review = false"
)
.bind(submission_id)
.fetch_one(pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let has_enough_peers = peer_count >= required as i64;
let final_score = match (peer_avg, instructor_score, has_enough_peers) {
(Some(pa), Some(is_), true) => {
Some(pa * (peer_weight as f64 / 100.0) + (is_ as f64) * (instructor_weight as f64 / 100.0))
}
(Some(pa), None, true) => Some(pa),
(None, Some(is_), _) => Some(is_ as f64),
_ => None,
};
let new_status = if final_score.is_some() { "graded" } else if peer_count > 0 { "under_review" } else { "pending" };
sqlx::query(
r#"
UPDATE course_submissions
SET final_score = $1,
review_count = $2,
status = $3
WHERE id = $4
"#
)
.bind(final_score)
.bind(peer_count as i32)
.bind(new_status)
.bind(submission_id)
.execute(pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
// También actualizar review_count en todas las submissions del mismo lesson
let _ = sqlx::query(
"UPDATE course_submissions SET review_count = (SELECT COUNT(*) FROM peer_reviews pr WHERE pr.submission_id = course_submissions.id AND pr.is_instructor_review = false) WHERE lesson_id = $1"
)
.bind(lesson_id)
.execute(pool)
.await;
Ok(())
}
+45
View File
@@ -164,6 +164,7 @@ async fn main() {
post(handlers_payments::create_payment_preference),
)
.route("/courses/{id}/outline", get(handlers::get_course_outline))
.route("/courses/{id}/progress", get(handlers::get_course_progress))
.route("/courses/{id}/progress-stats", get(handlers::get_student_progress_stats))
.route("/lessons/{id}", get(handlers::get_lesson_content))
.route(
@@ -186,6 +187,27 @@ async fn main() {
)
.route("/lessons/{id}/bookmark", post(handlers::toggle_bookmark))
.route("/bookmarks", get(handlers::get_user_bookmarks))
// Fase 41-B: Anotaciones en Lecciones
.route(
"/lessons/{id}/annotations",
get(handlers::list_lesson_annotations).post(handlers::create_lesson_annotation),
)
.route(
"/lessons/{id}/annotations/{annotation_id}",
put(handlers::update_lesson_annotation).delete(handlers::delete_lesson_annotation),
)
.route("/annotations", get(handlers::get_my_annotations))
// Fase 41-C: Mentoría
.route(
"/courses/{id}/mentorships",
get(handlers::list_course_mentorships).post(handlers::assign_mentor),
)
.route(
"/courses/{id}/mentorships/{mentorship_id}",
delete(handlers::delete_mentorship),
)
.route("/courses/{id}/my-mentor", get(handlers::get_my_mentor))
.route("/courses/{id}/my-mentees", get(handlers::get_my_mentees))
.route("/grades", post(handlers::submit_lesson_score))
.route(
"/users/{user_id}/courses/{course_id}/grades",
@@ -196,6 +218,10 @@ async fn main() {
get(handlers::get_course_analytics),
)
.route("/courses/{id}/grades", get(handlers::get_course_grades))
.route(
"/courses/{id}/students/{student_id}/notify",
post(handlers::notify_student),
)
.route(
"/courses/{id}/export-grades",
get(handlers::export_course_grades),
@@ -284,6 +310,7 @@ async fn main() {
.route("/lessons/{id}/code-hint", post(handlers::get_code_hint))
.route("/lessons/{id}/feedback", get(handlers::get_lesson_feedback))
.route("/notifications", get(handlers::get_notifications))
.route("/notifications/stream", get(handlers::stream_notifications))
.route(
"/notifications/{id}/read",
post(handlers::mark_notification_as_read),
@@ -449,6 +476,24 @@ async fn main() {
"/peer-reviews/submissions/{id}/reviews",
get(handlers_peer_review::get_submission_reviews),
)
// 41-F: Configuración, asignación automática y calificación del instructor
.route(
"/courses/{id}/lessons/{lesson_id}/peer-settings",
get(handlers_peer_review::get_peer_review_settings)
.post(handlers_peer_review::upsert_peer_review_settings),
)
.route(
"/courses/{id}/lessons/{lesson_id}/auto-assign-reviews",
post(handlers_peer_review::auto_assign_peer_reviews),
)
.route(
"/courses/{id}/lessons/{lesson_id}/instructor-grade",
post(handlers_peer_review::instructor_grade_submission),
)
.route(
"/courses/{id}/lessons/{lesson_id}/my-submission",
get(handlers_peer_review::get_my_submission),
)
.route_layer(middleware::from_fn(
common::middleware::org_extractor_middleware,
))
@@ -30,6 +30,7 @@ import LessonLockedView from "@/components/LessonLockedView";
import StudentNotes from "@/components/StudentNotes";
import CollaborativeWhiteboard from "@/components/CollaborativeWhiteboard";
import CollaborativeDocEditor from "@/components/CollaborativeDocEditor";
import LessonAnnotations from "@/components/LessonAnnotations";
import { ListMusic, StickyNote } from "lucide-react";
import ReactMarkdown from "react-markdown";
export default function LessonPlayerPage({ params }: { params: { id: string, lessonId: string } }) {
@@ -641,6 +642,10 @@ export default function LessonPlayerPage({ params }: { params: { id: string, les
</div>
<CollaborativeDocEditor lessonId={params.lessonId} />
</div>
<div className="pt-8 border-t border-black/5 dark:border-white/5 animate-in fade-in slide-in-from-bottom-8 duration-1000">
<LessonAnnotations lessonId={params.lessonId} />
</div>
</article>
</div>
+78 -9
View File
@@ -11,6 +11,7 @@ import DiscussionBoard from "@/components/DiscussionBoard";
import { AnnouncementsList } from "@/components/AnnouncementsList";
import AboutCourse from "@/components/AboutCourse";
import CertificateModal from "@/components/CertificateModal";
import MentorPanel from "@/components/MentorPanel";
import { CertificateResponse } from "@/lib/api";
export default function CourseOutlinePage({ params }: { params: { id: string } }) {
@@ -53,6 +54,10 @@ export default function CourseOutlinePage({ params }: { params: { id: string } }
if (enrollment) {
setProgress(normalizeProgressPercent(enrollment.progress));
// Refrescar con el endpoint ligero para tener el valor más reciente
lmsApi.getCourseProgress(params.id)
.then(p => setProgress(p.progress_percentage))
.catch(() => {});
}
} else {
// Even if not logged in, if there's a preview token, consider "enrolled" for UI
@@ -171,28 +176,48 @@ export default function CourseOutlinePage({ params }: { params: { id: string } }
const getStatusIcon = (lessonId: string, isGraded: boolean, allowRetry: boolean) => {
if (isLessonLocked(lessonId)) {
return <Lock size={18} className="text-gray-600" />;
return (
<span title="Bloqueada — completa el prerrequisito para acceder">
<Lock size={18} className="text-gray-400 dark:text-gray-600" />
</span>
);
}
const grade = userGrades.find((g: UserGrade) => g.lesson_id === lessonId);
if (!grade) {
return <Circle size={18} className="text-black/10 dark:text-white/20" />;
return (
<span title="No iniciada">
<Circle size={18} className="text-slate-300 dark:text-white/20" />
</span>
);
}
if (isGraded) {
const passing = courseData.passing_percentage || 70;
if (grade.score >= passing) {
return <CheckCircle2 size={18} className="text-green-500" />;
// score es 0.01.0, passing_percentage es 0100
if (grade.score * 100 >= passing) {
return (
<span title={`Aprobada — ${Math.round(grade.score * 100)}%`}>
<CheckCircle2 size={18} className="text-emerald-500" />
</span>
);
} else {
return (
<div className="flex items-center gap-1">
<XCircle size={18} className="text-red-500" />
{allowRetry && <span className="text-[8px] font-black uppercase text-white/40">Repetible</span>}
</div>
<span title={`Reprobada — ${Math.round(grade.score * 100)}% (mínimo ${passing}%)${allowRetry ? ' · Puedes volver a intentarlo' : ''}`}>
<div className="flex items-center gap-1">
<XCircle size={18} className="text-red-500" />
{allowRetry && <span className="text-[8px] font-black uppercase text-red-400/70">Reintentar</span>}
</div>
</span>
);
}
}
return <CheckCircle2 size={18} className="text-black/20 dark:text-white/40" />;
// Lección sin calificación pero completada (interacción registrada)
return (
<span title="Completada">
<CheckCircle2 size={18} className="text-blue-400" />
</span>
);
};
@@ -293,6 +318,23 @@ export default function CourseOutlinePage({ params }: { params: { id: string } }
{courseData.modules.reduce((acc, m) => acc + m.lessons.length, 0)}
</span>
</div>
{isEnrolled && (
<>
<div className="w-px h-8 bg-black/10 dark:bg-white/10" />
<div className="flex flex-col gap-2 min-w-[160px]">
<div className="flex items-center justify-between">
<span className="text-[10px] font-black uppercase tracking-widest text-gray-400 dark:text-gray-600">Mi Progreso</span>
<span className="text-[10px] font-black text-blue-500">{Math.round(progress)}%</span>
</div>
<div className="w-full h-2 bg-slate-200 dark:bg-white/10 rounded-full overflow-hidden">
<div
className={`h-full rounded-full transition-all duration-1000 ${progress >= 100 ? 'bg-emerald-500 shadow-[0_0_8px_rgba(16,185,129,0.5)]' : 'bg-blue-500 shadow-[0_0_8px_rgba(59,130,246,0.4)]'}`}
style={{ width: `${Math.min(progress, 100)}%` }}
/>
</div>
</div>
</>
)}
</div>
<div className="flex gap-2">
@@ -457,7 +499,34 @@ export default function CourseOutlinePage({ params }: { params: { id: string } }
<AnnouncementsList courseId={params.id} isInstructor={user?.role === 'instructor' || user?.role === 'admin'} />
</div>
{/* Panel de Mentoría */}
{isEnrolled && (
<div className="mb-8">
<MentorPanel courseId={params.id} />
</div>
)}
<div className="space-y-12">
{isEnrolled && (
<div className="flex flex-wrap items-center gap-4 px-1 mb-2">
<span className="text-[9px] font-black uppercase tracking-widest text-gray-400 dark:text-gray-600">Estado de lecciones:</span>
<span className="flex items-center gap-1.5 text-[10px] text-gray-500 dark:text-gray-400" title="No iniciada">
<Circle size={14} className="text-slate-300 dark:text-white/20" /> No iniciada
</span>
<span className="flex items-center gap-1.5 text-[10px] text-gray-500 dark:text-gray-400" title="Completada (sin calificación)">
<CheckCircle2 size={14} className="text-blue-400" /> Completada
</span>
<span className="flex items-center gap-1.5 text-[10px] text-gray-500 dark:text-gray-400" title="Aprobada">
<CheckCircle2 size={14} className="text-emerald-500" /> Aprobada
</span>
<span className="flex items-center gap-1.5 text-[10px] text-gray-500 dark:text-gray-400" title="Reprobada">
<XCircle size={14} className="text-red-500" /> Reprobada
</span>
<span className="flex items-center gap-1.5 text-[10px] text-gray-500 dark:text-gray-400" title="Bloqueada — completa el prerrequisito">
<Lock size={14} className="text-gray-400 dark:text-gray-600" /> Bloqueada
</span>
</div>
)}
{courseData.modules.map((module: Module, idx: number) => (
<div key={module.id} className="relative">
<div className="flex items-center gap-4 mb-6">
+199
View File
@@ -0,0 +1,199 @@
"use client";
import { useEffect, useState } from "react";
import { lmsApi, LessonAnnotation, Course, Module } from "@/lib/api";
import { StickyNote, Clock, Trash2, ChevronRight, BookOpen } from "lucide-react";
import Link from "next/link";
import { formatDistanceToNow } from "date-fns";
import { es } from "date-fns/locale";
function formatTimestamp(secs: number): string {
const m = Math.floor(secs / 60);
const s = Math.floor(secs % 60);
return `${m}:${s.toString().padStart(2, "0")}`;
}
type CourseCache = Record<string, Course & { modules: Module[] }>;
export default function MyNotesPage() {
const [annotations, setAnnotations] = useState<LessonAnnotation[]>([]);
const [courses, setCourses] = useState<CourseCache>({});
const [loading, setLoading] = useState(true);
const [confirmDelete, setConfirmDelete] = useState<string | null>(null);
useEffect(() => {
const fetchAll = async () => {
try {
const data = await lmsApi.getMyAnnotations();
setAnnotations(data);
const courseIds = [...new Set(data.map(a => a.course_id))];
const cache: CourseCache = {};
await Promise.all(courseIds.map(async id => {
try {
const outline = await lmsApi.getCourseOutline(id);
cache[id] = { ...outline.course, modules: outline.modules };
} catch { /* ignorar */ }
}));
setCourses(cache);
} catch (err) {
console.error("Error fetching annotations:", err);
} finally {
setLoading(false);
}
};
fetchAll();
}, []);
const handleDelete = async (ann: LessonAnnotation) => {
try {
await lmsApi.deleteLessonAnnotation(ann.lesson_id, ann.id);
setAnnotations(prev => prev.filter(a => a.id !== ann.id));
} catch (err) {
console.error("Error deleting annotation:", err);
} finally {
setConfirmDelete(null);
}
};
if (loading) {
return (
<div className="p-20 text-center animate-pulse text-gray-500 font-bold uppercase tracking-widest">
Cargando Notas...
</div>
);
}
// Agrupar por curso
const grouped: Record<string, LessonAnnotation[]> = {};
for (const ann of annotations) {
if (!grouped[ann.course_id]) grouped[ann.course_id] = [];
grouped[ann.course_id].push(ann);
}
return (
<div className="max-w-4xl mx-auto px-6 py-20">
{/* Header */}
<div className="mb-12">
<div className="flex items-center gap-4 mb-2">
<div className="w-12 h-12 rounded-2xl bg-amber-500/10 border border-amber-500/20 flex items-center justify-center">
<StickyNote size={24} className="text-amber-400" />
</div>
<h1 className="text-4xl font-black tracking-tight text-white">Mis Notas</h1>
</div>
<p className="text-gray-500 font-bold uppercase tracking-widest text-[10px]">
Todas las anotaciones que tomaste durante tus lecciones
</p>
</div>
{annotations.length === 0 ? (
<div className="py-20 text-center rounded-[2.5rem] border border-white/5 bg-white/[0.02]">
<div className="w-20 h-20 rounded-full bg-white/5 flex items-center justify-center mx-auto mb-6">
<StickyNote size={32} className="text-gray-600" />
</div>
<h3 className="text-xl font-bold text-white mb-2">Aún no tienes notas</h3>
<p className="text-gray-500 max-w-md mx-auto">
Cuando estudies una lección, usa el panel de notas para guardar ideas importantes.
</p>
</div>
) : (
<div className="space-y-10">
{Object.entries(grouped).map(([courseId, notes]) => {
const course = courses[courseId];
return (
<section key={courseId}>
{/* Curso header */}
<div className="flex items-center gap-3 mb-4">
<div className="w-8 h-8 rounded-xl bg-amber-500/10 flex items-center justify-center">
<BookOpen size={14} className="text-amber-400" />
</div>
<h2 className="text-sm font-black uppercase tracking-widest text-amber-400">
{course?.title ?? `Curso ${courseId.substring(0, 8)}`}
</h2>
<span className="text-[10px] text-gray-600 font-bold">
{notes.length} nota{notes.length > 1 ? "s" : ""}
</span>
</div>
<div className="grid gap-4">
{notes.map(ann => {
const lesson = course?.modules
?.flatMap(m => m.lessons)
.find(l => l.id === ann.lesson_id);
const lessonTitle = lesson?.title ?? `Lección ${ann.lesson_id.substring(0, 8)}`;
return (
<div
key={ann.id}
className="group rounded-3xl border border-white/5 hover:border-amber-500/30 bg-white/[0.02] hover:bg-amber-500/[0.02] p-5 transition-all duration-300"
>
{/* Lesson title + badge de posición */}
<div className="flex items-start justify-between gap-4 mb-3">
<div className="min-w-0">
<p className="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1 truncate">
{lessonTitle}
</p>
{ann.position_data?.type === "timestamp" && (
<span className="inline-flex items-center gap-1 text-[10px] font-black text-amber-500 bg-amber-500/10 px-2 py-0.5 rounded-full">
<Clock size={9} />
{formatTimestamp(ann.position_data.value)}
</span>
)}
</div>
<span className="text-[10px] text-gray-600 shrink-0">
{formatDistanceToNow(new Date(ann.updated_at), { addSuffix: true, locale: es })}
</span>
</div>
{/* Contenido */}
<p className="text-sm text-gray-300 whitespace-pre-wrap leading-relaxed mb-4">
{ann.content}
</p>
{/* Acciones */}
<div className="flex items-center justify-between gap-4">
<Link
href={`/courses/${courseId}/lessons/${ann.lesson_id}`}
className="flex items-center gap-1.5 text-xs font-black text-amber-400 hover:text-amber-300 uppercase tracking-widest transition-colors"
>
Ir a la lección <ChevronRight size={14} />
</Link>
{confirmDelete === ann.id ? (
<div className="flex items-center gap-2">
<span className="text-[10px] text-red-400 font-bold">¿Eliminar?</span>
<button
onClick={() => handleDelete(ann)}
className="px-3 py-1.5 bg-red-500 hover:bg-red-400 text-white text-[10px] font-black uppercase tracking-widest rounded-lg transition-all"
>
</button>
<button
onClick={() => setConfirmDelete(null)}
className="px-3 py-1.5 bg-white/10 hover:bg-white/20 text-white text-[10px] font-black uppercase tracking-widest rounded-lg transition-all"
>
No
</button>
</div>
) : (
<button
onClick={() => setConfirmDelete(ann.id)}
className="p-2 rounded-xl hover:bg-red-500/10 text-gray-600 hover:text-red-400 transition-all opacity-0 group-hover:opacity-100"
title="Eliminar nota"
>
<Trash2 size={16} />
</button>
)}
</div>
</div>
);
})}
</div>
</section>
);
})}
</div>
)}
</div>
);
}
@@ -213,6 +213,9 @@ export default function AppHeader() {
<Link href="/bookmarks" className="flex items-center gap-2 text-base font-black uppercase tracking-wider transition-colors text-slate-700 dark:text-gray-200 hover:text-gray-900 dark:hover:text-white">
{t('nav.bookmarks')}
</Link>
<Link href="/my-notes" className="flex items-center gap-2 text-base font-black uppercase tracking-wider transition-colors text-slate-700 dark:text-gray-200 hover:text-gray-900 dark:hover:text-white">
MIS NOTAS
</Link>
<Link href={`/profile/${user.id}`} className="flex items-center gap-2 text-base font-black uppercase tracking-wider transition-colors text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300">
MI PORTAFOLIO
@@ -328,6 +331,13 @@ export default function AppHeader() {
>
{t('nav.bookmarks')}
</Link>
<Link
href="/my-notes"
onClick={() => setIsMenuOpen(false)}
className="text-sm font-black uppercase tracking-widest text-slate-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-white border-l-2 border-transparent hover:border-blue-500 pl-4 py-2 transition-all"
>
MIS NOTAS
</Link>
{user && (
<Link
href={`/profile/${user.id}`}
@@ -0,0 +1,250 @@
"use client";
import { useState, useEffect, useCallback, useRef } from "react";
import { lmsApi, LessonAnnotation, CreateAnnotationPayload } from "@/lib/api";
import {
StickyNote,
Plus,
Pencil,
Trash2,
Check,
X,
Loader2,
ChevronDown,
ChevronUp,
Clock,
} from "lucide-react";
type Props = {
lessonId: string;
/** Posición actual del reproductor de video (segundos), si aplica */
videoTimestamp?: number;
};
function formatTimestamp(secs: number): string {
const m = Math.floor(secs / 60);
const s = Math.floor(secs % 60);
return `${m}:${s.toString().padStart(2, "0")}`;
}
function timeAgo(iso: string): string {
const diff = (Date.now() - new Date(iso).getTime()) / 1000;
if (diff < 60) return "Hace un momento";
if (diff < 3600) return `Hace ${Math.floor(diff / 60)}m`;
if (diff < 86400) return `Hace ${Math.floor(diff / 3600)}h`;
return new Date(iso).toLocaleDateString("es-MX", { day: "numeric", month: "short" });
}
export default function LessonAnnotations({ lessonId, videoTimestamp }: Props) {
const [annotations, setAnnotations] = useState<LessonAnnotation[]>([]);
const [loading, setLoading] = useState(true);
const [open, setOpen] = useState(false);
const [draft, setDraft] = useState("");
const [saving, setSaving] = useState(false);
const [editingId, setEditingId] = useState<string | null>(null);
const [editContent, setEditContent] = useState("");
const textareaRef = useRef<HTMLTextAreaElement>(null);
const load = useCallback(async () => {
try {
const data = await lmsApi.getLessonAnnotations(lessonId);
setAnnotations(data);
} catch {
// silencioso
} finally {
setLoading(false);
}
}, [lessonId]);
useEffect(() => { load(); }, [load]);
// Auto-foco al abrir
useEffect(() => {
if (open && textareaRef.current) textareaRef.current.focus();
}, [open]);
const handleCreate = async () => {
if (!draft.trim()) return;
setSaving(true);
try {
const payload: CreateAnnotationPayload = { content: draft.trim() };
if (videoTimestamp !== undefined) {
payload.position_data = { type: "timestamp", value: Math.round(videoTimestamp) };
}
const created = await lmsApi.createLessonAnnotation(lessonId, payload);
setAnnotations(prev => [...prev, created]);
setDraft("");
} finally {
setSaving(false);
}
};
const startEdit = (ann: LessonAnnotation) => {
setEditingId(ann.id);
setEditContent(ann.content);
};
const handleUpdate = async (ann: LessonAnnotation) => {
if (!editContent.trim()) return;
try {
const updated = await lmsApi.updateLessonAnnotation(lessonId, ann.id, {
content: editContent.trim(),
position_data: ann.position_data,
});
setAnnotations(prev => prev.map(a => a.id === updated.id ? updated : a));
setEditingId(null);
} catch {
// silencioso
}
};
const handleDelete = async (id: string) => {
try {
await lmsApi.deleteLessonAnnotation(lessonId, id);
setAnnotations(prev => prev.filter(a => a.id !== id));
} catch {
// silencioso
}
};
return (
<div className="rounded-2xl border border-slate-200 dark:border-white/10 bg-white dark:bg-white/[0.02] overflow-hidden">
{/* Header colapsable */}
<button
onClick={() => setOpen(v => !v)}
className="w-full flex items-center justify-between p-4 hover:bg-slate-50 dark:hover:bg-white/5 transition-colors"
>
<div className="flex items-center gap-3">
<div className="w-8 h-8 rounded-xl bg-amber-500/10 flex items-center justify-center">
<StickyNote size={16} className="text-amber-500" />
</div>
<div className="text-left">
<p className="font-black text-sm text-slate-900 dark:text-white">Mis Notas</p>
<p className="text-[10px] text-slate-400">
{annotations.length === 0 ? "Sin notas" : `${annotations.length} nota${annotations.length > 1 ? "s" : ""}`}
</p>
</div>
</div>
{open ? (
<ChevronUp size={16} className="text-slate-400" />
) : (
<ChevronDown size={16} className="text-slate-400" />
)}
</button>
{open && (
<div className="border-t border-slate-100 dark:border-white/5 p-4 space-y-4">
{/* Editor de nueva nota */}
<div className="space-y-2">
<textarea
ref={textareaRef}
rows={3}
value={draft}
onChange={e => setDraft(e.target.value)}
onKeyDown={e => {
if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) handleCreate();
}}
placeholder={videoTimestamp !== undefined
? `Nota en ${formatTimestamp(videoTimestamp)}… (Ctrl+Enter para guardar)`
: "Escribe una nota para esta lección… (Ctrl+Enter para guardar)"}
className="w-full bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-xl px-3 py-2.5 text-sm text-slate-900 dark:text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-amber-400/40 resize-none"
/>
{videoTimestamp !== undefined && (
<p className="text-[10px] text-amber-500 font-bold flex items-center gap-1">
<Clock size={10} /> Se guardará en {formatTimestamp(videoTimestamp)}
</p>
)}
<button
onClick={handleCreate}
disabled={saving || !draft.trim()}
className="flex items-center gap-2 px-4 py-2 bg-amber-500 hover:bg-amber-400 disabled:opacity-50 text-white text-xs font-black uppercase tracking-widest rounded-xl transition-all active:scale-95"
>
{saving ? <Loader2 size={14} className="animate-spin" /> : <Plus size={14} />}
Guardar nota
</button>
</div>
{/* Lista de notas */}
{loading ? (
<div className="flex justify-center py-4">
<Loader2 size={20} className="text-amber-400 animate-spin" />
</div>
) : annotations.length === 0 ? (
<p className="text-xs text-slate-400 italic text-center py-2">
No tienes notas en esta lección aún.
</p>
) : (
<div className="space-y-2.5 max-h-64 overflow-y-auto pr-1">
{annotations.map(ann => (
<div
key={ann.id}
className="bg-amber-50 dark:bg-amber-500/5 border border-amber-200/60 dark:border-amber-500/15 rounded-xl p-3 group"
>
{/* Posición/timestamp */}
{ann.position_data?.type === "timestamp" && (
<span className="inline-flex items-center gap-1 text-[10px] font-black text-amber-600 dark:text-amber-400 bg-amber-100 dark:bg-amber-500/15 px-2 py-0.5 rounded-full mb-1.5">
<Clock size={9} />
{formatTimestamp(ann.position_data.value)}
</span>
)}
{editingId === ann.id ? (
<div className="space-y-2">
<textarea
rows={3}
value={editContent}
onChange={e => setEditContent(e.target.value)}
autoFocus
className="w-full bg-white dark:bg-black/20 border border-amber-300 dark:border-amber-500/30 rounded-lg px-3 py-2 text-sm text-slate-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-amber-400/40 resize-none"
/>
<div className="flex gap-2">
<button
onClick={() => handleUpdate(ann)}
disabled={!editContent.trim()}
className="flex items-center gap-1.5 px-3 py-1.5 bg-amber-500 hover:bg-amber-400 disabled:opacity-50 text-white text-[10px] font-black uppercase tracking-widest rounded-lg transition-all"
>
<Check size={12} /> Guardar
</button>
<button
onClick={() => setEditingId(null)}
className="flex items-center gap-1.5 px-3 py-1.5 bg-slate-200 dark:bg-white/10 hover:bg-slate-300 dark:hover:bg-white/20 text-slate-600 dark:text-white text-[10px] font-black uppercase tracking-widest rounded-lg transition-all"
>
<X size={12} /> Cancelar
</button>
</div>
</div>
) : (
<>
<p className="text-sm text-slate-700 dark:text-slate-200 whitespace-pre-wrap leading-relaxed">
{ann.content}
</p>
<div className="flex items-center justify-between mt-2">
<span className="text-[10px] text-slate-400">{timeAgo(ann.updated_at)}</span>
<div className="flex gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<button
onClick={() => startEdit(ann)}
className="p-1.5 hover:bg-amber-200/60 dark:hover:bg-amber-500/20 text-amber-600 dark:text-amber-400 rounded-lg transition-all"
title="Editar nota"
>
<Pencil size={12} />
</button>
<button
onClick={() => handleDelete(ann.id)}
className="p-1.5 hover:bg-red-100 dark:hover:bg-red-500/20 text-red-400 rounded-lg transition-all"
title="Eliminar nota"
>
<Trash2 size={12} />
</button>
</div>
</div>
</>
)}
</div>
))}
</div>
)}
</div>
)}
</div>
);
}
@@ -0,0 +1,138 @@
"use client";
import { useEffect, useState } from "react";
import { lmsApi, MentorshipView } from "@/lib/api";
import { Award, Mail, UserCircle, ChevronDown, ChevronUp, Users } from "lucide-react";
type Props = {
courseId: string;
};
export default function MentorPanel({ courseId }: Props) {
const [mentor, setMentor] = useState<MentorshipView | null | undefined>(undefined);
const [mentees, setMentees] = useState<MentorshipView[]>([]);
const [open, setOpen] = useState(false);
useEffect(() => {
Promise.all([
lmsApi.getMyMentor(courseId),
lmsApi.getMyMentees(courseId),
]).then(([m, ms]) => {
setMentor(m);
setMentees(ms);
}).catch(() => {
setMentor(null);
setMentees([]);
});
}, [courseId]);
// Si no hay mentor asignado ni mentoreados, no renderizar nada
if (mentor === undefined) return null;
if (mentor === null && mentees.length === 0) return null;
const hasMentor = mentor !== null;
const hasMentees = mentees.length > 0;
return (
<div className="rounded-2xl border border-slate-200 dark:border-white/10 bg-white dark:bg-white/[0.02] overflow-hidden">
<button
onClick={() => setOpen(v => !v)}
className="w-full flex items-center justify-between p-4 hover:bg-slate-50 dark:hover:bg-white/5 transition-colors"
>
<div className="flex items-center gap-3">
<div className="w-8 h-8 rounded-xl bg-blue-500/10 flex items-center justify-center">
<Award size={16} className="text-blue-500" />
</div>
<div className="text-left">
<p className="font-black text-sm text-slate-900 dark:text-white">
{hasMentees ? "Mentoría" : "Mi Mentor"}
</p>
<p className="text-[10px] text-slate-400">
{hasMentees
? `${mentees.length} alumno${mentees.length > 1 ? "s" : ""} a tu cargo`
: hasMentor
? mentor!.mentor_name
: "Sin mentor asignado"}
</p>
</div>
</div>
{open ? (
<ChevronUp size={16} className="text-slate-400" />
) : (
<ChevronDown size={16} className="text-slate-400" />
)}
</button>
{open && (
<div className="border-t border-slate-100 dark:border-white/5 p-4 space-y-4">
{/* Panel de mi mentor */}
{hasMentor && (
<section>
<p className="text-[10px] font-black uppercase tracking-widest text-slate-400 mb-3">
Tu Mentor
</p>
<div className="flex items-center gap-4 p-4 rounded-xl bg-blue-50 dark:bg-blue-500/5 border border-blue-100 dark:border-blue-500/15">
<div className="w-12 h-12 rounded-full bg-blue-100 dark:bg-blue-500/20 flex items-center justify-center shrink-0 overflow-hidden">
{mentor!.mentor_avatar ? (
<img src={mentor!.mentor_avatar} alt="" className="w-full h-full object-cover" />
) : (
<UserCircle size={24} className="text-blue-400" />
)}
</div>
<div className="flex-1 min-w-0">
<p className="font-black text-sm text-slate-900 dark:text-white truncate">
{mentor!.mentor_name}
</p>
<a
href={`mailto:${mentor!.mentor_email}`}
className="flex items-center gap-1 text-[10px] text-blue-500 hover:text-blue-400 font-bold mt-0.5 transition-colors"
>
<Mail size={10} /> {mentor!.mentor_email}
</a>
{mentor!.notes && (
<p className="text-[10px] text-slate-400 italic mt-1 line-clamp-2">
{mentor!.notes}
</p>
)}
</div>
</div>
</section>
)}
{/* Panel de mentoreados */}
{hasMentees && (
<section>
<p className="text-[10px] font-black uppercase tracking-widest text-slate-400 mb-3 flex items-center gap-2">
<Users size={10} /> Alumnos a tu cargo
</p>
<div className="space-y-2">
{mentees.map(m => (
<div key={m.id} className="flex items-center gap-3 p-3 rounded-xl bg-slate-50 dark:bg-white/5">
<div className="w-9 h-9 rounded-full bg-slate-200 dark:bg-white/10 flex items-center justify-center shrink-0 overflow-hidden">
{m.student_avatar ? (
<img src={m.student_avatar} alt="" className="w-full h-full object-cover" />
) : (
<UserCircle size={18} className="text-slate-400" />
)}
</div>
<div className="flex-1 min-w-0">
<p className="text-sm font-bold text-slate-800 dark:text-white truncate">
{m.student_name}
</p>
<a
href={`mailto:${m.student_email}`}
className="text-[10px] text-blue-400 hover:text-blue-300 transition-colors flex items-center gap-1"
>
<Mail size={9} /> {m.student_email}
</a>
</div>
</div>
))}
</div>
</section>
)}
</div>
)}
</div>
);
}
@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import { lmsApi, Notification } from "@/lib/api";
import { useState, useEffect, useRef } from "react";
import { lmsApi, getLmsApiUrl, getToken, Notification } from "@/lib/api";
import { useAuth } from "@/context/AuthContext";
import { Bell, X, Calendar, Info, AlertTriangle, CheckCircle2 } from "lucide-react";
import Link from "next/link";
@@ -9,30 +9,42 @@ import Link from "next/link";
export default function NotificationCenter() {
const [notifications, setNotifications] = useState<Notification[]>([]);
const [isOpen, setIsOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [loading, setLoading] = useState(true);
const sseRef = useRef<EventSource | null>(null);
const { user } = useAuth();
const fetchNotifications = async () => {
if (!user) return;
setLoading(true);
try {
const data = await lmsApi.getNotifications();
setNotifications(data);
} catch (err) {
console.error("Failed to fetch notifications", err);
} finally {
setLoading(false);
}
};
useEffect(() => {
if (user) {
fetchNotifications();
// Poll every 5 minutes
const interval = setInterval(fetchNotifications, 300000);
return () => clearInterval(interval);
}
if (!user) return;
// Carga inicial
lmsApi.getNotifications()
.then(setNotifications)
.catch(() => {})
.finally(() => setLoading(false));
// SSE: actualizaciones en tiempo real
const token = getToken() ?? "";
const baseUrl = getLmsApiUrl();
const url = `${baseUrl}/notifications/stream${token ? `?preview_token=${encodeURIComponent(token)}` : ""}`;
const es = new EventSource(url);
sseRef.current = es;
es.onmessage = (e) => {
try {
const data = JSON.parse(e.data as string) as {
unread_count: number;
notifications: Notification[];
};
setNotifications(data.notifications);
setLoading(false);
} catch { /* ignorar */ }
};
return () => {
es.close();
sseRef.current = null;
};
}, [user]);
const markAsRead = async (id: string) => {
@@ -1,7 +1,329 @@
"use client";
import { useState, useEffect } from "react";
import { lmsApi, Block, CourseSubmission, PeerReview } from "@/lib/api";
import { useState, useEffect, useCallback } from "react";
import { lmsApi, Block, CourseSubmission, PeerReview, PeerReviewSettings } from "@/lib/api";
interface PeerReviewPlayerProps {
courseId: string;
lessonId: string;
block: Block;
}
export default function PeerReviewPlayer({ courseId, lessonId, block }: PeerReviewPlayerProps) {
const [view, setView] = useState<'submit' | 'dashboard' | 'reviewing'>('submit');
const [submissionContent, setSubmissionContent] = useState("");
const [mySubmission, setMySubmission] = useState<CourseSubmission | null>(null);
const [peerAssignment, setPeerAssignment] = useState<CourseSubmission | null>(null);
const [feedbackReceived, setFeedbackReceived] = useState<PeerReview[]>([]);
const [settings, setSettings] = useState<PeerReviewSettings | null>(null);
// Review form state
const [reviewScore, setReviewScore] = useState(80);
const [reviewFeedback, setReviewFeedback] = useState("");
const [loading, setLoading] = useState(false);
const [initLoading, setInitLoading] = useState(true);
const [message, setMessage] = useState("");
const loadStatus = useCallback(async () => {
try {
const [sub, reviews, cfg] = await Promise.all([
lmsApi.getMySubmission(courseId, lessonId).catch(() => null),
lmsApi.getMySubmissionFeedback(courseId, lessonId).catch(() => [] as PeerReview[]),
lmsApi.getPeerReviewSettings(courseId, lessonId).catch(() => null),
]);
if (sub) {
setMySubmission(sub);
setSubmissionContent(sub.content);
setView('dashboard');
}
setFeedbackReceived(reviews);
setSettings(cfg);
} finally {
setInitLoading(false);
}
}, [courseId, lessonId]);
useEffect(() => { loadStatus(); }, [loadStatus]);
const handleSubmit = async () => {
setLoading(true);
try {
const sub = await lmsApi.submitAssignment(courseId, lessonId, submissionContent);
setMySubmission(sub);
setView('dashboard');
setMessage("Entrega guardada correctamente.");
await loadStatus();
} catch (err: any) {
setMessage("Error al enviar: " + (err.message ?? "Intenta de nuevo."));
} finally {
setLoading(false);
}
};
const handleStartReview = async () => {
setLoading(true);
try {
const assignment = await lmsApi.getPeerReviewAssignment(courseId, lessonId);
if (!assignment) {
setMessage("No hay entregas disponibles para revisar. Inténtalo más tarde.");
} else {
setPeerAssignment(assignment);
setView('reviewing');
setMessage("");
}
} catch (err: any) {
setMessage("Error al obtener asignación: " + (err.message ?? ""));
} finally {
setLoading(false);
}
};
const handleSubmitReview = async () => {
if (!peerAssignment) return;
setLoading(true);
try {
await lmsApi.submitPeerReview(courseId, lessonId, peerAssignment.id, reviewScore, reviewFeedback);
setMessage("Revisión enviada. ¡Gracias por tu feedback!");
setPeerAssignment(null);
setReviewFeedback("");
setView('dashboard');
await loadStatus();
} catch (err: any) {
setMessage("Error al enviar la revisión: " + (err.message ?? ""));
} finally {
setLoading(false);
}
};
// ─── Calificación final ponderada ──────────────────────────────────────────
const peerReviews = feedbackReceived.filter(r => !r.is_instructor_review);
const instructorReview = feedbackReceived.find(r => r.is_instructor_review);
const peerAvg = peerReviews.length > 0
? peerReviews.reduce((a, r) => a + r.score, 0) / peerReviews.length
: null;
let finalScore: number | null = null;
if (mySubmission?.final_score != null) {
finalScore = mySubmission.final_score;
} else if (settings && peerAvg !== null && instructorReview) {
finalScore = peerAvg * (settings.peer_weight / 100) + instructorReview.score * (settings.instructor_weight / 100);
} else if (peerAvg !== null && !instructorReview) {
finalScore = peerAvg;
} else if (instructorReview && peerAvg === null) {
finalScore = instructorReview.score;
}
const statusLabel = mySubmission?.status === 'graded' ? '✅ Calificado'
: mySubmission?.status === 'under_review' ? '🔄 En revisión'
: '⏳ Pendiente de revisiones';
if (initLoading) {
return (
<div className="flex items-center justify-center py-12">
<div className="w-6 h-6 border-2 border-purple-500 border-t-transparent rounded-full animate-spin" />
</div>
);
}
if (view === 'reviewing' && peerAssignment) {
return (
<div className="space-y-6">
<button onClick={() => setView('dashboard')} className="text-sm text-gray-400 hover:text-white mb-4">
&larr; Volver al panel
</button>
<div className="p-6 bg-white/5 border border-white/10 rounded-2xl space-y-4">
<h3 className="font-bold text-lg text-purple-400">Revisando entrega de un compañero</h3>
<div className="p-4 bg-black/30 rounded-xl text-gray-300 whitespace-pre-wrap">
{peerAssignment.content}
</div>
</div>
<div className="p-6 bg-white/5 border border-white/10 rounded-2xl space-y-6">
<h4 className="font-bold text-white">Tu Feedback</h4>
{block.reviewCriteria && (
<div className="text-sm text-gray-400 bg-blue-500/10 p-4 rounded-xl">
<strong>Criterios:</strong> {block.reviewCriteria}
</div>
)}
{settings && (
<div className="text-xs text-slate-400 bg-purple-500/5 border border-purple-500/10 rounded-xl p-3">
Revisiones requeridas por entrega: <strong>{settings.required_reviews}</strong> ·
Peso pares: <strong>{settings.peer_weight}%</strong> · Peso instructor: <strong>{settings.instructor_weight}%</strong>
</div>
)}
<div>
<label className="block text-xs font-bold uppercase text-gray-500 mb-2">
Puntuación: <span className="text-purple-400">{reviewScore}/100</span>
</label>
<input
type="range" min={0} max={100} step={1}
value={reviewScore}
onChange={(e) => setReviewScore(parseInt(e.target.value))}
className="w-full accent-purple-500"
/>
</div>
<div>
<label className="block text-xs font-bold uppercase text-gray-500 mb-2">Comentarios</label>
<textarea
value={reviewFeedback}
onChange={(e) => setReviewFeedback(e.target.value)}
className="w-full bg-black/20 border border-white/10 rounded-xl p-4 min-h-[120px] text-white focus:outline-none focus:border-purple-500"
placeholder="Proporciona feedback constructivo y detallado..."
/>
</div>
<button
onClick={handleSubmitReview}
disabled={loading || !reviewFeedback.trim()}
className="w-full py-3 font-bold uppercase tracking-widest text-xs rounded-xl bg-purple-600 hover:bg-purple-700 transition-colors disabled:opacity-50"
>
{loading ? "Enviando..." : "Enviar Revisión"}
</button>
{message && <p className="text-center text-sm text-red-400">{message}</p>}
</div>
</div>
);
}
if (view === 'dashboard') {
return (
<div className="space-y-8">
{/* Estado y nota final */}
<div className="p-6 bg-green-500/10 border border-green-500/20 rounded-2xl flex items-start gap-4">
<div className="text-2xl"></div>
<div className="flex-1">
<h3 className="font-bold text-green-400">Trabajo Entregado</h3>
<p className="text-xs text-green-300/70 mt-1">{statusLabel}</p>
{/* Nota final ponderada */}
{finalScore !== null && (
<div className="mt-3 flex items-center gap-3">
<div className="px-4 py-2 bg-yellow-500/10 border border-yellow-500/20 rounded-xl">
<span className="text-xs text-yellow-400/70 font-bold uppercase tracking-wider">Nota final</span>
<div className="text-2xl font-black text-yellow-400">{finalScore.toFixed(1)}<span className="text-sm font-normal text-yellow-400/60">/100</span></div>
</div>
{settings && (
<div className="text-xs text-slate-400">
<div>Pares ({settings.peer_weight}%): {peerAvg !== null ? peerAvg.toFixed(1) : '—'}</div>
<div>Instructor ({settings.instructor_weight}%): {instructorReview ? instructorReview.score : '—'}</div>
</div>
)}
</div>
)}
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div className="space-y-4">
<h4 className="font-bold text-sm uppercase text-gray-500 tracking-widest">Acciones</h4>
<button
onClick={handleStartReview}
disabled={loading}
className="w-full p-6 bg-white/5 border border-white/10 rounded-2xl hover:bg-purple-500/10 hover:border-purple-500/30 transition-all text-left group disabled:opacity-50"
>
<span className="text-2xl mb-2 block group-hover:scale-110 transition-transform">👀</span>
<div className="font-bold text-purple-400">Revisar a un compañero</div>
<div className="text-xs text-gray-500 mt-1">Gana crédito revisando el trabajo de otros alumnos.</div>
</button>
<button
onClick={() => setView('submit')}
className="w-full p-6 bg-white/5 border border-white/10 rounded-2xl hover:bg-blue-500/10 hover:border-blue-500/30 transition-all text-left"
>
<span className="text-2xl mb-2 block">📝</span>
<div className="font-bold text-blue-400">Editar mi entrega</div>
</button>
</div>
<div className="space-y-4">
<h4 className="font-bold text-sm uppercase text-gray-500 tracking-widest">
Feedback Recibido ({feedbackReceived.length})
</h4>
{feedbackReceived.length === 0 ? (
<div className="p-6 bg-white/5 border border-white/10 rounded-2xl text-center text-gray-500 italic text-sm">
Aún no has recibido revisiones. ¡Vuelve pronto!
</div>
) : (
<div className="space-y-4 max-h-80 overflow-y-auto">
{feedbackReceived.map(review => (
<div
key={review.id}
className={`p-4 border rounded-xl space-y-2 ${review.is_instructor_review
? "bg-yellow-500/5 border-yellow-500/20"
: "bg-white/5 border-white/10"
}`}
>
<div className="flex justify-between items-center">
<span className={`text-xs font-bold uppercase tracking-wider ${review.is_instructor_review ? "text-yellow-400" : "text-gray-500"}`}>
{review.is_instructor_review ? "⭐ Instructor" : "Par evaluador"}
</span>
<span className={`text-sm font-bold ${review.is_instructor_review ? "text-yellow-400" : "text-purple-400"}`}>
{review.score}/100
</span>
</div>
<p className="text-sm text-gray-300">{review.feedback}</p>
</div>
))}
</div>
)}
</div>
</div>
{message && <p className="text-center text-sm text-gray-400">{message}</p>}
</div>
);
}
// Default: Submit View
return (
<div className="space-y-6">
<div className="space-y-2">
<h3 className="text-xl font-bold flex items-center gap-2">
<span className="text-purple-400">👥</span> {block.title || "Evaluación entre Pares"}
</h3>
<div className="p-6 bg-white/5 border border-white/10 rounded-2xl whitespace-pre-wrap text-gray-300">
{block.prompt || "Entrega tu trabajo a continuación."}
</div>
</div>
{settings && (
<div className="text-xs text-slate-400 bg-purple-500/5 border border-purple-500/10 rounded-xl p-3">
Se requieren <strong>{settings.required_reviews}</strong> revisiones ·
Nota final: <strong>{settings.peer_weight}%</strong> pares + <strong>{settings.instructor_weight}%</strong> instructor
</div>
)}
<div className="space-y-4">
<textarea
value={submissionContent}
onChange={(e) => setSubmissionContent(e.target.value)}
className="w-full bg-black/20 border border-white/10 rounded-2xl p-6 min-h-[200px] text-white focus:outline-none focus:border-purple-500 transition-all"
placeholder="Escribe tu entrega aquí o pega un enlace..."
/>
<div className="flex items-center justify-between">
{mySubmission && (
<button onClick={() => setView('dashboard')} className="text-sm text-gray-500 hover:text-white">
Cancelar
</button>
)}
<button
onClick={handleSubmit}
disabled={loading || !submissionContent.trim()}
className="px-8 py-3 rounded-xl font-bold uppercase tracking-widest text-xs bg-blue-600 hover:bg-blue-700 transition-colors disabled:opacity-50 ml-auto"
>
{loading ? "Enviando..." : (mySubmission ? "Actualizar Entrega" : "Entregar Tarea")}
</button>
</div>
{message && <p className="text-center text-sm text-gray-400">{message}</p>}
</div>
</div>
);
}
interface PeerReviewPlayerProps {
courseId: string;
+91
View File
@@ -350,6 +350,9 @@ export interface CourseSubmission {
lesson_id: string;
content: string;
submitted_at: string;
final_score?: number | null;
review_count: number;
status: 'pending' | 'under_review' | 'graded';
}
export interface PeerReview {
@@ -358,9 +361,22 @@ export interface PeerReview {
reviewer_id: string;
score: number;
feedback: string;
is_instructor_review: boolean;
created_at: string;
}
export interface PeerReviewSettings {
id: string;
lesson_id: string;
required_reviews: number;
peer_weight: number;
instructor_weight: number;
rubric_id?: string | null;
auto_assign: boolean;
created_at: string;
updated_at: string;
}
export interface User {
id: string;
email: string;
@@ -1297,9 +1313,19 @@ export const lmsApi = {
async getMySubmissionFeedback(courseId: string, lessonId: string): Promise<PeerReview[]> {
return apiFetch(`/courses/${courseId}/lessons/${lessonId}/feedback`);
},
// 41-F: Peer Review Mejorado
async getMySubmission(courseId: string, lessonId: string): Promise<CourseSubmission | null> {
return apiFetch(`/courses/${courseId}/lessons/${lessonId}/my-submission`);
},
async getPeerReviewSettings(courseId: string, lessonId: string): Promise<PeerReviewSettings | null> {
return apiFetch(`/courses/${courseId}/lessons/${lessonId}/peer-settings`);
},
async getProgressStats(courseId: string): Promise<ProgressStats> {
return apiFetch(`/courses/${courseId}/progress-stats`);
},
async getCourseProgress(courseId: string): Promise<{ progress_percentage: number; completed_lessons: number; total_lessons: number; completed: boolean }> {
return apiFetch(`/courses/${courseId}/progress`);
},
async toggleBookmark(lessonId: string): Promise<void> {
return apiFetch(`/lessons/${lessonId}/bookmark`, { method: 'POST' });
},
@@ -1308,6 +1334,37 @@ export const lmsApi = {
return apiFetch(`/bookmarks${query}`);
},
// Anotaciones en Lecciones (Fase 41-B)
async getLessonAnnotations(lessonId: string): Promise<LessonAnnotation[]> {
return apiFetch(`/lessons/${lessonId}/annotations`);
},
async createLessonAnnotation(lessonId: string, payload: CreateAnnotationPayload): Promise<LessonAnnotation> {
return apiFetch(`/lessons/${lessonId}/annotations`, {
method: 'POST',
body: JSON.stringify(payload),
});
},
async updateLessonAnnotation(lessonId: string, annotationId: string, payload: CreateAnnotationPayload): Promise<LessonAnnotation> {
return apiFetch(`/lessons/${lessonId}/annotations/${annotationId}`, {
method: 'PUT',
body: JSON.stringify(payload),
});
},
async deleteLessonAnnotation(lessonId: string, annotationId: string): Promise<void> {
return apiFetch(`/lessons/${lessonId}/annotations/${annotationId}`, { method: 'DELETE' });
},
async getMyAnnotations(): Promise<LessonAnnotation[]> {
return apiFetch(`/annotations`);
},
// Fase 41-C: Mentoría
async getMyMentor(courseId: string): Promise<MentorshipView | null> {
return apiFetch(`/courses/${courseId}/my-mentor`);
},
async getMyMentees(courseId: string): Promise<MentorshipView[]> {
return apiFetch(`/courses/${courseId}/my-mentees`);
},
// Live Learning & Portfolio
async getMeetings(courseId: string): Promise<Meeting[]> {
return apiFetch(`/courses/${courseId}/meetings`, {}, false);
@@ -1415,3 +1472,37 @@ export interface UpdateCollaborativeDocResponse {
server_content?: string;
server_revision?: number;
}
// ─── Anotaciones en Lecciones (Fase 41-B) ────────────────────────────────────
export interface LessonAnnotation {
id: string;
user_id: string;
lesson_id: string;
organization_id: string;
course_id: string;
content: string;
position_data: { type: 'timestamp'; value: number } | { type: 'scroll'; value: number } | null;
created_at: string;
updated_at: string;
}
export interface CreateAnnotationPayload {
content: string;
position_data?: LessonAnnotation['position_data'];
}
export interface MentorshipView {
id: string;
course_id: string;
notes: string | null;
created_at: string;
mentor_id: string;
mentor_name: string;
mentor_email: string;
mentor_avatar: string | null;
student_id: string;
student_name: string;
student_email: string;
student_avatar: string | null;
}
@@ -0,0 +1,388 @@
"use client";
import React, { useState, useEffect, useCallback } from "react";
import { useParams } from "next/navigation";
import { lmsApi, cmsApi, StudioMentorshipView, User } from "@/lib/api";
import {
Award,
Search,
Loader2,
X,
Plus,
Trash2,
UserCircle,
ChevronDown,
ChevronUp,
} from "lucide-react";
import CourseEditorLayout from "@/components/CourseEditorLayout";
export default function CourseMentorshipsPage() {
const { id: courseId } = useParams() as { id: string };
const [mentorships, setMentorships] = useState<StudioMentorshipView[]>([]);
const [loading, setLoading] = useState(true);
// Modal de asignación
const [isModalOpen, setIsModalOpen] = useState(false);
const [orgUsers, setOrgUsers] = useState<User[]>([]);
const [orgUsersLoading, setOrgUsersLoading] = useState(false);
const [selectedMentorId, setSelectedMentorId] = useState("");
const [selectedStudentId, setSelectedStudentId] = useState("");
const [notes, setNotes] = useState("");
const [saving, setSaving] = useState(false);
const [searchMentor, setSearchMentor] = useState("");
const [searchStudent, setSearchStudent] = useState("");
const load = useCallback(async () => {
try {
const data = await lmsApi.listCourseMentorships(courseId);
setMentorships(data);
} catch (e) {
console.error(e);
} finally {
setLoading(false);
}
}, [courseId]);
useEffect(() => { load(); }, [load]);
const openModal = async () => {
setIsModalOpen(true);
if (orgUsers.length === 0) {
setOrgUsersLoading(true);
try {
const users = await cmsApi.getAllUsers();
setOrgUsers(users);
} catch (e) {
console.error(e);
} finally {
setOrgUsersLoading(false);
}
}
};
const handleAssign = async () => {
if (!selectedMentorId || !selectedStudentId) return;
if (selectedMentorId === selectedStudentId) return;
setSaving(true);
try {
const created = await lmsApi.assignMentor(courseId, selectedMentorId, selectedStudentId, notes || undefined);
setMentorships(prev => {
// Si ya existía (upsert), reemplazar; si no, añadir
const exists = prev.find(m => m.id === created.id);
if (exists) return prev.map(m => m.id === created.id ? created : m);
return [created, ...prev];
});
setIsModalOpen(false);
setSelectedMentorId("");
setSelectedStudentId("");
setNotes("");
} catch (e) {
console.error(e);
} finally {
setSaving(false);
}
};
const handleDelete = async (mentorshipId: string) => {
try {
await lmsApi.deleteMentorship(courseId, mentorshipId);
setMentorships(prev => prev.filter(m => m.id !== mentorshipId));
} catch (e) {
console.error(e);
}
};
const filteredMentors = orgUsers.filter(u =>
(u.full_name + u.email).toLowerCase().includes(searchMentor.toLowerCase())
);
const filteredStudents = orgUsers.filter(u =>
(u.full_name + u.email).toLowerCase().includes(searchStudent.toLowerCase())
);
// Agrupar por mentor para mostrar vista compacta
const grouped = mentorships.reduce<Record<string, StudioMentorshipView[]>>((acc, m) => {
if (!acc[m.mentor_id]) acc[m.mentor_id] = [];
acc[m.mentor_id].push(m);
return acc;
}, {});
return (
<CourseEditorLayout
activeTab="mentorships"
pageTitle="Sistema de Mentoría"
pageDescription="Asigna mentores a alumnos para acompañamiento personalizado durante el curso."
pageActions={
<button
onClick={openModal}
className="flex items-center gap-2 px-6 py-2.5 bg-blue-600 hover:bg-blue-500 text-white rounded-xl font-bold text-sm shadow-md shadow-blue-600/20 transition-all active:scale-95"
>
<Plus size={18} /> Nueva Asignación
</button>
}
>
{loading ? (
<div className="flex justify-center py-20">
<Loader2 size={32} className="text-blue-400 animate-spin" />
</div>
) : mentorships.length === 0 ? (
<div className="py-20 text-center rounded-3xl border border-white/5 bg-white/[0.02]">
<div className="w-16 h-16 rounded-2xl bg-blue-500/10 flex items-center justify-center mx-auto mb-4">
<Award size={28} className="text-blue-400" />
</div>
<h3 className="text-lg font-black text-slate-900 dark:text-white mb-1">Sin asignaciones de mentoría</h3>
<p className="text-slate-500 text-sm">Asigna un mentor a un alumno para comenzar el acompañamiento.</p>
</div>
) : (
<div className="space-y-6">
<p className="text-xs text-slate-400 font-bold uppercase tracking-widest">
{mentorships.length} asignación{mentorships.length > 1 ? "es" : ""} {Object.keys(grouped).length} mentor{Object.keys(grouped).length > 1 ? "es" : ""}
</p>
{Object.entries(grouped).map(([mentorId, items]) => (
<MentorGroup
key={mentorId}
items={items}
onDelete={handleDelete}
/>
))}
</div>
)}
{/* Modal de asignación */}
{isModalOpen && (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
<div className="bg-white dark:bg-slate-900 rounded-3xl shadow-2xl w-full max-w-lg border border-slate-200 dark:border-white/10 overflow-hidden">
<div className="flex items-center justify-between p-6 border-b border-slate-100 dark:border-white/5">
<div className="flex items-center gap-3">
<div className="w-9 h-9 rounded-xl bg-blue-500/10 flex items-center justify-center">
<Award size={18} className="text-blue-500" />
</div>
<div>
<h2 className="font-black text-sm text-slate-900 dark:text-white">Nueva Asignación</h2>
<p className="text-[10px] text-slate-400 mt-0.5">Selecciona un mentor y un alumno</p>
</div>
</div>
<button onClick={() => setIsModalOpen(false)} className="p-2 hover:bg-slate-100 dark:hover:bg-white/10 rounded-xl transition-all">
<X size={18} className="text-slate-400" />
</button>
</div>
<div className="p-6 space-y-5">
{orgUsersLoading ? (
<div className="flex justify-center py-8">
<Loader2 size={24} className="text-blue-400 animate-spin" />
</div>
) : (
<>
{/* Selector mentor */}
<UserSelector
label="Mentor"
users={filteredMentors}
search={searchMentor}
onSearch={setSearchMentor}
selectedId={selectedMentorId}
onSelect={setSelectedMentorId}
excludeId={selectedStudentId}
/>
{/* Selector alumno */}
<UserSelector
label="Alumno"
users={filteredStudents}
search={searchStudent}
onSearch={setSearchStudent}
selectedId={selectedStudentId}
onSelect={setSelectedStudentId}
excludeId={selectedMentorId}
/>
{/* Notas */}
<div>
<label className="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2">
Notas internas (opcional)
</label>
<textarea
rows={2}
value={notes}
onChange={e => setNotes(e.target.value)}
placeholder="Ej: Apoyo en módulos 3-5, seguimiento semanal…"
className="w-full bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-xl px-3 py-2.5 text-sm text-slate-900 dark:text-white placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500/40 resize-none"
/>
</div>
</>
)}
</div>
<div className="px-6 pb-6 flex justify-end gap-3">
<button
onClick={() => setIsModalOpen(false)}
className="px-5 py-2.5 rounded-xl border border-slate-200 dark:border-white/10 text-sm font-black text-slate-500 hover:text-slate-700 dark:hover:text-white transition-all"
>
Cancelar
</button>
<button
onClick={handleAssign}
disabled={saving || !selectedMentorId || !selectedStudentId || selectedMentorId === selectedStudentId}
className="flex items-center gap-2 px-6 py-2.5 bg-blue-600 hover:bg-blue-500 disabled:opacity-50 text-white rounded-xl font-black text-sm transition-all active:scale-95"
>
{saving ? <Loader2 size={16} className="animate-spin" /> : <Plus size={16} />}
Asignar
</button>
</div>
</div>
</div>
)}
</CourseEditorLayout>
);
}
// ─── Sub-componentes ───────────────────────────────────────────────────────────
function UserSelector({
label,
users,
search,
onSearch,
selectedId,
onSelect,
excludeId,
}: {
label: string;
users: User[];
search: string;
onSearch: (v: string) => void;
selectedId: string;
onSelect: (id: string) => void;
excludeId: string;
}) {
const [open, setOpen] = useState(false);
const selected = users.find(u => u.id === selectedId);
return (
<div>
<label className="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2">
{label}
</label>
<button
type="button"
onClick={() => setOpen(v => !v)}
className="w-full flex items-center justify-between px-3 py-2.5 bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-xl text-sm transition-all hover:border-blue-400/50"
>
<span className={selected ? "text-slate-900 dark:text-white font-bold" : "text-slate-400"}>
{selected ? `${selected.full_name} (${selected.email})` : `Selecciona ${label.toLowerCase()}`}
</span>
{open ? <ChevronUp size={14} className="text-slate-400" /> : <ChevronDown size={14} className="text-slate-400" />}
</button>
{open && (
<div className="mt-1 border border-slate-200 dark:border-white/10 rounded-xl overflow-hidden bg-white dark:bg-slate-800 shadow-lg">
<div className="flex items-center gap-2 px-3 py-2 border-b border-slate-100 dark:border-white/5">
<Search size={14} className="text-slate-400" />
<input
autoFocus
value={search}
onChange={e => onSearch(e.target.value)}
placeholder="Buscar…"
className="flex-1 bg-transparent text-sm text-slate-900 dark:text-white placeholder-slate-400 focus:outline-none"
/>
</div>
<div className="max-h-40 overflow-y-auto">
{users.filter(u => u.id !== excludeId).slice(0, 30).map(u => (
<button
key={u.id}
onClick={() => { onSelect(u.id); setOpen(false); }}
className={`w-full flex items-center gap-3 px-3 py-2.5 text-left hover:bg-blue-50 dark:hover:bg-blue-500/10 transition-all ${u.id === selectedId ? "bg-blue-50 dark:bg-blue-500/10" : ""}`}
>
<div className="w-7 h-7 rounded-full bg-slate-200 dark:bg-white/10 flex items-center justify-center shrink-0 overflow-hidden">
{u.avatar_url ? (
<img src={u.avatar_url} alt="" className="w-full h-full object-cover" />
) : (
<UserCircle size={16} className="text-slate-400" />
)}
</div>
<div className="min-w-0">
<p className="text-xs font-bold text-slate-900 dark:text-white truncate">{u.full_name}</p>
<p className="text-[10px] text-slate-400 truncate">{u.email}</p>
</div>
</button>
))}
{users.filter(u => u.id !== excludeId).length === 0 && (
<p className="text-xs text-slate-400 text-center py-4">Sin resultados</p>
)}
</div>
</div>
)}
</div>
);
}
function MentorGroup({
items,
onDelete,
}: {
items: StudioMentorshipView[];
onDelete: (id: string) => void;
}) {
const [expanded, setExpanded] = useState(true);
const mentor = items[0];
return (
<div className="rounded-2xl border border-slate-200 dark:border-white/10 overflow-hidden bg-white dark:bg-white/[0.02]">
{/* Header del mentor */}
<button
onClick={() => setExpanded(v => !v)}
className="w-full flex items-center gap-4 p-4 hover:bg-slate-50 dark:hover:bg-white/5 transition-all"
>
<div className="w-10 h-10 rounded-full bg-blue-500/10 flex items-center justify-center shrink-0 overflow-hidden">
{mentor.mentor_avatar ? (
<img src={mentor.mentor_avatar} alt="" className="w-full h-full object-cover" />
) : (
<UserCircle size={20} className="text-blue-400" />
)}
</div>
<div className="text-left flex-1 min-w-0">
<p className="font-black text-sm text-slate-900 dark:text-white truncate">{mentor.mentor_name}</p>
<p className="text-[10px] text-slate-400 truncate">{mentor.mentor_email}</p>
</div>
<div className="flex items-center gap-3">
<span className="text-[10px] font-black text-blue-500 bg-blue-500/10 px-2.5 py-1 rounded-full">
{items.length} mentoreado{items.length > 1 ? "s" : ""}
</span>
{expanded ? <ChevronUp size={14} className="text-slate-400" /> : <ChevronDown size={14} className="text-slate-400" />}
</div>
</button>
{expanded && (
<div className="border-t border-slate-100 dark:border-white/5 divide-y divide-slate-100 dark:divide-white/5">
{items.map(m => (
<div key={m.id} className="flex items-center gap-4 px-4 py-3 group hover:bg-slate-50 dark:hover:bg-white/5 transition-all">
<div className="w-8 h-8 rounded-full bg-slate-100 dark:bg-white/5 flex items-center justify-center shrink-0 overflow-hidden ml-6">
{m.student_avatar ? (
<img src={m.student_avatar} alt="" className="w-full h-full object-cover" />
) : (
<UserCircle size={16} className="text-slate-400" />
)}
</div>
<div className="flex-1 min-w-0">
<p className="text-sm font-bold text-slate-800 dark:text-white truncate">{m.student_name}</p>
<p className="text-[10px] text-slate-400 truncate">{m.student_email}</p>
</div>
{m.notes && (
<p className="text-[10px] text-slate-400 italic max-w-[180px] truncate hidden md:block" title={m.notes}>
{m.notes}
</p>
)}
<button
onClick={() => onDelete(m.id)}
className="p-2 rounded-xl hover:bg-red-50 dark:hover:bg-red-500/10 text-slate-300 hover:text-red-400 transition-all opacity-0 group-hover:opacity-100"
title="Eliminar asignación"
>
<Trash2 size={14} />
</button>
</div>
))}
</div>
)}
</div>
);
}
@@ -1,8 +1,8 @@
"use client";
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useCallback } from "react";
import { useParams, useRouter } from "next/navigation";
import { cmsApi, lmsApi, Course, Lesson, SubmissionWithReviews, PeerReview } from "@/lib/api";
import { cmsApi, lmsApi, Course, Lesson, SubmissionWithReviews, PeerReview, PeerReviewSettings } from "@/lib/api";
import {
Users,
MessageSquare,
@@ -12,10 +12,584 @@ import {
CheckCircle,
Clock,
ChevronRight,
Award
Award,
Settings,
Zap,
Star,
SlidersHorizontal,
BadgeCheck,
} from "lucide-react";
import CourseEditorLayout from "@/components/CourseEditorLayout";
// ─── Componente: Panel de configuración de rúbrica/pesos ─────────────────────
function PeerSettingsPanel({
courseId,
lessonId,
onAssignDone,
}: {
courseId: string;
lessonId: string;
onAssignDone: () => void;
}) {
const [settings, setSettings] = useState<PeerReviewSettings | null>(null);
const [peerWeight, setPeerWeight] = useState(70);
const [instructorWeight, setInstructorWeight] = useState(30);
const [requiredReviews, setRequiredReviews] = useState(2);
const [autoAssign, setAutoAssign] = useState(true);
const [saving, setSaving] = useState(false);
const [assigning, setAssigning] = useState(false);
const [assignResult, setAssignResult] = useState<{ submissions_processed: number; assignments_created: number } | null>(null);
const [open, setOpen] = useState(false);
useEffect(() => {
lmsApi.getPeerReviewSettings(courseId, lessonId).then(s => {
if (s) {
setSettings(s);
setPeerWeight(s.peer_weight);
setInstructorWeight(s.instructor_weight);
setRequiredReviews(s.required_reviews);
setAutoAssign(s.auto_assign);
}
}).catch(() => {});
}, [courseId, lessonId]);
const handleSave = async () => {
setSaving(true);
try {
const updated = await lmsApi.upsertPeerReviewSettings(courseId, lessonId, {
peer_weight: peerWeight,
instructor_weight: instructorWeight,
required_reviews: requiredReviews,
auto_assign: autoAssign,
});
setSettings(updated);
} finally {
setSaving(false);
}
};
const handleAutoAssign = async () => {
setAssigning(true);
setAssignResult(null);
try {
const result = await lmsApi.autoAssignPeerReviews(courseId, lessonId);
setAssignResult(result);
onAssignDone();
} finally {
setAssigning(false);
}
};
const handlePeerWeightChange = (v: number) => {
setPeerWeight(v);
setInstructorWeight(100 - v);
};
return (
<div className="bg-white dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-[2rem] overflow-hidden shadow-sm mb-8">
<button
onClick={() => setOpen(o => !o)}
className="w-full flex items-center justify-between px-8 py-5 hover:bg-slate-50 dark:hover:bg-white/5 transition-colors"
>
<div className="flex items-center gap-3 text-sm font-black uppercase tracking-widest text-slate-600 dark:text-slate-300">
<SlidersHorizontal className="w-4 h-4 text-purple-500" />
Configuración de Pesos y Asignación Automática
</div>
<ChevronRight className={`w-5 h-5 text-slate-400 transition-transform ${open ? "rotate-90" : ""}`} />
</button>
{open && (
<div className="px-8 pb-8 space-y-6 border-t border-slate-100 dark:border-white/5 pt-6">
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
{/* Revisiones requeridas */}
<div>
<label className="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2">
Revisiones requeridas por entrega
</label>
<input
type="number"
min={1}
max={10}
value={requiredReviews}
onChange={e => setRequiredReviews(Number(e.target.value))}
className="w-full bg-slate-50 dark:bg-black/30 border border-slate-200 dark:border-white/10 rounded-xl px-4 py-2 text-sm font-bold focus:outline-none focus:ring-2 focus:ring-purple-500/30"
/>
</div>
{/* Peso de pares */}
<div>
<label className="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2">
Peso pares: <span className="text-purple-500">{peerWeight}%</span>
</label>
<input
type="range"
min={0}
max={100}
step={5}
value={peerWeight}
onChange={e => handlePeerWeightChange(Number(e.target.value))}
className="w-full accent-purple-500"
/>
<div className="flex justify-between text-[9px] text-slate-400 font-bold mt-1">
<span>Instructor: {instructorWeight}%</span>
<span>Pares: {peerWeight}%</span>
</div>
</div>
{/* Asignación automática */}
<div className="flex items-center gap-3 pt-5">
<button
onClick={() => setAutoAssign(a => !a)}
className={`relative w-12 h-6 rounded-full transition-colors ${autoAssign ? "bg-purple-500" : "bg-slate-300 dark:bg-slate-700"}`}
>
<span className={`absolute top-0.5 left-0.5 w-5 h-5 rounded-full bg-white shadow transition-transform ${autoAssign ? "translate-x-6" : ""}`} />
</button>
<span className="text-sm font-bold text-slate-600 dark:text-slate-300">
Asignación automática al entregar
</span>
</div>
</div>
<div className="flex gap-4 flex-wrap">
<button
onClick={handleSave}
disabled={saving}
className="flex items-center gap-2 px-6 py-3 bg-purple-600 hover:bg-purple-700 text-white rounded-xl text-sm font-black uppercase tracking-wider disabled:opacity-50 transition-colors"
>
{saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Settings className="w-4 h-4" />}
Guardar configuración
</button>
<button
onClick={handleAutoAssign}
disabled={assigning}
className="flex items-center gap-2 px-6 py-3 bg-gradient-to-r from-yellow-500 to-orange-500 hover:from-yellow-600 hover:to-orange-600 text-white rounded-xl text-sm font-black uppercase tracking-wider disabled:opacity-50 transition-all shadow-md shadow-yellow-500/20"
>
{assigning ? <Loader2 className="w-4 h-4 animate-spin" /> : <Zap className="w-4 h-4" />}
Asignar revisiones automáticamente
</button>
</div>
{assignResult && (
<div className="flex items-center gap-3 text-sm text-green-600 dark:text-green-400 bg-green-50 dark:bg-green-500/10 border border-green-200 dark:border-green-500/20 rounded-xl px-4 py-3">
<CheckCircle className="w-4 h-4" />
<span className="font-bold">
{assignResult.submissions_processed} entregas procesadas · {assignResult.assignments_created} asignaciones creadas
</span>
</div>
)}
{settings && (
<p className="text-[10px] text-slate-400 font-medium">
Config guardada · Actualizada {new Date(settings.updated_at).toLocaleString()}
</p>
)}
</div>
)}
</div>
);
}
// ─── Componente: Modal para calificación del instructor ───────────────────────
function InstructorGradeModal({
courseId,
lessonId,
submissionId,
studentName,
onClose,
onGraded,
}: {
courseId: string;
lessonId: string;
submissionId: string;
studentName: string;
onClose: () => void;
onGraded: () => void;
}) {
const [score, setScore] = useState(75);
const [feedback, setFeedback] = useState("");
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
const handleSubmit = async () => {
if (!feedback.trim()) { setError("El feedback es obligatorio"); return; }
setSaving(true);
setError("");
try {
await lmsApi.instructorGradeSubmission(courseId, lessonId, submissionId, score, feedback);
onGraded();
onClose();
} catch (e: any) {
setError(e.message ?? "Error al guardar la calificación");
} finally {
setSaving(false);
}
};
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm">
<div className="bg-white dark:bg-gray-900 border border-slate-200 dark:border-white/10 rounded-[2rem] p-8 w-full max-w-md shadow-2xl space-y-6">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-yellow-50 dark:bg-yellow-500/10 flex items-center justify-center">
<Star className="w-5 h-5 text-yellow-500" />
</div>
<div>
<h3 className="font-black text-slate-900 dark:text-white uppercase tracking-tight">Calificación del Instructor</h3>
<p className="text-xs text-slate-400">{studentName}</p>
</div>
</div>
<div>
<label className="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2">
Puntuación: <span className="text-yellow-500">{score}/100</span>
</label>
<input
type="range" min={0} max={100} step={1} value={score}
onChange={e => setScore(Number(e.target.value))}
className="w-full accent-yellow-500"
/>
</div>
<div>
<label className="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2">
Feedback del instructor *
</label>
<textarea
rows={4}
value={feedback}
onChange={e => setFeedback(e.target.value)}
placeholder="Escribe tu retroalimentación detallada..."
className="w-full bg-slate-50 dark:bg-black/30 border border-slate-200 dark:border-white/10 rounded-xl px-4 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-yellow-500/30 resize-none"
/>
{error && <p className="text-xs text-red-500 mt-1">{error}</p>}
</div>
<div className="flex gap-3">
<button onClick={onClose} className="flex-1 px-4 py-3 border border-slate-200 dark:border-white/10 rounded-xl text-sm font-bold text-slate-600 dark:text-slate-300 hover:bg-slate-50 dark:hover:bg-white/5 transition-colors">
Cancelar
</button>
<button
onClick={handleSubmit}
disabled={saving}
className="flex-1 flex items-center justify-center gap-2 px-4 py-3 bg-yellow-500 hover:bg-yellow-600 text-white rounded-xl text-sm font-black uppercase tracking-wider disabled:opacity-50 transition-colors"
>
{saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <BadgeCheck className="w-4 h-4" />}
Calificar
</button>
</div>
</div>
</div>
);
}
// ─── Página Principal ─────────────────────────────────────────────────────────
export default function PeerReviewDashboard() {
const { id } = useParams() as { id: string };
const router = useRouter();
const [course, setCourse] = useState<Course | null>(null);
const [lessons, setLessons] = useState<Lesson[]>([]);
const [selectedLessonId, setSelectedLessonId] = useState<string | null>(null);
const [submissions, setSubmissions] = useState<SubmissionWithReviews[]>([]);
const [selectedSubmissionId, setSelectedSubmissionId] = useState<string | null>(null);
const [reviews, setReviews] = useState<PeerReview[]>([]);
const [loading, setLoading] = useState(true);
const [submissionsLoading, setSubmissionsLoading] = useState(false);
const [reviewsLoading, setReviewsLoading] = useState(false);
const [searchTerm, setSearchTerm] = useState("");
const [gradeModal, setGradeModal] = useState<{ submissionId: string; studentName: string } | null>(null);
useEffect(() => {
const loadInitialData = async () => {
try {
setLoading(true);
const courseData = await cmsApi.getCourseWithFullOutline(id);
setCourse(courseData);
const peerReviewLessons: Lesson[] = [];
courseData.modules?.forEach(m => {
m.lessons.forEach(l => {
const hasPeerReview = l.metadata?.blocks?.some((b: any) => b.type === 'peer-review');
if (hasPeerReview) {
peerReviewLessons.push(l);
}
});
});
setLessons(peerReviewLessons);
if (peerReviewLessons.length > 0) {
setSelectedLessonId(peerReviewLessons[0].id);
}
} catch (error) {
console.error("Error loading course data:", error);
} finally {
setLoading(false);
}
};
loadInitialData();
}, [id]);
const loadSubmissions = useCallback(async () => {
if (!selectedLessonId) return;
try {
setSubmissionsLoading(true);
const data = await lmsApi.listLessonSubmissions(id, selectedLessonId);
setSubmissions(data);
} catch (error) {
console.error("Error loading submissions:", error);
} finally {
setSubmissionsLoading(false);
}
}, [id, selectedLessonId]);
useEffect(() => {
loadSubmissions();
}, [loadSubmissions]);
useEffect(() => {
if (!selectedSubmissionId) {
setReviews([]);
return;
}
const loadReviews = async () => {
try {
setReviewsLoading(true);
const data = await lmsApi.getSubmissionReviews(selectedSubmissionId);
setReviews(data);
} catch (error) {
console.error("Error loading reviews:", error);
} finally {
setReviewsLoading(false);
}
};
loadReviews();
}, [selectedSubmissionId]);
const filteredSubmissions = submissions.filter(s =>
s.full_name.toLowerCase().includes(searchTerm.toLowerCase()) ||
s.email.toLowerCase().includes(searchTerm.toLowerCase())
);
if (loading) {
return (
<div className="min-h-screen bg-transparent flex items-center justify-center">
<Loader2 className="w-10 h-10 text-blue-500 animate-spin" />
</div>
);
}
return (
<div className="min-h-screen bg-transparent text-gray-900 dark:text-white p-8">
<div className="max-w-7xl mx-auto">
<div className="flex items-center justify-between mb-8">
<div className="flex items-center gap-4">
<button onClick={() => router.back()} className="p-2 hover:bg-white/10 rounded-full transition-colors">
<ArrowLeft className="w-6 h-6" />
</button>
<div>
<h1 className="text-4xl font-black bg-gradient-to-r from-purple-600 to-blue-600 bg-clip-text text-transparent uppercase tracking-tighter">
Peer Assessment
</h1>
<p className="text-slate-500 dark:text-gray-400 mt-1 font-medium">Monitor, asignación automática y calificación con rúbricas configurables</p>
</div>
</div>
</div>
<CourseEditorLayout activeTab="peer-reviews">
<div className="grid grid-cols-1 lg:grid-cols-4 gap-10">
{/* Lessons List */}
<div className="space-y-6">
<h3 className="text-[10px] font-black text-slate-400 dark:text-gray-500 uppercase tracking-[0.2em] px-4">Actividades</h3>
<div className="space-y-2">
{lessons.length === 0 ? (
<div className="p-6 bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-2xl text-sm text-slate-400 italic">
No se encontraron actividades de peer review.
</div>
) : (
lessons.map(lesson => (
<button
key={lesson.id}
onClick={() => {
setSelectedLessonId(lesson.id);
setSelectedSubmissionId(null);
}}
className={`w-full text-left p-5 rounded-[1.5rem] border transition-all active:scale-95 ${selectedLessonId === lesson.id
? "bg-purple-50 dark:bg-purple-500/10 border-purple-200 dark:border-purple-500/50 text-purple-600 dark:text-purple-400 shadow-md shadow-purple-500/5 font-black uppercase tracking-tight"
: "bg-white dark:bg-white/5 border-slate-200 dark:border-white/10 text-slate-500 dark:text-gray-400 hover:border-purple-500/30 font-bold"
}`}
>
<div className="truncate text-sm">{lesson.title}</div>
<div className="text-[9px] uppercase font-black tracking-widest opacity-60 mt-1">Peer Review</div>
</button>
))
)}
</div>
</div>
{/* Main content */}
<div className="lg:col-span-3 space-y-0">
{/* Panel de configuración — solo cuando hay lección seleccionada */}
{selectedLessonId && (
<PeerSettingsPanel
courseId={id}
lessonId={selectedLessonId}
onAssignDone={loadSubmissions}
/>
)}
{/* Submissions List */}
<div className="bg-white dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-[2.5rem] p-10 shadow-sm">
<div className="flex flex-col md:flex-row md:items-center justify-between gap-6 mb-10">
<h2 className="text-2xl font-black flex items-center gap-4 uppercase tracking-tight text-slate-900 dark:text-white">
<div className="w-12 h-12 rounded-2xl bg-blue-50 dark:bg-blue-500/10 border border-blue-100 dark:border-blue-500/20 flex items-center justify-center text-blue-600 dark:text-blue-400 shadow-sm">
<Users size={24} />
</div>
Entregas
</h2>
<div className="relative w-full md:w-80 group">
<Search className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-300 dark:text-gray-500 w-5 h-5 group-focus-within:text-blue-500 transition-colors" />
<input
type="text"
placeholder="Buscar alumno o email..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full bg-slate-50 dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-[1.25rem] py-4 pl-12 pr-6 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500/30 text-slate-900 dark:text-white transition-all shadow-inner"
/>
</div>
</div>
{submissionsLoading ? (
<div className="flex flex-col items-center justify-center py-32 space-y-4">
<Loader2 className="w-12 h-12 text-blue-500 animate-spin" />
<span className="text-xs font-black uppercase tracking-widest text-slate-400">Cargando entregas...</span>
</div>
) : filteredSubmissions.length === 0 ? (
<div className="text-center py-32 bg-slate-50 dark:bg-black/20 rounded-3xl border border-dashed border-slate-200 dark:border-white/10">
<div className="w-20 h-20 bg-white dark:bg-white/5 rounded-2xl flex items-center justify-center mx-auto mb-6 shadow-sm">
<MessageSquare className="w-10 h-10 text-slate-300 dark:text-gray-700" />
</div>
<p className="text-slate-500 dark:text-gray-500 font-bold uppercase tracking-tight">No se encontraron entregas para esta actividad.</p>
</div>
) : (
<div className="space-y-4 overflow-y-auto max-h-[700px] pr-4 custom-scrollbar">
{filteredSubmissions.map(sub => (
<div key={sub.id} className="group">
<div
onClick={() => setSelectedSubmissionId(selectedSubmissionId === sub.id ? null : sub.id)}
className={`p-6 rounded-[1.5rem] border transition-all cursor-pointer shadow-sm active:scale-[0.99] ${selectedSubmissionId === sub.id
? "bg-blue-50 dark:bg-blue-500/5 border-blue-200 dark:border-blue-500/30"
: "bg-slate-50/50 dark:bg-white/[0.02] border-slate-200 dark:border-white/5 hover:bg-white dark:hover:bg-white/[0.05] hover:border-blue-500/30"
}`}
>
<div className="flex items-center justify-between">
<div className="flex items-center gap-5">
<div className="w-12 h-12 rounded-2xl bg-gradient-to-br from-blue-600 to-indigo-700 flex items-center justify-center font-black text-white text-lg shadow-lg shadow-blue-500/20">
{sub.full_name.charAt(0)}
</div>
<div>
<div className="font-black text-slate-900 dark:text-white group-hover:text-blue-600 dark:group-hover:text-blue-400 transition-colors uppercase tracking-tight text-sm">{sub.full_name}</div>
<div className="text-[10px] font-bold text-slate-400 dark:text-gray-500 uppercase tracking-widest">{sub.email}</div>
</div>
</div>
<div className="flex items-center gap-6">
{/* Calificación promedio */}
<div className="text-right">
<div className="text-lg font-black text-slate-900 dark:text-white flex items-center gap-2 justify-end">
<Award className="w-5 h-5 text-yellow-500" />
{sub.average_score !== null ? `${(sub.average_score).toFixed(1)}` : '—'}
</div>
<div className="text-[9px] text-slate-400 dark:text-gray-500 font-black uppercase tracking-[0.2em]">Prom. pares</div>
</div>
{/* Nº de revisiones */}
<div className="text-right">
<div className={`text-lg font-black flex items-center gap-2 justify-end ${sub.review_count >= 2 ? 'text-green-600' : 'text-orange-500'}`}>
<CheckCircle className="w-5 h-5" />
{sub.review_count}
</div>
<div className="text-[9px] text-slate-400 dark:text-gray-500 font-black uppercase tracking-[0.2em]">Revisiones</div>
</div>
{/* Fecha */}
<div className="text-right hidden xl:block">
<div className="text-sm font-bold text-slate-400 dark:text-gray-500 flex items-center gap-2 justify-end">
<Clock className="w-4 h-4" />
{new Date(sub.submitted_at).toLocaleDateString()}
</div>
<div className="text-[9px] text-slate-400 dark:text-gray-500 font-black uppercase tracking-[0.2em]">Entregado</div>
</div>
{/* Botón calificar instructor */}
<button
onClick={(e) => {
e.stopPropagation();
setGradeModal({ submissionId: sub.id, studentName: sub.full_name });
}}
className="p-2 rounded-xl bg-yellow-50 dark:bg-yellow-500/10 border border-yellow-200 dark:border-yellow-500/20 text-yellow-600 dark:text-yellow-400 hover:bg-yellow-100 dark:hover:bg-yellow-500/20 transition-colors"
title="Calificar como instructor"
>
<Star className="w-4 h-4" />
</button>
<ChevronRight className={`w-6 h-6 text-slate-300 transition-all ${selectedSubmissionId === sub.id ? 'rotate-90 text-blue-500' : ''}`} />
</div>
</div>
</div>
{/* Reviews expandidas */}
{selectedSubmissionId === sub.id && (
<div className="mt-4 ml-8 p-10 bg-slate-50 dark:bg-black/40 border-l-4 border-blue-500 dark:border-blue-500/50 rounded-r-[2rem] space-y-8 animate-in slide-in-from-left-4 duration-300 shadow-inner">
<h4 className="text-[10px] font-black uppercase tracking-[0.3em] text-blue-600 dark:text-blue-400 ml-1">Revisiones Recibidas</h4>
{reviewsLoading ? (
<div className="flex py-10"><Loader2 className="w-8 h-8 animate-spin text-blue-500" /></div>
) : reviews.length === 0 ? (
<div className="p-8 bg-white dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-2xl text-center text-slate-400 italic">
Aún no hay revisiones para esta entrega.
</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 gap-8">
{reviews.map(review => (
<div key={review.id} className={`p-6 bg-white dark:bg-white/5 border rounded-2xl space-y-4 shadow-sm transition-all ${review.is_instructor_review ? "border-yellow-200 dark:border-yellow-500/30" : "border-slate-100 dark:border-white/10"}`}>
<div className="flex justify-between items-center pb-3 border-b border-slate-50 dark:border-white/5">
<span className={`text-[9px] font-black uppercase tracking-widest ${review.is_instructor_review ? "text-yellow-600" : "text-slate-400 dark:text-gray-500"}`}>
{review.is_instructor_review ? "⭐ Instructor" : "Par evaluador"}
</span>
<span className={`px-3 py-1 text-sm font-black rounded-lg ${review.is_instructor_review ? "bg-yellow-50 dark:bg-yellow-500/10 text-yellow-600 dark:text-yellow-500" : "bg-blue-50 dark:bg-blue-500/10 text-blue-600 dark:text-blue-500"}`}>
{review.score}/100
</span>
</div>
<p className="text-sm text-slate-600 dark:text-gray-300 leading-relaxed italic font-medium px-2">
"{review.feedback}"
</p>
</div>
))}
</div>
)}
</div>
)}
</div>
))}
</div>
)}
</div>
</div>
</div>
</CourseEditorLayout>
</div>
{/* Modal calificación instructor */}
{gradeModal && (
<InstructorGradeModal
courseId={id}
lessonId={selectedLessonId!}
submissionId={gradeModal.submissionId}
studentName={gradeModal.studentName}
onClose={() => setGradeModal(null)}
onGraded={loadSubmissions}
/>
)}
</div>
);
}
export default function PeerReviewDashboard() {
const { id } = useParams() as { id: string };
const router = useRouter();
@@ -234,12 +234,11 @@ export default function CourseSettingsPage() {
const handleExport = async () => {
setExporting(true);
try {
const data = await cmsApi.exportCourse(id);
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const blob = await cmsApi.exportCourse(id);
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `course_${id}_export.json`;
a.download = `course_${id}.ccb`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
@@ -258,17 +257,15 @@ export default function CourseSettingsPage() {
setImporting(true);
try {
const text = await file.text();
const data = JSON.parse(text);
const newCourse = await cmsApi.importCourse(data);
const newCourse = await cmsApi.importCourse(file);
alert(`Curso importado con éxito: ${newCourse.title}`);
router.push(`/courses/${newCourse.id}/settings`);
} catch (err) {
console.error("Import failed", err);
alert("Error al importar el curso. Asegúrate de que el formato sea válido.");
alert("Error al importar el curso. Asegúrate de que el archivo sea un .ccb válido.");
} finally {
setImporting(false);
if (e.target) e.target.value = ''; // Reset input
if (e.target) e.target.value = '';
}
};
@@ -654,13 +651,13 @@ export default function CourseSettingsPage() {
<div className="space-y-4">
<h3 className="text-sm font-black text-slate-800 dark:text-gray-300 uppercase tracking-wider">Import Course</h3>
<p className="text-xs text-slate-500 dark:text-gray-500 font-medium">
Upload a previously exported course JSON file. This will create a NEW course
within the current organization based on that data.
Sube un archivo <code>.ccb</code> exportado previamente. Se creará un NUEVO curso
en la organización actual con todos sus módulos, lecciones y categorías de calificación.
</p>
<div className="relative">
<input
type="file"
accept=".json"
accept=".ccb,.zip"
onChange={handleImport}
disabled={importing}
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer disabled:cursor-not-allowed"
+341 -202
View File
@@ -2,309 +2,448 @@
import React, { useState, useEffect, useCallback } from "react";
import { useParams, useRouter } from "next/navigation";
import { lmsApi, cmsApi, StudentGradeReport, Cohort, User } from "@/lib/api";
import { lmsApi, cmsApi, StudentGradeReport, User } from "@/lib/api";
import {
Users,
UserPlus,
Search,
ArrowLeft,
Loader2,
X,
Filter,
CheckCircle2,
Trash2,
Mail,
Plus,
UserCircle,
MoreHorizontal,
ChevronRight
Bell,
TrendingDown,
AlertTriangle,
Clock,
BarChart2,
} from "lucide-react";
import CourseEditorLayout from "@/components/CourseEditorLayout";
function riskLevel(student: StudentGradeReport): "critical" | "high" | "medium" | "ok" {
const daysInactive = student.last_active_at
? Math.floor((Date.now() - new Date(student.last_active_at).getTime()) / 86400000)
: 999;
const avgScore = student.average_score ?? null;
if (daysInactive >= 14 || (avgScore !== null && avgScore * 100 < 40)) return "critical";
if (daysInactive >= 7 || (avgScore !== null && avgScore * 100 < 60)) return "high";
if (daysInactive >= 3 || (avgScore !== null && avgScore * 100 < 70)) return "medium";
return "ok";
}
function RiskBadge({ level }: { level: ReturnType<typeof riskLevel> }) {
const map = {
critical: { label: "Crítico", cls: "bg-red-500/10 text-red-500 border-red-500/20" },
high: { label: "Alto", cls: "bg-orange-500/10 text-orange-500 border-orange-500/20" },
medium: { label: "Medio", cls: "bg-yellow-500/10 text-yellow-500 border-yellow-500/20" },
ok: { label: "Bien", cls: "bg-emerald-500/10 text-emerald-500 border-emerald-500/20" },
};
const { label, cls } = map[level];
return (
<span className={`inline-flex items-center px-2.5 py-1 rounded-full border text-[9px] font-black uppercase tracking-widest ${cls}`}>
{label}
</span>
);
}
export default function CourseStudentsPage() {
const { id } = useParams() as { id: string };
const router = useRouter();
const [students, setStudents] = useState<StudentGradeReport[]>([]);
const [cohorts, setCohorts] = useState<Cohort[]>([]);
const [loading, setLoading] = useState(true);
const [searchTerm, setSearchTerm] = useState("");
const [selectedCohortId, setSelectedCohortId] = useState<string>("all");
const [riskFilter, setRiskFilter] = useState<"all" | "critical" | "high" | "medium">("all");
const [isEnrollModalOpen, setIsEnrollModalOpen] = useState(false);
const [allOrgUsers, setAllOrgUsers] = useState<User[]>([]);
const [orgUsersLoading, setOrgUsersLoading] = useState(false);
const [enrollSearch, setEnrollSearch] = useState("");
const [notifyTarget, setNotifyTarget] = useState<StudentGradeReport | null>(null);
const [notifyTitle, setNotifyTitle] = useState("");
const [notifyMessage, setNotifyMessage] = useState("");
const [notifySending, setNotifySending] = useState(false);
const [notifySuccess, setNotifySuccess] = useState(false);
const fetchData = useCallback(async () => {
try {
setLoading(true);
const [gradesData, cohortsData] = await Promise.all([
lmsApi.getCourseGrades(id),
lmsApi.getCohorts()
]);
const gradesData = await lmsApi.getCourseGrades(id);
setStudents(gradesData);
setCohorts(cohortsData);
} catch (error) {
console.error("Error fetching students and cohorts:", error);
console.error("Error fetching students:", error);
} finally {
setLoading(false);
}
}, [id]);
useEffect(() => {
fetchData();
}, [fetchData]);
const loadOrgUsers = async () => {
try {
setOrgUsersLoading(true);
const users = await cmsApi.getAllUsers();
// Filter out those already enrolled
const enrolledIds = new Set(students.map(s => s.user_id));
setAllOrgUsers(users.filter(u => u.role === 'student' && !enrolledIds.has(u.id)));
} catch (error) {
console.error("Error loading org users:", error);
} finally {
setOrgUsersLoading(false);
}
};
useEffect(() => { fetchData(); }, [fetchData]);
useEffect(() => {
if (isEnrollModalOpen) {
loadOrgUsers();
}
if (!isEnrollModalOpen) return;
setOrgUsersLoading(true);
cmsApi.getAllUsers()
.then((users: User[]) => {
const enrolled = new Set(students.map(s => s.user_id));
setAllOrgUsers(users.filter(u => u.role === "student" && !enrolled.has(u.id)));
})
.catch(console.error)
.finally(() => setOrgUsersLoading(false));
}, [isEnrollModalOpen, students]);
const handleEnroll = async (emails: string[]) => {
try {
await lmsApi.bulkEnroll(id, emails);
fetchData();
setIsEnrollModalOpen(false);
} catch (error) {
console.error("Enrollment failed:", error);
alert("Failed to enroll students.");
}
await lmsApi.bulkEnroll(id, emails);
fetchData();
setIsEnrollModalOpen(false);
};
const handleCohortAssignment = async (userId: string, cohortId: string, remove: boolean = false) => {
const openNotify = (student: StudentGradeReport) => {
setNotifyTarget(student);
setNotifyTitle("");
setNotifyMessage("");
setNotifySuccess(false);
};
const handleNotify = async () => {
if (!notifyTarget || !notifyTitle.trim() || !notifyMessage.trim()) return;
setNotifySending(true);
try {
if (remove) {
await lmsApi.removeMember(cohortId, userId);
} else {
await lmsApi.addMember(cohortId, userId);
}
// In a real app we'd need to refresh specifically which cohorts each student is in.
// Since StudentGradeReport doesn't include cohorts, we might need a better API or just a toast.
alert(`Student ${remove ? 'removed from' : 'added to'} cohort.`);
} catch (error) {
console.error("Cohort assignment failed:", error);
await lmsApi.notifyStudent(id, notifyTarget.user_id, notifyTitle, notifyMessage);
setNotifySuccess(true);
setTimeout(() => {
setNotifyTarget(null);
setNotifyTitle("");
setNotifyMessage("");
setNotifySuccess(false);
}, 1500);
} catch (e) {
console.error(e);
} finally {
setNotifySending(false);
}
};
const filteredStudents = students.filter(s => {
const matchesSearch = s.full_name.toLowerCase().includes(searchTerm.toLowerCase()) ||
const matchSearch =
s.full_name.toLowerCase().includes(searchTerm.toLowerCase()) ||
s.email.toLowerCase().includes(searchTerm.toLowerCase());
// Note: Filtering by cohort is tricky because the grades API doesn't return cohorts per student directly.
// For now we'll just implement search. Real cohort filtering would need backend support in getCourseGrades.
return matchesSearch;
const matchRisk = riskFilter === "all" || riskLevel(s) === riskFilter;
return matchSearch && matchRisk;
});
if (loading) {
return (
<div className="min-h-screen bg-transparent flex items-center justify-center">
<Loader2 className="w-10 h-10 text-blue-500 animate-spin" />
</div>
);
}
const atRisk = students.filter(s => ["critical", "high"].includes(riskLevel(s)));
if (loading) return (
<div className="min-h-screen flex items-center justify-center">
<Loader2 className="w-10 h-10 text-blue-500 animate-spin" />
</div>
);
return (
<>
<CourseEditorLayout
activeTab="students"
pageTitle="Estudiantes y Grupos"
pageDescription="Gestiona las inscripciones y segmenta a tu audiencia por cohortes."
pageDescription="Gestiona inscripciones, monitorea progreso y comunícate con tus alumnos."
pageActions={
<button
onClick={() => setIsEnrollModalOpen(true)}
className="flex items-center gap-2 px-6 py-2.5 bg-blue-600 hover:bg-blue-500 text-white rounded-xl font-bold text-sm shadow-md shadow-blue-600/20 transition-all active:scale-95"
>
<UserPlus size={18} />
Inscribir Estudiantes
<UserPlus size={18} /> Inscribir Estudiantes
</button>
}
>
<div className="space-y-8">
<h2 className="section-title">
<Users className="text-blue-500" />
Listado de Estudiantes
</h2>
{/* Search and Filters */}
<div className="bg-slate-50/50 dark:bg-white/5 border border-slate-200 dark:border-white/10 p-5 rounded-3xl flex flex-col md:flex-row items-center gap-4 shadow-sm">
{/* Alerta de riesgo */}
{atRisk.length > 0 && (
<div className="flex items-start gap-4 p-5 bg-red-500/5 border border-red-500/20 rounded-2xl">
<AlertTriangle size={20} className="text-red-500 shrink-0 mt-0.5" />
<div>
<p className="text-sm font-black text-red-500">
{atRisk.length} alumno{atRisk.length > 1 ? "s" : ""} necesita{atRisk.length === 1 ? "" : "n"} atención
</p>
<p className="text-xs text-red-400/70 mt-0.5">
Sin actividad reciente o con calificaciones por debajo del umbral. Usa el botón 🔔 para contactarlos.
</p>
</div>
</div>
)}
{/* Filtros */}
<div className="bg-slate-50/50 dark:bg-white/5 border border-slate-200 dark:border-white/10 p-5 rounded-3xl flex flex-col md:flex-row items-center gap-4">
<div className="relative flex-1 w-full">
<Search className="absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400 dark:text-gray-500 w-4 h-4" />
<Search className="absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400 w-4 h-4" />
<input
type="text"
placeholder="Search by name or email..."
placeholder="Buscar por nombre o email..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full bg-white dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-2xl py-2.5 pl-11 pr-4 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500/50 transition-all font-bold text-slate-900 dark:text-white placeholder-slate-400 dark:placeholder-gray-600 shadow-inner"
onChange={e => setSearchTerm(e.target.value)}
className="w-full bg-white dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-2xl py-2.5 pl-11 pr-4 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500/50 font-bold text-slate-900 dark:text-white placeholder-slate-400"
/>
</div>
<div className="flex items-center gap-3 w-full md:w-auto">
<Filter size={16} className="text-slate-400 dark:text-gray-400" />
<Filter size={16} className="text-slate-400 shrink-0" />
<select
className="bg-white dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-2xl px-5 py-2.5 text-xs font-black uppercase tracking-widest focus:outline-none focus:ring-2 focus:ring-blue-500/50 text-slate-900 dark:text-white min-w-[180px] shadow-sm cursor-pointer"
value={selectedCohortId}
onChange={(e) => setSelectedCohortId(e.target.value)}
value={riskFilter}
onChange={e => setRiskFilter(e.target.value as typeof riskFilter)}
className="bg-white dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-2xl px-4 py-2.5 text-xs font-black uppercase tracking-widest focus:outline-none text-slate-900 dark:text-white min-w-[160px]"
>
<option value="all">All Cohorts</option>
{cohorts.map(c => <option key={c.id} value={c.id}>{c.name}</option>)}
<option value="all">Todos los alumnos</option>
<option value="critical"> Riesgo Crítico</option>
<option value="high">🟠 Riesgo Alto</option>
<option value="medium">🟡 Riesgo Medio</option>
</select>
</div>
<span className="text-xs text-slate-400 font-bold whitespace-nowrap">
{filteredStudents.length} de {students.length}
</span>
</div>
{/* Student List */}
{/* Tabla */}
<div className="rounded-3xl border border-slate-200 dark:border-white/10 bg-white dark:bg-white/[0.02] overflow-hidden shadow-sm">
<table className="w-full text-left border-collapse">
<thead>
<tr className="bg-slate-50 dark:bg-white/5 border-b border-slate-200 dark:border-white/5 font-black text-[10px] uppercase tracking-[0.2em] text-slate-400 dark:text-gray-500">
<th className="p-6">Student</th>
<th className="p-6 text-center">Enrollment Status</th>
<th className="p-6 text-right">Actions</th>
<tr className="bg-slate-50 dark:bg-white/5 border-b border-slate-200 dark:border-white/5 text-[10px] uppercase tracking-[0.2em] text-slate-400 font-black">
<th className="p-5">Alumno</th>
<th className="p-5 text-center hidden md:table-cell">Progreso</th>
<th className="p-5 text-center hidden lg:table-cell">Promedio</th>
<th className="p-5 text-center hidden lg:table-cell">Última Actividad</th>
<th className="p-5 text-center">Riesgo</th>
<th className="p-5 text-right">Acción</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-100 dark:divide-white/5">
{filteredStudents.length === 0 ? (
<tr>
<td colSpan={3} className="p-12 text-center text-slate-500 dark:text-gray-500 italic">No students found.</td>
<td colSpan={6} className="p-12 text-center text-slate-400 italic text-sm">
No se encontraron alumnos.
</td>
</tr>
) : filteredStudents.map(student => (
<tr key={student.user_id} className="hover:bg-white/[0.02] transition-colors group">
<td className="p-6">
<div className="flex items-center gap-4">
<div className="w-12 h-12 rounded-2xl bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center font-black text-white text-lg shadow-lg shadow-blue-500/20">
{student.full_name.charAt(0)}
</div>
<div>
<div className="font-black text-slate-900 dark:text-white group-hover:text-blue-600 dark:group-hover:text-blue-400 transition-colors uppercase tracking-tight text-sm">{student.full_name}</div>
<div className="text-xs text-slate-400 dark:text-gray-500 flex items-center gap-1.5 mt-1 font-medium italic"><Mail size={12} className="text-blue-400" /> {student.email}</div>
</div>
</div>
</td>
<td className="p-6 text-center">
<div className="inline-flex items-center gap-2 px-3 py-1.5 bg-green-500/10 text-green-600 dark:text-green-400 rounded-full border border-green-500/20 text-[10px] font-black uppercase tracking-widest">
<CheckCircle2 size={12} /> Active
</div>
</td>
<td className="p-6 text-right">
<div className="flex items-center justify-end gap-2">
<div className="relative group/actions">
<button className="p-2.5 hover:bg-slate-100 dark:hover:bg-white/10 rounded-xl transition-all text-slate-400 dark:text-gray-500 active:scale-90">
<MoreHorizontal size={20} />
</button>
<div className="absolute right-0 top-full mt-2 w-56 bg-white dark:bg-[#1a1c1e] border border-slate-200 dark:border-white/10 rounded-2xl shadow-2xl invisible group-hover/actions:visible z-10 p-2 animate-in fade-in slide-in-from-top-2 duration-200">
<div className="text-[10px] font-black uppercase tracking-widest text-slate-400 dark:text-gray-600 px-3 py-2.5 border-b border-slate-100 dark:border-white/5 mb-1.5 flex items-center gap-2">
<Filter size={10} /> Move to Cohort
) : filteredStudents.map(student => {
const risk = riskLevel(student);
const daysInactive = student.last_active_at
? Math.floor((Date.now() - new Date(student.last_active_at).getTime()) / 86400000)
: null;
const progress = Math.min(Math.round(student.progress ?? 0), 100);
const avgPct = student.average_score != null ? Math.round(student.average_score * 100) : null;
return (
<tr key={student.user_id} className="hover:bg-slate-50 dark:hover:bg-white/[0.03] transition-colors">
{/* Alumno */}
<td className="p-5">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center font-black text-white text-sm shrink-0">
{student.full_name.charAt(0)}
</div>
<div>
<div className="font-black text-slate-900 dark:text-white text-sm">{student.full_name}</div>
<div className="text-[10px] text-slate-400 flex items-center gap-1 mt-0.5">
<Mail size={10} /> {student.email}
</div>
{cohorts.map(c => (
<button
key={c.id}
onClick={() => handleCohortAssignment(student.user_id, c.id)}
className="w-full text-left px-4 py-2.5 text-xs font-bold text-slate-700 dark:text-gray-300 hover:bg-slate-50 dark:hover:bg-white/5 hover:text-blue-600 dark:hover:text-white rounded-xl transition-all flex items-center justify-between group/item"
>
{c.name}
<ChevronRight size={14} className="opacity-0 group-hover/item:opacity-100 -translate-x-2 group-hover/item:translate-x-0 transition-all" />
</button>
))}
<button
className="w-full text-left px-4 py-2.5 text-xs font-black uppercase tracking-widest text-red-500 hover:bg-red-50 dark:hover:bg-red-500/10 rounded-xl transition-all mt-2 border-t border-slate-100 dark:border-white/5 pt-3"
onClick={() => { if (confirm("Unenroll student?")) handleCohortAssignment(student.user_id, "", true) }}
>
Unenroll Student
</button>
</div>
</div>
</div>
</td>
</tr>
))}
</td>
{/* Progreso */}
<td className="p-5 hidden md:table-cell">
<div className="flex flex-col gap-1.5 min-w-[120px]">
<div className="flex items-center justify-between">
<span className="text-[9px] font-black text-slate-400 uppercase">Progreso</span>
<span className={`text-[10px] font-black ${progress >= 80 ? "text-emerald-500" : progress >= 40 ? "text-blue-400" : "text-slate-400"}`}>{progress}%</span>
</div>
<div className="w-full h-1.5 bg-slate-100 dark:bg-white/10 rounded-full overflow-hidden">
<div
className={`h-full rounded-full transition-all duration-700 ${progress >= 80 ? "bg-emerald-500" : progress >= 40 ? "bg-blue-500" : "bg-slate-300 dark:bg-white/20"}`}
style={{ width: `${progress}%` }}
/>
</div>
</div>
</td>
{/* Promedio */}
<td className="p-5 text-center hidden lg:table-cell">
{avgPct !== null ? (
<span className={`text-sm font-black ${avgPct >= 70 ? "text-emerald-500" : avgPct >= 50 ? "text-orange-400" : "text-red-500"}`}>
{avgPct}%
</span>
) : (
<span className="text-xs text-slate-300 dark:text-white/20"></span>
)}
</td>
{/* Última actividad */}
<td className="p-5 text-center hidden lg:table-cell">
{daysInactive !== null ? (
<div className={`flex items-center justify-center gap-1 text-xs font-bold ${daysInactive >= 14 ? "text-red-400" : daysInactive >= 7 ? "text-orange-400" : "text-slate-500 dark:text-gray-400"}`}>
<Clock size={12} />
{daysInactive === 0 ? "Hoy" : daysInactive === 1 ? "Ayer" : `Hace ${daysInactive}d`}
</div>
) : (
<span className="text-xs text-slate-300 dark:text-white/20">Sin datos</span>
)}
</td>
{/* Riesgo */}
<td className="p-5 text-center">
<RiskBadge level={risk} />
</td>
{/* Acciones */}
<td className="p-5 text-right">
<div className="flex items-center justify-end gap-2">
<button
onClick={() => openNotify(student)}
title="Enviar notificación"
className="p-2 rounded-xl bg-blue-500/10 hover:bg-blue-600 text-blue-500 hover:text-white transition-all active:scale-95"
>
<Bell size={16} />
</button>
<button
onClick={() => router.push(`/courses/${id}/grades`)}
title="Ver libro de notas"
className="p-2 rounded-xl bg-slate-100 dark:bg-white/5 hover:bg-slate-200 dark:hover:bg-white/10 text-slate-400 hover:text-slate-700 dark:hover:text-white transition-all active:scale-95"
>
<BarChart2 size={16} />
</button>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
{/* Leyenda */}
<div className="flex flex-wrap items-center gap-4 px-1">
<span className="text-[9px] font-black uppercase tracking-widest text-slate-400">Indicador de riesgo:</span>
<span className="flex items-center gap-1.5 text-[10px] text-slate-500"><TrendingDown size={12} className="text-red-500" /> Crítico inactivo 14d o promedio &lt;40%</span>
<span className="flex items-center gap-1.5 text-[10px] text-slate-500"><TrendingDown size={12} className="text-orange-400" /> Alto inactivo 7d o promedio &lt;60%</span>
<span className="flex items-center gap-1.5 text-[10px] text-slate-500"><TrendingDown size={12} className="text-yellow-400" /> Medio inactivo 3d o promedio &lt;70%</span>
<span className="flex items-center gap-1.5 text-[10px] text-slate-500"><CheckCircle2 size={12} className="text-emerald-500" /> Bien</span>
</div>
</div>
</CourseEditorLayout>
{/* Enroll Modal */}
{
isEnrollModalOpen && (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-slate-900/40 dark:bg-black/80 backdrop-blur-md animate-in fade-in duration-300">
<div className="bg-white dark:bg-[#16181b] border border-slate-200 dark:border-white/10 rounded-[2.5rem] w-full max-w-2xl overflow-hidden shadow-2xl scale-in-center">
<div className="p-8 border-b border-slate-100 dark:border-white/5 flex items-center justify-between bg-slate-50/50 dark:bg-transparent">
<div>
<h2 className="text-2xl font-black flex items-center gap-3 text-slate-900 dark:text-white">
<UserPlus className="text-blue-600 dark:text-blue-500" />
Enroll Students
</h2>
<p className="text-xs text-slate-400 dark:text-gray-500 font-bold uppercase tracking-widest mt-1">Select from organization directory</p>
</div>
<button onClick={() => setIsEnrollModalOpen(false)} className="p-3 hover:bg-slate-200 dark:hover:bg-white/10 rounded-2xl transition-all group active:scale-90">
<X size={20} className="text-slate-400 group-hover:text-slate-900 dark:group-hover:text-white" />
</button>
{/* Modal: Inscribir */}
{isEnrollModalOpen && (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm animate-in fade-in duration-200">
<div className="bg-white dark:bg-[#16181b] border border-slate-200 dark:border-white/10 rounded-[2.5rem] w-full max-w-2xl overflow-hidden shadow-2xl">
<div className="p-8 border-b border-slate-100 dark:border-white/5 flex items-center justify-between">
<div>
<h2 className="text-xl font-black flex items-center gap-3 text-slate-900 dark:text-white">
<UserPlus className="text-blue-600" /> Inscribir Estudiantes
</h2>
<p className="text-xs text-slate-400 font-bold uppercase tracking-widest mt-1">Directorio de la organización</p>
</div>
<div className="p-8 space-y-8">
<div className="relative">
<Search className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 dark:text-gray-500 w-5 h-5" />
<input
type="text"
placeholder="Search by name or email..."
value={enrollSearch}
onChange={(e) => setEnrollSearch(e.target.value)}
className="w-full bg-slate-100 dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-[1.5rem] py-4 pl-12 pr-4 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500/50 font-bold text-slate-900 dark:text-white shadow-inner"
/>
</div>
<div className="space-y-3 max-h-[400px] overflow-y-auto pr-2 custom-scrollbar">
{orgUsersLoading ? (
<div className="flex justify-center p-12 text-blue-500 items-center flex-col gap-4">
<Loader2 className="w-10 h-10 animate-spin" />
<span className="text-xs font-black uppercase tracking-widest text-slate-400">Fetching Directory...</span>
</div>
) : allOrgUsers.filter(u => u.full_name.toLowerCase().includes(enrollSearch.toLowerCase())).length === 0 ? (
<div className="text-center p-12 text-slate-400 dark:text-gray-500 italic font-medium">No remaining students found in organization directory.</div>
) : (
allOrgUsers.filter(u => u.full_name.toLowerCase().includes(enrollSearch.toLowerCase())).map(user => (
<div key={user.id} className="flex items-center justify-between p-5 bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-3xl group/user hover:bg-white dark:hover:bg-white/[0.08] transition-all shadow-sm">
<div className="flex items-center gap-4">
<div className="w-12 h-12 rounded-2xl bg-white dark:bg-black/20 border border-slate-100 dark:border-white/10 flex items-center justify-center shadow-sm">
<UserCircle className="text-slate-400 dark:text-gray-500" size={24} />
</div>
<div>
<div className="font-black text-slate-900 dark:text-white text-sm tracking-tight group-hover/user:text-blue-600 dark:group-hover/user:text-blue-400 transition-colors uppercase">{user.full_name}</div>
<div className="text-xs text-slate-400 dark:text-gray-500 font-medium flex items-center gap-1.5 mt-0.5 italic"><Mail size={10} className="text-blue-400" /> {user.email}</div>
</div>
</div>
<button
onClick={() => handleEnroll([user.email])}
className="px-6 py-2 bg-blue-600 hover:bg-blue-500 text-white text-xs font-black uppercase tracking-widest rounded-xl transition-all shadow-md shadow-blue-500/20 active:scale-95"
>
Enroll
</button>
<button onClick={() => setIsEnrollModalOpen(false)} className="p-3 hover:bg-slate-100 dark:hover:bg-white/10 rounded-2xl transition-all">
<X size={20} className="text-slate-400" />
</button>
</div>
<div className="p-8 space-y-6">
<div className="relative">
<Search className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 w-4 h-4" />
<input
type="text"
placeholder="Buscar por nombre o email..."
value={enrollSearch}
onChange={e => setEnrollSearch(e.target.value)}
className="w-full bg-slate-50 dark:bg-black/20 border border-slate-200 dark:border-white/10 rounded-2xl py-3 pl-11 pr-4 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500/50 font-bold text-slate-900 dark:text-white"
/>
</div>
<div className="space-y-3 max-h-[360px] overflow-y-auto">
{orgUsersLoading ? (
<div className="flex justify-center p-12"><Loader2 className="w-8 h-8 text-blue-500 animate-spin" /></div>
) : allOrgUsers.filter(u => u.full_name.toLowerCase().includes(enrollSearch.toLowerCase())).length === 0 ? (
<p className="text-center p-10 text-slate-400 italic text-sm">No hay más estudiantes disponibles.</p>
) : allOrgUsers.filter(u => u.full_name.toLowerCase().includes(enrollSearch.toLowerCase())).map(u => (
<div key={u.id} className="flex items-center justify-between p-4 bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-2xl">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-white dark:bg-black/20 border border-slate-100 dark:border-white/10 flex items-center justify-center">
<UserCircle size={22} className="text-slate-400" />
</div>
))
)}
</div>
<div className="bg-blue-50 dark:bg-blue-500/10 border border-blue-200 dark:border-blue-500/20 p-5 rounded-3xl flex gap-4 shadow-sm">
<div className="w-10 h-10 rounded-2xl bg-white dark:bg-black/20 flex items-center justify-center shrink-0 shadow-sm">
<Plus size={20} className="text-blue-600 dark:text-blue-400" />
<div>
<div className="font-black text-slate-900 dark:text-white text-sm">{u.full_name}</div>
<div className="text-xs text-slate-400 flex items-center gap-1"><Mail size={10} /> {u.email}</div>
</div>
</div>
<button
onClick={() => handleEnroll([u.email])}
className="px-5 py-2 bg-blue-600 hover:bg-blue-500 text-white text-xs font-black uppercase tracking-widest rounded-xl transition-all active:scale-95"
>
Inscribir
</button>
</div>
<div className="text-xs text-blue-800/80 dark:text-blue-300 leading-relaxed font-bold uppercase tracking-wide">
You can also enroll external students by going to the <strong>Gradebook</strong> and using the Bulk Enroll feature.
</div>
</div>
))}
</div>
<div className="bg-blue-50 dark:bg-blue-500/10 border border-blue-200 dark:border-blue-500/20 p-4 rounded-2xl flex gap-3 text-xs text-blue-700 dark:text-blue-300 font-medium">
<Plus size={16} className="text-blue-500 shrink-0 mt-0.5" />
También puedes inscribir alumnos externos desde el <strong>Libro de Notas</strong> con la función de inscripción masiva.
</div>
</div>
</div>
)}
</div>
)}
{/* Modal: Notificar alumno */}
{notifyTarget && (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm animate-in fade-in duration-200">
<div className="bg-white dark:bg-[#16181b] border border-slate-200 dark:border-white/10 rounded-[2rem] w-full max-w-lg shadow-2xl">
<div className="p-7 border-b border-slate-100 dark:border-white/5 flex items-center justify-between">
<div>
<h2 className="text-lg font-black flex items-center gap-2 text-slate-900 dark:text-white">
<Bell size={18} className="text-blue-500" /> Notificar Alumno
</h2>
<p className="text-xs text-slate-400 mt-0.5">{notifyTarget.full_name} · {notifyTarget.email}</p>
</div>
<button onClick={() => setNotifyTarget(null)} className="p-2 hover:bg-slate-100 dark:hover:bg-white/10 rounded-xl transition-all">
<X size={18} className="text-slate-400" />
</button>
</div>
<div className="p-7 space-y-5">
{notifySuccess ? (
<div className="flex flex-col items-center gap-3 py-8">
<CheckCircle2 size={40} className="text-emerald-500" />
<p className="font-black text-emerald-500">Notificación enviada</p>
</div>
) : (
<>
<div>
<label className="text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 block">Título</label>
<input
type="text"
value={notifyTitle}
onChange={e => setNotifyTitle(e.target.value)}
placeholder="Ej: Recordatorio de actividad pendiente"
className="w-full bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-xl px-4 py-3 text-sm font-bold text-slate-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500/50"
/>
</div>
<div>
<label className="text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 block">Mensaje</label>
<textarea
value={notifyMessage}
onChange={e => setNotifyMessage(e.target.value)}
placeholder="Escribe el mensaje para el alumno..."
rows={4}
className="w-full bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-xl px-4 py-3 text-sm font-medium text-slate-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500/50 resize-none"
/>
</div>
<button
onClick={handleNotify}
disabled={notifySending || !notifyTitle.trim() || !notifyMessage.trim()}
className="w-full py-3 bg-blue-600 hover:bg-blue-500 disabled:opacity-50 disabled:cursor-not-allowed text-white font-black text-sm uppercase tracking-widest rounded-xl transition-all active:scale-95 flex items-center justify-center gap-2"
>
{notifySending ? <Loader2 size={16} className="animate-spin" /> : <Bell size={16} />}
{notifySending ? "Enviando..." : "Enviar Notificación"}
</button>
</>
)}
</div>
</div>
</div>
)}
</>
);
}
+12 -17
View File
@@ -86,15 +86,15 @@ export default function StudioDashboard() {
e.preventDefault();
e.stopPropagation();
try {
const data = await cmsApi.exportCourse(courseId);
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const blob = await cmsApi.exportCourse(courseId);
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `course_${title.replace(/\s+/g, '_')}.json`;
link.download = `course_${title.replace(/\s+/g, '_')}.ccb`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} catch (err) {
console.error("Export failed", err);
alert("Failed to export course");
@@ -105,19 +105,14 @@ export default function StudioDashboard() {
const file = e.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = async (event) => {
try {
const json = JSON.parse(event.target?.result as string);
const newCourse = await cmsApi.importCourse(json);
setCourses((prev: Course[]) => [...prev, newCourse]);
alert("Course imported successfully!");
} catch (err) {
console.error("Import failed", err);
alert("Failed to import course. Ensure the file is a valid OpenCCB course export.");
}
};
reader.readAsText(file);
try {
const newCourse = await cmsApi.importCourse(file);
setCourses((prev: Course[]) => [...prev, newCourse]);
alert("Course imported successfully!");
} catch (err) {
console.error("Import failed", err);
alert("Failed to import course. Ensure the file is a valid .ccb export.");
}
};
const handleDeleteCourse = async (e: React.MouseEvent, courseId: string, title: string) => {
@@ -146,7 +141,7 @@ export default function StudioDashboard() {
<label className="flex items-center gap-2 px-5 py-2.5 bg-slate-50 dark:bg-white/5 hover:bg-slate-100 dark:hover:bg-white/10 border border-slate-200 dark:border-white/10 rounded-xl font-bold text-sm transition-all cursor-pointer active:scale-95 text-slate-900 dark:text-white">
<Upload size={16} />
Importar
<input type="file" accept=".json" onChange={handleImport} className="hidden" />
<input type="file" accept=".ccb,.zip" onChange={handleImport} className="hidden" />
</label>
<button
onClick={() => setIsAIModalOpen(true)}
@@ -24,6 +24,7 @@ type TabKey =
| "team"
| "peer-reviews"
| "students"
| "mentorships"
| "sessions"
| "pedagogical"
| "lti-tools"
@@ -101,6 +102,7 @@ export default function CourseEditorLayout({
tabs: [
{ key: "team", label: "Equipo", icon: Users, href: `/courses/${id}/team` },
{ key: "students", label: "Estudiantes y Grupos", icon: GraduationCap, href: `/courses/${id}/students` },
{ key: "mentorships", label: "Mentoría", icon: Award, href: `/courses/${id}/mentorships` },
],
},
{
+94 -5
View File
@@ -903,6 +903,9 @@ export interface CourseSubmission {
lesson_id: string;
content: string;
submitted_at: string;
final_score?: number | null;
review_count: number;
status: 'pending' | 'under_review' | 'graded';
}
export interface PeerReview {
@@ -911,9 +914,26 @@ export interface PeerReview {
reviewer_id: string;
score: number;
feedback: string;
is_instructor_review: boolean;
created_at: string;
}
export interface PeerReviewSettings {
id: string;
lesson_id: string;
required_reviews: number;
peer_weight: number;
instructor_weight: number;
rubric_id?: string | null;
auto_assign: boolean;
created_at: string;
updated_at: string;
}
export interface PeerReviewWithFlag extends PeerReview {
is_instructor_review: boolean;
}
export interface CourseInstructor {
id: string;
course_id: string;
@@ -1116,11 +1136,39 @@ export const cmsApi = {
return apiFetch(`/courses/${id}/analytics/advanced${query}`, {}, true);
},
getLessonHeatmap: (lessonId: string): Promise<{ second: number, count: number }[]> => apiFetch(`/lessons/${lessonId}/heatmap`),
exportCourse: (id: string): Promise<Record<string, unknown>> => apiFetch(`/courses/${id}/export`),
importCourse: (data: Record<string, unknown>): Promise<Course> => apiFetch(`/courses/import`, {
method: 'POST',
body: JSON.stringify(data)
}),
exportCourse: (id: string): Promise<Blob> => {
const token = getToken();
const orgId = getSelectedOrgId();
return fetch(`${API_BASE_URL}/courses/${id}/export`, {
headers: {
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
...(orgId ? { 'X-Organization-Id': orgId } : {}),
},
}).then(res => {
if (!res.ok) return Promise.reject(new Error('Export failed'));
return res.blob();
});
},
importCourse: (file: File): Promise<Course> => {
const token = getToken();
const orgId = getSelectedOrgId();
const formData = new FormData();
formData.append('file', file);
return fetch(`${API_BASE_URL}/courses/import`, {
method: 'POST',
body: formData,
headers: {
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
...(orgId ? { 'X-Organization-Id': orgId } : {}),
},
}).then(async res => {
if (!res.ok) {
const text = await res.text();
return Promise.reject(new Error(text || 'Import failed'));
}
return res.json();
});
},
// Course Templates
listCourseTemplates: (): Promise<CourseTemplateSummary[]> => apiFetch('/course-templates'),
@@ -1668,6 +1716,11 @@ export const lmsApi = {
},
bulkEnroll: (courseId: string, emails: string[]): Promise<BulkEnrollResponse> =>
apiFetch('/bulk-enroll', { method: 'POST', body: JSON.stringify({ course_id: courseId, emails }) }, true),
notifyStudent: (courseId: string, studentId: string, title: string, message: string, linkUrl?: string): Promise<void> =>
apiFetch(`/courses/${courseId}/students/${studentId}/notify`, {
method: 'POST',
body: JSON.stringify({ title, message, link_url: linkUrl ?? null }),
}, true),
// Peer Assessment
submitAssignment: (courseId: string, lessonId: string, content: string): Promise<CourseSubmission> =>
apiFetch(`/courses/${courseId}/lessons/${lessonId}/submit`, { method: 'POST', body: JSON.stringify({ content }) }, true),
@@ -1681,6 +1734,15 @@ export const lmsApi = {
apiFetch(`/courses/${courseId}/lessons/${lessonId}/submissions`, {}, true),
getSubmissionReviews: (submissionId: string): Promise<PeerReview[]> =>
apiFetch(`/peer-reviews/submissions/${submissionId}/reviews`, {}, true),
// 41-F: Peer Review Mejorado
getPeerReviewSettings: (courseId: string, lessonId: string): Promise<PeerReviewSettings | null> =>
apiFetch(`/courses/${courseId}/lessons/${lessonId}/peer-settings`, {}, true),
upsertPeerReviewSettings: (courseId: string, lessonId: string, payload: Partial<PeerReviewSettings>): Promise<PeerReviewSettings> =>
apiFetch(`/courses/${courseId}/lessons/${lessonId}/peer-settings`, { method: 'POST', body: JSON.stringify(payload) }, true),
autoAssignPeerReviews: (courseId: string, lessonId: string): Promise<{ submissions_processed: number; assignments_created: number }> =>
apiFetch(`/courses/${courseId}/lessons/${lessonId}/auto-assign-reviews`, { method: 'POST' }, true),
instructorGradeSubmission: (courseId: string, lessonId: string, submissionId: string, score: number, feedback: string): Promise<PeerReviewWithFlag> =>
apiFetch(`/courses/${courseId}/lessons/${lessonId}/instructor-grade`, { method: 'POST', body: JSON.stringify({ submission_id: submissionId, score, feedback }) }, true),
// Announcements
listAnnouncements: (courseId: string): Promise<AnnouncementWithAuthor[]> =>
@@ -1857,6 +1919,17 @@ export const lmsApi = {
apiFetch(`/courses/${courseId}/study-rooms/${roomId}/recordings`, {}, true),
updateLessonCollaborativeDoc: (lessonId: string, payload: UpdateCollaborativeDocPayload): Promise<UpdateCollaborativeDocResponse> =>
apiFetch(`/lessons/${lessonId}/collaborative-doc`, { method: 'PUT', body: JSON.stringify(payload) }, true),
// Fase 41-C: Mentoría
listCourseMentorships: (courseId: string): Promise<StudioMentorshipView[]> =>
apiFetch(`/courses/${courseId}/mentorships`, {}, true),
assignMentor: (courseId: string, mentorId: string, studentId: string, notes?: string): Promise<StudioMentorshipView> =>
apiFetch(`/courses/${courseId}/mentorships`, {
method: 'POST',
body: JSON.stringify({ mentor_id: mentorId, student_id: studentId, notes: notes ?? null }),
}, true),
deleteMentorship: (courseId: string, mentorshipId: string): Promise<void> =>
apiFetch(`/courses/${courseId}/mentorships/${mentorshipId}`, { method: 'DELETE' }, true),
};
export interface StudyRoom {
@@ -2393,3 +2466,19 @@ export async function updateLessonCollaborativeDoc(
body: JSON.stringify(payload),
}, true);
}
// Fase 41-C: Mentoría
export interface StudioMentorshipView {
id: string;
course_id: string;
notes: string | null;
created_at: string;
mentor_id: string;
mentor_name: string;
mentor_email: string;
mentor_avatar: string | null;
student_id: string;
student_name: string;
student_email: string;
student_avatar: string | null;
}