UNPKG

@defikitdotnet/education-module-ai

Version:
295 lines (294 loc) 16.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnrollmentService = void 0; /** * Service for managing student enrollments and progress tracking */ var EnrollmentService = /** @class */ (function () { function EnrollmentService(enrollmentRepository, progressRepository, dbAdapter) { this.enrollmentRepository = enrollmentRepository; this.progressRepository = progressRepository; this.dbAdapter = dbAdapter; } /** * Enroll a student in a course * @param studentId The ID of the student * @param courseId The ID of the course * @returns The created enrollment or existing enrollment */ EnrollmentService.prototype.enrollStudent = function (studentId, courseId) { return __awaiter(this, void 0, void 0, function () { var existingEnrollment, enrollment; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.enrollmentRepository.findByStudentAndCourse(studentId, courseId)]; case 1: existingEnrollment = _a.sent(); if (existingEnrollment) { return [2 /*return*/, existingEnrollment]; } return [4 /*yield*/, this.enrollmentRepository.create({ studentId: studentId, courseId: courseId, progress: 0, completed: false, enrolledAt: new Date(), updatedAt: new Date(), })]; case 2: enrollment = _a.sent(); return [2 /*return*/, enrollment]; } }); }); }; /** * Get all enrollments for a student * @param studentId The ID of the student * @returns Array of enrollments */ EnrollmentService.prototype.getStudentEnrollments = function (studentId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.enrollmentRepository.findByStudentId(studentId)]; }); }); }; /** * Get enrollment for a specific course * @param studentId The ID of the student * @param courseId The ID of the course * @returns The enrollment or null */ EnrollmentService.prototype.getStudentCourseEnrollment = function (studentId, courseId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.enrollmentRepository.findByStudentAndCourse(studentId, courseId)]; }); }); }; /** * Mark a lecture as completed and update overall course progress * @param studentId The ID of the student * @param lectureId The ID of the lecture * @param courseId The ID of the course * @returns Object with progress information */ EnrollmentService.prototype.markLectureCompleted = function (studentId, lectureId, courseId) { return __awaiter(this, void 0, void 0, function () { var success, completedLectures, db, countQuery, total_lectures, progress; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.progressRepository.markLectureCompleted(studentId, lectureId)]; case 1: success = _a.sent(); if (!success) { throw new Error("Failed to mark lecture as completed"); } return [4 /*yield*/, this.progressRepository.findCompletedLecturesForStudentInCourse(studentId, courseId)]; case 2: completedLectures = _a.sent(); db = this.dbAdapter.getDb(); countQuery = db.prepare("\n SELECT COUNT(*) as total_lectures\n FROM lectures\n JOIN sections ON lectures.section_id = sections.id\n WHERE sections.course_id = ?\n "); total_lectures = countQuery.get(courseId).total_lectures; progress = total_lectures > 0 ? Math.round((completedLectures.length / total_lectures) * 100) : 0; // Update enrollment progress return [4 /*yield*/, this.enrollmentRepository.updateProgress(studentId, courseId, progress)]; case 3: // Update enrollment progress _a.sent(); return [2 /*return*/, { success: true, completedLectures: completedLectures.length, totalLectures: total_lectures, progress: progress, }]; } }); }); }; /** * Get progress records for a student in a course * @param studentId The ID of the student * @param courseId The ID of the course * @returns Array of progress records */ EnrollmentService.prototype.getStudentCourseProgress = function (studentId, courseId) { return __awaiter(this, void 0, void 0, function () { var db, lecturesQuery, lectureRows, lectureIds, progressRecords, _i, lectureIds_1, lectureId, progress; return __generator(this, function (_a) { switch (_a.label) { case 0: db = this.dbAdapter.getDb(); lecturesQuery = db.prepare("\n SELECT lectures.id\n FROM lectures\n JOIN sections ON lectures.section_id = sections.id\n WHERE sections.course_id = ?\n "); lectureRows = lecturesQuery.all(courseId); lectureIds = lectureRows.map(function (row) { return row.id; }); progressRecords = []; _i = 0, lectureIds_1 = lectureIds; _a.label = 1; case 1: if (!(_i < lectureIds_1.length)) return [3 /*break*/, 4]; lectureId = lectureIds_1[_i]; return [4 /*yield*/, this.progressRepository.findByStudentAndLecture(studentId, lectureId)]; case 2: progress = _a.sent(); if (progress) { progressRecords.push(progress); } _a.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/, progressRecords]; } }); }); }; /** * Get enrollment details and completed lecture IDs for a student in a specific course. * Throws ApiError if enrollment is not found. * @param studentId The ID of the student * @param courseId The ID of the course * @returns Object containing the enrollment and an array of completed lecture IDs */ EnrollmentService.prototype.getEnrollmentAndProgressForStudent = function (studentId, courseId) { return __awaiter(this, void 0, void 0, function () { var enrollment, completedProgressRecords, completedLectures; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.enrollmentRepository.findByStudentAndCourse(studentId, courseId)]; case 1: enrollment = _a.sent(); if (!enrollment) { // Throw an error that the controller can catch and translate to 404 throw new Error("Enrollment not found for student ".concat(studentId, " in course ").concat(courseId)); } return [4 /*yield*/, this.progressRepository.findCompletedLecturesForStudentInCourse(studentId, courseId)]; case 2: completedProgressRecords = _a.sent(); completedLectures = completedProgressRecords; // 3. Return the combined data return [2 /*return*/, { enrollment: enrollment, completedLectures: completedLectures, }]; } }); }); }; /** * Get completed quizzes for a student in a course * @param studentId The ID of the student * @param courseId The ID of the course * @returns Array of quiz IDs that have been completed */ EnrollmentService.prototype.getCompletedQuizzesForStudent = function (studentId, courseId) { return __awaiter(this, void 0, void 0, function () { var db, tableExists, stmt, rows; return __generator(this, function (_a) { db = this.dbAdapter.getDb(); tableExists = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='quiz_completions'").get(); if (!tableExists) { console.log("Creating quiz_completions table..."); db.prepare("\n CREATE TABLE quiz_completions (\n id TEXT PRIMARY KEY,\n student_id TEXT NOT NULL,\n quiz_id TEXT NOT NULL,\n course_id TEXT NOT NULL,\n score INTEGER,\n completed_at TEXT NOT NULL,\n UNIQUE(student_id, quiz_id)\n )\n ").run(); } stmt = db.prepare("\n SELECT quiz_id FROM quiz_completions \n WHERE student_id = ? AND course_id = ?\n "); rows = stmt.all(studentId, courseId); return [2 /*return*/, rows.map(function (row) { return row.quiz_id; })]; }); }); }; /** * Record a quiz as completed and update overall course progress * @param studentId The ID of the student * @param quizId The ID of the quiz * @param courseId The ID of the course * @param score Optional score from the quiz * @returns Object with progress information */ EnrollmentService.prototype.recordQuizCompleted = function (studentId, quizId, courseId, score) { return __awaiter(this, void 0, void 0, function () { var db, tableExists, insertStmt, uuid, completedLectures, completedQuizzes, countLecturesQuery, countQuizzesQuery, total_lectures, total_quizzes, totalItems, completedItems, progress, isCompleted; return __generator(this, function (_a) { switch (_a.label) { case 0: db = this.dbAdapter.getDb(); tableExists = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='quiz_completions'").get(); if (!tableExists) { console.log("Creating quiz_completions table..."); db.prepare("\n CREATE TABLE quiz_completions (\n id TEXT PRIMARY KEY,\n student_id TEXT NOT NULL,\n quiz_id TEXT NOT NULL,\n course_id TEXT NOT NULL,\n score INTEGER,\n completed_at TEXT NOT NULL,\n UNIQUE(student_id, quiz_id)\n )\n ").run(); } insertStmt = db.prepare("\n INSERT OR REPLACE INTO quiz_completions \n (id, student_id, quiz_id, course_id, score, completed_at)\n VALUES (?, ?, ?, ?, ?, ?)\n "); uuid = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); insertStmt.run(uuid, studentId, quizId, courseId, score || 0, new Date().toISOString()); return [4 /*yield*/, this.progressRepository.findCompletedLecturesForStudentInCourse(studentId, courseId)]; case 1: completedLectures = _a.sent(); return [4 /*yield*/, this.getCompletedQuizzesForStudent(studentId, courseId)]; case 2: completedQuizzes = _a.sent(); countLecturesQuery = db.prepare("\n SELECT COUNT(*) as total_lectures\n FROM lectures\n JOIN sections ON lectures.section_id = sections.id\n WHERE sections.course_id = ?\n "); countQuizzesQuery = db.prepare("\n SELECT COUNT(*) as total_quizzes\n FROM quizzes\n JOIN sections ON quizzes.section_id = sections.id\n WHERE sections.course_id = ?\n "); total_lectures = countLecturesQuery.get(courseId).total_lectures; total_quizzes = countQuizzesQuery.get(courseId).total_quizzes; totalItems = total_lectures + total_quizzes; completedItems = completedLectures.length + completedQuizzes.length; progress = totalItems > 0 ? Math.round((completedItems / totalItems) * 100) : 0; isCompleted = progress >= 100; // Update enrollment progress return [4 /*yield*/, this.enrollmentRepository.updateProgress(studentId, courseId, progress)]; case 3: // Update enrollment progress _a.sent(); return [2 /*return*/, { success: true, progress: progress, isCompleted: isCompleted }]; } }); }); }; return EnrollmentService; }()); exports.EnrollmentService = EnrollmentService;