Aggiunta gestione calcolo media su colonne
This commit is contained in:
parent
44a9355477
commit
540fe7b4d2
10
ajax.php
10
ajax.php
|
@ -167,5 +167,15 @@ switch (filter('op')) {
|
|||
|
||||
echo json_encode($results);
|
||||
|
||||
break;
|
||||
|
||||
case 'avg-results':
|
||||
$ids = post('ids') ?: [];
|
||||
$results = Util\Query::getAverages($structure, [
|
||||
'id' => $ids,
|
||||
]);
|
||||
|
||||
echo json_encode($results);
|
||||
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ $results = [
|
|||
'recordsTotal' => 0,
|
||||
'recordsFiltered' => 0,
|
||||
'summable' => [],
|
||||
'avg' => [],
|
||||
'draw' => $draw_numer,
|
||||
];
|
||||
|
||||
|
@ -76,6 +77,9 @@ if (!empty($query)) {
|
|||
// SOMME
|
||||
$results['summable'] = Query::getSums($structure, $search);
|
||||
|
||||
// MEDIE
|
||||
$results['avg'] = Query::getAverages($structure, $search);
|
||||
|
||||
// Allineamento delle righe
|
||||
$align = [];
|
||||
$row = isset($rows[0]) ? $rows[0] : [];
|
||||
|
|
|
@ -423,11 +423,17 @@ function footerCallback(row, data, start, end, display) {
|
|||
|
||||
this.api().columns().every(function () {
|
||||
if (json.summable[i] !== undefined) {
|
||||
$(this.footer()).addClass("text-right")
|
||||
$(this.footer()).css("text-align", "right")
|
||||
.attr("id", "summable")
|
||||
.html(json.summable[i]);
|
||||
}
|
||||
|
||||
if (json.avg[i] !== undefined) {
|
||||
$(this.footer()).css("text-align", "right")
|
||||
.attr("id", "avg")
|
||||
.html(json.avg[i]);
|
||||
}
|
||||
|
||||
i++;
|
||||
});
|
||||
}
|
||||
|
@ -532,7 +538,11 @@ function getTable(selector) {
|
|||
getSelectedRowsFooter: function () {
|
||||
let ids = this.getSelectedRows();
|
||||
|
||||
return $.ajax({
|
||||
let summable_results = [];
|
||||
let avg_results = [];
|
||||
let results = [];
|
||||
|
||||
summable_results = $.ajax({
|
||||
url: globals.rootdir + "/ajax.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
@ -543,10 +553,25 @@ function getTable(selector) {
|
|||
ids: ids,
|
||||
}
|
||||
});
|
||||
|
||||
avg_results = $.ajax({
|
||||
url: globals.rootdir + "/ajax.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: {
|
||||
id_module: this.id_module,
|
||||
id_plugin: this.id_plugin,
|
||||
op: "avg-results",
|
||||
ids: ids,
|
||||
}
|
||||
});
|
||||
|
||||
results = $.when(summable_results, avg_results);
|
||||
return results;
|
||||
},
|
||||
|
||||
/**
|
||||
* Aggiornamento dei campi summable
|
||||
* Aggiornamento dei campi summable e avg
|
||||
*/
|
||||
updateFooterForSelectedRows: function () {
|
||||
let datatable = this.datatable;
|
||||
|
@ -555,9 +580,12 @@ function getTable(selector) {
|
|||
for (let [column, value] of Object.entries(response)) {
|
||||
let index = parseInt(column) + 1;
|
||||
let sel = datatable.column(index).footer();
|
||||
$(sel).addClass("text-right")
|
||||
$(sel).css("text-align", "right")
|
||||
.attr("id", "summable")
|
||||
.html(value);
|
||||
$(sel).css("text-align", "right")
|
||||
.attr("id", "avg")
|
||||
.html(value);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
|
|
@ -241,7 +241,7 @@ function translateTemplate()
|
|||
|
||||
// Informazioni estese sulle azioni dell'utente
|
||||
$op = post('op');
|
||||
if (!empty($op) && $op != 'summable-results') {
|
||||
if (!empty($op) && $op != 'summable-results' && $op != 'avg-results') {
|
||||
OperationLog::setInfo('id_module', $id_module);
|
||||
OperationLog::setInfo('id_plugin', $id_plugin);
|
||||
OperationLog::setInfo('id_record', $id_record);
|
||||
|
|
|
@ -58,7 +58,8 @@ switch (filter('op')) {
|
|||
'slow' => post('slow')[$c],
|
||||
'format' => post('format')[$c],
|
||||
'html_format' => post('html_format')[$c],
|
||||
'summable' => post('sum')[$c],
|
||||
'summable' => post('sum-avg')[$c] === 'sum' ? 1 : 0,
|
||||
'avg' => post('sum-avg')[$c] === 'avg' ? 1 : 0,
|
||||
'search_inside' => post('search_inside')[$c],
|
||||
'order_by' => post('order_by')[$c],
|
||||
'id_module' => $id_record,
|
||||
|
|
|
@ -96,7 +96,7 @@ foreach ($fields as $key => $field) {
|
|||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
{[ "type": "checkbox", "label": "'.tr('Sommabile').'", "name": "sum['.$key.']", "value": "'.$field->summable.'", "help": "'.tr('Indica se il campo è da sommare').'" ]}
|
||||
{[ "type": "select", "label": "'.tr('Calcolo a fine colonna').'", "name": "sum-avg['.$key.']", "values": "list=\"0\":\"'.tr('Nessuno').'\",\"sum\":\"'.tr('Somma').'\",\"avg\":\"'.tr('Media').'\"", "value": "'.( $field->summable ? 'sum' : ($field->avg ? 'avg' : '' )).'", "help": "'.tr('Scegli quale tipo di totale valori vuoi calcolare a fine tabella').'" ]}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -210,17 +210,21 @@ echo '
|
|||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<div class="col-md-3">
|
||||
{[ "type": "checkbox", "label": "'.tr('Ricercabile').'", "name": "search[-id-]" ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<div class="col-md-3">
|
||||
{[ "type": "checkbox", "label": "'.tr('Ricerca lenta').'", "name": "slow[-id-]" ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<div class="col-md-3">
|
||||
{[ "type": "checkbox", "label": "'.tr('Sommabile').'", "name": "sum[-id-]" ]}
|
||||
</div>
|
||||
|
||||
<div class="col-md-3">
|
||||
{[ "type": "checkbox", "label": "'.tr('Media').'", "name": "avg[-id-]" ]}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
|
|
|
@ -350,6 +350,46 @@ class Query
|
|||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce la media dei valori dalla query prevista dalla struttura.
|
||||
*
|
||||
* @param array $search
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAverages($structure, $search = [])
|
||||
{
|
||||
$total = self::readQuery($structure);
|
||||
|
||||
// Calcolo di eventuali somme
|
||||
if (empty($total['avg'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result_query = self::getQuery($structure, $search);
|
||||
|
||||
// Filtri derivanti dai permessi (eventuali)
|
||||
if (empty($structure->originalModule)) {
|
||||
$result_query = \Modules::replaceAdditionals($structure->id, $result_query);
|
||||
}
|
||||
|
||||
$query = self::str_replace_once('SELECT', 'SELECT '.implode(', ', $total['avg']).' FROM(SELECT ', $result_query).') AS `z`';
|
||||
$avgs = database()->fetchOne($query);
|
||||
|
||||
$results = [];
|
||||
if (!empty($avgs)) {
|
||||
foreach ($avgs as $key => $avg) {
|
||||
if (string_contains($key, 'avg_')) {
|
||||
$results[str_replace('avg_', '', $key)] = \Translator::numberToLocale($avg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sostituisce la prima occorenza di una determinata stringa.
|
||||
*
|
||||
|
@ -383,6 +423,7 @@ class Query
|
|||
{
|
||||
$fields = [];
|
||||
$summable = [];
|
||||
$avg = [];
|
||||
$search_inside = [];
|
||||
$search = [];
|
||||
$format = [];
|
||||
|
@ -423,6 +464,10 @@ class Query
|
|||
if ($view['summable']) {
|
||||
$summable[] = 'SUM(`'.trim($view['name']."`) AS 'sum_".(count($fields) - 1)."'");
|
||||
}
|
||||
|
||||
if ($view['avg']) {
|
||||
$avg[] = 'AVG(`'.trim($view['name']."`) AS 'avg_".(count($fields) - 1)."'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -440,6 +485,7 @@ class Query
|
|||
'format' => $format,
|
||||
'html_format' => $html_format,
|
||||
'summable' => $summable,
|
||||
'avg' => $avg,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -74,4 +74,7 @@ UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_module
|
|||
INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`) VALUES
|
||||
((SELECT `id` FROM `zz_modules` WHERE `name` = 'Interventi'), 'Tags', 'tags.nomi', 10);
|
||||
INSERT INTO `zz_views_lang` (`id_lang`, `id_record`, `title`) VALUES
|
||||
(1, (SELECT MAX(`id`) FROM `zz_views` ), 'Tags');
|
||||
(1, (SELECT MAX(`id`) FROM `zz_views` ), 'Tags');
|
||||
|
||||
-- Aggiunta flag per calcolo media su viste
|
||||
ALTER TABLE `zz_views` ADD `avg` BOOLEAN NOT NULL DEFAULT FALSE AFTER `summable`;
|
||||
|
|
Loading…
Reference in New Issue