Skip to content

Commit 99e64e7

Browse files
committed
#65 added analytic with filter range based on creation dates
1 parent bce136f commit 99e64e7

File tree

4 files changed

+149
-3
lines changed

4 files changed

+149
-3
lines changed

Diff for: Analytics/CustomFieldAnalytics.php

+60-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Kanboard\Core\Base;
66
use Kanboard\Model\TaskMetadataModel;
7+
use Kanboard\Model\TaskModel;
78
use Kanboard\Model\TaskFinderModel;
89
use Kanboard\Plugin\MetaMagik\Model\MetadataTypeModel;
910

@@ -16,7 +17,7 @@
1617
class CustomFieldAnalytics extends Base
1718
{
1819

19-
public function build($project_id)
20+
public function build_norange($project_id)
2021
{
2122
$metrics = array();
2223
$total = 0;
@@ -54,4 +55,62 @@ public function build($project_id)
5455

5556
return $metrics;
5657
}
58+
59+
public function build_range($project_id, $from, $to)
60+
{
61+
$metrics = array();
62+
$total = 0;
63+
$fields = $this->metadataTypeModel->getAllInScope($project_id);
64+
$tasks = $this->getTasks($project_id, $from, $to);
65+
66+
foreach ($fields as $field) {
67+
68+
$field_total = 0;
69+
70+
foreach ($tasks as $task) {
71+
if (!empty($this->taskMetadataModel->get($task['id'], $field['human_name'], '')) && $field['data_type'] === 'number' ) {
72+
$field_total += $this->taskMetadataModel->get($task['id'], $field['human_name'], '');
73+
$total += $this->taskMetadataModel->get($task['id'], $field['human_name'], '');
74+
}
75+
76+
}
77+
78+
if ($field_total !== 0) {
79+
$metrics[] = array(
80+
'column_title' => $field['human_name'],
81+
'nb_tasks' => $field_total,
82+
);
83+
}
84+
85+
}
86+
87+
if ($total === 0) {
88+
return array();
89+
}
90+
91+
foreach ($metrics as &$metric) {
92+
$metric['percentage'] = round(($metric['nb_tasks'] * 100) / $total, 2);
93+
}
94+
95+
return $metrics;
96+
}
97+
98+
/**
99+
* Get date range of tasks created
100+
*
101+
* @access private
102+
* @param integer $project_id
103+
* @return array
104+
*/
105+
private function getTasks($project_id, $from, $to)
106+
{
107+
return $this->db
108+
->table(TaskModel::TABLE)
109+
->eq('project_id', $project_id)
110+
->gte('date_creation', strtotime($from))
111+
->lte('date_creation', strtotime($to))
112+
->asc('id')
113+
->findAll();
114+
}
115+
57116
}

Diff for: Controller/AnalyticExtensionController.php

+38-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,45 @@ public function fieldTotalDistribution()
2727

2828
$this->response->html($this->helper->layout->analytic('metaMagik:analytic/custom_field_totals', array(
2929
'project' => $project,
30-
'metrics' => $this->customFieldAnalytics->build($project['id']),
30+
'metrics' => $this->customFieldAnalytics->build_norange($project['id']),
3131
'title' => t('Custom Field Total distribution'),
3232
)));
3333
}
34+
/**
35+
* Show custom field values as distribution graph with range
36+
*
37+
* @access public
38+
*/
39+
public function fieldTotalDistributionRange()
40+
{
41+
42+
$project = $this->getProject();
43+
list($from, $to) = $this->getDates();
44+
45+
$this->response->html($this->helper->layout->analytic('metaMagik:analytic/custom_field_totals_range', array(
46+
'values' => array(
47+
'from' => $from,
48+
'to' => $to,
49+
),
50+
'project' => $project,
51+
'metrics' => $this->customFieldAnalytics->build_range($project['id'], $from, $to),
52+
'title' => t('Custom Field Total distribution'),
53+
)));
54+
}
55+
56+
private function getDates()
57+
{
58+
$values = $this->request->getValues();
59+
60+
$from = $this->request->getStringParam('from', date('Y-m-d', strtotime('-1week')));
61+
$to = $this->request->getStringParam('to', date('Y-m-d'));
62+
63+
if (! empty($values)) {
64+
$from = $this->dateParser->getIsoDate($values['from']);
65+
$to = $this->dateParser->getIsoDate($values['to']);
66+
}
67+
68+
return array($from, $to);
69+
}
70+
3471
}

