Commit 29fb0a3d authored by kanellov's avatar kanellov
Browse files

fix some issues with staff

parent ad15c8f7
...@@ -13,7 +13,7 @@ return [ ...@@ -13,7 +13,7 @@ return [
'routes' => [ 'routes' => [
['/school', ['school'], ['get']], ['/school', ['school'], ['get']],
['/school/labs', ['school'], ['get']], ['/school/labs', ['school'], ['get']],
['/school/staff', ['school'], ['get']], ['/school/staff', ['school'], ['get', 'post']],
['/school/assets', ['school'], ['get']], ['/school/assets', ['school'], ['get']],
], ],
], ],
......
...@@ -54,30 +54,34 @@ return function (Slim\App $app) { ...@@ -54,30 +54,34 @@ return function (Slim\App $app) {
return new GrEduLabs\Schools\Action\Assets($c->get('view')); return new GrEduLabs\Schools\Action\Assets($c->get('view'));
}; };
$container['schoolservice'] = function($c){ $container['schoolservice'] = function ($c) {
return new GrEduLabs\Schools\Service\SchoolService(); return new GrEduLabs\Schools\Service\SchoolService();
}; };
$container['staffservice'] = function($c){ $container['staffservice'] = function ($c) {
return new GrEduLabs\Schools\Service\StaffService( return new GrEduLabs\Schools\Service\StaffService(
$c->get('schoolservice') $c->get('schoolservice')
); );
}; };
$container['labservice'] = function($c){ $container['labservice'] = function ($c) {
return new GrEduLabs\Schools\Service\LabService( return new GrEduLabs\Schools\Service\LabService(
$c->get('schoolservice'), $c->get('schoolservice'),
$c->get('staffservice') $c->get('staffservice')
); );
}; };
$container['assetservice'] = function($c){ $container['assetservice'] = function ($c) {
return new GrEduLabs\Schools\Service\AssetService( return new GrEduLabs\Schools\Service\AssetService(
$c->get('schoolservice'), $c->get('schoolservice'),
$c->get('labservice') $c->get('labservice')
); );
}; };
$container[GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity::class] = function ($c) {
return new GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity($c['authentication_service']);
};
}); });
$events('on', 'app.bootstrap', function ($stop, $app, $container) { $events('on', 'app.bootstrap', function ($stop, $app, $container) {
...@@ -90,5 +94,6 @@ return function (Slim\App $app) { ...@@ -90,5 +94,6 @@ return function (Slim\App $app) {
$this->get('/labs', GrEduLabs\Schools\Action\Labs::class)->setName('school.labs'); $this->get('/labs', GrEduLabs\Schools\Action\Labs::class)->setName('school.labs');
$this->post('/labs', GrEduLabs\Schools\Action\LabCreate::class)->setName('school.labcreate'); $this->post('/labs', GrEduLabs\Schools\Action\LabCreate::class)->setName('school.labcreate');
$this->get('/assets', GrEduLabs\Schools\Action\Assets::class)->setName('school.assets'); $this->get('/assets', GrEduLabs\Schools\Action\Assets::class)->setName('school.assets');
})->add(GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity::class);
}); });
}; };
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
return function (Slim\App $app) {
$container = $app->getContainer();
$events = $container['events'];
<<<<<<< HEAD
$events('on', 'app.autoload', function ($stop, $autoloader) {
$autoloader->addPsr4('GrEduLabs\\Schools\\', __DIR__ . '/src/');
});
$events('on', 'app.services', function ($stop, $container) {
$container[GrEduLabs\Schools\Action\Index::class] = function ($c) {
return new GrEduLabs\Schools\Action\Index($c->get('view'));
};
$container[GrEduLabs\Schools\Action\Staff::class] = function ($c) {
return new GrEduLabs\Schools\Action\Staff($c->get('view'));
};
=======
$container['autoloader']->addPsr4('GrEduLabs\\Schools\\', __DIR__ . '/src/');
$container[GrEduLabs\Schools\Action\Index::class] = function ($c) {
return new GrEduLabs\Schools\Action\Index(
$c->get('view'),
$c->get('schoolservice')
);
};
$container[GrEduLabs\Schools\Action\Staff::class] = function ($c) {
return new GrEduLabs\Schools\Action\Staff(
$c->get('view'),
$c->get('staffservice')
);
};
$container[GrEduLabs\Schools\Action\StaffCreate::class] = function ($c) {
return new GrEduLabs\Schools\Action\StaffCreate(
$c->get('staffservice')
);
};
$container[GrEduLabs\Schools\Action\Labs::class] = function ($c) {
return new GrEduLabs\Schools\Action\Labs(
$c->get('view')
);
};
$container[GrEduLabs\Schools\Action\LabCreate::class] = function ($c) {
return new GrEduLabs\Schools\Action\LabCreate(
$c->get('labservice')
);
};
>>>>>>> gtsakalos/modular
$container[GrEduLabs\Schools\Action\Labs::class] = function ($c) {
return new GrEduLabs\Schools\Action\Labs($c->get('view'));
};
<<<<<<< HEAD
$container[GrEduLabs\Schools\Action\Assets::class] = function ($c) {
return new GrEduLabs\Schools\Action\Assets($c->get('view'));
};
});
=======
$container['schoolservice'] = function($c){
return new GrEduLabs\Schools\Service\SchoolService();
};
$container['staffservice'] = function($c){
return new GrEduLabs\Schools\Service\StaffService(
$c->get('schoolservice')
);
};
$container['labservice'] = function($c){
return new GrEduLabs\Schools\Service\LabService(
$c->get('schoolservice'),
$c->get('staffservice')
);
};
$container['assetservice'] = function($c){
return new GrEduLabs\Schools\Service\AssetService(
$c->get('schoolservice'),
$c->get('labservice')
);
};
$events = $container['events'];
>>>>>>> gtsakalos/modular
$events('on', 'app.bootstrap', function ($stop, $app, $container) {
$container['view']->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');
<<<<<<< HEAD
$app->group('/school', function () {
$this->get('', GrEduLabs\Schools\Action\Index::class)->setName('school');
$this->get('/staff', GrEduLabs\Schools\Action\Staff::class)->setName('school.staff');
$this->get('/labs', GrEduLabs\Schools\Action\Labs::class)->setName('school.labs');
$this->get('/assets', GrEduLabs\Schools\Action\Assets::class)->setName('school.assets');
});
=======
$app->group('/school', function () {
$this->get('', GrEduLabs\Schools\Action\Index::class)->setName('school');
$this->get('/staff', GrEduLabs\Schools\Action\Staff::class)->setName('school.staff');
$this->post('/staff', GrEduLabs\Schools\Action\StaffCreate::class)->setName('school.staffcreate');
$this->get('/labs', GrEduLabs\Schools\Action\Labs::class)->setName('school.labs');
$this->post('/labs', GrEduLabs\Schools\Action\LabCreate::class)->setName('school.labcreate');
$this->get('/assets', GrEduLabs\Schools\Action\Assets::class)->setName('school.assets');
>>>>>>> gtsakalos/modular
});
};
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
evt.preventDefault(); evt.preventDefault();
this.modal.render(function (data) { this.modal.render(function (data) {
that.model.add(data); that.model.add(data);
that.$el.find('.no-records').remove();
}); });
return this; return this;
}, },
...@@ -109,13 +110,16 @@ ...@@ -109,13 +110,16 @@
hash[pair.name] = pair.value; hash[pair.name] = pair.value;
return hash; return hash;
}, {}); }, {});
var that = this;
evt.preventDefault(); evt.preventDefault();
if (!data.id) { $.post("", data).
data.id = (100 * Math.random()).toFixed(0); done(function(response){
} that.form.data('done')(response);
this.form.data('done')(data); that.form.data('done', undefined);
this.form.data('done', undefined); that.hide();
this.hide(); }).fail(function (xhr, err) {
alert(xhr.statusText);
});
} }
}); });
......
...@@ -25,9 +25,7 @@ class Index ...@@ -25,9 +25,7 @@ class Index
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
{ {
$identity = $req->getAttribute('identity'); $school = $req->getAttribute('school');
$user = \RedBeanPHP\R::load('user', $identity->id);
$school = $user->school;
return $this->view->render($res, 'schools/index.twig', [ return $this->view->render($res, 'schools/index.twig', [
'school' => $school->export(), 'school' => $school->export(),
......
...@@ -24,18 +24,18 @@ class LabCreate ...@@ -24,18 +24,18 @@ class LabCreate
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
{ {
$params = $req->getParams(); $params = $req->getParams();
if (array_key_exists('id', $params)){ if (array_key_exists('id', $params)) {
$id = $params['id']; $id = $params['id'];
unset($params['id']); unset($params['id']);
$id = $this->labservice->updateLab($params, $id); $id = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id); $lab = $this->labservice->getLabById($id);
} } else {
else{
$id = $this->labservice->createLab($params); $id = $this->labservice->createLab($params);
$lab = $this->labservice->getLabById($id); $lab = $this->labservice->getLabById($id);
} }
$res = $res->withJson($lab->export()); $res = $res->withJson($lab->export());
return $res; return $res;
} }
} }
...@@ -27,17 +27,21 @@ class Staff ...@@ -27,17 +27,21 @@ class Staff
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
{ {
$staff = $this->staffservice->getTeachersBySchoolId(1); $staff = $this->staffservice->getTeachersBySchoolId(1);
return $this->view->render($res, 'schools/staff.twig', [ return $this->view->render($res, 'schools/staff.twig', [
'staff' => $staff, 'staff' => array_map(function ($employee) {
return array_merge($employee->export(), [
'branch' => $employee->branch->name,
]);
}, $staff),
'positions' => [ 'positions' => [
['value' => 1, 'label' => 'Εκπαδευτικός'], ['value' => 1, 'label' => 'Εκπαδευτικός'],
['value' => 2, 'label' => 'Διευθυντής σχολείου'], ['value' => 2, 'label' => 'Διευθυντής σχολείου'],
['value' => 3, 'label' => 'Υπεύθυνος εργαστηρίου'], ['value' => 3, 'label' => 'Υπεύθυνος εργαστηρίου'],
], ],
'branches' => [ 'branches' => array_map(function ($branch) {
['value' => 'branch1', 'label' => 'branch'], return ['value' => $branch['id'], 'label' => $branch['name']];
['value' => 'branch2', 'label' => 'branch2'], }, $this->staffservice->getBranches()),
],
]); ]);
} }
} }
...@@ -23,24 +23,29 @@ class StaffCreate ...@@ -23,24 +23,29 @@ class StaffCreate
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
{ {
$school = $req->getAttribute('school', false);
if (!$school) {
return $res->withStatus(403, 'No school');
}
$params = $req->getParams(); $params = $req->getParams();
$id = $params['id']; $id = $params['id'];
unset($params['id']); unset($params['id']);
if ($id > 0){ $params['school_id'] = $school->id;
if ($id > 0) {
$id = $this->staffservice->updateTeacher($params, $id); $id = $this->staffservice->updateTeacher($params, $id);
$teacher = $this->staffservice->getTeacherById($id); $teacher = $this->staffservice->getTeacherById($id);
} } else {
else{
$id = $this->staffservice->createTeacher($params); $id = $this->staffservice->createTeacher($params);
if ($id > 0){ if ($id > 0) {
$teacher = $this->staffservice->getTeacherById($id); $teacher = $this->staffservice->getTeacherById($id);
} }
} }
if (isset($teacher)){ if (isset($teacher)) {
return $res->withJson($teacher->export())->withStatus(201); return $res->withJson(array_merge($teacher->export(), [
} 'branch' => $teacher->branch->name,
else{ ]))->withStatus(201);
} else {
return $res->withStatus(400); return $res->withStatus(400);
} }
} }
......
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\Middleware;
use RedBeanPHP\R;
class FetchSchoolFromIdentity
{
protected $authService;
public function __construct($authService)
{
$this->authService = $authService;
}
public function __invoke($req, $res, $next)
{
$identity = $this->authService->getIdentity();
if (!$identity) {
return $res->withStatus(403, 'No identity');
}
$user = R::load('user', $identity->id);
if (!($school = $user->school)) {
return $res->withStatus(403, 'No school');
}
return $next($req->withAttribute('school', $school), $res);
}
}
...@@ -28,13 +28,11 @@ class LabService implements LabServiceInterface ...@@ -28,13 +28,11 @@ class LabService implements LabServiceInterface
{ {
$lab = R::dispense('lab'); $lab = R::dispense('lab');
$required = ['school_id', 'name', 'type', 'area', 'in_school_use', 'out_school_use', $required = ['school_id', 'name', 'type', 'area', 'in_school_use', 'out_school_use',
'courses', 'attachment', 'has_network', 'has_server' ]; 'courses', 'attachment', 'has_network', 'has_server', ];
foreach ($required as $value){ foreach ($required as $value) {
if (array_key_exists($value, $data)){ if (array_key_exists($value, $data)) {
$lab[$value] = $data[$value]; $lab[$value] = $data[$value];
} } else {
else
{
return -1; return -1;
} }
} }
...@@ -44,27 +42,32 @@ class LabService implements LabServiceInterface ...@@ -44,27 +42,32 @@ class LabService implements LabServiceInterface
} }
$id = R::store($lab); $id = R::store($lab);
return $id; return $id;
} }
public function updateLab(array $data, $id){ public function updateLab(array $data, $id)
{
$lab= R::load('lab', $id); $lab= R::load('lab', $id);
foreach ($data as $key => $value){ foreach ($data as $key => $value) {
$lab[$key] = $value; $lab[$key] = $value;
} }
$id = R::store($lab); $id = R::store($lab);
return $id; return $id;
} }
public function getLabById($id) public function getLabById($id)
{ {
$lab = R::load('lab', $id); $lab = R::load('lab', $id);
return $lab; return $lab;
} }
public function getLabsBySchoolId($id) public function getLabsBySchoolId($id)
{ {
$school = $this->schoolService->getSchool($id); $school = $this->schoolService->getSchool($id);
$labs = $school->ownLab; $labs = $school->ownLab;
return $labs; return $labs;
} }
} }
...@@ -17,7 +17,7 @@ class SchoolService implements SchoolServiceInterface ...@@ -17,7 +17,7 @@ class SchoolService implements SchoolServiceInterface
{ {
$school = R::dispense('school'); $school = R::dispense('school');
$required = ['registry_no', 'name', 'municipality','schooltype_id', 'prefecture_id', $required = ['registry_no', 'name', 'municipality','schooltype_id', 'prefecture_id',
'educationlevel_id', 'eduadmin_id', 'created', 'creator']; 'educationlevel_id', 'eduadmin_id', 'created', 'creator', ];
$optional = ['street_address', 'postal_code', 'phone_number', 'fax_number', 'email']; $optional = ['street_address', 'postal_code', 'phone_number', 'fax_number', 'email'];
foreach ($required as $value) { foreach ($required as $value) {
if (array_key_exists($value, $data)) { if (array_key_exists($value, $data)) {
...@@ -34,11 +34,13 @@ class SchoolService implements SchoolServiceInterface ...@@ -34,11 +34,13 @@ class SchoolService implements SchoolServiceInterface
} }
} }
$id = R::store($school); $id = R::store($school);
return $id; return $id;
} }
public function getSchool($id) public function getSchool($id)
{ {
$school = R::load('school', $id); $school = R::load('school', $id);
return $school; return $school;
} }
} }
...@@ -24,8 +24,8 @@ class StaffService implements StaffServiceInterface ...@@ -24,8 +24,8 @@ class StaffService implements StaffServiceInterface
unset($data['id']); unset($data['id']);
$teacher = R::dispense('teacher'); $teacher = R::dispense('teacher');
$required = ['school_id', 'name','email', 'surname', 'telephone', $required = ['school_id', 'name','email', 'surname', 'telephone',
'position', 'branch']; 'position', 'branch_id', ];
$data['school_id'] = 1;
foreach ($required as $value) { foreach ($required as $value) {
if (array_key_exists($value, $data)) { if (array_key_exists($value, $data)) {
$teacher[$value] = $data[$value]; $teacher[$value] = $data[$value];
...@@ -34,21 +34,25 @@ class StaffService implements StaffServiceInterface ...@@ -34,21 +34,25 @@ class StaffService implements StaffServiceInterface
} }
} }
$id = R::store($teacher); $id = R::store($teacher);
return $id; return $id;
} }
public function updateTeacher(array $data, $id){ public function updateTeacher(array $data, $id)
{
$teacher = R::load('teacher', $id); $teacher = R::load('teacher', $id);
foreach ($data as $key => $value){ foreach ($data as $key => $value) {
$teacher[$key] = $value; $teacher[$key] = $value;
} }
$id = R::store($teacher); $id = R::store($teacher);
return $id; return $id;
} }
public function getTeacherById($id) public function getTeacherById($id)
{ {
$teacher = R::load('teacher', $id); $teacher = R::load('teacher', $id);
return $teacher; return $teacher;
} }
...@@ -56,6 +60,14 @@ class StaffService implements StaffServiceInterface ...@@ -56,6 +60,14 @@ class StaffService implements StaffServiceInterface
{