119 lines
3.5 KiB
PHP
Executable File
119 lines
3.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class BadgePrintLog extends Model
|
|
{
|
|
use HasFactory;
|
|
protected $table = 'badge_print_logs';
|
|
protected $primaryKey = 'id';
|
|
public $timestamps = false; // matches CI model
|
|
protected $fillable = [
|
|
'user_id',
|
|
'printed_by',
|
|
'school_year',
|
|
'printed_at',
|
|
'role',
|
|
'class_section_name',
|
|
'copies',
|
|
];
|
|
|
|
/**
|
|
* Laravel version of CI4 logPrints()
|
|
*
|
|
* @param array $userIds
|
|
* @param int|null $printedBy
|
|
* @param string|null $schoolYear
|
|
* @param array $roleMap userId => 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;
|
|
}
|
|
}
|