update controllers logic
This commit is contained in:
@@ -4,16 +4,13 @@ namespace App\Services\ClassProgress;
|
||||
|
||||
use App\Models\ClassProgressAttachment;
|
||||
use App\Models\ClassProgressReport;
|
||||
use App\Models\Configuration;
|
||||
use App\Models\TeacherClass;
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
class ClassProgressQueryService
|
||||
{
|
||||
public function __construct(
|
||||
private ClassProgressRuleService $rules
|
||||
) {}
|
||||
|
||||
public function listReports(User $user, array $filters): LengthAwarePaginator
|
||||
{
|
||||
$query = ClassProgressReport::query()
|
||||
@@ -21,8 +18,13 @@ class ClassProgressQueryService
|
||||
->orderBy($filters['sort_by'] ?? 'week_start', $filters['sort_dir'] ?? 'desc');
|
||||
|
||||
if (!$this->isAdmin($user)) {
|
||||
$query->where('teacher_id', $user->id);
|
||||
} elseif (!empty($filters['teacher_id'])) {
|
||||
if (! empty($filters['class_section_id'])) {
|
||||
$ids = $this->resolveAssignedTeacherIds((int) $filters['class_section_id']);
|
||||
$query->whereIn('teacher_id', $ids !== [] ? $ids : [(int) $user->id]);
|
||||
} else {
|
||||
$query->where('teacher_id', $user->id);
|
||||
}
|
||||
} elseif (! empty($filters['teacher_id'])) {
|
||||
$query->where('teacher_id', (int) $filters['teacher_id']);
|
||||
}
|
||||
|
||||
@@ -53,12 +55,17 @@ class ClassProgressQueryService
|
||||
|
||||
public function getReport(User $user, int $reportId): ClassProgressReport
|
||||
{
|
||||
$query = ClassProgressReport::query()->with('classSection');
|
||||
if (!$this->isAdmin($user)) {
|
||||
$query->where('teacher_id', $user->id);
|
||||
$report = ClassProgressReport::query()->with('classSection')->findOrFail($reportId);
|
||||
|
||||
if ($this->isAdmin($user)) {
|
||||
return $report;
|
||||
}
|
||||
|
||||
return $query->findOrFail($reportId);
|
||||
if ($this->viewerMayAccessReport($user, $report)) {
|
||||
return $report;
|
||||
}
|
||||
|
||||
abort(404);
|
||||
}
|
||||
|
||||
public function weeklyReports(User $user, ClassProgressReport $report): array
|
||||
@@ -69,8 +76,9 @@ class ClassProgressQueryService
|
||||
->whereDate('week_start', $report->week_start)
|
||||
->orderBy('subject', 'asc');
|
||||
|
||||
if (!$this->isAdmin($user)) {
|
||||
$query->where('teacher_id', $user->id);
|
||||
if (! $this->isAdmin($user)) {
|
||||
$ids = $this->resolveAssignedTeacherIds((int) $report->class_section_id);
|
||||
$query->whereIn('teacher_id', $ids !== [] ? $ids : [(int) $user->id]);
|
||||
}
|
||||
|
||||
$rows = $query->get();
|
||||
@@ -137,4 +145,40 @@ class ClassProgressQueryService
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* CI `ClassProgressController::resolveAssignedTeacherIds`.
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
public function resolveAssignedTeacherIds(int $classSectionId): array
|
||||
{
|
||||
$schoolYear = (string) (Configuration::getConfig('school_year') ?? '');
|
||||
if ($schoolYear === '' || $classSectionId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$semester = (string) (Configuration::getConfig('semester') ?? '');
|
||||
$rows = TeacherClass::assignedForSectionTerm($classSectionId, $semester, $schoolYear);
|
||||
$ids = [];
|
||||
foreach ($rows as $row) {
|
||||
$id = (int) ($row['teacher_id'] ?? 0);
|
||||
if ($id > 0) {
|
||||
$ids[$id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return array_map('intval', array_keys($ids));
|
||||
}
|
||||
|
||||
private function viewerMayAccessReport(User $user, ClassProgressReport $report): bool
|
||||
{
|
||||
if ((int) $report->teacher_id === (int) $user->id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$ids = $this->resolveAssignedTeacherIds((int) $report->class_section_id);
|
||||
|
||||
return $ids !== [] && in_array((int) $user->id, $ids, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user