Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m29s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m38s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m40s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m7s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m24s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
118 lines
5 KiB
PHP
118 lines
5 KiB
PHP
<?php
|
|
|
|
namespace Inhaus\Plugins\Qm;
|
|
|
|
use App\Models\User;
|
|
use App\Support\Audit;
|
|
use App\Support\Permissions\Audiences;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Inhaus\Plugins\Qm\Models\QmEntry;
|
|
|
|
/**
|
|
* Lesebestätigungen: Wer ein Dokument mit „Lesebestätigung“ sehen darf, bestätigt
|
|
* die aktuelle Fassung. Eine neue Fassung braucht eine neue Bestätigung.
|
|
*/
|
|
class Acknowledgements
|
|
{
|
|
public function __construct(private readonly QmTree $tree, private readonly QmAccess $access) {}
|
|
|
|
/**
|
|
* Bestätigungen der Person zu den Einträgen: Eintrag => bestätigt am (nur für die aktuelle Fassung).
|
|
*
|
|
* @param iterable<QmEntry> $entries
|
|
* @return array<int, string>
|
|
*/
|
|
public function stateFor(iterable $entries, User $user): array
|
|
{
|
|
$entries = collect($entries)->filter(fn (QmEntry $e) => $e->requires_acknowledgement);
|
|
|
|
if ($entries->isEmpty()) {
|
|
return [];
|
|
}
|
|
|
|
$rows = DB::table('qm_acknowledgements')->where('user_id', $user->id)->whereIn('entry_id', $entries->pluck('id'))->get()->keyBy('entry_id');
|
|
$state = [];
|
|
|
|
foreach ($entries as $entry) {
|
|
$row = $rows[$entry->id] ?? null;
|
|
|
|
if ($row && (int) $row->file_id === (int) $entry->currentFileId()) {
|
|
$state[$entry->id] = (string) $row->acknowledged_at;
|
|
}
|
|
}
|
|
|
|
return $state;
|
|
}
|
|
|
|
public function acknowledge(QmEntry $entry, User $user): void
|
|
{
|
|
DB::table('qm_acknowledgements')->updateOrInsert(
|
|
['entry_id' => $entry->id, 'user_id' => $user->id],
|
|
['file_id' => $entry->currentFileId(), 'acknowledged_at' => now()],
|
|
);
|
|
|
|
Audit::log('qm.acknowledged', ['entry' => $entry->id, 'title' => $entry->title]);
|
|
}
|
|
|
|
/**
|
|
* Noch zu bestätigende Dokumente der Person.
|
|
*
|
|
* @return Collection<int, QmEntry>
|
|
*/
|
|
public function outstanding(User $user): Collection
|
|
{
|
|
if (! $this->access->allows($user)) {
|
|
return collect();
|
|
}
|
|
|
|
$entries = QmEntry::query()
|
|
->where('requires_acknowledgement', true)
|
|
->whereIn('folder_id', $this->tree->visibleIds($user) ?: [0])
|
|
->with(['readingFile', 'editableFile'])
|
|
->orderBy('title')
|
|
->get();
|
|
|
|
$done = $this->stateFor($entries, $user);
|
|
|
|
return $entries->reject(fn (QmEntry $entry) => isset($done[$entry->id]) || ! $entry->currentFileId())->values();
|
|
}
|
|
|
|
/**
|
|
* Auswertung für das QM-Team: wer hat die aktuelle Fassung bestätigt, wer nicht.
|
|
*
|
|
* @return array{confirmed: list<array<string, mixed>>, missing: list<array<string, mixed>>}
|
|
*/
|
|
public function report(QmEntry $entry): array
|
|
{
|
|
$fileId = $entry->currentFileId();
|
|
$category = $this->tree->categoryId($entry->folder_id);
|
|
|
|
// Alle aktiven Personen, die den Ordner sehen dürfen (Zielgruppe der Kategorie, Rollen-Ausschluss des Moduls)
|
|
$excluded = $this->access->excludedRoles();
|
|
$rules = $category !== null ? Audiences::get('qm_category', $category) : [];
|
|
$include = array_filter(array_intersect_key($rules, array_flip(['role', 'department', 'location'])));
|
|
$people = User::query()->where('is_active', true)
|
|
->when($include !== [], fn (Builder $q) => $q->where(fn (Builder $q) => $q
|
|
->whereHas('roles', fn (Builder $r) => $r->whereIn('roles.id', $include['role'] ?? [0]))
|
|
->orWhereIn('department', $include['department'] ?? [])
|
|
->orWhereIn('location', $include['location'] ?? [])))
|
|
->when(filled($rules['exclude_role'] ?? []), fn (Builder $q) => $q->whereDoesntHave('roles', fn (Builder $r) => $r->whereIn('roles.id', $rules['exclude_role'])))
|
|
// Rollen-Ausschluss des Moduls gilt nicht für das QM-Team
|
|
->when($excluded !== [], fn (Builder $q) => $q->where(fn (Builder $q) => $q->whereDoesntHave('roles', fn (Builder $r) => $r->whereIn('roles.id', $excluded))
|
|
->orWhereHas('roles.grants', fn (Builder $g) => $g->whereIn('ability', ['qm.manage', '*']))))
|
|
->orderBy('last_name')->orderBy('first_name')
|
|
->get(['id', 'name', 'first_name', 'last_name', 'department', 'job_title', 'avatar_path']);
|
|
|
|
$acks = DB::table('qm_acknowledgements')->where('entry_id', $entry->id)->where('file_id', $fileId)->pluck('acknowledged_at', 'user_id');
|
|
$row = fn (User $u) => ['id' => $u->id, 'name' => $u->name, 'department' => $u->department, 'job_title' => $u->job_title, 'initials' => $u->initials, 'avatar_url' => $u->avatarUrl(96)];
|
|
|
|
[$confirmed, $missing] = $people->partition(fn (User $u) => isset($acks[$u->id]));
|
|
|
|
return [
|
|
'confirmed' => $confirmed->map(fn (User $u) => [...$row($u), 'acknowledged_at' => (string) $acks[$u->id]])->sortByDesc('acknowledged_at')->values()->all(),
|
|
'missing' => $missing->map($row)->values()->all(),
|
|
];
|
|
}
|
|
}
|