labs; need to fix ajax file upload

parent 0e973a68
...@@ -18,4 +18,9 @@ return [ ...@@ -18,4 +18,9 @@ return [
], ],
], ],
], ],
'schools' => [
'file_upload' => [
'target_path' => 'data/uploads',
],
],
]; ];
...@@ -28,7 +28,8 @@ return function (Slim\App $app) { ...@@ -28,7 +28,8 @@ return function (Slim\App $app) {
$container['settings']['db']['user'], $container['settings']['db']['user'],
$container['settings']['db']['pass'] $container['settings']['db']['pass']
); );
RedBeanPHP\R::freeze(); // RedBeanPHP\R::freeze();
// RedBeanPHP\R::debug( true );
$container['view'] = function ($c) { $container['view'] = function ($c) {
$settings = $c['settings']; $settings = $c['settings'];
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--> <![endif]-->
{% block htmlHead %}{% endblock %}
</head> </head>
<body class="{% block bodyClass %}{% endblock %}"> <body class="{% block bodyClass %}{% endblock %}">
{% include 'navigation/main.twig' %} {% include 'navigation/main.twig' %}
......
...@@ -110,10 +110,7 @@ return function (Slim\App $app) { ...@@ -110,10 +110,7 @@ return function (Slim\App $app) {
}; };
$container[Service\LabServiceInterface::class] = function ($c) { $container[Service\LabServiceInterface::class] = function ($c) {
return new Service\LabService( return new Service\LabService();
$c->get(Service\SchoolServiceInterface::class),
$c->get(Service\StaffServiceInterface::class)
);
}; };
$container[Service\AssetServiceInterface::class] = function ($c) { $container[Service\AssetServiceInterface::class] = function ($c) {
...@@ -168,7 +165,12 @@ return function (Slim\App $app) { ...@@ -168,7 +165,12 @@ return function (Slim\App $app) {
}; };
$container[InputFilter\Lab::class] = function ($c) { $container[InputFilter\Lab::class] = function ($c) {
return new InputFilter\Lab(); $settings = $c->get('settings');
$fileUploadSettings = $settings['schools']['file_upload'];
return new InputFilter\Lab(
$fileUploadSettings,
$c->get(Service\LabServiceInterface::class)
);
}; };
}); });
......
...@@ -270,7 +270,7 @@ CREATE TABLE `lab` ( ...@@ -270,7 +270,7 @@ CREATE TABLE `lab` (
`use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `use_in_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`attachment` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `attachment` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_network` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `has_network` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_server` tinyint(1) unsigned DEFAULT NULL, `has_server` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`school_id` int(11) unsigned DEFAULT NULL, `school_id` int(11) unsigned DEFAULT NULL,
`teacher_id` int(11) unsigned DEFAULT NULL, `teacher_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
......
This diff is collapsed.
...@@ -12,23 +12,38 @@ namespace GrEduLabs\Schools\Action\Lab; ...@@ -12,23 +12,38 @@ namespace GrEduLabs\Schools\Action\Lab;
use GrEduLabs\Schools\Service\LabServiceInterface; use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\StaffServiceInterface; use GrEduLabs\Schools\Service\StaffServiceInterface;
use RedBeanPHP\R;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
use Slim\Views\Twig; use Slim\Views\Twig;
class ListAll class ListAll
{ {
/**
*
* @var Twig
*/
protected $view; protected $view;
/**
*
* @var LabServiceInterface
*/
protected $labService;
/**
*
* @var StaffServiceInterface
*/
protected $staffService;
public function __construct( public function __construct(
Twig $view, Twig $view,
LabServiceInterface $labservice, LabServiceInterface $labservice,
StaffServiceInterface $staffservice StaffServiceInterface $staffservice
) { ) {
$this->view = $view; $this->view = $view;
$this->labservice = $labservice; $this->labService = $labservice;
$this->staffservice = $staffservice; $this->staffService = $staffservice;
} }
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
...@@ -38,44 +53,25 @@ class ListAll ...@@ -38,44 +53,25 @@ class ListAll
return $res->withStatus(403, 'No school'); return $res->withStatus(403, 'No school');
} }
$labs = $this->labservice->getLabsBySchoolId($school->id); $labs = $this->labService->getLabsBySchoolId($school->id);
$staff = $this->staffservice->getTeachersBySchoolId($school->id);
$clean_staff = [];
foreach ($staff as $obj) {
$clean_staff[] = [
'value' => $obj['id'],
'label' => $obj['name'] . " " . $obj['surname'],
];
}
$lessons = $this->labservice->getLessons();
$lessons_formatted = [];
foreach ($lessons as $lesson) {
$lessons_formatted[] = ['value' => $lesson->id, 'label' => $lesson->name];
}
$labs_formatted = [];
foreach($labs as $lab) {
$lab['responsible'] = $lab['teacher_id'];
$labs_formatted[] = $lab;
}
return $this->view->render($res, 'schools/labs.twig', [ return $this->view->render($res, 'schools/labs.twig', [
'labs' => $labs_formatted, 'labs' => $labs,
'staff' => $clean_staff, 'staff' => array_map(function ($teacher) {
'lab_types' => [ return ['value' => $teacher['id'], 'label' => $teacher['fullname']];
[ }, $this->staffService->getTeachersBySchoolId($school->id)),
'value' => 1, 'network_options' => array_map(function ($option) {
'label' => 'ΕΡΓΑΣΤΗΡΙΟ', return ['value' => $option, 'label' => $option];
], }, $this->labService->getHasNetworkValues()),
[ 'server_options' => array_map(function ($option) {
'value' => 2, return ['value' => $option, 'label' => $option];
'label' => 'ΑΙΘΟΥΣΑ', }, $this->labService->getHasServerValues()),
], 'lab_types' => array_map(function ($type) {
[ return ['value' => $type['id'], 'label' => $type['name']];
'value' => 3, }, $this->labService->getLabTypes()),
'label' => 'ΓΡΑΦΕΙΟ', 'lessons_options' => array_map(function ($lesson) {
], return ['value' => $lesson['id'], 'label' => $lesson['name']];
], }, $this->labService->getLessons()),
'lessons' => $lessons_formatted,
]); ]);
} }
} }
...@@ -33,26 +33,21 @@ class PersistLab ...@@ -33,26 +33,21 @@ class PersistLab
$params = $req->getParams(); $params = $req->getParams();
$id = $params['id']; $id = $params['id'];
$params['school_id'] = $school->id; $params['school_id'] = $school->id;
$params['lessons'] = [1,2];
unset($params['id']); unset($params['id']);
try { try {
if ($id > 0) { if ($id) {
$id = $this->labservice->updateLab($params, $id); $lab = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id); $res = $res->withStatus(200);
} else { } else {
$lab = $this->labservice->createLab($params);
$id = $this->labservice->createLab($params); $res = $res->withStatus(201);
if ($id > 0) {
$lab = $this->labservice->getLabById($id);
}
}
if (isset($lab)) {
return $res->withJson($lab->export())->withStatus(201);
} }
$res = $res->withJson($lab);
} catch (Exception $ex) { } catch (Exception $ex) {
$res = $res->withStatus(500, $ex->getMessage()); $res = $res->withStatus(500, $ex->getMessage());
} }
return $res->withStatus(400); return $res;
} }
} }
...@@ -17,7 +17,7 @@ trait InputFilterTrait ...@@ -17,7 +17,7 @@ trait InputFilterTrait
{ {
$this->inputFilter->setData($data); $this->inputFilter->setData($data);
$isValid = $this->inputFilter->isValid(); $isValid = $this->inputFilter->isValid();
return [ return [
'is_valid' => $isValid, 'is_valid' => $isValid,
'values' => $isValid ? $this->inputFilter->getValues() : [], 'values' => $isValid ? $this->inputFilter->getValues() : [],
......
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
namespace GrEduLabs\Schools\InputFilter; namespace GrEduLabs\Schools\InputFilter;
use GrEduLabs\Schools\Service\LabServiceInterface;
use Zend\Filter; use Zend\Filter;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\Input; use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilter;
use Zend\Validator; use Zend\Validator;
...@@ -17,9 +19,11 @@ use Zend\Validator; ...@@ -17,9 +19,11 @@ use Zend\Validator;
class Lab class Lab
{ {
use InputFilterTrait; use InputFilterTrait;
public function __construct() public function __construct(
{ array $fileUploadSettings,
LabServiceInterface $labService
) {
$id = new Input('id'); $id = new Input('id');
$id->setRequired(false) $id->setRequired(false)
->getValidatorChain() ->getValidatorChain()
...@@ -33,13 +37,13 @@ class Lab ...@@ -33,13 +37,13 @@ class Lab
->attach(new Validator\NotEmpty()) ->attach(new Validator\NotEmpty())
->attach(new Validator\StringLength(['min' => 3])); ->attach(new Validator\StringLength(['min' => 3]));
$type = new Input('type'); $labTypeId = new Input('labtype_id');
$type->setRequired(true); $labTypeId->setRequired(true);
$type->getValidatorChain() $labTypeId->getValidatorChain()
->attach(new Validator\NotEmpty()); ->attach(new Validator\NotEmpty());
$responsible = new Input('responsible'); $responsibleId = new Input('responsible_id');
$responsible->setRequired(false) $responsibleId->setRequired(false)
->getValidatorChain() ->getValidatorChain()
->attach(new Validator\Digits()); ->attach(new Validator\Digits());
...@@ -50,32 +54,48 @@ class Lab ...@@ -50,32 +54,48 @@ class Lab
->attach(new Validator\Digits()); ->attach(new Validator\Digits());
$lessons = new Input('lessons'); $lessons = new Input('lessons');
$lessons->setRequired(true) $lessons->setRequired(false);
->getValidatorChain() $lessons->getValidatorChain()
->attach(new Validator\NotEmpty()); ->attach(new Validator\NotEmpty());
$attachment = new Input('attachment'); $attachment = new FileInput('attachment');
$attachment->setRequired(false); $attachment->setRequired(false)
->getFilterChain()
->attach(new Filter\File\RenameUpload([
'target' => $fileUploadSettings['target_path'],
'randomize' => true,
]));
$attachment->getValidatorChain()
->attach(new Validator\File\UploadFile());
$use_ext_program= new Input('use_ext_program'); $use_ext_program= new Input('use_ext_program');
$use_ext_program->setRequired(false); $use_ext_program->setRequired(false);
$use_in_program = new Input('use_in_program'); $use_in_program = new Input('use_in_program');
$use_in_program->setRequired(false); $use_in_program->setRequired(false);
$has_server = new Input('has_server');
$has_server->setRequired(false);
$has_network = new Input('has_network'); $has_network = new Input('has_network');
$has_network->setRequired(false); $has_network->setRequired(false);
$has_network->getValidatorChain()
->attach(new Validator\NotEmpty())->attach(new Validator\InArray([
'haystack' => $labService->getHasNetworkValues(),
]));
$has_server = new Input('has_server');
$has_server->setRequired(false);
$has_server->getValidatorChain()
->attach(new Validator\NotEmpty())
->attach(new Validator\InArray([
'haystack' => $labService->getHasServerValues(),
]));
$this->inputFilter = new InputFilter(); $this->inputFilter = new InputFilter();
$this->inputFilter $this->inputFilter
->add($id) ->add($id)
->add($name) ->add($name)
->add($type) ->add($labTypeId)
->add($responsible) ->add($responsibleId)
->add($area) ->add($area)
->add($lessons) ->add($lessons)
->add($attachment) ->add($attachment)
......
...@@ -10,12 +10,36 @@ ...@@ -10,12 +10,36 @@
namespace GrEduLabs\Schools\Middleware; namespace GrEduLabs\Schools\Middleware;
use Slim\Http\Request;
use Slim\Http\Response;
class InputFilterLab class InputFilterLab
{ {
use InputFilterTrait; private $inputFilter;
public function __construct(callable $inputFilter) public function __construct(callable $inputFilter)
{ {
$this->inputFilter = $inputFilter; $this->inputFilter = $inputFilter;
} }
public function __invoke(Request $req, Response $res, callable $next)
{
$data = array_merge_recursive($req->getParams(), $req->getUploadedFiles());
$inputFilter = $this->inputFilter;
$result = $inputFilter($data);
var_dump($req->getParams(), $req->getUploadedFiles(), $data);
die();
if (!$result['is_valid']) {
$res = $res->withStatus(422, 'validation error');
$res->withJson($result);
return $res;
}
$req = $req->withParsedBody($result['values']);
return $next($req, $res);
}
} }
...@@ -9,20 +9,11 @@ ...@@ -9,20 +9,11 @@
namespace GrEduLabs\Schools\Service; namespace GrEduLabs\Schools\Service;
use InvalidArgumentException;
use RedBeanPHP\R; use RedBeanPHP\R;
class LabService implements LabServiceInterface class LabService implements LabServiceInterface
{ {
protected $schoolService;
protected $staffService;
public function __construct(
SchoolServiceInterface $schoolService,
StaffServiceInterface $staffService
) {
$this->schoolService = $schoolService;
$this->staffService = $staffService;
}
public function createLab(array $data) public function createLab(array $data)
{ {
...@@ -30,60 +21,61 @@ class LabService implements LabServiceInterface ...@@ -30,60 +21,61 @@ class LabService implements LabServiceInterface
$lab = R::dispense('lab'); $lab = R::dispense('lab');
$this->persist($lab, $data); $this->persist($lab, $data);
//return $this->export($lab); return $this->exportLab($lab);
return $lab;
} }
public function updateLab(array $data, $id) public function updateLab(array $data, $id)
{ {
$lab = R::load('lab', $id); $lab = R::load('lab', $id);
if (!$lab->id) { if (!$lab->id) {
throw new \InvalidArgumentException('No lab found'); throw new InvalidArgumentException('No lab found');
} }
$this->persist($lab, $data); $this->persist($lab, $data);
return $this->export($lab); return $this->exportLab($lab);
} }
private function persist($lab, $data) private function persist($lab, $data)
{ {
$lab->school_id = $data['school_id']; $lab->school_id = $data['school_id'];
$lab->name = $data['name']; $lab->name = $data['name'];
$lab->type = $data['type']; $lab->labtype_id = $data['labtype_id'];
$lab->area = $data['area']; $lab->area = $data['area'];
$lab->sharedLesson[] = $this->getLessonsById($data['lessons']); $lab->sharedLesson = $this->getLessonsById($data['lessons']);
$lab->use_ext_program = $data['use_ext_program']; $lab->use_ext_program = $data['use_ext_program'];
$lab->use_in_program = $data['use_in_program']; $lab->use_in_program = $data['use_in_program'];
$lab->attachment = $data['attachment']; $lab->attachment = $data['attachment'];
$lab->has_network = $data['has_network']; $lab->has_network = $data['has_network'];
$lab->has_server = isset($data['has_server']); $lab->has_server = $data['has_server'];
$lab->responsible = $data['responsible']; $lab->responsible = R::load('teacher', $data['responsible_id']);
$id = R::store($lab); R::store($lab);
} }
public function getLabById($id) public function getLabById($id)
{ {
$lab = R::load('lab', $id); $lab = R::load('lab', $id);
if (!$lab->id) {
throw new InvalidArgumentException('No lab found');
}
return $lab; return $this->export($lab);
} }
public function getLabsBySchoolId($id) public function getLabsBySchoolId($id)
{ {
$labs = R::findAll('lab', 'school_id = ?', [$id]); $labs = R::findAll('lab', 'school_id = ?', [$id]);
$elabs=[];
foreach($labs as $lab) { return array_map([$this, 'exportLab'], $labs);
$elabs[] = $lab->export();
}
return $elabs;
} }
public function getLessons() public function getLessons()
{ {
$lessons = R::findAll('lesson'); $lessons = R::findAll('lesson');
return $lessons; return array_map(function ($lesson) {
return $lesson->export();
}, $lessons);
} }
public function getLessonsByLabId($id) public function getLessonsByLabId($id)
...@@ -91,17 +83,57 @@ class LabService implements LabServiceInterface ...@@ -91,17 +83,57 @@ class LabService implements LabServiceInterface
$lab = R::load('lab', $id); $lab = R::load('lab', $id);
$lessons = $lab->sharedLesson; $lessons = $lab->sharedLesson;
return $lessons; return array_map(function ($lesson) {
return $lesson->export();
}, $lessons);
}
public function getLabTypes()
{
return array_map(function ($lab) {
return $lab->export();
}, R::find('labtype'));
} }
private function getLessonsById(array $ids) private function getLessonsById($ids)
{