role label * @param array $classMap userId => class name * @param int $copies * @return int rows inserted */ public function logPrints(array $userIds, ?int $printedBy, ?string $schoolYear, array $roleMap = [], array $classMap = [], int $copies = 1): int { $now = Carbon::now('UTC')->toDateTimeString(); $rows = []; foreach ($userIds as $uid) { $uid = (int)$uid; if ($uid <= 0) { continue; } $rows[] = [ 'user_id' => $uid, 'printed_by' => $printedBy, 'school_year' => $schoolYear, 'printed_at' => $now, 'role' => (string)($roleMap[$uid] ?? ''), 'class_section_name' => (string)($classMap[$uid] ?? ''), 'copies' => max(1, (int)$copies), ]; } if (empty($rows)) { return 0; } try { DB::table($this->table)->insert($rows); return count($rows); } catch (\Throwable $e) { \Log::error("BadgePrintLog::logPrints failed: " . $e->getMessage()); return 0; } } /** * Laravel version of CI4 getStatus() * * @param array $userIds * @param string|null $schoolYear * @return array */ public function getStatus(array $userIds, ?string $schoolYear = null): array { $userIds = array_values(array_unique(array_map('intval', array_filter($userIds)))); if (empty($userIds)) { return []; } try { $query = DB::table($this->table . ' as l') ->selectRaw(' l.user_id, COUNT(*) as prints, MAX(l.printed_at) as last_printed_at, MAX(l.printed_by) as last_printed_by ') ->whereIn('l.user_id', $userIds) ->groupBy('l.user_id'); if (!empty($schoolYear)) { $query->where('l.school_year', $schoolYear); } $rows = $query->get(); } catch (\Throwable $e) { \Log::error("BadgePrintLog::getStatus failed: " . $e->getMessage()); return []; } $result = []; foreach ($rows as $row) { $result[(int)$row->user_id] = [ 'count' => (int)$row->prints, 'last_printed_at' => (string)$row->last_printed_at, 'last_printed_by' => $row->last_printed_by !== null ? (int)$row->last_printed_by : null, ]; } return $result; } }