override in_numbers module and fetch totals for school types from mm

parent 7a645fec
......@@ -22,6 +22,7 @@ return [
'module/application_form/bootstrap.php',
'module/tpe_survey/bootstrap.php',
'module/in_numbers/bootstrap.php',
'module/in_numbers_mm/bootstrap.php',
],
'cache_config' => 'data/cache/config/settings.php',
];
<?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 [
'in_numbers' => [
'cache' => __DIR__ . '/../../data/cache/in_numbers_totals.php',
'cache_lifetime' => 1440, // minutes
]
];
\ No newline at end of file
*
!.gitignore
!cache
!templates
......@@ -47,6 +47,7 @@ return function (\Slim\App $app) {
continue;
}
$results[$type] = [
'type_id' => $type,
'type' => $schoolTypes[$type],
'schools_cnt' => (int) $cnt,
'appforms_cnt' => isset($appFormByType[$type]) ? (int) $appFormByType[$type] : 0,
......@@ -62,10 +63,12 @@ return function (\Slim\App $app) {
});
$events('on', 'app.bootstrap', function ($app, $c) {
$view = $c->get('view');
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/../application/templates', 'application');
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');
$app->get('/in_numbers', function (Slim\Http\Request $req, \Slim\Http\Response $res) use ($c) {
try {
$view = $c->get('view');
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');
$inNumbersFunction = $c->get('in_numbers_by_school_type');
$schoolTypes = $inNumbersFunction();
$totals = array_reduce($schoolTypes, function ($result, $type) {
......@@ -90,16 +93,13 @@ return function (\Slim\App $app) {
$events('on', 'app.bootstrap', function ($app, $c) {
$router = $c['router'];
$route = $router->getNamedRoute('index');
$route->add(function (Slim\Http\Request $req, Slim\Http\Response $res, callable $next) use ($c) {
$router->getNamedRoute('index')->add(function (Slim\Http\Request $req, Slim\Http\Response $res, callable $next) use ($c) {
try {
$view = $c->get('view');
$inNumbersFunction = $c->get('in_numbers_totals');
list($total_schools, $total_app_forms) = array_values($inNumbersFunction());
$view['total_schools'] = $total_schools;
$view['total_app_forms'] = $total_app_forms;
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/../application/templates', 'application');
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');
} catch (\Exception $ex) {
$c->get('logger')->error(sprintf('Exception: %s', $ex->getMessage()), ['file' => __FILE__, 'line' => __LINE__]);
}
......
<?php
use Slim\App;
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();
$events = $container['events'];
$events('on', 'app.services', function ($c) {
$c['in_numbers_get_totals_from_mm'] = function ($c) {
$settings = $c['settings'];
$httpClient = new GuzzleHttp\Client([
'base_uri' => $settings['sch_mm']['api_url'],
'auth' => [
$settings['sch_mm']['api_user'],
$settings['sch_mm']['api_pass'],
],
]);
return function ($type) use ($httpClient) {
$config = $httpClient->getConfig();
$baseUri = $config['base_uri'];
$auth = $config['auth'];
$url = $baseUri->withQueryValue($baseUri, 'unit_type', $type);
$url = $url->withQueryValue($url, 'pagesize', 1);
$response = $httpClient->request('GET', $url, ['auth' => $auth]);
$responseData = json_decode($response->getBody()->getContents(), true);
return isset($responseData['total']) ? $responseData['total'] : null;
};
};
$c['in_numbers_get_totals_from_mm'] = $c->extend('in_numbers_get_totals_from_mm', function ($fn, $c) {
$settings = $c['settings'];
$cacheFile = isset($settings['in_numbers']['cache']) ? $settings['in_numbers']['cache'] : false;
$cacheLifetime = isset($settings['in_numbers']['cache_lifetime']) ? $settings['in_numbers']['cache_lifetime'] : false;
if (!$cacheFile) {
return $fn;
}
return function ($type) use (&$fn, $cacheFile, $cacheLifetime) {
$cache = [];
if (file_exists($cacheFile) && !(time() - filemtime($cacheFile) > $cacheLifetime * 60)) {
$cache = include $cacheFile;
if (isset($cache[$type])) {
return $cache[$type];
}
}
$result = $fn($type);
$cache[$type] = $result;
file_put_contents($cacheFile, '<?php return ' . var_export($cache, true) . ';');
return $result;
};
});
$c['in_numbers_by_school_type'] = $c->extend('in_numbers_by_school_type', function ($fn, $c) {
$fromMMFunction = $c['in_numbers_get_totals_from_mm'];
return function () use (&$fn, &$fromMMFunction) {
$result = $fn();
try {
$result = array_map(function ($type) use (&$fromMMFunction) {
$total = $fromMMFunction($type['type_id']);
$type['schools_total'] = $total;
return $type;
}, $result);
} catch (\Exception $ex) {
// eat it
}
return $result;
};
});
});
$events('on', 'app.bootstrap', function ($app, $c) {
$router = $c['router'];
$view = $c['view'];
$view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');
$router->getNamedRoute('in_numbers')->add(function (Request $req, Response $res, $next) use ($c) {
$inNumbersFunction = $c['in_numbers_by_school_type'];
$view = $c['view'];
$schoolTypes = $inNumbersFunction();
$view['schools_total_global'] = array_reduce($schoolTypes, function ($total, $type) {
if (isset($type['schools_total'])) {
if (null === $total) {
$total = 0;
}
$total += $type['schools_total'];
}
return $total;
}, null);
if (null === $view['schools_total_global']) {
unset($view['schools_total_global']);
}
return $next($req, $res);
});
}, -10);
};
......@@ -6,6 +6,9 @@
<thead>
<tr>
<th>Τύπος σχολείου</th>
{% if schools_total_global is defined %}
<th style="width: 1%;white-space: nowrap">Σχολεία συνολικά</th>
{% endif %}
<th style="width: 1%;white-space: nowrap">Πλήθος καταχωρήσεων</th>
<th style="width: 1%;white-space: nowrap">Πλήθος αιτήσεων</th>
</tr>
......@@ -14,6 +17,9 @@
{% for row in school_types %}
<tr>
<th>{{ row.type }}</th>
{% if row.schools_total is defined %}
<td class="text-center">{{ row.schools_total }}</td>
{% endif %}
<td class="text-center">{{ row.schools_cnt }}</td>
<td class="text-center">{{ row.appforms_cnt }}</td>
</tr>
......@@ -22,6 +28,9 @@
<tfoot>
<tr>
<td></td>
{% if schools_total_global is defined %}
<th class="text-center">{{ schools_total_global }}</th>
{% endif %}
<th class="text-center">{{ totals.schools_total }}</th>
<th class="text-center">{{ totals.appforms_total }}</th>
</tr>
......
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