-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add in teacher timetable controller, refactor code to share reu…
…sable code between pupil and teacher controllers
- Loading branch information
1 parent
12822ed
commit 6ec5b16
Showing
4 changed files
with
143 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace spkm\isams\Controllers; | ||
|
||
use spkm\isams\Endpoint; | ||
use spkm\isams\TimetableControllerTrait; | ||
|
||
|
||
/** | ||
* Class PupilTimetableController. | ||
*/ | ||
class TeacherTimetableController extends Endpoint | ||
{ | ||
use TimetableControllerTrait; | ||
|
||
/** | ||
* Set the URL the request is made to. | ||
* | ||
* @return void | ||
* | ||
* @throws Exception | ||
*/ | ||
protected function setEndpoint(): void | ||
{ | ||
$this->endpoint = $this->getDomain() . '/api/timetables/employees'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace spkm\isams; | ||
|
||
use GuzzleHttp\Exception\GuzzleException; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Cache; | ||
use spkm\isams\Controllers\TeachingSubjectController; | ||
use spkm\isams\Controllers\TimetableStructureController; | ||
use spkm\isams\Wrappers\Lesson; | ||
use spkm\isams\Wrappers\TeachingSubject; | ||
use spkm\isams\Wrappers\TimetableDay; | ||
|
||
trait TimetableControllerTrait | ||
{ | ||
/** | ||
* @param string $schoolId | ||
* @return Collection | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function getWeekCalendar(string $schoolId): Collection | ||
{ | ||
$timetable = $this->show($schoolId); | ||
$result = []; | ||
foreach ($this->getTimetableStructure() as $day => $days) { | ||
foreach ($days as $period) { | ||
$lesson = collect($timetable['sets'])->filter(function ($item) use ($period) { | ||
return $item->periodId === $period->id; | ||
})->map(function ($item) { | ||
return new Lesson($item); | ||
})->first(); | ||
|
||
if ($lesson) { | ||
$subjectName = $this->getSubject($lesson->subjectId)->name; | ||
$lesson->subjectName = $subjectName; | ||
} | ||
$period->lesson = $lesson; | ||
$result[$day][] = $period; | ||
} | ||
} | ||
|
||
return collect($result)->map(function ($item) { | ||
return new TimetableDay($item); | ||
}); | ||
} | ||
|
||
/** | ||
* Get the timetable for the specified pupil. | ||
* | ||
* @param string $schoolId | ||
* @return Collection | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function show(string $schoolId): Collection | ||
{ | ||
$this->endpoint = $this->endpoint . '/' . $schoolId; | ||
$response = $this->guzzle->request( | ||
'GET', | ||
$this->endpoint, | ||
['headers' => $this->getHeaders()] | ||
); | ||
|
||
$decoded = json_decode($response->getBody()->getContents()); | ||
|
||
return collect($decoded); | ||
} | ||
|
||
/** | ||
* @return Collection | ||
*/ | ||
private function getTimetableStructure(): Collection | ||
{ | ||
$key = $this->institution->getConfigName() . 'timetableStructure.index'; | ||
|
||
return Cache::remember($key, now()->addWeek(), function () { | ||
$schedule = new TimetableStructureController($this->institution); | ||
|
||
return $schedule->index(); | ||
}); | ||
} | ||
|
||
/** | ||
* @param int $subjectId | ||
* @return TeachingSubject | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
private function getSubject(int $subjectId): TeachingSubject | ||
{ | ||
$subjects = new TeachingSubjectController($this->institution); | ||
|
||
return $subjects->index()->filter(function ($subject) use ($subjectId) { | ||
return $subject->id === $subjectId; | ||
})->first(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters