Commit 9cf92dfa authored by Georgios Tsakalos's avatar Georgios Tsakalos

update lab action

implement lab listing and creation
improve file structure
parent b8e88164
......@@ -8,55 +8,53 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\Action;
namespace GrEduLabs\Schools\Action\Lab;
use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\StaffServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
use RedBeanPHP\R;
class Labs
class ListAll
{
protected $view;
public function __construct(Twig $view)
public function __construct(
Twig $view,
LabServiceInterface $labservice,
StaffServiceInterface $staffservice
)
{
$this->view = $view;
$this->view = $view;
$this->labservice = $labservice;
$this->staffservice = $staffservice;
}
public function __invoke(Request $req, Response $res, array $args = [])
{
return $this->view->render($res, 'schools/labs.twig', [
'labs' => [
[
'id' => 1,
'name' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 1',
'type' => 1,
'typeName' => 'ΕΡΓΑΣΤΗΡΙΟ',
'responsible' => 1,
'responsibleName' => 'Γιώργος Τάδε',
'area' => 24,
],
[
'id' => 2,
'name' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 2',
'type' => 2,
'typeName' => 'ΑΙΘΟΥΣΑ',
'responsible' => 2,
'responsibleName' => 'Νίκος Τάδε',
'area' => 50,
],
],
'staff' => [
[
'value' => 1,
'label' => 'Γιώργος Τάδε',
$school = $req->getAttribute('school', false);
if (!$school) {
return $res->withStatus(403, 'No school');
}
],
[
'value' => 2,
'label' => 'Νίκος Τάδε',
],
],
$labs = $this->labservice->getLabsBySchoolId($school->id);
$staff = $this->staffservice->getTeachersBySchoolId($school->id);
$staff = R::exportAll($staff);
$clean_staff = [];
foreach ($staff as $obj) {
$clean_staff[] = [
if ($obj['is_responsible'])
'value' => $obj['id'],
'label' => $obj['name']." ".$obj['surname']
];
}
return $this->view->render($res, 'schools/labs.twig', [
'labs' => $labs ,
'staff' => $clean_staff,
'lab_types' => [
[
'value' => 1,
......
......@@ -8,34 +8,51 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\Action;
namespace GrEduLabs\Schools\Action\Lab;
use Exception;
use GrEduLabs\Schools\Service\LabServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class LabCreate
class PersistLab
{
public function __construct($labservice)
private $labservice;
public function __construct(LabServiceInterface $labservice)
{
$this->labservice = $labservice;
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$params = $req->getParams();
if (array_key_exists('id', $params)) {
$id = $params['id'];
unset($params['id']);
$id = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id);
} else {
$id = $this->labservice->createLab($params);
$lab = $this->labservice->getLabById($id);
$school = $req->getAttribute('school', false);
if (!$school) {
return $res->withStatus(403, 'No school');
}
$params = $req->getParams();
$id = $params['id'];
$params['school_id'] = $school->id;
unset($params['id']);
$res = $res->withJson($lab->export());
try {
if ($id > 0) {
$id = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id);
} else {
$id = $this->labservice->createLab($params);
if ($id > 0) {
$lab = $this->labservice->getLabById($id);
}
}
if (isset($lab)) {
return $res->withJson($lab->export())->withStatus(201);
}
} catch (Exception $ex){
$res = $res->withStatus(500, $ex->getMessage());
}
return $res;
return $res->withStatus(400);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment