Commit 1a6dfd93 authored by kanellov's avatar kanellov
Browse files

labs completed

parent ec0782e2
...@@ -15,11 +15,13 @@ return [ ...@@ -15,11 +15,13 @@ return [
['/school/labs', ['school'], ['get', 'post']], ['/school/labs', ['school'], ['get', 'post']],
['/school/staff', ['school'], ['get', 'post', 'delete']], ['/school/staff', ['school'], ['get', 'post', 'delete']],
['/school/assets', ['school'], ['get', 'post', 'delete']], ['/school/assets', ['school'], ['get', 'post', 'delete']],
['/school/labs/attachment', ['school'], ['get', 'delete']],
], ],
], ],
], ],
'schools' => [ 'schools' => [
'file_upload' => [ 'file_upload' => [
'tmp_path' => 'data/tmp',
'target_path' => 'data/uploads', 'target_path' => 'data/uploads',
], ],
], ],
......
...@@ -28,7 +28,7 @@ return function (Slim\App $app) { ...@@ -28,7 +28,7 @@ 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 ); // RedBeanPHP\R::debug( true );
$container['view'] = function ($c) { $container['view'] = function ($c) {
......
...@@ -66,6 +66,22 @@ return function (Slim\App $app) { ...@@ -66,6 +66,22 @@ return function (Slim\App $app) {
); );
}; };
$container[Action\Lab\DownloadAttachment::class] = function ($c) {
$settings = $c->get('settings');
$uploadTargetPath = $settings['schools']['file_upload']['target_path'];
return new Action\Lab\DownloadAttachment(
$c->get(Service\LabServiceInterface::class),
$uploadTargetPath
);
};
$container[Action\Lab\RemoveAttachment::class] = function ($c) {
return new Action\Lab\RemoveAttachment(
$c->get(Service\LabServiceInterface::class)
);
};
$container[Action\Assets\ListAssets::class] = function ($c) { $container[Action\Assets\ListAssets::class] = function ($c) {
return new Action\Assets\ListAssets( return new Action\Assets\ListAssets(
$c->get('view'), $c->get('view'),
...@@ -110,7 +126,10 @@ return function (Slim\App $app) { ...@@ -110,7 +126,10 @@ return function (Slim\App $app) {
}; };
$container[Service\LabServiceInterface::class] = function ($c) { $container[Service\LabServiceInterface::class] = function ($c) {
return new Service\LabService(); $settings = $c->get('settings');
$uploadTargetPath = $settings['schools']['file_upload']['target_path'];
return new Service\LabService($uploadTargetPath);
}; };
$container[Service\AssetServiceInterface::class] = function ($c) { $container[Service\AssetServiceInterface::class] = function ($c) {
...@@ -166,9 +185,10 @@ return function (Slim\App $app) { ...@@ -166,9 +185,10 @@ return function (Slim\App $app) {
$container[InputFilter\Lab::class] = function ($c) { $container[InputFilter\Lab::class] = function ($c) {
$settings = $c->get('settings'); $settings = $c->get('settings');
$fileUploadSettings = $settings['schools']['file_upload']; $uploadTmpPath = $settings['schools']['file_upload']['tmp_path'];
return new InputFilter\Lab( return new InputFilter\Lab(
$fileUploadSettings, $uploadTmpPath,
$c->get(Service\LabServiceInterface::class) $c->get(Service\LabServiceInterface::class)
); );
}; };
...@@ -188,8 +208,11 @@ return function (Slim\App $app) { ...@@ -188,8 +208,11 @@ return function (Slim\App $app) {
$this->delete('/staff', Action\Staff\DeleteTeacher::class); $this->delete('/staff', Action\Staff\DeleteTeacher::class);
$this->get('/labs', Action\Lab\ListAll::class)->setName('school.labs'); $this->get('/labs', Action\Lab\ListAll::class)->setName('school.labs');
$this->post('/labs', Action\Lab\PersistLab::class)->setName('school.labcreate') $this->post('/labs', Action\Lab\PersistLab::class)
->add(Middleware\InputFilterLab::class); ->add(Middleware\InputFilterLab::class);
$this->get('/labs/attachment', Action\Lab\DownloadAttachment::class)
->setName('school.labs.attachment');
$this->delete('/labs/attachment', Action\Lab\RemoveAttachment::class);
$this->get('/assets', Action\Assets\ListAssets::class)->setName('school.assets'); $this->get('/assets', Action\Assets\ListAssets::class)->setName('school.assets');
$this->post('/assets', Action\Assets\PersistAsset::class) $this->post('/assets', Action\Assets\PersistAsset::class)
......
...@@ -241,19 +241,6 @@ INSERT INTO `itemcategory` VALUES (8,'ACCESS POINT'),(26,'LAPTOP'),(7,'MEDIA CON ...@@ -241,19 +241,6 @@ INSERT INTO `itemcategory` VALUES (8,'ACCESS POINT'),(26,'LAPTOP'),(7,'MEDIA CON
/*!40000 ALTER TABLE `itemcategory` ENABLE KEYS */; /*!40000 ALTER TABLE `itemcategory` ENABLE KEYS */;
UNLOCK TABLES; UNLOCK TABLES;
--
-- Table structure for table `lesson`
--
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `lesson` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
-- --
-- Table structure for table `lab` -- Table structure for table `lab`
-- --
...@@ -269,6 +256,7 @@ CREATE TABLE `lab` ( ...@@ -269,6 +256,7 @@ CREATE TABLE `lab` (
`use_ext_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `use_ext_program` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`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,
`attachment_mime` 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` varchar(191) COLLATE utf8mb4_unicode_ci 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,
...@@ -281,6 +269,43 @@ CREATE TABLE `lab` ( ...@@ -281,6 +269,43 @@ CREATE TABLE `lab` (
COLLATE=utf8mb4_unicode_ci; COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `labtype`
--
DROP TABLE IF EXISTS `labtype`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `labtype` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(191) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `labtype`
--
LOCK TABLES `labtype` WRITE;
/*!40000 ALTER TABLE `labtype` DISABLE KEYS */;
INSERT INTO `labtype` VALUES (1,' ΕΡΓΑΣΤΗΡΙΟ ΠΛΗΡ/ΚΗΣ'),(2,'ΕΡΓΑΣΤΗΡΙΟ ΦΥΣΙΚΩΝ ΕΠΙΣΤΗΜΩΝ'),(3,'ΕΡΓΑΣΤΗΡΙΟ ΤΕΧΝΟΛΟΓΙΑΣ'),(4,'ΑΛΛΟ ΕΡΓΑΣΤΗΡΙΟ '),(5,'ΓΩΝΙΑ ΥΠΟΛΟΓΙΣΤΗ'),(6,'ΒΙΒΛΙΟΘΗΚΗ'),(7,'ΑΙΘΟΥΣΑ ΔΙΔΑΣΚΑΛΙΑΣ'),(8,'ΑΛΛΗ ΑΙΘΟΥΣΑ');
/*!40000 ALTER TABLE `labtype` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `lesson`
--
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `lesson` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
-- --
-- Table structure for table `lesson_lab` -- Table structure for table `lesson_lab`
......
...@@ -85,9 +85,10 @@ ...@@ -85,9 +85,10 @@
el: '#lab-form-modal', el: '#lab-form-modal',
form: null, form: null,
lab: null, lab: null,
attachment: null,
url: null, url: null,
events: { events: {
// 'submit': 'persistLab', 'submit': 'persistLab',
'click button.remove': 'removeLab' 'click button.remove': 'removeLab'
}, },
initialize: function () { initialize: function () {
...@@ -99,31 +100,12 @@ ...@@ -99,31 +100,12 @@
that.form[0].reset(); that.form[0].reset();
that.form.find('input[type="hidden"]').val(''); that.form.find('input[type="hidden"]').val('');
}); });
this.form.find('input[type="file"]').fileupload({ this.attachment = this.form.find('.uploaded');
url: that.form.data('url'), this.attachment.on('click', 'a.btn-remove', function (evt) {
multipart: true, var url;
submit: function (e, data) { evt.preventDefault();
data.formData = utils.serializeObject(that.form); url = $(evt.target).closest('a').attr('href');
data.jqXHR = $(this).fileupload('send', data); that.removeAttachment(url);
return false;
},
done: function (response) {
if (that.lab) {
that.lab.set(response);
} else {
that.model.add(response);
}
that.hide();
},
fail: function (xhr, err) {
var messages;
if (422 === xhr.status) {
messages = JSON.parse(xhr.responseText).messages || {};
utils.formMessages.render(that.form, messages);
} else {
alert('Προέκυψε κάποιο σφάλμα');
}
}
}); });
}, },
render: function (labId) { render: function (labId) {
...@@ -145,12 +127,20 @@ ...@@ -145,12 +127,20 @@
var name; var name;
element = $(element); element = $(element);
name = element.attr('name'); name = element.attr('name');
if ('file' === element.attr('type')) return;
if ('checkbox' === element.attr('type')) { if ('checkbox' === element.attr('type')) {
element.prop('checked', utils.parseInt(labAttributes[name])); element.prop('checked', utils.parseInt(labAttributes[name]));
} else { } else {
element.val(labAttributes[name] || ''); element.val(labAttributes[name] || '');
} }
}); });
if (!this.lab || !this.lab.get('attachment')) {
this.attachment.find('a').attr('href', '#');
this.attachment.hide();
} else {
this.attachment.find('a').attr('href', this.attachment.data('href').replace('__lab_id__', this.lab.get('id')));
this.attachment.show();
}
this.show(); this.show();
return this; return this;
}, },
...@@ -162,52 +152,43 @@ ...@@ -162,52 +152,43 @@
this.$el.modal('hide'); this.$el.modal('hide');
return this; return this;
}, },
// persistLab: function (evt) { persistLab: function (evt) {
// var formData = utils.serializeObject(this.form), var formData = utils.serializeObject(this.form),
// that = this, that = this,
// upload = this.form.find('input[type="file"]'); upload = this.form.find('input[type="file"]').fileupload({
// evt.preventDefault(); autoUpload: false
// upload }),
// $('#fileupload').fileupload('send', { postPromise;
// files: upload[0].files evt.preventDefault();
// });
//// if (upload[0].files.length === 0) {
//// ({ postPromise = $.ajax({
//// url: that.url, url: that.url,
//// submit: function (e, data) { type: 'post',
//// var $this = $(this); data: formData
//// data.formData = formData; });
//// data.jqXHR = $this.fileupload('send', data); } else {
//// return false; postPromise = upload.fileupload('send', {files: upload[0].files});
//// }, }
//// done: function (response) {
//// if (that.lab) { postPromise.done(function (response) {
//// that.lab.set(response); if (that.lab) {
//// } else { that.lab.set(response);
//// that.model.add(response); } else {
//// } that.model.add(response);
//// that.hide(); }
//// }, that.hide();
//// error: function (xhr, err) { }).fail(function (xhr, err) {
//// var messages; var messages;
//// if (422 === xhr.status) { if (xhr && 422 === xhr.status) {
//// messages = JSON.parse(xhr.responseText).messages || {}; messages = JSON.parse(xhr.responseText).messages || {};
//// utils.formMessages.render(that.form, messages); utils.formMessages.render(that.form, messages);
//// } else { } else {
//// alert('Προέκυψε κάποιο σφάλμα'); alert('Προέκυψε κάποιο σφάλμα');
//// } }
//// } });
//// }); upload.fileupload('destroy');
//// upload.fileupload('send'); },
// upload.fileupload('destroy');
////
//// $.ajax({
//// url: that.url,
//// type: 'post',
//// contentType: 'multipart/form-data',
//// data: data,
//// }).).fail(function );
// },
removeLab: function(evt) { removeLab: function(evt) {
var that = this; var that = this;
if (!confirm('Να διαγραφεί ο χώρος;')) { if (!confirm('Να διαγραφεί ο χώρος;')) {
...@@ -227,6 +208,22 @@ ...@@ -227,6 +208,22 @@
alert('Δεν ήταν δυνατή η διαγραφή του χώρου'); alert('Δεν ήταν δυνατή η διαγραφή του χώρου');
}); });
},
removeAttachment: function (url) {
var that = this;
if (!confirm('Να διαγραφή το αρχείο;')) {
return;
}
$.ajax({
url: url,
type: 'delete',
}).done(function (response) {
that.lab.set('attachment', null);
that.lab.set('attachment_mime', null);
that.attachment.hide();
}).fail(function () {
alert('Δεν ήταν δυνατή η διαγραφή του αρχείου');
});
} }
}); });
......
<?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\Lab;
use GrEduLabs\Schools\Service\LabServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class DownloadAttachment
{
private $labService;
private $uploadDir;
public function __construct(LabServiceInterface $labService, $uploadDir)
{
$this->labService = $labService;
$this->uploadDir = $uploadDir;
}
public function __invoke(Request $req, Response $res)
{
$school = $req->getAttribute('school', false);
if (!$school) {
return $res->withStatus(403, 'No school');
}
$lab_id = $req->getParam('lab_id', false);
if (!$lab_id) {
return $res->withStatus(404, 'No lab id');
}
$lab = $this->labService->getLabForSchool($school->id, $lab_id);
if ($lab['attachment'] && is_readable($this->uploadDir . '/' . $lab['attachment'])) {
$contents = file_get_contents($this->uploadDir . '/' . $lab['attachment']);
$contentType = $lab['attachment_mime'] ? $lab['attachment_mime'] : 'application/octet-stream';
$res = $res->withHeader('Content-Type', $contentType);
$res = $res->withHeader(
'Content-Disposition',
'filename="' . basename($lab['attachment']) . '"'
);
$res->getBody()->write($contents);
} else {
$res->withStatus(404, 'No attachment');
}
return $res;
}
}
<?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\Lab;
use GrEduLabs\Schools\Service\LabServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class RemoveAttachment
{
private $labService;
public function __construct(LabServiceInterface $labService)
{
$this->labService = $labService;
}
public function __invoke(Request $req, Response $res)
{
$school = $req->getAttribute('school', false);
if (!$school) {
return $res->withStatus(403, 'No school');
}
$lab_id = $req->getParam('lab_id', false);
if (!$lab_id) {
return $res->withStatus(404, 'No lab id');
}
$lab = $this->labService->getLabForSchool($school->id, $lab_id);
try {
$this->labService->removeLabAttachment($lab['id']);
return $res->withStatus(204);
} catch (Exception $ex) {
return $res->withStatus(500, $ex->getMessage());
}
}
}
...@@ -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() : [],
......
...@@ -19,9 +19,9 @@ use Zend\Validator; ...@@ -19,9 +19,9 @@ use Zend\Validator;
class Lab class Lab
{ {
use InputFilterTrait; use InputFilterTrait;
public function __construct( public function __construct(
array $fileUploadSettings, $uploadTmpPath,
LabServiceInterface $labService LabServiceInterface $labService
) { ) {
$id = new Input('id'); $id = new Input('id');
...@@ -57,17 +57,17 @@ class Lab ...@@ -57,17 +57,17 @@ class Lab
$lessons->setRequired(false); $lessons->setRequired(false);
$lessons->getValidatorChain() $lessons->getValidatorChain()
->attach(new Validator\NotEmpty()); ->attach(new Validator\NotEmpty());
$attachment = new FileInput('attachment'); $attachment = new FileInput('attachment');
$attachment->setRequired(false) $attachment->setRequired(false)
->getFilterChain() ->getFilterChain()
->attach(new Filter\File\RenameUpload([ ->attach(new Filter\File\RenameUpload([
'target' => $fileUploadSettings['target_path'], 'target' => $uploadTmpPath,
'randomize' => true, 'randomize' => true,
])); ]));
$attachment->getValidatorChain() $attachment->getValidatorChain()
->attach(new Validator\File\UploadFile()); ->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);
...@@ -80,7 +80,7 @@ class Lab ...@@ -80,7 +80,7 @@ class Lab
->attach(new Validator\NotEmpty())->attach(new Validator\InArray([ ->attach(new Validator\NotEmpty())->attach(new Validator\InArray([
'haystack' => $labService->getHasNetworkValues(), 'haystack' => $labService->getHasNetworkValues(),
])); ]));
$has_server = new Input('has_server'); $has_server = new Input('has_server');
$has_server->setRequired(false); $has_server->setRequired(false);
$has_server->getValidatorChain() $has_server->getValidatorChain()
......
...@@ -16,21 +16,18 @@ use Slim\Http\Response; ...@@ -16,21 +16,18 @@ use Slim\Http\Response;
class InputFilterLab class InputFilterLab
{ {
private $inputFilter;