Architecture
Data Flow
Create Link
- User submits form ->
api::create()called. - Resolves code length from type (6/12/12).
- If Unguessable, generates 16-char base62 code.
- DB transaction: create
linkpersistent (fireslink_createdevent), attach tags. - Calls
\core\shortlink::create_public_shortlink()for actual short URL. - If Unguessable, appends
?c=<code>to short URL. - Updates link record with final URL. Commit.
Visit Short Link
- Moodle core shortlink system calls
shortlink_handler::process_shortlink('url', $id). - Looks up link by ID.
- If Unguessable, checks
?c=param matches stored code. - Fires
link_redirectedevent. - Redirects to destination URL.
Delete Link
- Route
/{link}/delete-> confirmation -> sesskey check. - Deletes persistent (fires
link_deletedevent). - Redirects home.
Database
Table local_shortlinks:
| Field | Type | Notes |
|---|---|---|
id | INT PK | Auto |
userid | INT FK | Owner |
destinationurl | TEXT | Target URL |
shorturl | TEXT | Generated short URL |
unguessablecode | CHAR(128) | Nullable, random 16-char base62 |
timecreated | INT | Timestamp |
timemodified | INT | Timestamp |
usermodified | INT FK | Last modifier |
Router
Uses Moodle’s router with PHP 8 attributes:
| Route | Controller | Description |
|---|---|---|
GET '' | links_controller::home() | Home page with report |
GET|POST '/{link}/delete' | links_controller::delete_link() | Delete confirmation |
Shim route /redirect/delete?id=X bridges report builder actions (query params) to the path-param-based controller.
Events
All extend core\event\base:
| Event | CRUD | Fired |
|---|---|---|
link_created | c | After link saved |
link_updated | u | After link updated |
link_deleted | d | After link deleted |
link_redirected | r | When short link is visited |
Handler
shortlink_handler implements core\shortlink_handler_interface. Processes type url. Resolves link ID -> destination URL.
Observer
observers::user_deleted on core\event\user_deleted:
- Sets
usermodified=0for deleted user’s edits. - Deletes all links owned by deleted user.
Report Builder
System report links with entity link:
- Columns: shorturl, destinationurl, timecreated.
- Filters: timecreated, userid, tags.
- Conditions: userid = current user.
- Actions: delete with modal confirmation.
- Favicons from DuckDuckGo (
icons.duckduckgo.com/ip3/).
Tags
Tag area registered for local_shortlinks itemtype. Callback local_shortlinks_get_tagged_links() is a stub (not yet implemented).