Diff for: Template/analytic/custom_field_totals_range.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php if (! $is_ajax): ?>
2+
<div class="page-header">
3+
<h2><?= t('Custom Field Total distribution') ?></h2>
4+
</div>
5+
<?php endif ?>
6+
7+
8+
<?php if (empty($metrics)): ?>
9+
<p class="alert"><?= t('Not enough data to show the graph.') ?></p>
10+
<form method="post" class="form-inline" action="<?= $this->url->href('AnalyticExtensionController', 'fieldTotalDistributionRange', array('plugin' => 'metaMagik', 'project_id' => $project['id'])) ?>" autocomplete="off">
11+
<?= $this->form->csrf() ?>
12+
<?= $this->form->date(t('From creation date'), 'from', $values) ?>
13+
<?= $this->form->date(t('To creation date'), 'to', $values) ?>
14+
<?= $this->modal->submitButtons(array('submitLabel' => t('Execute'))) ?>
15+
</form>
16+
<?php else: ?>
17+
<?= $this->app->component('chart-project-task-distribution', array(
18+
'metrics' => $metrics,
19+
)) ?>
20+
<form method="post" class="form-inline" action="<?= $this->url->href('AnalyticExtensionController', 'fieldTotalDistributionRange', array('plugin' => 'metaMagik', 'project_id' => $project['id'])) ?>" autocomplete="off">
21+
<?= $this->form->csrf() ?>
22+
<?= $this->form->date(t('From creation date'), 'from', $values) ?>
23+
<?= $this->form->date(t('To creation date'), 'to', $values) ?>
24+
<?= $this->modal->submitButtons(array('submitLabel' => t('Execute'))) ?>
25+
</form>
26+
<table class="table-striped">
27+
<tr>
28+
<th><?= t('Custom Field') ?></th>
29+
<th><?= t('Total Value') ?></th>
30+
<th><?= t('Percentage') ?></th>
31+
</tr>
32+
<?php foreach ($metrics as $metric): ?>
33+
<tr>
34+
<td>
35+
<?= $this->text->e($metric['column_title']) ?>
36+
</td>
37+
<td>
38+
<?= $metric['nb_tasks'] ?>
39+
</td>
40+
<td>
41+
<?= n($metric['percentage']) ?>%
42+
</td>
43+
</tr>
44+
<?php endforeach ?>
45+
</table>
46+
47+
<?php endif ?>

Diff for: Template/analytic/layout_hook.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<li <?= $this->app->checkMenuSelection('AnalyticExtensionController', 'fieldTotalDistribution', 'metaMagik') ?>>
2-
<?= $this->modal->replaceLink(t('Custom field total distribution'), 'AnalyticExtensionController', 'fieldTotalDistribution', array('plugin' => 'metaMagik', 'project_id' => $project['id'])) ?>
2+
<?= $this->modal->replaceLink(t('Custom field total distribution for open tasks'), 'AnalyticExtensionController', 'fieldTotalDistribution', array('plugin' => 'metaMagik', 'project_id' => $project['id'])) ?>
3+
</li>
4+
<li <?= $this->app->checkMenuSelection('AnalyticExtensionController', 'fieldTotalDistributionRange', 'metaMagik') ?>>
5+
<?= $this->modal->replaceLink(t('Custom field total distribution with date range'), 'AnalyticExtensionController', 'fieldTotalDistributionRange', array('plugin' => 'metaMagik', 'project_id' => $project['id'])) ?>
36
</li>

0 commit comments

Comments
 (0)