school service, inputfilter; use in createschool in sch_sync; update delete teacher like asset

parent 0b138bbe
......@@ -13,8 +13,7 @@ return [
'routes' => [
['/school', ['school'], ['get']],
['/school/labs', ['school'], ['get']],
['/school/staff', ['school'], ['get', 'post']],
['/school/staff/{id:[1-9][0-9]*}', ['school'], ['delete']],
['/school/staff', ['school'], ['get', 'post', 'delete']],
['/school/assets', ['school'], ['get', 'post', 'delete']],
],
],
......
<?php
use GrEduLabs\Schools\InputFilter\School as SchoolInputFilter;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use SchMM\FetchUnit;
use SchSync\Middleware\CreateSchool;
use SchSync\Middleware\CreateUser;
use Slim\App;
/**
* gredu_labs.
*
......@@ -8,7 +15,7 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
return function (Slim\App $app) {
return function (App $app) {
$container = $app->getContainer();
$events = $container['events'];
......@@ -18,33 +25,35 @@ return function (Slim\App $app) {
});
$events('on', 'app.services', function ($stop, $container) {
$container[SchSync\Middleware\CreateUser::class] = function ($c) {
return new SchSync\Middleware\CreateUser(
$c['authentication_service'],
$c['router']->pathFor('user.login'),
$c['router']->pathFor('user.logout.sso'),
$c['flash'],
$c['logger']
$container[CreateUser::class] = function ($c) {
return new CreateUser(
$c->get('authentication_service'),
$c->get('router')->pathFor('user.login'),
$c->get('router')->pathFor('user.logout.sso'),
$c->get('flash'),
$c->get('logger')
);
};
$container[SchSync\Middleware\CreateSchool::class] = function ($c) {
return new SchSync\Middleware\CreateSchool(
$c['ldap'],
$c[SchMM\FetchUnit::class],
$c['authentication_service'],
$c['router']->pathFor('user.login'),
$c['router']->pathFor('user.logout.sso'),
$c['flash'],
$c['logger']
$container[CreateSchool::class] = function ($c) {
return new CreateSchool(
$c->get('ldap'),
$c->get(FetchUnit::class),
$c->get('authentication_service'),
$c->get(SchoolServiceInterface::class),
$c->get(SchoolInputFilter::class),
$c->get('router')->pathFor('user.login'),
$c->get('router')->pathFor('user.logout.sso'),
$c->get('flash'),
$c->get('logger')
);
};
});
$events('on', 'app.bootstrap', function ($stop, $app, $container) {
foreach ($container['router']->getRoutes() as $route) {
foreach ($container->get('router')->getRoutes() as $route) {
if ('user.login.sso' === $route->getName()) {
$route->add(SchSync\Middleware\CreateUser::class)
->add(SchSync\Middleware\CreateSchool::class);
$route->add(CreateUser::class)
->add(CreateSchool::class);
break;
}
}
......
......@@ -10,7 +10,10 @@
namespace SchSync\Middleware;
use Exception;
use GrEduLabs\Authentication\Identity;
use GrEduLabs\Schools\InputFilter\School as SchoolInputFilter;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
......@@ -38,6 +41,18 @@ class CreateSchool
*/
private $authService;
/**
*
* @var SchoolServiceInterface
*/
private $schoolService;
/**
*
* @var SchoolInputFilter
*/
private $schoolInputFilter;
/**
* @var string
*/
......@@ -62,6 +77,8 @@ class CreateSchool
Ldap $ldap,
callable $fetchUnitFromMM,
AuthenticationServiceInterface $authService,
SchoolServiceInterface $schoolService,
SchoolInputFilter $schoolInputFilter,
$unitNotFoundRedirectUrl,
$ssoLogoutUrl,
Messages $flash,
......@@ -70,6 +87,8 @@ class CreateSchool
$this->ldap = $ldap;
$this->fetchUnit = $fetchUnitFromMM;
$this->authService = $authService;
$this->schoolService = $schoolService;
$this->schoolInputFilter = $schoolInputFilter;
$this->unitNotFoundRedirectUrl = (string) $unitNotFoundRedirectUrl;
$this->ssoLogoutUrl = (string) $ssoLogoutUrl;
$this->flash = $flash;
......@@ -113,33 +132,36 @@ class CreateSchool
$school = R::findOne('school', 'registry_no = ?', [$registryNo]);
try {
if (!$school) {
R::begin();
$school = R::dispense('school');
$school->registry_no = $unit['registry_no'];
$school->name = $unit['name'];
$school->street_address = $unit['street_address'];
$school->postal_code = $unit['postal_code'];
$school->phone_number = $unit['phone_number'];
$school->fax_number = $unit['fax_number'];
$school->email = $unit['email'];
$school->municipality = $unit['municipality'];
$school->schooltype_id = $unit['unit_type_id'];
$school->prefecture_id = $unit['prefecture_id'];
$school->educationlevel_id = $unit['education_level_id'];
$school->eduadmin_id = $unit['edu_admin_id'];
$school->created = time();
$school->creator = $identity->mail;
$school_id = R::store($school);
$this->logger->info(sprintf('School %s imported from MM to database', $registryNo), ['creator' => $identity->mail]);
$data = [
'id' => '',
'registry_no' => $unit['registry_no'],
'name' => $unit['name'],
'street_address' => $unit['street_address'],
'postal_code' => $unit['postal_code'],
'phone_number' => $unit['phone_number'],
'fax_number' => $unit['fax_number'],
'email' => $unit['email'],
'municipality' => $unit['municipality'],
'schooltype_id' => $unit['unit_type_id'],
'prefecture_id' => $unit['prefecture_id'],
'educationlevel_id' => $unit['education_level_id'],
'eduadmin_id' => $unit['edu_admin_id'],
'creator' => $identity->mail,
];
$filtered = call_user_func($this->schoolInputFilter, $data);
if (!$filtered['is_valid']) {
$this->logger->error('Invalid data', $filtered);
throw new Exception('Invalid data');
}
$school = $this->schoolService->createSchool($filtered['values']);
$this->logger->info(sprintf('School %s imported from MM to database', $registryNo), $filtered['values']);
$user = R::load('user', $identity->id);
$user->school_id = $school_id;
$user->school_id = $school['id'];
R::store($user);
$this->logger->info(sprintf('Set school %s to user %s', $registryNo, $identity->mail));
R::commit();
}
} catch (\Exception $e) {
R::rollback();
} catch (Exception $e) {
$this->logger->error(sprintf('Problem inserting school %s form MM in database', $registryNo));
$this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);
......
This diff is collapsed.
......@@ -162,9 +162,12 @@
return;
}
$.ajax({
url: this.url + '/' + this.teacher.get('id'),
url: that.url,
type: 'delete',
dataType: 'json'
dataType: 'json',
data: {
'id': that.teacher.get('id')
}
}).done(function () {
that.model.remove(that.teacher.get('id'));
that.hide();
......
<?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\Action;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
class Assets
{
protected $view;
public function __construct(Twig $view)
{
$this->view = $view;
}
public function __invoke(Request $req, Response $res, array $args = [])
{
return $this->view->render($res, 'schools/assets.twig', [
'assets' => [
[
'id' => 1,
'type' => 1,
'typeName' => 'ΒΙΝΤΕΟΠΡΟΒΟΛΕΑΣ',
'lab' => 1,
'labName' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 1',
'quantity' => 2,
'acquisitionYear' => '2001',
'comments' => 'test',
],
[
'id' => 2,
'type' => 3,
'typeName' => 'LAPTOP',
'lab' => 2,
'labName' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 2',
'quantity' => 1,
'acquisitionYear' => '2003',
'comments' => 'test',
],
[
'id' => 3,
'type' => 2,
'typeName' => 'ΠΛΗΚΤΡΟΛΟΓΙΟ',
'lab' => 2,
'labName' => 'ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡΟΦ/ΚΗΣ 2',
'quantity' => 10,
'acquisitionYear' => '2008',
'comments' => 'test',
],
],
]);
}
}
......@@ -28,7 +28,7 @@ class Index
$school = $req->getAttribute('school');
return $this->view->render($res, 'schools/index.twig', [
'school' => $school->export(),
'school' => $school,
]);
}
}
......@@ -28,13 +28,13 @@ class DeleteTeacher
$this->staffService = $staffService;
}
public function __invoke(Request $req, Response $res, array $args = [])
public function __invoke(Request $req, Response $res)
{
$school = $req->getAttribute('school', false);
if (!$school->id) {
return $res->withStatus(403, 'No school');
}
$id = isset($args['id']) ? $args['id'] : false;
$id = $req->getParam('id', false);
if (!$id) {
$res = $res->withStatus(404);
......
<?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\InputFilter;
use Exception;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Zend\Filter;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
class School
{
use InputFilterTrait;
public function __construct(
SchoolServiceInterface $schoolService
) {
$id = new Input('id');
$id->setRequired(false)
->getValidatorChain()
->attach(new Validator\Digits());
$registryNo = new Input('registry_no');
$registryNo->setRequired(true)
->getValidatorChain()
->attach(new Validator\NotEmpty())
->attach(new Validator\Callback([
'callback' => function ($value, $context = []) use ($schoolService) {
$id = isset($context['id']) ? $context['id'] : false;
if (false === $id) {
throw new Exception('No id in context');
}
try {
$school = $schoolService->findSchoolByRegistryNo($value);
if (!$school || (isset($school['id']) && !$school['id'])) {
return true;
}
return $school['id'] == $id;
} catch (Exception $ex) {
return false;
}
},
'messageTemplate' => 'Το σχολείο με κωδικό %value% υπάρχει ήδη',
]));
$name = new Input('name');
$name->setRequired(true)
->getFilterChain()
->attach(new Filter\StripTags())
->attach(new Filter\StringTrim());
$name->getValidatorChain()
->attach(new Validator\NotEmpty());
$streetAddress = new Input('street_address');
$streetAddress->setRequired(false)
->getFilterChain()
->attach(new Filter\StripTags())
->attach(new Filter\StringTrim());
$postalCode = new Input('postal_code');
$postalCode->setRequired(false)
->getFilterChain()
->attach(new Filter\Digits());
$phoneNumber = new Input('phone_number');
$phoneNumber->setRequired(false)
->getFilterChain()
->attach(new Filter\Digits());
$faxNumber = new Input('fax_number');
$faxNumber->setRequired(false)
->getFilterChain()
->attach(new Filter\Digits());
$email = new Input('email');
$email->setRequired(false)
->getValidatorChain()
->attach(new Validator\EmailAddress());
$municipality = new Input('municipality');
$municipality->setRequired(true)
->getFilterChain()
->attach(new Filter\StripTags())
->attach(new Filter\StringTrim());
$schoolTypeId = new Input('schooltype_id');
$schoolTypeId->setRequired(true)
->getFilterChain()
->attach(new Filter\ToInt());
$prefectureId = new Input('prefecture_id');
$prefectureId->setRequired(true)
->getFilterChain()
->attach(new Filter\ToInt());
$educationlevelId = new Input('educationlevel_id');
$educationlevelId->setRequired(true)
->getFilterChain()
->attach(new Filter\ToInt());
$eduadminId = new Input('eduadmin_id');
$eduadminId->setRequired(true)
->getFilterChain()
->attach(new Filter\ToInt());
$creator = new Input('creator');
$creator->setRequired(true)
->getValidatorChain()
->attach(new Validator\EmailAddress());
$this->inputFilter = new InputFilter();
$this->inputFilter
->add($id)
->add($registryNo)
->add($name)
->add($streetAddress)
->add($postalCode)
->add($phoneNumber)
->add($faxNumber)
->add($email)
->add($municipality)
->add($schoolTypeId)
->add($prefectureId)
->add($educationlevelId)
->add($eduadminId)
->add($creator);
}
}
......@@ -33,6 +33,6 @@ class FetchSchoolFromIdentity
return $res->withStatus(403, 'No school');
}
return $next($req->withAttribute('school', $school), $res);
return $next($req->withAttribute('school', (object) $school->export()), $res);
}
}
<?php
/**
* gredu_labs
*
......@@ -15,32 +16,50 @@ class SchoolService implements SchoolServiceInterface
{
public function createSchool(array $data)
{
$school = R::dispense('school');
$required = ['registry_no', 'name', 'municipality','schooltype_id', 'prefecture_id',
'educationlevel_id', 'eduadmin_id', 'created', 'creator', ];
$optional = ['street_address', 'postal_code', 'phone_number', 'fax_number', 'email'];
foreach ($required as $value) {
if (array_key_exists($value, $data)) {
$school[$value] = $data[$value];
} else {
return -1;
}
}
foreach ($optional as $value) {
if (array_key_exists($value, $data)) {
$school[$value] = $data[$value];
} else {
$school[$value] = '';
}
}
$id = R::store($school);
$school = $this->importSchool(R::dispense('school'), $data);
$school->created = time();
R::store($school);
return $id;
return $this->exportSchool($school);
}
public function getSchool($id)
{
$school = R::load('school', $id);
return $school;
return $this->exportSchool($school);
}
public function findSchoolByRegistryNo($registryNo)
{
$school = R::findOne('school', ' registry_no = ? ', [$registryNo]);
if (null === $school) {
return;
}
return $this->exportSchool($school);
}
private function exportSchool($bean)
{
return $bean->export();
}
private function importSchool($bean, array $data)
{
$bean->registry_no = $data['registry_no'];
$bean->name = $data['name'];
$bean->street_address = $data['street_address'];
$bean->postal_code = $data['postal_code'];
$bean->phone_number = $data['phone_number'];
$bean->fax_number = $data['fax_number'];
$bean->email = $data['email'];
$bean->municipality = $data['municipality'];
$bean->schooltype_id = $data['schooltype_id'];
$bean->prefecture_id = $data['prefecture_id'];
$bean->educationlevel_id = $data['educationlevel_id'];
$bean->eduadmin_id = $data['eduadmin_id'];
$bean->creator = $data['creator'];
return $bean;
}
}
......@@ -13,4 +13,5 @@ interface SchoolServiceInterface
{
public function createSchool(array $data);
public function getSchool($id);
public function findSchoolByRegistryNo($registryNo);
}
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