category = Category::GENERAL; } public function on(Study|int $study): static { if ($study instanceof Study) { $this->studyId = $study->id; } else { $this->studyId = (int) $study; } return $this; } public function by(Authenticatable|int|null $user = null): static { if ($user === null) { $user = auth()->user(); } if ($user instanceof Authenticatable) { $this->userId = (int) $user->getAuthIdentifier(); } else { $this->userId = $user; } return $this; } public function did(Activity $activity): static { $this->activity = $activity; return $this; } public function category(Category $category): static { $this->category = $category; return $this; } public function url(?string $url = null): static { $this->url = $url ?? request()->path(); return $this; } public function orthanc(string $uuid): static { $this->orthancId = $uuid; return $this; } public function notes(string $notes): static { $this->notes = $notes; return $this; } public function ua(?string $agent = null): static { $this->userAgentId = BrowserService::upsertBrowser($agent); return $this; } public function ip(?string $addr = null): static { $this->ipAddr = $addr ?? request()->ip(); return $this; } public function anon() { $this->anonymous = true; $this->userId = null; return $this; } public function log(bool $initDefaults = true): bool { if ($initDefaults) { $this->ip(); $this->url(); $this->ua(); if ($this->userId === null && ! $this->anonymous) { $this->by(); } } return DB::table('audit_logs') ->insert([ 'study_id' => $this->studyId, 'user_id' => $this->userId, 'category' => $this->category->value, 'activity' => $this->activity->value, 'orthanc_uuid' => $this->orthancId, 'ip_addr' => $this->ipAddr, 'user_agent_id' => $this->userAgentId, 'url' => $this->url, 'notes' => $this->notes, 'created_at' => now(), ]); } }