View source
<?php
function casetracker_dashboard_help($section) {
switch ($section) {
case 'casetracker/dashboard':
return '<p>' . t('Overview of number of cases for each project') . '</p>';
break;
case 'admin/content/casetracker/dashboard':
return '<p>' . t('Adminster dashboard') . '</p>';
break;
}
}
function casetracker_dashboard_perm() {
return array(
'access case tracker dashboard',
);
}
function casetracker_dashboard_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array(
'access' => user_access('access case tracker dashboard'),
'callback' => 'casetracker_dashboard_overview',
'path' => 'casetracker/dashboard',
'title' => t('Dashboard'),
'weight' => 2,
);
$items[] = array(
'access' => user_access('administer case tracker'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'casetracker_dashboard_admin_form',
),
'description' => t('Configure the Case Tracker dashboard options with these settings.'),
'path' => 'admin/settings/casetracker/dashboard',
'title' => t('Dashboard'),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function casetracker_dashboard_overview() {
drupal_set_breadcrumb(array(
l(t('Home'), NULL),
l(t('Case Tracker'), 'casetracker'),
l(t('Dashboard'), 'casetracker/dashboard'),
));
drupal_add_css(drupal_get_path('module', 'casetracker') . '/casetracker.css');
$output = array();
$output[] = _casetracker_dashboard_states();
return implode("\n\n", $output);
}
function casetracker_dashboard_admin_form() {
$form = array();
$form['casetracker_dashboard_admin'] = array(
'#type' => 'fieldset',
'#title' => t('Dashboard settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => t('Assign the case status that need to be counted as Done'),
);
$states = casetracker_case_state_load('status');
$statesChecked = variable_get('casetracker_dashboard_states_done', array());
foreach ($states as $csid => $title) {
$checked = isset($statesChecked[$csid]) ? $statesChecked[$csid] : false;
$form['casetracker_dashboard_admin'][$csid] = array(
'#type' => 'checkbox',
'#title' => check_plain(t($title)),
'#default_value' => $checked,
);
}
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
function casetracker_dashboard_admin_form_submit($_formId, $_formValues) {
if ($_formValues['op'] == t('Save configuration')) {
$states = casetracker_case_state_load('status');
$settings = array();
foreach ($states as $csid => $title) {
$settings[$csid] = isset($_formValues[$csid]) ? (bool) $_formValues[$csid] : false;
}
variable_set('casetracker_dashboard_states_done', $settings);
drupal_set_message(t('The configuration options have been saved.'));
}
}
function _casetracker_dashboard_states() {
$output = array();
$projects = _casetracker_dashboard_getProjects();
$states = casetracker_case_state_load('status');
$caseCount = _casetracker_dashboard_projectCasesCount();
$projectCaseCount = _casetracker_dashboard_createProjectCountArray($projects, $states, $caseCount);
$headers = array(
array(
'data' => t('Project'),
'class' => 'project',
),
);
foreach ($states as $state) {
$headers[] = check_plain(t($state));
}
$headers[] = array(
'data' => t('Todo'),
'class' => 'devider',
);
$headers[] = t('Done');
$headers[] = array(
'data' => t('Total'),
'class' => 'devider',
);
$headers[] = array(
'data' => t('Completed<br />%'),
'class' => 'percent',
);
$output[] = theme('table', $headers, $projectCaseCount, array(
'id' => 'casetracker-dashboard',
));
return implode("\n", $output);
}
function _casetracker_dashboard_getProjects() {
static $projects;
if (!is_array($projects)) {
$filter_sql = NULL;
$filter_args = array_filter(variable_get('casetracker_project_node_types', array(
'casetracker_basic_project',
)));
if ($project_filter == 'my') {
global $user;
$filter_sql = 'AND n.uid = %d';
$filter_args[] = $user->uid;
}
$sql = db_rewrite_sql('SELECT n.nid, n.title, cp.project_number ' . 'FROM {node} n ' . 'LEFT JOIN {casetracker_project} cp ON (n.vid = cp.vid) ' . 'WHERE n.type ' . 'IN (\'' . implode('\',\'', $filter_args) . '\') ' . 'AND n.status = 1 ' . $filter_sql);
$sql .= 'ORDER BY n.title ASC';
$results = db_query($sql);
$projects = array();
while ($row = db_fetch_object($results)) {
$projects[$row->nid] = array(
'project_number' => $row->project_number,
'title' => $row->title,
);
}
}
return $projects;
}
function _casetracker_dashboard_projectCasesCount() {
$filter_args = array_filter(variable_get('casetracker_project_node_types', array(
'casetracker_basic_project',
)));
$sql = db_rewrite_sql('SELECT ' . ' n.nid, c.csid, Count(1) AS number_of_cases ' . 'FROM ' . ' {node} AS n ' . ' Inner Join {casetracker_case} AS b ON (n.nid = b.pid) ' . ' Inner Join {node} AS d ON (b.vid = d.vid)' . ' Inner Join {casetracker_case_states} AS c ON (b.case_status_id = c.csid) ' . 'WHERE ' . ' n.type IN (\'' . implode('\',\'', $filter_args) . '\') ' . ' AND n.status = 1 ');
$sql .= 'GROUP BY n.nid, c.csid ';
$sql .= 'ORDER BY n.nid ASC, c.csid ASC ';
$results = db_query($sql);
$caseCounter = array();
while ($row = db_fetch_object($results)) {
$caseCounter[] = $row;
}
return $caseCounter;
}
function _casetracker_dashboard_createProjectCountArray($_projects, $_states, $_caseCount) {
$overview = array();
$info = array();
$completedKeys = array();
$statesDone = variable_get('casetracker_dashboard_states_done', array());
foreach ($statesDone as $key => $done) {
if (true === $done) {
$completedKeys[] = $key;
}
}
$totals = array(
'project' => array(
'data' => t('Total'),
'class' => 'project',
),
);
foreach ($_states as $key => $value) {
$totals['state_' . $key] = $info['state_' . $key] = array(
'data' => 0,
);
}
$totals['todo'] = $info['todo'] = array(
'data' => 0,
'class' => 'devider',
);
$totals['done'] = $info['done'] = array(
'data' => 0,
);
$totals['total'] = $info['total'] = array(
'data' => 0,
'class' => 'devider',
);
$totals['completed'] = $info['completed'] = array(
'data' => 0,
'class' => 'percent',
);
foreach ($_projects as $key => $project) {
$link = l($project['title'], 'casetracker/cases/' . $key . '/all');
$overview[$key]['project'] = array(
'data' => $link,
'class' => 'project',
);
$overview[$key] = array_merge($overview[$key], $info);
}
foreach ($_caseCount as $count) {
$nid = (int) $count->nid;
$csid = (int) $count->csid;
$overview[$nid]['state_' . $csid]['data'] = (int) $count->number_of_cases;
$totals['state_' . $csid]['data'] += (int) $count->number_of_cases;
if (in_array($csid, $completedKeys)) {
$overview[$nid]['done']['data'] += (int) $count->number_of_cases;
$totals['done']['data'] += (int) $count->number_of_cases;
}
else {
$overview[$nid]['todo']['data'] += (int) $count->number_of_cases;
$totals['todo']['data'] += (int) $count->number_of_cases;
}
$overview[$nid]['total']['data'] += (int) $count->number_of_cases;
$totals['total']['data'] += (int) $count->number_of_cases;
}
foreach ($overview as $key => $project) {
if (0 < $project['total']['data']) {
$overview[$key]['completed']['data'] = round($project['done']['data'] / $project['total']['data'] * 100);
}
else {
$overview[$key]['completed']['data'] = 100;
}
}
if (0 < $totals['todo'] && 0 < $totals['total']['data']) {
$totals['completed']['data'] = round($totals['done']['data'] / $totals['total']['data'] * 100);
}
else {
$totals['completed']['data'] = 100;
}
$overview['totals'] = array(
'data' => $totals,
'class' => 'totals',
);
return $overview;
}