Commit 38f7d3a7 authored by sampaioprimo's avatar sampaioprimo

refactor of daily reports code

parent 500f97a5
......@@ -1908,6 +1908,10 @@ lib/core/Report/Builder.js -text
lib/core/Report/Builder.php -text
lib/core/Report/Definition/Logs.php -text
lib/core/Report/Definition/Tracker.php -text
lib/core/Reports/Cache.php -text
lib/core/Reports/Factory.php -text
lib/core/Reports/Send.php -text
lib/core/Reports/Users.php -text
lib/core/Request.php -text
lib/core/Search/ContentFilter/VersionNumber.php -text
lib/core/Search/ContentSource/ArticleSource.php -text
......@@ -3258,6 +3262,16 @@ lib/test/core/Perms/ResolverFactory/CategoryFactoryTest.php -text
lib/test/core/Perms/ResolverFactory/GlobalFactoryTest.php -text
lib/test/core/Perms/ResolverFactory/ObjectFactoryTest.php -text
lib/test/core/Perms/ResolverFactory/TestFactoryTest.php -text
lib/test/core/Reports/CacheTest.php -text
lib/test/core/Reports/SendTest.php -text
lib/test/core/Reports/UsersTest.php -text
lib/test/core/Reports/fixtures/reports_cache_dataset.xml -text
lib/test/core/Reports/fixtures/reports_cache_dataset_delete.xml -text
lib/test/core/Reports/fixtures/user_reports_dataset.xml -text
lib/test/core/Reports/fixtures/user_reports_dataset_delete.xml -text
lib/test/core/Reports/fixtures/user_reports_dataset_insert.xml -text
lib/test/core/Reports/fixtures/user_reports_dataset_update.xml -text
lib/test/core/Reports/fixtures/user_reports_dataset_update_last_report.xml -text
lib/test/core/Request/RequestTest.php -text
lib/test/core/Search/Expr/ParserTest.php -text
lib/test/core/Search/Expr/TokenizerTest.php -text
......
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
/**
* @package Tiki
* @subpackage Reports
*
* Manage the cache of changes to send to users
* in a period report.
*/
class Reports_Cache
{
/**
* @var TikiDb
*/
protected $db;
protected $table;
/**
* @param TikiDb $db
* @return null
*/
public function __construct(TikiDb $db)
{
$this->db = $db;
$this->table = $db->table('tiki_user_reports_cache');
}
/**
* Return cache entries for a given user.
* @param string $user
* @return array
*/
public function get($user)
{
$entries = $this->table->fetchAll(array('user', 'event', 'data', 'time'), array('user' => $user), -1, -1, 'time ASC');
$ret = array();
foreach ($entries as $entry) {
$entry['data'] = unserialize($entry['data']);
$ret[] = $entry;
}
return $ret;
}
/**
* Delete all cache entries for a given user.
*
* @param string $user
* @return null
*/
public function delete($user)
{
$this->db->table('tiki_user_reports_cache')->deleteMultiple(array('user' => $user));
}
}
\ No newline at end of file
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
/**
* @package Tiki
* @subpackage Reports
*
* Factory for reports classes
*/
class Reports_Factory
{
static public function build($className)
{
$db = TikiDb::get();
switch($className) {
case 'Reports_Users':
return new Reports_Users($db, new DateTime, new Reports_Cache($db));
case 'Reports_Cache':
return new Reports_Cache($db);
}
}
}
\ No newline at end of file
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
/**
* @package Tiki
* @subpackage Reports
*
* Send e-mail reports to users with changes in Tiki
* in a given period of time.
*/
class Reports_Send
{
protected $db;
/**
* @param TikiDb $db
* @return null
*/
public function __construct(TikiDb $db)
{
$this->db = $db;
}
}
\ No newline at end of file
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
/**
* @package Tiki
* @subpackage Reports
*
* Manage users preferences regarding periodic
* reports of what have changed in Tiki.
*/
class Reports_Users
{
/**
* @var TikiDb
*/
protected $db;
protected $table;
/**
* @var DateTime
*/
protected $dt;
/**
* @var Reports_Cache
*/
protected $reportsCache;
/**
* @param TikiDb $db
* @param Reports_Cache $reportsCache
* @return null
*/
public function __construct(TikiDb $db, DateTime $dt, Reports_Cache $reportsCache)
{
$this->db = $db;
$this->table = $db->table('tiki_user_reports');
$this->dt = $dt;
$this->reportsCache = $reportsCache;
}
/**
* Return the preferences for receiving the reports
* for a given user.
*
* @param string $user
* @return array
*/
public function get($user)
{
return $this->table->fetchRow(
array('id', 'interval', 'view', 'type', 'always_email', 'last_report'),
array('user' => $user)
);
}
/**
* Remove user preferences for reports and
* the changes cache for this user.
*
* @param $user
* @return null
*/
public function delete($user)
{
$this->table->deleteMultiple(array('user' => $user));
$this->reportsCache->delete($user);
}
/**
* Add or update user preferences regarding receiving periodic
* reports with changes in Tiki.
*
* @param string $user
* @param string $interval report interval (can be 'daily', 'weekly' and 'monthly')
* @param string $view
* @param string $type whether the report should be send in plain text or html
* @param bool $always_email if true the user will receive an e-mail even if there are no changes
* @return null
*/
public function save($user, $interval, $view, $type, $always_email = 0)
{
$this->db->query("set time_zone='+00:00'");
if (!$this->get($user)) {
$this->table->insert(array(
'user' => $user, 'interval' => $interval, 'view' => $view, 'type' => $type,
'always_email' => $always_email, 'last_report' => $this->dt->format('Y-m-d H:i:s')
));
} else {
$this->table->update(
array('interval' => $interval, 'view' => $view, 'type' => $type, 'always_email' => $always_email),
array('user' => $user)
);
}
}
/**
* Called by event tiki.user.create when feature
* dailyreports_enabled_for_new_users is enabled.
*
* @param $context
* @return null
*/
public function addUserToDailyReports($context)
{
$this->save($context['user'], 'daily', 'detailed', 'html', 1);
}
/**
* Return a list of users that should receive the report.
* @return array
*/
public function getUsersForReport()
{
$users = $this->db->fetchAll('select `user`, `interval`, UNIX_TIMESTAMP(`last_report`) as last_report from tiki_user_reports');
$ret = array();
foreach ($users as $user) {
if ($user['interval'] == "daily" && ($user['last_report'] + 86400) <= $this->dt->getTimestamp()) {
$ret[] = $user['user'];
}
if ($user['interval'] == "weekly" && ($user['last_report'] + 604800) <= $this->dt->getTimestamp()) {
$ret[] = $user['user'];
}
if ($user['interval'] == "monthly" && ($user['last_report'] + 2419200) <= $this->dt->getTimestamp()) {
$ret[] = $user['user'];
}
}
return $ret;
}
/**
* Update date and time of last report sent
* to the user.
* @param strin $user
* @return null
*/
function updateLastReport($user)
{
$this->table->update(
array('last_report' => $this->dt->format('Y-m-d H:i:s')),
array('user' => $user)
);
}
}
\ No newline at end of file
......@@ -271,71 +271,6 @@ class reportsLib extends TikiLib
}
}
function add_user_to_daily_reports($context) {
$this->add_user_report($context['user'], 'daily', 'detailed', 'html', 1);
}
function add_user_report($user, $interval, $view, $type, $always_email) {
if (!isset($always_email)) {
$always_email = 0;
}
if (!$this->get_report_preferences_by_user($user)) {
//Add new report entry
$query = "insert into `tiki_user_reports`(`user`, `interval`, `view`, `type`, `always_email`, `last_report`) ";
$query.= "values(?,?,?,?,?,NOW())";
$this->query($query,array($user,$interval,$view,$type,$always_email));
} else {
//Update report entry
$query = "update `tiki_user_reports` set `interval`=?, `view`=?, `type`=?, `always_email`=? where `user`=?";
$this->query($query,array($interval,$view,$type,$always_email,$user));
}
return true;
}
function delete_user_report($user) {
$query = "delete from `tiki_user_reports` where `user`=?";
$this->query($query,array($user));
$this->deleteUsersReportCache($user);
return true;
}
function get_report_preferences_by_user($user) {
$query = "select `id`, `interval`, `view`, `type`, `always_email`, `last_report` from `tiki_user_reports` where `user` = ?";
$result = $this->query($query, array($user));
if (!$result->numRows()) {
return false;
}
$ret = array();
while ($res = $result->fetchRow()) {
$ret = $res;
}
return $ret;
}
function getUsersForSendingReport() {
$query = "select `user`, `interval`, UNIX_TIMESTAMP(`last_report`) as last_report from tiki_user_reports";
$result = $this->query($query);
if (!$result->numRows()) {
return false;
}
$ret = array();
while ($res = $result->fetchRow()) {
if ($res['interval']=="daily" AND ($res['last_report']+86400)<=time()) {
$ret[] = $res['user'];
}
if ($res['interval']=="weekly" AND ($res['last_report']+604800)<=time()) {
$ret[] = $res['user'];
}
if ($res['interval']=="monthly" AND ($res['last_report']+2419200)<=time()) {
$ret[] = $res['user'];
}
}
return $ret;
}
function makeReportCache(&$nots, $cache_data) {
//Get all users that have enabled reports
$query = "select `user` from tiki_user_reports";
......@@ -358,33 +293,6 @@ class reportsLib extends TikiLib
}
}
}
function get_report_cache_entries_by_user($user, $order_by) {
$query = "select `user`, `event`, `data`, `time` from `tiki_user_reports_cache` where `user` = ? ORDER BY $order_by";
$result = $this->query($query, array($user));
if (!$result->numRows()) {
return false;
}
$ret = array();
while ($res = $result->fetchRow()) {
$res['data'] = unserialize($res['data']);
$ret[] = $res;
}
return $ret;
}
function deleteUsersReportCache($user) {
$query = "delete from `tiki_user_reports_cache` where `user`=?";
$this->query($query,array($user));
return true;
}
function updateLastSent($user) {
$query = "update `tiki_user_reports` set last_report = NOW() where `user`=?";
$this->query($query,array($user));
return true;
}
}
global $reportslib;
......
......@@ -69,7 +69,7 @@ if ($prefs['feature_file_galleries'] == 'y') {
}
if ($prefs['dailyreports_enabled_for_new_users'] == 'y') {
$events->bind('tiki.user.create', Event_Lib::defer('reports', 'add_user_to_daily_reports'));
$events->bind('tiki.user.create', array(Reports_Factory::build('Reports_Users'), 'addUserToDailyReports'));
}
if ($prefs['scorm_enabled'] == 'y') {
......
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
class Reports_CacheTest extends TikiDatabaseTestCase
{
protected $obj;
protected function setUp()
{
$db = TikiDb::get();
$this->obj = new Reports_Cache($db);
parent::setUp();
}
public function getDataSet()
{
return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/fixtures/reports_cache_dataset.xml');
}
public function testDelete_shouldDeleteAllEntriesForAUser()
{
$expectedTable = $this->createMySQLXmlDataSet(dirname(__FILE__) . '/fixtures/reports_cache_dataset_delete.xml')
->getTable('tiki_user_reports_cache');
$this->obj->delete('admin');
$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports_cache', 'SELECT * FROM tiki_user_reports_cache');
$this->assertTablesEqual($expectedTable, $queryTable);
}
public function testGet_shouldReturnEntriesForGivenUser()
{
$expectedResult = array(array('user' => 'test', 'event' => 'wiki_page_changed',
'data' =>
array(
'event' => 'wiki_page_changed', 'pageName' => 'test', 'object' => 'test',
'editUser' => 'test', 'editComment' => '', 'oldVer' => 2
),
'time' => '2012-01-19 16:23:30'
));
$entries = $this->obj->get('test');
$this->assertEquals($expectedResult, $entries);
}
}
\ No newline at end of file
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
class Reports_SendTest extends TikiDatabaseTestCase
{
protected $obj;
protected $dt;
protected function setUp()
{
$db = TikiDb::get();
$this->obj = new Reports_Send($db);
$this->dt = new DateTime;
$this->dt->setTimestamp('1326909909');
parent::setUp();
}
public function getDataSet()
{
return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset.xml');
}
public function test()
{
}
}
\ No newline at end of file
<?php
// (c) Copyright 2002-2012 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
class Reports_UsersTest extends TikiDatabaseTestCase
{
protected $obj;
protected $db;
protected $dt;
protected $reportsCache;
protected function setUp()
{
$this->db = TikiDb::get();
$this->reportsCache = $this->getMock('Reports_Cache', array('delete'), array($this->db));
$this->dt = new DateTime();
$this->dt->setTimezone(new DateTimeZone('UTC'));
$this->dt->setTimestamp('1326734528');
$this->obj = new Reports_Users($this->db, $this->dt, $this->reportsCache);
parent::setUp();
}
public function getDataSet()
{
return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset.xml');
}
public function testDelete_shouldDeleteUserReportsPreferencesAndCacheEntries()
{
$user = 'admin';
$expectedTable = $this->createMySQLXmlDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset_delete.xml')
->getTable('tiki_user_reports');
$this->reportsCache->expects($this->once())->method('delete')->with($user);
$this->obj->delete($user);
$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports', 'SELECT * FROM tiki_user_reports');
$this->assertTablesEqual($expectedTable, $queryTable);
}
public function testGet_shouldReturnEmptyIfUserIsNotUsingReports()
{
$this->assertEmpty($this->obj->get('someuserNotUsingReports'));
}
public function testGet_shouldReturnUsersReportsPreferences()
{
$expectedResult = array('id' => 2, 'interval' => 'daily', 'view' => 'detailed', 'type' => 'html',
'always_email' => 1, 'last_report' => '2012-01-15 12:22:08');
$this->assertEquals($expectedResult, $this->obj->get('test'));
}
public function testSave_shouldInsertData()
{
$expectedTable = $this->createMySQLXmlDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset_insert.xml')
->getTable('tiki_user_reports');
$this->obj->save('newUser', 'weekly', 'detailed', 'html', 1);
$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports', 'SELECT * FROM tiki_user_reports');
$this->assertTablesEqual($expectedTable, $queryTable);
}
public function testSave_shouldUpdateData()
{
$expectedTable = $this->createMySQLXmlDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset_update.xml')
->getTable('tiki_user_reports');
$this->obj->save('test', 'weekly', 'detailed', 'html', 1);
$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports', 'SELECT * FROM tiki_user_reports');
$this->assertTablesEqual($expectedTable, $queryTable);
}
public function testAddUserToDailyReport_shouldCallSave()
{
$obj = $this->getMock('Reports_Users', array('save'), array(), 'Mock_Reports_Users', false);
$obj->expects($this->once())->method('save')->with('test', 'daily', 'detailed', 'html', 1);
$obj->addUserToDailyReports(array('user' => 'test'));
}
public function testGetUsers_shouldReturnArrayWithUsers()
{
$expectedResult = array('test');
$users = $this->obj->getUsersForReport();
$this->assertEquals($expectedResult, $users);
}
public function testUpdateLastReport_shouldUpdateLastReportField()
{
$expectedTable = $this->createMySQLXmlDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset_update_last_report.xml')
->getTable('tiki_user_reports');
$this->dt->setTimestamp('1326896528');
$obj = new Reports_Users($this->db, $this->dt, $this->reportsCache);
$obj->updateLastReport('test');
$queryTable = $this->getConnection()->createQueryTable('tiki_user_reports', 'SELECT * FROM tiki_user_reports');
$this->assertTablesEqual($expectedTable, $queryTable);
}
}
\ No newline at end of file
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="tiki9">
<table_data name="tiki_user_reports_cache">
<row>
<field name="id">1</field>
<field name="user">admin</field>
<field name="event">wiki_page_changed</field>
<field name="data">a:6:{s:5:&quot;event&quot;;s:17:&quot;wiki_page_changed&quot;;s:8:&quot;pageName&quot;;s:8:&quot;HomePage&quot;;s:6:&quot;object&quot;;s:8:&quot;HomePage&quot;;s:8:&quot;editUser&quot;;s:4:&quot;test&quot;;s:11:&quot;editComment&quot;;s:0:&quot;&quot;;s:6:&quot;oldVer&quot;;s:1:&quot;2&quot;;}</field>
<field name="time">2012-01-19 16:22:59</field>
</row>
<row>
<field name="id">2</field>
<field name="user">admin</field>
<field name="event">wiki_page_changed</field>
<field name="data">a:6:{s:5:&quot;event&quot;;s:17:&quot;wiki_page_changed&quot;;s:8:&quot;pageName&quot;;s:4:&quot;test&quot;;s:6:&quot;object&quot;;s:4:&quot;test&quot;;s:8:&quot;editUser&quot;;s:4:&quot;test&quot;;s:11:&quot;editComment&quot;;s:0:&quot;&quot;;s:6:&quot;oldVer&quot;;s:1:&quot;2&quot;;}</field>
<field name="time">2012-01-19 16:23:30</field>
</row>
<row>
<field name="id">3</field>
<field name="user">test</field>
<field name="event">wiki_page_changed</field>
<field name="data">a:6:{s:5:&quot;event&quot;;s:17:&quot;wiki_page_changed&quot;;s:8:&quot;pageName&quot;;s:4:&quot;test&quot;;s:6:&quot;object&quot;;s:4:&quot;test&quot;;s:8:&quot;editUser&quot;;s:4:&quot;test&quot;;s:11:&quot;editComment&quot;;s:0:&quot;&quot;;s:6:&quot;oldVer&quot;;s:1:&quot;2&quot;;}</field>
<field name="time">2012-01-19 16:23:30</field>
</row>
</table_data>
</database>
</mysqldump>
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="tiki9">
<table_data name="tiki_user_reports_cache">
<row>
<field name="id">3</field>
<field name="user">test</field>
<field name="event">wiki_page_changed</field>
<field name="data">a:6:{s:5:&quot;event&quot;;s:17:&quot;wiki_page_changed&quot;;s:8:&quot;pageName&quot;;s:4:&quot;test&quot;;s:6:&quot;object&quot;;s:4:&quot;test&quot;;s:8:&quot;editUser&quot;;s:4:&quot;test&quot;;s:11:&quot;editComment&quot;;s:0:&quot;&quot;;s:6:&quot;oldVer&quot;;s:1:&quot;2&quot;;}</field>
<field name="time">2012-01-19 16:23:30</field>
</row>
</table_data>
</database>
</mysqldump>
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="tiki9">
<table_data name="tiki_user_reports">
<row>
<field name="id">1</field>
<field name="user">admin</field>
<field name="interval">daily</field>
<field name="view">detailed</field>
<field name="type">html</field>
<field name="always_email">1</field>
<field name="last_report">2012-01-18 15:22:08</field>
</row>
<row>
<field name="id">2</field>
<field name="user">test</field>
<field name="interval">daily</field>
<field name="view">detailed</field>
<field name="type">html</field>
<field name="always_email">1</field>
<field name="last_report">2012-01-15 12:22:08</field>
</row>
</table_data>
</database>
</mysqldump>
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="tiki9">
<table_data name="tiki_user_reports">
<row>
<field name="id">2</field>
<field name="user">test</field>
<field name="interval">daily</field>
<field name="view">detailed</field>
<field name="type">html</field>
<field name="always_email">1</field>
<field name="last_report">2012-01-15 12:22:08</field>
</row>
</table_data>
</database>
</mysqldump>
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="tiki9">
<table_data name="tiki_user_reports">
<row>