Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
STYLE: refs #0339. Add style tests to statistics module
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Nov 10, 2011
1 parent bf4909d commit 75bd3a9
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 92 deletions.
25 changes: 13 additions & 12 deletions modules/statistics/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@
class Statistics_Notification extends MIDAS_Notification
{
public $moduleName = 'statistics';
public $_moduleModels=array('Download');
public $_moduleComponents=array('Report');
public $_moduleModels = array('Download');
public $_moduleComponents = array('Report');

/** init notification process*/
public function init()
{
$this->addCallBack('CALLBACK_CORE_GET_FOOTER_LAYOUT', 'getFooter');
$this->addCallBack('CALLBACK_CORE_GET_USER_MENU', 'getUserMenu');
$this->addCallBack('CALLBACK_CORE_PLUS_ONE_DOWNLOAD', 'addDownload');
$this->addTask("TASK_STATISTICS_SEND_REPORT", 'sendReport', "Send a daily report");

$this->addTask('TASK_STATISTICS_SEND_REPORT', 'sendReport', 'Send a daily report');
}//end init


/** send the batch report to admins */
public function sendReport()
{
echo $this->ModuleComponent->Report->generate();
$this->ModuleComponent->Report->send();
}

/** add download stat*/
public function addDownload($params)
{
$item = $params['item'];
$user = $this->userSession->Dao;
$this->Statistics_Download->addDownload($item, $user);
}

/** user Menu link */
public function getUserMenu()
{
if($this->logged && $this->userSession->Dao->getAdmin()==1)
if($this->logged && $this->userSession->Dao->getAdmin() == 1)
{
$fc = Zend_Controller_Front::getInstance();
$moduleWebroot = $fc->getBaseUrl().'/statistics';
Expand All @@ -44,15 +45,15 @@ public function getUserMenu()
return null;
}
}

/** get layout footer */
public function getFooter()
{
$modulesConfig = Zend_Registry::get('configsModules');
$url = $modulesConfig['statistics']->piwik->url;
$id = $modulesConfig['statistics']->piwik->id;
return "
<!-- Piwik -->
<!-- Piwik -->
<script type=\"text/javascript\">
var pkBaseURL = '".$url."/';
document.write(unescape(\"%3Cscript src='\" + pkBaseURL + \"piwik.js' type='text/javascript'%3E%3C/script%3E\"));
Expand All @@ -64,7 +65,7 @@ public function getFooter()
} catch( err ) {}
</script><noscript><p><img src=\"".$url."/piwik.php?idsite=".$id."\" style=\"border:0\" alt=\"\" /></p></noscript>
<!-- End Piwik Tracking Code -->
";
";
}
} //end class
?>
Expand Down
2 changes: 1 addition & 1 deletion modules/statistics/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function indexAction()
$job->setTask('TASK_STATISTICS_SEND_REPORT');
$job->setPriority('1');
$job->setRunOnlyOnce(false);
$job->setFireTime(date('Y-m-j', strtotime ('+1 day'.date( 'Y-m-j G:i:s'))).' 1:00:00');
$job->setFireTime(date('Y-m-j', strtotime('+1 day'.date('Y-m-j G:i:s'))).' 1:00:00');
$job->setTimeInterval(24 * 60 * 60);
$job->setStatus(SCHEDULER_JOB_STATUS_TORUN);
$job->setParams(JsonComponent::encode(array()));
Expand Down
19 changes: 10 additions & 9 deletions modules/statistics/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

/** Index controller for the statistics module */
class Statistics_IndexController extends Statistics_AppController
{
public $_moduleModels=array('Download');
public $_models=array('Errorlog', 'Assetstore');
public $_moduleModels = array('Download');
public $_models = array('Errorlog', 'Assetstore');
public $_components = array('Utility');

/** index action*/
function indexAction()
{
if(!$this->logged||!$this->userSession->Dao->getAdmin()==1)
if(!$this->logged || !$this->userSession->Dao->getAdmin() == 1)
{
throw new Zend_Exception("You should be an administrator");
}
Expand All @@ -24,8 +25,8 @@ function indexAction()
$assetstores[$key]->totalSpace = disk_total_space($assetstore->getPath());
$assetstores[$key]->usedSpace = disk_total_space($assetstore->getPath()) - disk_free_space($assetstore->getPath());
$assetstores[$key]->freeSpace = disk_free_space($assetstore->getPath());
$assetstores[$key]->usedSpaceText = round(($assetstores[$key]->usedSpace / $assetstores[$key]->totalSpace)*100, 2) ;
$assetstores[$key]->freeSpaceText = round((disk_free_space($assetstore->getPath()) / $assetstores[$key]->totalSpace)*100, 2) ;
$assetstores[$key]->usedSpaceText = round(($assetstores[$key]->usedSpace / $assetstores[$key]->totalSpace) * 100, 2);
$assetstores[$key]->freeSpaceText = round((disk_free_space($assetstore->getPath()) / $assetstores[$key]->totalSpace) * 100, 2);
}
else
{
Expand All @@ -45,18 +46,18 @@ function indexAction()
);
}

$errors = $this->Errorlog->getLog(date( 'c', strtotime ('-20 day'.date( 'Y-m-j G:i:s'))), date('c'), 'all', 2);
$errors = $this->Errorlog->getLog(date('c', strtotime('-20 day'.date('Y-m-j G:i:s'))), date('c'), 'all', 2);
$arrayErrors = array();

$format = 'Y-m-j';
for($i = 0; $i<21; $i++)
for($i = 0; $i < 21; $i++)
{
$key = date($format, strtotime(date( 'c', strtotime ('-'.$i.' day'.date( 'Y-m-j G:i:s')))));
$key = date($format, strtotime(date('c', strtotime('-'.$i.' day'.date('Y-m-j G:i:s')))));
$arrayErrors[$key] = 0;
}
foreach($errors as $error)
{
$key = date($format, strtotime($error->getDatetime()));
$key = date($format, strtotime($error->getDatetime()));
$arrayErrors[$key]++;
}

Expand Down
11 changes: 5 additions & 6 deletions modules/statistics/controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
/** Controller for statistics about an item */
class Statistics_ItemController extends Statistics_AppController
{
public $_moduleModels = array('Download');
public $_models = array('Item');
public $_components = array('Utility');
public $_moduleModels = array('Download');
public $_models = array('Item');
public $_components = array('Utility');

/** index action*/
function indexAction()
/** index action*/
function indexAction()
{

$item = $this->Item->load($_GET['id']);
if(!$item)
{
Expand Down
68 changes: 36 additions & 32 deletions modules/statistics/controllers/components/ReportComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,71 @@
PURPOSE. See the above copyright notices for more information.
=========================================================================*/

/**
* Generates and sends piwik statistics reports to admin users
*/
class Statistics_ReportComponent extends AppComponent
{
/** geenrate report */
{
/** generate report */
public function generate()
{
$loader = new MIDAS_ModelLoader();
$errorModel = $loader->loadModel('Errorlog');
$assetstoreModel = $loader->loadModel('Assetstore');
$reportContent = '';
$reportContent .= "<b>Midas Report: ".Zend_Registry::get('configGlobal')->application->name."</b>";
$reportContent .= "<br/>http://".$_SERVER['SERVER_NAME'];
$reportContent .= "<br/><br/><b>Status</b>";
$errors = $errorModel->getLog(date( 'c', strtotime ('-1 day'.date( 'Y-m-j G:i:s'))), date('c'), 'all', 2);
$reportContent .= '<b>Midas Report: '.Zend_Registry::get('configGlobal')->application->name.'</b>';
$reportContent .= '<br/>http://'.$_SERVER['SERVER_NAME'];

$reportContent .= '<br/><br/><b>Status</b>';
$errors = $errorModel->getLog(date('c', strtotime('-1 day'.date('Y-m-j G:i:s'))), date('c'), 'all', 2);
$reportContent .= "<br/>Yesterday Errors: ".count($errors);
$assetstores = $assetstoreModel->getAll();
foreach($assetstores as $key => $assetstore)
{
$reportContent .= "<br/>Assetstore ".$assetstore->getName().", Free space: ".round((disk_free_space($assetstore->getPath()) / disk_total_space($assetstore->getPath()))*100, 2).'%';
$freeSpace = round((disk_free_space($assetstore->getPath()) / disk_total_space($assetstore->getPath())) * 100, 2);
$reportContent .= '<br/>Assetstore '.$assetstore->getName().', Free space: '.$freeSpace.'%';
}
$reportContent .= "<br/><br/><b>Dashboard</b><br/>";
$dashboard = Zend_Registry::get('notifier')->callback("CALLBACK_CORE_GET_DASHBOARD");

$reportContent .= '<br/><br/><b>Dashboard</b><br/>';
$dashboard = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_GET_DASHBOARD');
ksort($dashboard);
foreach($dashboard as $module => $dasboard)
{
$reportContent .= '-'.ucfirst($module);
$reportContent .= "<table>";
$reportContent .= '-'.ucfirst($module);
$reportContent .= '<table>';
foreach($dasboard as $name => $status)
{
$reportContent .= "<tr>";
$reportContent .= " <td>$name </td>";
$reportContent .= '<tr>';
$reportContent .= ' <td>'.$name.'</td>';
if($status)
{
$reportContent .= " <td>ok</td>";
$reportContent .= ' <td>ok</td>';
}
else
{
$reportContent .= " <td>Error</td>";
$reportContent .= ' <td>Error</td>';
}
if(isset($status[1]))
{
$reportContent .= " <td>$status[1]</td>";
$reportContent .= ' <td>'.$status[1].'</td>';
}
$reportContent .= "</tr>";
$reportContent .= '</tr>';
}
$reportContent .= "</table>";
$reportContent .= '</table>';
}

$modulesConfig = Zend_Registry::get('configsModules');
$content = file_get_contents($modulesConfig['statistics']->piwik->url.'/?module=API&format=json&method=VisitsSummary.get&period=day&date=yesterday&idSite='.$modulesConfig['statistics']->piwik->id.'&token_auth='.$modulesConfig['statistics']->piwik->apikey);
$piwik = json_decode($content);
$reportContent .= "<br/><b>Statistics (yesterday)</b>";
$reportContent .= "<br/>Number of visit: ".$piwik->nb_uniq_visitors;
$reportContent .= "<br/>Number of actions: ".$piwik->nb_actions;
$reportContent .= "<br/>Average time on the website: ".$piwik->avg_time_on_site;
$reportContent .= '<br/><b>Statistics (yesterday)</b>';
$reportContent .= '<br/>Number of visit: '.$piwik->nb_uniq_visitors;
$reportContent .= '<br/>Number of actions: '.$piwik->nb_actions;
$reportContent .= '<br/>Average time on the website: '.$piwik->avg_time_on_site;
$this->report = $reportContent;
return $reportContent;
}//end generate
/* send a report*/

/** send the report to admins */
public function send()
{
$loader = new MIDAS_ModelLoader();
Expand All @@ -79,14 +83,14 @@ public function send()
$mail->setBodyHtml($this->report);
$mail->setFrom('admin@foo.com', 'MIDAS');
$mail->setSubject('MIDAS Report');

$admins = $userModel->getAdmins();
foreach($admins as $admin)
{
$mail->addTo($admin->getEmail(), $admin->getFullName());
$mail->send();
}
}//end send
}
}//end send

} // end class
?>
?>
4 changes: 2 additions & 2 deletions modules/statistics/controllers/forms/ConfigForm.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/** Form for configuring the statistics module */
class Statistics_ConfigForm extends AppForm
{

/** create form */
public function createConfigForm()
{
Expand All @@ -24,4 +24,4 @@ public function createConfigForm()
}

} // end class
?>
?>
8 changes: 4 additions & 4 deletions modules/statistics/models/AppDao.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

/** Dao base class for the statistics module */
class Statistics_AppDao extends MIDAS_GlobalDao
{
public $_module='statistics';

} //end class
{
public $_module = 'statistics';
} //end class

?>
4 changes: 2 additions & 2 deletions modules/statistics/models/AppModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
PURPOSE. See the above copyright notices for more information.
=========================================================================*/

/** Base class for models in the statistics module */
class Statistics_AppModel extends MIDASModel
{
public $moduleName='statistics';

public $moduleName = 'statistics';
}
?>
30 changes: 15 additions & 15 deletions modules/statistics/models/base/DownloadBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
/** Download model base */
class Statistics_DownloadModelBase extends Statistics_AppModel
{

/** constructor */
public function __construct()
{
parent::__construct();
$this->_name = 'statistics_download';
$this->_key = 'job_id';

$this->_mainData= array(
'download_id'=> array('type'=>MIDAS_DATA),
'item_id'=> array('type'=>MIDAS_DATA),
'user_id'=> array('type'=>MIDAS_DATA),
'ip'=> array('type'=>MIDAS_DATA),
'date'=> array('type'=>MIDAS_DATA),
'latitude'=> array('type'=>MIDAS_DATA),
'longitude'=> array('type'=>MIDAS_DATA),
$this->_mainData = array(
'download_id' => array('type' => MIDAS_DATA),
'item_id' => array('type' => MIDAS_DATA),
'user_id' => array('type' => MIDAS_DATA),
'ip' => array('type' => MIDAS_DATA),
'date' => array('type' => MIDAS_DATA),
'latitude' => array('type' => MIDAS_DATA),
'longitude' => array('type' => MIDAS_DATA),
'item' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'Item', 'parent_column' => 'item_id', 'child_column' => 'item_id'),
'user' => array('type' => MIDAS_MANY_TO_ONE, 'model' => 'User', 'parent_column' => 'user_id', 'child_column' => 'user_id')
);
Expand Down Expand Up @@ -76,15 +76,15 @@ public function addDownload($item, $user)
private function _getGeolocation($ip)
{
if(function_exists('curl_init') == false)
{
$location['latitude'] = '';
$location['longitude'] = '';
return $location;
}
{
$location['latitude'] = '';
$location['longitude'] = '';
return $location;
}

$applicationConfig = parse_ini_file(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini', true);
$apiKey = $applicationConfig['global']['ipinfodb.apikey'];
$url = "http://api.ipinfodb.com/v3/ip-city/?key=$apiKey&ip=$ip&format=json";
$url = 'http://api.ipinfodb.com/v3/ip-city/?key='.$apiKey.'&ip='.$ip.'&format=json';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
Expand Down
8 changes: 4 additions & 4 deletions modules/statistics/models/dao/DownloadDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
PURPOSE. See the above copyright notices for more information.
=========================================================================*/

/** dao for the statistics_download model */
class Statistics_DownloadDao extends Statistics_AppDao
{
public $_model='Download';

}
{
public $_model = 'Download';
}
?>
Loading

0 comments on commit 75bd3a9

Please sign in to comment.