# hoangpd2001/tnol-php architecture

> Architecture diagram of the GitHub repository hoangpd2001/tnol-php, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hoangpd2001/tnol-php
- Repository: https://github.com/hoangpd2001/tnol-php

## Overview

##### Project Type and Purpose
- This is a monolithic full-stack web application built in PHP.
- It implements an online examination system supporting user authentication (token, Google OAuth, OTP), role-based access control, question/exam management, test-taking with anti-cheat features, reporting, and import/export utilities.

##### High-Level File Structure (inferred)
├─ config.php                    # database credentials, site config
├─ public/                       # public-facing assets
│   ├─ index.php                 # routing entry point
│   ├─ css/, js/, img/           # static resources
├─ src/                          # application source
│   ├─ controllers/              # request handlers (AuthController, ExamController…)
│   ├─ models/                   # database entities (User, Question, Exam…)
│   ├─ views/                    # PHP/HTML templates
│   ├─ services/                 # business logic (OTPService, ImportService…)
│   └─ lib/                      # third-party libs (Google OAuth, PHPWord)
├─ tests/                        # unit/integration tests
├─ vendor/                       # Composer dependencies
└─ README.md                     # project overview

a. Presentation Layer (Frontend)
- User’s Browser (landing pages, login, exam UI)
- Static assets (HTML/CSS/JS, AJAX calls)
b. Application Layer (Backend)
- Web Server + PHP Runtime (Apache/Nginx + PHP-FPM)
- MVC Components:
- Controllers (Auth, Exam, Question, Report, Import)
- Services (TokenManager, OTPManager, FileParser)
- Models (ORM or raw DB access)
c. Data Layer
- Relational Database (MySQL/MariaDB)
- File storage for imported Word docs (or BLOB)
d. External Services
- Google OAuth 2.0 API
- SMTP/Email Provider (OTP delivery)

##### Interactions and Data Flow
- User → Browser UI → AJAX/HTTP → Controllers
- Controllers call Services → Services read/write Models → Database
- AuthController → TokenManager for JWT storage in cookies/localStorage
- OTPController → OTPService → SMTP
- ImportController → PHPWord (in lib/) → Service parses Word → persists Questions
- ExamController monitors browser tab events via JS → AJAX logs to backend
- ReportingController queries DB → formats CSV/PDF for export

##### Architectural Patterns
- MVC: clear separation between controllers, views, models
- Service-oriented components for cross-cutting concerns (Auth, OTP, Import)
- Modularization by domain (Question module, Exam module, User module)
- Asynchronous interactions via AJAX for search/pagination and cheat detection

##### Final Advice
- Be exhaustive: break the monolith into functional slices (Auth, UserProfile, QuestionBank, ExamEngine, Reporting).
- With key technologies/frameworks (PHP 7.x/8.x, MySQL, AJAX, PHPWord, Google API client).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (User Interface)" 
        style FrontendLayer fill:#D0E8FF,stroke:#000
        Browser["User's Browser"]:::frontend
        StaticAssets["Static Assets (HTML/CSS/JS)"]:::frontend
        CheatJS["Cheat Detection JS"]:::frontend
    end

    Browser -->|"renders UI / AJAX"| StaticAssets
    Browser -->|"logs tab switches"| CheatJS
    CheatJS -->|"POST /exam/logcheat"| ExamController

    %% Backend Application Layer
    subgraph "Backend Application" 
        style BackendLayer fill:#E0FFE0,stroke:#000
        subgraph "Controllers" 
            AuthController["AuthController"]:::backend
            ExamController["ExamController"]:::backend
            QuestionController["QuestionController"]:::backend
            ReportController["ReportingController"]:::backend
            ImportController["ImportController"]:::backend
            ManualExamController["ManualExamController"]:::backend
        end
        subgraph "Services"
            TokenManager["TokenManager Service"]:::backend
            OTPService["OTPService"]:::backend
            FileParser["ImportService / PHPWord"]:::backend
            AutoExamService["AutoExamService"]:::backend
        end
        subgraph "Models"
            UserModel["User Model"]:::backend
            QuestionModel["Question Model"]:::backend
            ExamModel["Exam Model"]:::backend
            ReportModel["Report Model"]:::backend
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        DB["MySQL/MariaDB Database"]:::database
        FileStorage["File Storage (Word Docs)"]:::database
    end

    %% External Services
    subgraph "External Services"
        GoogleOAuth["Google OAuth 2.0 API"]:::external
        SMTP["SMTP / Email Provider"]:::external
    end

    %% Interactions
    StaticAssets -->|"POST /login"| AuthController
    StaticAssets -->|"GET /questions"| QuestionController
    StaticAssets -->|"POST /startExam"| ExamController
    StaticAssets -->|"GET /report"| ReportController
    StaticAssets -->|"POST /import"| ImportController

    AuthController -->|"uses"| TokenManager
    AuthController -->|"redirects OAuth"| GoogleOAuth
    AuthController -->|"returns JWT"| Browser

    OTPService -->|"sends OTP email"| SMTP
    AuthController -->|"requests OTP"| OTPService

    ImportController -->|"parses Word"| FileParser
    FileParser -->|"persists"| QuestionModel

    ExamController -->|"calls"| AutoExamService
    ManualExamController -->|"calls"| ExamModel

    %% Service to Model interactions
    TokenManager --> UserModel
    OTPService --> UserModel
    AutoExamService --> ExamModel
    FileParser --> QuestionModel
    ReportController --> ReportModel

    %% Model to DB interactions
    UserModel -->|"SELECT/INSERT"| DB
    QuestionModel -->|"SELECT/INSERT"| DB
    ExamModel -->|"SELECT/INSERT"| DB
    ReportModel -->|"SELECT"| DB
    FileStorage -->|"stores docs"| FileStorage

    %% External Data Storage
    FileParser --> FileStorage

    %% Click Events
    click README "https://github.com/hoangpd2001/tnol-php/blob/main/README.md"

    %% Styles
    classDef frontend fill:#B3D4FC,stroke:#0366D6,color:#000
    classDef backend fill:#C3E6CB,stroke:#28A745,color:#000
    classDef database fill:#FFD8A8,stroke:#E69500,color:#000
    classDef external fill:#E2D0F7,stroke:#6F42C1,color:#000
```
