bootstrap.php 23 KB
Newer Older
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

use League\Csv\Reader;
use League\Csv\Writer;
use RedBeanPHP\R;
use Slim\App;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;

/**
 * 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 (App $app) {
    $container = $app->getContainer();
21
    $events = $container['events'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
22 23 24 25 26 27 28 29

    $events('on', 'app.services', function (Container $c) {

        $c['csv_export_config'] = function ($c) {

            return [
                'schools' => [
                    'data_callback' => 'csv_export_schools',
30
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
31 32 33 34 35 36 37 38 39 40 41
                        'Κωδικός μονάδας',
                        'Ονομασία',
                        'Τύπος μονάδας',
                        'Περιφερειακή ενότητα',
                        'Διεύθυνση εκπαίδευσης',
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'Βαθμίδα εκπαίδευσης',
                    ],
                ],
                'labs' => [
                    'data_callback' => 'csv_export_labs',
42
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ονομασία χώρου',
                        'Τύπος χώρου',
                        'Ειδικότητα υπευθύνου',
                        'Νέος χώρος',
                        'Επιφάνεια (m^2)',
                        'Σύνδεση στο δίκτυο',
                        'Διαθέτει server',
                        'Μαθήματα',
                        'Χρήση στα πλαίσια μαθημάτων',
                        'Χρήση για δραστηριότητες εκτός εκπαιδευτικού προγράμματος',
                    ],
                ],
                'assets' => [
                    'data_callback' => 'csv_export_assets',
60
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
61 62 63 64 65 66 67 68 69
                        'Είδος',
                        'Πλήθος ',
                        'Έτος κτήσης',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Σχόλια - Παρατηρήσεις',
                    ],
70 71 72 73 74 75 76 77 78 79 80 81 82
                ],
                 'software' => [
                    'data_callback' => 'csv_export_software',
                    'headers' => [
                        'Τύπος',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Ονομασία',
                        'Κατασκευαστής',
                        'URL',
                    ],
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
83 84 85
                ],
                'appforms' => [
                    'data_callback' => 'csv_export_appforms',
86
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
87 88 89 90 91 92 93
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
94 95 96 97 98 99 100 101 102 103 104 105 106
             
 				'appnewforms' => [
                    'data_callback' => 'csv_export_appnewforms',
                    'headers' => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
             

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
107 108
                'appforms_items' => [
                    'data_callback' => 'csv_export_appforms_items',
109
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
110 111 112 113 114 115 116 117 118 119 120 121
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ημερομηνία υποβολής',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Νέος χώρος',
                        'Είδος',
                        'Πλήθος ',
                        'Αιτιολογία χρήσης',
                    ],
                ],
122 123 124 125 126 127 128 129 130
                'newapplication' => [
                    'data_callback' => 'csv_export_newapplication',
                    'headers' => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Είδος',
                        'Πλήθος Υπαρχόντων που λειτουργούν',
                        'Πλήθος Αιτουμένων',
131
                        'Αιτιολογία χρήσης',
132 133
                    ],
                ],
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            ];
        };

        $c['csv_export_csv_response'] = function ($c) {

            return function ($res, callable $dataCallback, array $headers, $filename) use ($c) {
                $csvFactory = $c['csv_export_csv_factory'];
                $csv = $csvFactory($dataCallback(), $headers);
                $res = $res->withHeader('Content-Type', 'application/octet-stream');
                $res = $res->withHeader('Content-Transfer-Encoding', 'binary');
                $res = $res->withHeader('Content-Disposition', 'attachment; filename=' . $filename);
                $res->write($csv);

                return $res;
            };
        };

        $c['csv_export_csv_factory'] = function ($c) {

            return function (array $data, array $headers = null) {
                $csv = Writer::createFromFileObject(new SplTempFileObject());
                $csv->setOutputBOM(Reader::BOM_UTF8);
                $csv->addFormatter(function ($row) {
                    return array_map(function ($value) {
                        return str_replace(["\n", "\r"], ' ', $value);
                    }, $row);
                });
                if (null !== $headers) {
                    $csv->insertOne($headers);
                }

                $csv->insertAll($data);

                return $csv;
            };
        };

        $c['csv_export_schools'] = function ($c) {

            return function () {
                $sql = 'SELECT school.registry_no AS registry_no, '
175 176 177 178 179 180 181 182 183 184 185 186 187 188
                        . ' school.name AS school_name, '
                        . ' schooltype.name as school_type, '
                        . ' prefecture.name AS prefecture, '
                        . ' eduadmin.name AS eduadmin, '
                        . ' regioneduadmin.name AS region_edu_admin, '
                        . ' educationlevel.name AS education_level '
                        . ' FROM school '
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                        . ' LEFT JOIN educationlevel ON school.educationlevel_id = educationlevel.id '
                        . ' LEFT JOIN schooltype ON school.schooltype_id = schooltype.id '
                        . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                        . ' GROUP BY school.id '
                        . ' ORDER BY school_name';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
189 190 191 192 193 194 195 196 197 198
                $schools = R::getAll($sql);

                return $schools;
            };
        };

        $c['csv_export_labs'] = function ($c) {

            return function () {
                $sql = 'SELECT lab.id AS id, '
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
                        . ' TRIM(lab.name) AS name, '
                        . ' TRIM(labtype.name) AS type, '
                        . ' branch.name AS responsible_branch, '
                        . ' IF(lab.is_new = 1, "ΝΑΙ", "ΟΧΙ") AS is_new, '
                        . ' lab.area AS area, '
                        . ' lab.has_network AS has_network, '
                        . ' lab.has_server AS has_server, '
                        . ' GROUP_CONCAT(lesson.name SEPARATOR ", ") AS lessons, '
                        . ' TRIM(lab.use_in_program) AS use_in_program, '
                        . ' TRIM(lab.use_ext_program) AS use_ext_program '
                        . ' FROM lab '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                        . ' LEFT JOIN school ON lab.school_id = school.id '
                        . ' LEFT JOIN lab_lesson ON lab_lesson.lab_id = lab.id '
                        . ' LEFT JOIN lesson ON lab_lesson.lesson_id = lesson.id '
                        . ' LEFT JOIN teacher ON lab.responsible_id = teacher.id '
                        . ' LEFT JOIN branch ON branch.id = teacher.branch_id '
                        . ' GROUP BY lab.id '
                        . ' ORDER BY school_name ';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
220 221 222 223 224 225 226 227 228 229 230

                $labs = R::getAll($sql);

                return $labs;
            };
        };

        $c['csv_export_assets'] = function ($c) {

            return function () {
                $sql = 'SELECT TRIM(itemcategory.name) AS category, '
231 232 233 234 235 236 237 238 239 240 241 242 243 244
                        . ' schoolasset.qty AS qty, '
                        . ' schoolasset.acquisition_year AS acquisition_year, '
                        . ' lab.id AS lab_id, '
                        . ' TRIM(labtype.name) AS lab_type, '
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
                        . ' schoolasset.comments AS comments '
                        . ' FROM schoolasset '
                        . ' LEFT JOIN itemcategory ON schoolasset.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN school ON schoolasset.school_id = school.id '
                        . ' LEFT JOIN lab ON schoolasset.lab_id = lab.id '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                        . ' GROUP BY schoolasset.id '
                        . ' ORDER BY lab.id';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
245 246 247 248 249 250 251

                $assets = R::getAll($sql);

                return $assets;
            };
        };

252 253 254 255
  


   $c['csv_export_software'] = function ($c) {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
256 257

            return function () {
258
                $sql = 'SELECT softwarecategory.name AS name, '
259
                        . ' school.registry_no AS school_registry_no, '
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
                        . ' school.name AS school_name, '
                        . ' lab.id AS lab_id, '
                        . ' TRIM(labtype.name) AS lab_type, '
                        . ' TRIM(software.title) AS title, '
                        . ' TRIM(software.vendor) AS vendor, '
                        . ' TRIM(software.url) AS url '
                        . ' FROM software '
                        . ' LEFT JOIN softwarecategory ON software.softwarecategory_id = softwarecategory.id '
                        . ' LEFT JOIN school ON software.school_id = school.id '
                        . ' LEFT JOIN lab ON software.lab_id = lab.id '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                        . ' ORDER BY school_name ';

                $software = R::getAll($sql);

                $software = array_map(function ($row) {
                    $row['url'] = strtolower($row['url']);
                    $row['url'] = str_replace('\\', '/', $row['url']);
                    $row['url'] = urldecode($row['url']);

                    return $row;
                }, $software);

                return $software;
            };
        };









        $c['csv_export_appforms'] = function ($c) {

            //return function () {
        	 return function () use ($c) {
				$settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted)IN (SELECT MAX( submitted )FROM applicationform '
                				. ' LEFT JOIN applicationformitem ON applicationformitem.applicationform_id = applicationform.id '
								. ' LEFT JOIN school ON applicationform.school_id = school.id'
								. ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id'
								. ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id'
								. ' LEFT JOIN labtype ON lab.labtype_id = labtype.id'
								. ' WHERE itemcategory.groupflag NOT IN(' . $version . ')'
								. ' GROUP BY school.id)';



                $appFormIds = R::getCol($appFormIdsSql);

                if (empty($appFormIds)) {
                    return [];
                }

                $in = implode(',', array_fill(0, count($appFormIds), '?'));


				 $sql = 'SELECT applicationform.id AS id, '
                       . ' school.registry_no AS school_registry_no, '
324 325
                        . ' school.name AS school_name, '
                        . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
326 327 328
                        . ' TRIM(applicationform.comments) AS comments' 
                        . ' FROM applicationformitem '
                        . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
329
                        . ' LEFT JOIN school ON applicationform.school_id = school.id '
330 331 332 333 334
                        . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                        . ' WHERE applicationform.id IN(' . $in . ')'
                          . ' GROUP BY school.id ';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
335

336
                $appForms = R::getAll($sql, $appFormIds);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
337 338

                return $appForms;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353


              //  $sql = 'SELECT applicationform.id AS id, '
              //          . ' school.registry_no AS school_registry_no, '
              //          . ' school.name AS school_name, '
               //         . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
               //         . ' TRIM(applicationform.comments) AS comments '
               //         . ' FROM applicationform '
                //        . ' LEFT JOIN school ON applicationform.school_id = school.id '
                //        . ' GROUP BY school.id '
                //        . ' HAVING MAX(applicationform.submitted)';

//                $appForms = R::getAll($sql);

  //              return $appForms;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
354 355 356 357
            };
        };


358
        $c['csv_export_appnewforms'] = function ($c) {
359

360
       return function () use ($c) {
361

362 363

                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
364 365 366

                $appFormIds = R::getCol($appFormIdsSql);

367

368 369 370
                if (empty($appFormIds)) {
                    return [];
                }
371
                $in = implode(',', array_fill(0, count($appFormIds), '?'));
372 373 374 375 376


                $settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
377
                $sql = 'SELECT applicationform.id AS id, '
378
                       . ' school.registry_no AS school_registry_no, '
379 380
                        . ' school.name AS school_name, '
                        . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
381
                        . ' TRIM(applicationform.comments) AS comments' 
382 383 384 385 386 387
                        . ' FROM applicationformitem '
                        . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                        . ' LEFT JOIN school ON applicationform.school_id = school.id '
                        . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
388 389 390
                        . ' WHERE applicationform.id IN(' . $in . ')'
                        . ' AND itemcategory.groupflag IN(' . $version . ')'
                        . ' GROUP BY school.id ';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
391 392 393 394 395 396

                $appForms = R::getAll($sql, $appFormIds);

                return $appForms;
            };
        };
397 398


399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

        $c['csv_export_appforms_items'] = function ($c) {

            return function () use ($c){

            	$settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted)IN (SELECT MAX( submitted )FROM applicationform '
                				. ' LEFT JOIN applicationformitem ON applicationformitem.applicationform_id = applicationform.id '
								. ' LEFT JOIN school ON applicationform.school_id = school.id'
								. ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id'
								. ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id'
								. ' LEFT JOIN labtype ON lab.labtype_id = labtype.id'
								. ' WHERE itemcategory.groupflag NOT IN(' . $version . ')'
								. ' GROUP BY school.id)';




              //  $appFormIdsSql = 'SELECT applicationform.id '
              //     . ' FROM applicationform '
              //     . ' GROUP BY school_id '
              //    . ' HAVING MAX(applicationform.submitted)';

                $appFormIds = R::getCol($appFormIdsSql);

                if (empty($appFormIds)) {
                    return [];
                }

                $in = implode(',', array_fill(0, count($appFormIds), '?'));
                $sql = 'SELECT applicationform.id AS id, '
432 433
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
434
                        . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
435 436
                        . ' lab.id AS lab_id, '
                        . ' TRIM(labtype.name) AS lab_type, '
437 438 439 440 441 442 443 444 445
                        . ' IF(lab.is_new = 1, "ΝΑΙ", "ΟΧΙ") AS is_new, '
                        . ' TRIM(itemcategory.name) AS category, '
                        . ' applicationformitem.qty AS qty, '
                        . ' TRIM(applicationformitem.reasons) AS reasons '
                        . ' FROM applicationformitem '
                        . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                        . ' LEFT JOIN school ON applicationform.school_id = school.id '
                        . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
446
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
447
                        . ' WHERE applicationform.id IN(' . $in . ') ';
448

449
                $appForms = R::getAll($sql, $appFormIds);
450

451
                return $appForms;
452 453
            };
        };
454

455
     
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        $c['csv_export_newapplication'] = function ($c) {

            return function () use ($c) {


                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)';

                $appFormIds = R::getCol($appFormIdsSql);


                if (empty($appFormIds)) {
                    return [];
                }
                $in = implode(',', array_fill(0, count($appFormIds), '?'));


                $settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

                $sql = 'SELECT applicationform.id AS id, '
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
                        . ' TRIM(itemcategory.name) AS category, '
                        . ' applicationformitem.qty AS qty, '
                        . ' applicationformitem.qtyacquired AS qtyacquired, '
                        . ' TRIM(applicationformitem.reasons) AS reasons '
                        . ' FROM applicationformitem '
                        . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                        . ' LEFT JOIN school ON applicationform.school_id = school.id '
                        . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                        . ' WHERE applicationform.id IN(' . $in . ')'
                        . ' AND itemcategory.groupflag IN(' . $version . ')';


                $appForms = R::getAll($sql, $appFormIds);

                return $appForms;
            };
        };
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
497 498 499 500 501 502
    });

    $events('on', 'app.bootstrap', function (App $app, Container $c) {

        $app->get('/export/csv/edulabs_{type}.csv', function (Request $req, Response $res, array $args) use ($c) {
            $type = $args['type'];
503 504
            try {
                $config = $c['csv_export_config'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
505

506 507 508 509 510 511
                if (!array_key_exists($type, $config)) {
                    return $res->withStatus(404);
                }

                $typeConfig = $config[$type];
                $csvResponse = $c['csv_export_csv_response'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
512

513 514 515 516 517 518 519 520
                return $csvResponse($res, $c[$typeConfig['data_callback']], $typeConfig['headers'], 'edulabs_' . $type . '.csv');
            } catch (Exception $ex) {
                $flash = $c['flash'];
                $log = $c['logger'];
                $router = $c['router'];
                $flash->addMessage('warning', 'Προέκυψε κάποιο σφάλμα. Δοκιμάστε αργότερα');
                $log->error(sprintf('Problem downloading %s file. Exception message %s', $type, $ex->getMessage()));
                $log->debug('Exception Trace', ['trace' => $ex->getTraceAsString()]);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
521

522 523
                return $res->withRedirect($router->pathFor('index'));
            }
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
524 525 526 527 528 529 530 531 532 533
            $config = $c['csv_export_config'];

            if (!array_key_exists($type, $config)) {
                return $res->withStatus(404);
            }

            $typeConfig = $config[$type];
            $csvResponse = $c['csv_export_csv_response'];

            return $csvResponse($res, $c[$typeConfig['data_callback']], $typeConfig['headers'], 'edulabs_' . $type . '.csv');
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
534 535 536
        })->setName('export.csv');
    });
};