# moodlehq/moodleapp architecture

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

- Interactive diagram: https://gitdiagram.com/moodlehq/moodleapp
- Repository: https://github.com/moodlehq/moodleapp

## Overview

##### Project type and purpose
- This is a hybrid mobile client (“Moodle App”) built with Ionic/Angular and Cordova.
- It provides a full-featured Moodle experience on mobile: UI (pages, components), offline‐first data caching, sync, plugin support, theming, testing.
- It connects to a remote Moodle server via REST web services, uses a local SQLite database for offline storage, and integrates native capabilities (camera, storage, push notifications) via Cordova plugins.

##### High‐level analysis of the file structure
- Top‐level config/deployment: Dockerfile, GitHub workflows, Travis, nginx.conf, gulp scripts, Cordova config.xml.
- Native plugin wrapper: cordova-plugin-moodleapp (in /cordova-plugin-moodleapp) providing low-level secure storage, diagnostics, referrer.
- Build and test tooling: gulp/ task scripts, Jest, Behat plugin, local_moodleappbehat tests.
- Source code under src/:
– /app: root Angular module, routing, app component.
– /core: shared services, classes, delegates, database proxies, native-to-Angular adapters, interceptors.
– /addons: feature modules (forums, quizzes, calendar, messages, etc.), each structured as Angular modules with pages, components, services, handlers and offline/db subfolders.
– /theme: SCSS and CSS assets for Ionic/Moodle theming.
– /assets, /types, polyfills, main.ts, index.html: Angular/Ionic scaffolding for PWA and Cordova build.
- Configuration: angular.json, tsconfig*, webpack.config.js, Cordova plugins, GoogleService-Info.plist & google-services.json for Android/iOS.

##### Key architectural patterns
- Modular Angular architecture: core module vs feature (addon) modules, lazy loading for features.
- Delegate/handler pattern: services delegate between feature modules and core (e.g., plugin-file-delegate, sync-cron handlers).
- Offline-first + sync: local SQLite DB proxies in core/classes/database, offline and sync services co-existing (-offline.ts, ‑sync.ts).
- Cordova native abstraction: Cordova plugin directory plus core/native services under core/features/native and core/services.
- Theming + PWA: theme SCSS overrides, service worker (if enabled by Ionic PWA support).

##### Main components
A. Mobile UI Layer
– Ionic/Angular Components & Pages (src/app + src/addons/*/pages + src/core/components)
– Angular Routing & Lazy Loading
B. Feature Modules (src/addons)
– Each major Moodle feature (courses, quiz, forum, calendar, messages, user profile, etc.) as its own Angular module with pages, components, services, handlers.
C. Core Library (src/core)
– Shared classes (cancellable-promise, caching, delegates, database-table proxies)
– Shared components (attachments, loading, navigation, context-menu)
– Shared services (app, config, network, sync manager, plugin-file delegate, analytics)
D. Data Layer
– Local SQLite / IndexedDB (via core/classes/database/) for caching web service data and user‐generated content.
– Offline services (.offline.ts) and Sync services (.sync.ts) within each addon.
E. Native Bridge / Plugins
– Cordova plugin wrapper (/cordova-plugin-moodleapp) for SecureStorage, Diagnostic, InstallReferrer
– Core feature “native” services (camera, file, geolocation, in-app browser, notifications)
F. Network/Web Service Layer
– Moodle REST APIs (SiteService, CoreService, Plugin-specific endpoints)
– HTTP interceptor and error handling (core/classes/interceptor, core/services/ws.ts)
G. External Services
– Google Firebase / Apple Push (google-services.json, GoogleService-Info.plist)
– Analytics, BrowserStack testing, CI/CD (GitHub Actions, Travis CI)
H. Build & Deployment
– Gulp/Node scripts for translation, patching, assets, Cordova hooks (scripts/cordova-hooks)
– Ionic/Cordova build chain via npm, webpack, angular.json

##### Relationships and data flows
- UI Components → Feature Services → Core Services → Network layer / Local DB
- Feature Services coordinate .offline.ts (write/read local DB) and .sync.ts (background or user-triggered sync to server)
- Core Services manage app lifecycle, site auth, network status, sync events, plugin-file delegation
- Cordova plugins invoked by core/native services (e.g., SecureStorage) and feature services when necessary
- External push/analytics events routed back into core services, then to feature modules as handlers

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "Presentation Layer (UI)" 
        direction TB
        UI[/"Ionic/Angular UI\n(src/app & core/components)"/]:::ui
        click UI "https://github.com/moodlehq/moodleapp/tree/main/src/app"
        click UI "https://github.com/moodlehq/moodleapp/tree/main/src/core/components"
    end

    %% Feature Modules
    subgraph "Feature Modules\n(src/addons)" 
        direction TB
        Forum["Forum Module"]:::feature
        Quiz["Quiz Module"]:::feature
        Calendar["Calendar Module"]:::feature
        Messages["Messages Module"]:::feature
        click Forum "https://github.com/moodlehq/moodleapp/tree/main/src/addons/forum"
        click Quiz "https://github.com/moodlehq/moodleapp/tree/main/src/addons/quiz"
        click Calendar "https://github.com/moodlehq/moodleapp/tree/main/src/addons/calendar"
        click Messages "https://github.com/moodlehq/moodleapp/tree/main/src/addons/messages"
    end

    %% Core Library
    subgraph "Core Library\n(src/core)" 
        direction TB
        CoreServices["Shared Services\n(app, config, network)"]:::core
        CoreComponents["Shared Components\n(attachments, loading)"]:::core
        CoreClasses["Shared Classes\n(database proxies, delegates)"]:::core
        click CoreServices "https://github.com/moodlehq/moodleapp/tree/main/src/core"
        click CoreComponents "https://github.com/moodlehq/moodleapp/tree/main/src/core"
        click CoreClasses "https://github.com/moodlehq/moodleapp/tree/main/src/core"
    end

    %% Data Layer
    subgraph "Data Layer\n(Local DB & Offline/Sync)" 
        direction TB
        LocalDB["Local SQLite DB\n(sqlitedb.ts, database-table-proxy.ts)"]:::data
        click LocalDB "https://github.com/moodlehq/moodleapp/blob/main/src/core/classes/database/sqlitedb.ts"
        click LocalDB "https://github.com/moodlehq/moodleapp/blob/main/src/core/classes/database/database-table-proxy.ts"
        OfflineSync["Offline & Sync Services\n(addon .offline.ts/.sync.ts)"]:::data
        click OfflineSync "https://github.com/moodlehq/moodleapp/blob/main/src/addons/blog/services/blog-offline.ts"
        click OfflineSync "https://github.com/moodlehq/moodleapp/blob/main/src/addons/blog/services/blog-sync.ts"
    end

    %% Native Bridge / Plugins
    subgraph "Native Bridge\n(Cordova Plugins)" 
        direction TB
        CordovaPlugin["cordova-plugin-moodleapp"]:::native
        CoreNative["Core Native Abstraction\n(src/core/features/native)"]:::native
        click CordovaPlugin "https://github.com/moodlehq/moodleapp/tree/main/src/cordova-plugin-moodleapp"
        click CoreNative "https://github.com/moodlehq/moodleapp/tree/main/src/core/features/native"
    end

    %% Network / Web Service Layer
    subgraph "Network Layer\n(REST & Interceptor)" 
        direction TB
        WS["Web Service Client\n(ws.ts)"]:::network
        Interceptor["HTTP Interceptor\n(interceptor.ts)"]:::network
        click WS "https://github.com/moodlehq/moodleapp/blob/main/src/core/services/ws.ts"
        click Interceptor "https://github.com/moodlehq/moodleapp/blob/main/src/core/classes/interceptor.ts"
    end

    %% External Services
    subgraph "External Services & Config" 
        direction TB
        MoodleServer[(Moodle REST API)]:::external
        Firebase[(Firebase/Push Config)]:::external
        click Firebase "https://github.com/moodlehq/moodleapp/blob/main/GoogleService-Info.plist"
        click Firebase "https://github.com/moodlehq/moodleapp/blob/main/google-services.json"
    end

    %% Build & Deployment
    subgraph "Build & Deployment" 
        direction TB
        CI["CI/CD & Testing\n(.github, Travis, BrowserStack)"]:::build
        Scripts["Gulp & Cordova Hooks\n(gulp/, scripts/cordova-hooks)"]:::build
        Configs["Angular/Cordova Config\n(angular.json, config.xml)"]:::build
        click CI "https://github.com/moodlehq/moodleapp/tree/main/.github/workflows/"
        click Scripts "https://github.com/moodlehq/moodleapp/tree/main/scripts/cordova-hooks"
        click Configs "https://github.com/moodlehq/moodleapp/blob/main/angular.json"
        click Configs "https://github.com/moodlehq/moodleapp/blob/main/config.xml"
    end

    %% Connections
    UI -->|uses| Forum
    UI -->|uses| Quiz
    UI -->|uses| Calendar
    UI -->|uses| Messages

    Forum -->|calls| CoreServices
    Quiz -->|calls| CoreServices
    Calendar -->|calls| CoreServices
    Messages -->|calls| CoreServices

    CoreServices -->|reads/writes| LocalDB
    OfflineSync -->|syncs| LocalDB
    CoreServices -->|invokes| OfflineSync
    CoreServices -->|invokes| CordovaPlugin

    CordovaPlugin -->|exposes| CoreNative
    CoreNative -->|used by| CoreServices

    CoreServices -->|auth & data via| WS
    Interceptor -->|intercepts| WS
    WS -->|calls| MoodleServer

    WS -- Push events --> Firebase
    Firebase -- notifications --> CoreServices

    CI -->|triggers build| Scripts
    Scripts -->|configures| Configs

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#039BE5,stroke-width:2px
    classDef feature fill:#DFFFD6,stroke:#388E3C,stroke-width:2px
    classDef core fill:#E0F7FA,stroke:#00838F,stroke-width:2px
    classDef data fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px
    classDef native fill:#EFEBE9,stroke:#5D4037,stroke-width:2px
    classDef network fill:#F5F5F5,stroke:#9E9E9E,stroke-width:2px
    classDef external fill:#ECEFF1,stroke:#607D8B,stroke-width:2px
    classDef build fill:#F3E5F5,stroke:#8E24AA,stroke-width:2px
```
