|
| 1 | +# Moodle Mobile App - AI Agent Guidelines |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +This is the official Moodle Mobile App - an Ionic/Angular hybrid application that runs on Android and iOS via Cordova. The app provides offline-first access to Moodle LMS functionality. |
| 5 | + |
| 6 | +## Architecture Patterns |
| 7 | + |
| 8 | +### Singleton Services via `makeSingleton` |
| 9 | +Services are NOT Angular-managed singletons. Use the `makeSingleton` pattern: |
| 10 | +```typescript |
| 11 | +@Injectable({ providedIn: 'root' }) |
| 12 | +export class MyService { } |
| 13 | +export const My = makeSingleton(MyService); |
| 14 | +``` |
| 15 | +Import from `@singletons` for platform APIs, only import directly from `@angular/core` or Ionic when necessary. |
| 16 | + |
| 17 | +### Delegate Pattern for Extensibility |
| 18 | +The codebase uses delegates extensively to allow addons to register handlers: |
| 19 | +- **CoreDelegate** base class in `src/core/classes/delegate.ts` |
| 20 | +- Handlers register in module providers using `provideAppInitializer`: |
| 21 | +```typescript |
| 22 | +@NgModule({ |
| 23 | + providers: [ |
| 24 | + provideAppInitializer(() => { |
| 25 | + CoreCourseModuleDelegate.registerHandler(AddonModPageModuleHandler.instance); |
| 26 | + }), |
| 27 | + ], |
| 28 | +}) |
| 29 | +``` |
| 30 | +- Delegate examples: `CoreCourseModuleDelegate`, `CoreContentLinksDelegate`, `CoreCourseModulePrefetchDelegate`, `CoreMainMenuDelegate` |
| 31 | + |
| 32 | +### Path Aliases (TypeScript) |
| 33 | +Use barrel exports for cleaner imports: |
| 34 | +- `@/` - Project root |
| 35 | +- `@singletons` / `@singletons/*` - Core singletons |
| 36 | +- `@services/*` - Core services |
| 37 | +- `@features/*` - Core features |
| 38 | +- `@components/*`, `@directives/*`, `@pipes/*` - UI elements |
| 39 | +- `@addons/*` - Addon modules |
| 40 | +- `@classes/*` - Shared classes |
| 41 | + |
| 42 | +### Lazy Loading with Dynamic Routes |
| 43 | +Routes use Angular's `ROUTES` injection token with factory functions: |
| 44 | +```typescript |
| 45 | +@NgModule({ |
| 46 | + providers: [ |
| 47 | + { provide: ROUTES, multi: true, useFactory: buildRoutes, deps: [Injector] }, |
| 48 | + ], |
| 49 | +}) |
| 50 | +export default class MyLazyModule {} |
| 51 | +``` |
| 52 | +Main routing modules: |
| 53 | +- `CoreMainMenuRoutingModule` - Top-level tabs (calendar, messages, etc.) |
| 54 | +- `CoreMainMenuTabRoutingModule` - Child routes within tabs |
| 55 | +- `CoreCourseContentsRoutingModule` - Module used to register routes in the course contents page. These are routes that will only be used on single activity courses where the activity uses split-view navigation in tablets, such as forum or glossary. |
| 56 | +- `CoreCourseIndexRoutingModule` - Course tab pages |
| 57 | +- `CoreMainMenuHomeRoutingModule` - Home page tabs |
| 58 | +- `CoreSitePreferencesRoutingModule` - Site preferences pages |
| 59 | +- Use `buildTabMainRoutes()` helper for consistent tab navigation |
| 60 | + |
| 61 | +### Cache Data Layer |
| 62 | +**SQLite Database**: Site-specific tables via `CORE_SITE_SCHEMAS` token: |
| 63 | +```typescript |
| 64 | +providers: [ |
| 65 | + { provide: CORE_SITE_SCHEMAS, useValue: [MY_SCHEMA], multi: true } |
| 66 | +] |
| 67 | +``` |
| 68 | + |
| 69 | +**File Management**: |
| 70 | +- `CoreFilepool` - Downloads/caches files with queue management |
| 71 | +- `CoreCourseModulePrefetchDelegate` - Handles module prefetching for offline |
| 72 | +- `CoreCourseHelper` - Manages course and section downloads for offline access |
| 73 | +- `CoreFileProvider` - Manages file operations (read, write, delete) across device storage |
| 74 | +- Files stored in filepool with metadata tracking (stale detection, external file flags) |
| 75 | + |
| 76 | +**Sync Pattern**: |
| 77 | +- Offline actions stored in local DB tables |
| 78 | +- Sync services (extend `CoreSyncBaseProvider`) process on connection |
| 79 | +- Use `CoreSyncCronHandler` for background sync |
| 80 | + |
| 81 | +## Development Workflows |
| 82 | + |
| 83 | +### Build & Serve |
| 84 | +```bash |
| 85 | +npm start # Dev server with SSL at localhost:8100 |
| 86 | +npm run build # Development build |
| 87 | +npm run build:prod # Production build |
| 88 | +npm run build:test # Testing build with NODE_ENV=testing |
| 89 | +``` |
| 90 | +Before serving, Gulp tasks auto-run: `lang`, `env`, `icons`, optionally `behat` |
| 91 | + |
| 92 | +### Testing |
| 93 | +**Unit Tests (Jest)**: |
| 94 | +```bash |
| 95 | +npm test # Run all tests |
| 96 | +npm run test:watch # Watch mode |
| 97 | +npm run test:coverage # With coverage |
| 98 | +``` |
| 99 | +Test files: `**/*.test.ts` with setup in `src/testing/setup.ts` |
| 100 | + |
| 101 | +**E2E Tests (Behat)**: |
| 102 | +- Feature files in `src/**/tests/behat/*.feature` |
| 103 | +- PHP-based Behat communicates with app via `window.behat` API |
| 104 | +- Build plugin: `gulp behat` (requires `MOODLE_APP_BEHAT_PLUGIN_PATH` or `MOODLE_DOCKER_WWWROOT`) |
| 105 | +- Behat runtime in `src/testing/services/behat-runtime.ts` |
| 106 | + |
| 107 | +### Mobile Development |
| 108 | +```bash |
| 109 | +npm run dev:android # Run on Android with livereload |
| 110 | +npm run dev:ios # Run on iOS |
| 111 | +npm run prod:android # Production Android build |
| 112 | +npm run prod:ios # Production iOS build |
| 113 | +``` |
| 114 | +Custom Cordova plugin in `cordova-plugin-moodleapp/` with TypeScript source compiled to `www/index.js` |
| 115 | + |
| 116 | +### Language Files |
| 117 | +Distributed lang files compiled into `src/assets/lang/{lang}.json`: |
| 118 | +```bash |
| 119 | +npm run lang:update-langpacks # Pull from Moodle language packs |
| 120 | +npm run lang:create-langindex # Generate language index |
| 121 | +``` |
| 122 | +Watch for changes: `gulp watch` |
| 123 | + |
| 124 | +## Project-Specific Conventions |
| 125 | + |
| 126 | +### Module Structure |
| 127 | +- Core modules: `src/core/features/{feature}/` |
| 128 | +- Addon modules: `src/addons/{type}/{name}/` |
| 129 | +- Each feature has `{name}.module.ts` (eager) and `{name}-lazy.module.ts` (lazy-loaded routes) when needed |
| 130 | +- Avoid creating separate `-lazy.module.ts` files; instead use `loadComponent` with dynamic imports for lazy loading |
| 131 | +- Services in `services/`, components in `components/`, pages in `pages/` |
| 132 | + |
| 133 | +### Handler Pattern |
| 134 | +Handlers are singletons with `.instance` property added by `makeSingleton`: |
| 135 | +```typescript |
| 136 | +@Injectable({ providedIn: 'root' }) |
| 137 | +export class MyHandler extends CoreContentLinksHandler { |
| 138 | + name = 'AddonMyHandler'; |
| 139 | + // ...implementation |
| 140 | +} |
| 141 | +export const MyHandler = makeSingleton(MyHandler); |
| 142 | +``` |
| 143 | + |
| 144 | +### Shared Module Usage |
| 145 | +Components import `CoreSharedModule` which re-exports: |
| 146 | +- `CoreBaseModule` (CommonModule, FormsModule, IonicModule, TranslateModule) |
| 147 | +- `CoreComponentsModule`, `CoreDirectivesModule`, `CorePipesModule` |
| 148 | + |
| 149 | +### Database Schema Versioning |
| 150 | +Define tables with columns, indexes in site schemas. Version increments trigger migrations. |
| 151 | + |
| 152 | +### Environment Configuration |
| 153 | +`moodle.config.json` (gitignored) extends `moodle.config.example.json`. Access via `CoreConstants.CONFIG` service. Test config overrides via `TestingBehatRuntime.init()`. |
| 154 | + |
| 155 | +## Common Pitfalls |
| 156 | + |
| 157 | +1. Always use singletons via `@singletons` instead of injecting Angular services directly |
| 158 | +2. Use standalone components for new modules instead of `@NgModule({ declarations: [] })` |
| 159 | +3. Always register handlers in `provideAppInitializer` |
| 160 | +4. Import only from barrel files that are configured in `tsconfig.json` paths |
| 161 | +5. **Do** run `gulp` before testing to ensure lang files are built |
| 162 | +6. **Do** use `CoreSitesReadingStrategy` when fetching data to control cache vs network behavior |
| 163 | + |
| 164 | + |
| 165 | +## Code reviews |
| 166 | +- When performing a code review, check that the language is in British English. |
0 commit comments