dhis2-8.x-1.x-dev/dhis.module
dhis.module
<?php
/**
* @file
* Contains dhis.module.
*/
use Drupal\dhis\Entity\OrganisationUnit;
use Drupal\dhis\Entity\DataElement;
use Drupal\Core\Routing\RouteMatchInterface;
define("MONTHLY", 2592000);
//define("MONTHLY", 60); //for testing purposes only...to be removed and replaced by line 13 above
/**
* Implements hook_help().
*/
function dhis_help($route_name, RouteMatchInterface $route_match)
{
switch ($route_name) {
// Main module help for the dhis module.
case 'help.page.dhis':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('DHIS2 Module for connecting and retrieving metadata from a DHIS2 Instance') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function dhis_theme($existing, $type, $theme, $path)
{
return [
'dhis' => [
'template' => 'dhis',
'variables' => array('test_var' => NULL),
],
];
}
/**
* Implements hook_toolbar_alter().
*/
function dhis_toolbar_alter(&$items)
{
$items['administration']['#attached']['library'][] = 'dhis/toolbar';
}
/**
* Implements hook_cron().
*/
function dhis_cron()
{
$dhisConfig = \Drupal::service('config.factory')->getEditable('dhis.settings');
//->get('dhis.api_version')
$dhis_sync = $dhisConfig->get('dhis.auto_sync');
if ($dhis_sync != 0){
$dhisService = \Drupal::service("dhis_service");
$dhis_analytics = \Drupal::service('dhis_analytics');
$dx = $dhisService->getDimensions('data_element');
$ou = $dhisService->getDimensions('organisation_unit');
$pe = $dhisService->getDimensions('taxonomy_term');
switch($dhis_sync){
case 1:
$next_quarterly_execution = \Drupal::state()->get('dhis.monthly_next_execution');
$next_monthly_execution = !empty($next_monthly_execution) ? $next_monthly_execution : 0;
if (\Drupal::time()->getRequestTime() >= $next_monthly_execution){
$analyticsData = $dhis_analytics->generateAnalytics($dx, $ou, $pe);
$rows = $dhisService->analyticData($analyticsData);
if (count($rows) != 0) {
$dhisService->deleteContent();
$dhisService->createContent($rows);
}
\Drupal::state()->set('dhis.monthly_next_execution', \Drupal::time()->getRequestTime() + MONTHLY);
\Drupal::logger('dhis')->notice('Monthly sync completed.');
}
break;
case 2:
$next_quarterly_execution = \Drupal::state()->get('dhis.quarterly_next_execution');
$next_quarterly_execution = !empty($next_quarterly_execution) ? $next_quarterly_execution : 0;
if (\Drupal::time()->getRequestTime() >= $next_quarterly_execution){
//Delete all content types and pull new ones...
//Set site in maintenance mood
$analyticsData = $dhis_analytics->generateAnalytics($dx, $ou, $pe);
$rows = $dhisService->analyticData($analyticsData);
if (count($rows) != 0) {
$dhisService->deleteContent();
$dhisService->createContent($rows);
}
\Drupal::state()->set('dhis.quarterly_next_execution', \Drupal::time()->getRequestTime() + (MONTHLY * 3));
\Drupal::logger('dhis')->notice('Quarterly sync completed.');
}
break;
default:
}
}
}