Skip to content

Commit

Permalink
feat: add in teacher timetable controller, refactor code to share reu…
Browse files Browse the repository at this point in the history
…sable code between pupil and teacher controllers
  • Loading branch information
fredbradley committed Aug 12, 2022
1 parent 12822ed commit 6ec5b16
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 118 deletions.
116 changes: 2 additions & 114 deletions src/Controllers/PupilTimetableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@

namespace spkm\isams\Controllers;

use Carbon\Carbon;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use spkm\isams\Endpoint;
use spkm\isams\Wrappers\Lesson;
use spkm\isams\Wrappers\SchoolTerm;
use spkm\isams\Wrappers\TimetableDay;
use spkm\isams\TimetableControllerTrait;

/**
* Class PupilTimetableController.
*/
class PupilTimetableController extends Endpoint
{
/**
* @var Carbon
*/
public $termStart;
/**
* @var Carbon
*/
public $termEnd;
use TimetableControllerTrait;

/**
* Set the URL the request is made to.
Expand All @@ -37,102 +23,4 @@ protected function setEndpoint(): void
{
$this->endpoint = $this->getDomain() . '/api/timetables/students';
}

/**
* @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 mixed
*
* @throws GuzzleException
*/
private function getSubject(int $subjectId)
{
$subjects = new TeachingSubjectController($this->institution);

return $subjects->index()->filter(function ($subject) use ($subjectId) {
return $subject->id === $subjectId;
})->first();
}

/**
* @return SchoolTerm
*
* @throws GuzzleException
*/
public function getCurrentTermDates(): SchoolTerm
{
$terms = new SchoolTermsController($this->institution);
$currentTerm = $terms->getCurrentTerm();

$this->termStart = $currentTerm->startDate;
$this->termEnd = $currentTerm->finishDate;

return $currentTerm;
}
}
27 changes: 27 additions & 0 deletions src/Controllers/TeacherTimetableController.php
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';
}
}
98 changes: 98 additions & 0 deletions src/TimetableControllerTrait.php
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();
}
}
20 changes: 16 additions & 4 deletions src/Wrappers/Lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace spkm\isams\Wrappers;

use Illuminate\Support\Collection;
use spkm\isams\Controllers\RoughAndReadyController;
use spkm\isams\Wrapper;

/**
Expand All @@ -16,9 +18,19 @@ class Lesson extends Wrapper
*/
protected function handle(): void
{
unset($this->employeeId);
$this->teacher = $this->employeeTitle . ' ' . $this->employeeSurname;
unset($this->employeeTitle);
unset($this->employeeSurname);
if (isset($this->employeeId)) {
unset($this->employeeId);
$this->teacher = $this->employeeTitle.' '.$this->employeeSurname;
unset($this->employeeTitle);
unset($this->employeeSurname);
} else {
// Must be a teacher...
$this->pupils = $this->getPupilsInSet($this->id);
}
}
private function getPupilsInSet(int $setId): Collection
{
$api = new RoughAndReadyController(\App\School::find(2));
return collect($api->get('teaching/sets/'.$setId.'/setList')->students)->pluck('schoolId');
}
}

0 comments on commit 6ec5b16

Please sign in to comment.