Commit 7b6a8447 authored by sampaioprimo's avatar sampaioprimo

daily reports: move remaining methods from reportslib.php to the new class structure

parent 05126d4e
......@@ -1915,6 +1915,7 @@ lib/core/Reports/Cache.php -text
lib/core/Reports/Factory.php -text
lib/core/Reports/Manager.php -text
lib/core/Reports/Send.php -text
lib/core/Reports/Send/BuildEmail.php -text
lib/core/Reports/Users.php -text
lib/core/Request.php -text
lib/core/Search/ContentFilter/VersionNumber.php -text
......@@ -2913,7 +2914,6 @@ lib/refererstats/refererlib.php -text
lib/registration/index.php -text
lib/registration/register_ajax.js -text
lib/registration/registrationlib.php -text
lib/reportslib.php -text
lib/rss/index.php -text
lib/rss/rsslib.php -text
lib/score/events.php -text
......@@ -3299,6 +3299,7 @@ lib/test/core/Perms/ResolverFactory/TestFactoryTest.php -text
lib/test/core/Reports/CacheTest.php -text
lib/test/core/Reports/FactoryTest.php -text
lib/test/core/Reports/ManagerTest.php -text
lib/test/core/Reports/Send/BuildEmailTest.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
......@@ -3434,7 +3435,6 @@ lib/test/phpunit.php -text
lib/test/phpunit.xml -text
lib/test/rating/AggregationTest.php -text
lib/test/rating/RegisterVoteTest.php -text
lib/test/reportslib/ReportsLibTest.php -text
lib/test/shipping/ShippingTest.php -text
lib/test/smarty_tiki/ModifierDurationTest.php -text
lib/test/smarty_tiki/index.php -text
......
......@@ -23,9 +23,15 @@ class Reports_Factory
case 'Reports_Cache':
return new Reports_Cache($db, new DateTime);
case 'Reports_Manager':
return new Reports_Manager(Reports_Factory::build('Reports_Users'), Reports_Factory::build('Reports_Cache'));
global $userlib;
return new Reports_Manager(Reports_Factory::build('Reports_Users'), Reports_Factory::build('Reports_Cache'),
Reports_Factory::build('Reports_Send'), $userlib);
case 'Reports_Send':
global $prefs;
require_once('lib/webmail/tikimaillib.php');
return new Reports_Send(new DateTime, new TikiMail);
default:
break;
throw new Exception("Unknown class $className");
}
}
}
\ No newline at end of file
......@@ -18,10 +18,42 @@ class Reports_Manager
protected $reportsCache;
public function __construct(Reports_Users $reportsUsers, Reports_Cache $reportsCache)
protected $reportsSend;
protected $userlib;
public function __construct(Reports_Users $reportsUsers, Reports_Cache $reportsCache, Reports_Send $reportsSend, UsersLib $userlib)
{
$this->reportsUsers = $reportsUsers;
$this->reportsCache = $reportsCache;
$this->reportsSend = $reportsSend;
$this->userlib = $userlib;
}
/**
* Send report to subscribed users.
* @return null
*/
public function send()
{
$users = $this->reportsUsers->getUsersForReport();
foreach ($users as $user) {
$userReportPreferences = $this->reportsUsers->get($user);
$userData = $this->userlib->get_user_info($user);
// if email address isn't set, do nothing but clear the cache
if (!empty($userData['email'])) {
$cache = $this->reportsCache->get($user);
if (!empty($cache) || $userReportPreferences['always_email']) {
$this->reportsSend->sendEmail($userData, $userReportPreferences, $cache);
$this->reportsUsers->updateLastReport($userData['login']);
}
}
$this->reportsCache->delete($userData['login']);
}
}
/**
......@@ -37,6 +69,20 @@ class Reports_Manager
$this->reportsCache->delete($user);
}
/**
* @see Reports_Users::save()
* @param string $user
* @param string $interval
* @param string $view
* @param string $type
* @param int $always_email
* @return null
*/
public function save($user, $interval, $view, $type, $always_email)
{
$this->reportsUsers->save($user, $interval, $view, $type, $always_email);
}
/**
* Add a new event to the periodic reports cache instead
* of sending an notification e-mail to the users.
......
......@@ -14,16 +14,53 @@
*/
class Reports_Send
{
protected $db;
protected $dt;
protected $mail;
/**
* @param TikiDb $db
* @param DateTime $dt
* @param TikiMail $mail
* @return null
*/
public function __construct(TikiDb $db)
public function __construct(DateTime $dt, TikiMail $mail)
{
$this->db = $db;
$this->dt = $dt;
$this->mail = $mail;
}
public function sendEmail($userData, $reportPreferences, $reportCache) {
$mailData = $this->buildEmailBody($userData, $reportPreferences, $reportCache);
$this->mail->setUser($userData['login']);
$this->setSubject($reportCache);
if ($reportPreferences['type'] == 'plain') {
$this->mail->setText($mailData);
} else {
$this->mail->setHtml(nl2br($mailData));
}
$this->mail->buildMessage();
$this->mail->send(array($userData['email']));
}
protected function setSubject($reportCache)
{
if (is_array($reportCache)) {
if (count($reportCache) == 1) {
$subject = tr('Report from %0 (1 change)',
TikiLib::date_format($this->tikiPrefs['short_date_format'], $this->dt->getTimestamp()));
} else {
$subject = tr('Report from %0 (%1 changes)',
TikiLib::date_format($this->tikiPrefs['short_date_format'], $this->dt->getTimestamp()), count($reportCache));
}
} else {
$subject = tr('Report from %0 (no changes)',
TikiLib::date_format($this->tikiPrefs['short_date_format'], $this->dt->getTimestamp()));
}
$this->mail->setSubject($subject);
}
}
\ No newline at end of file
<?php
<?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 reportsLib extends TikiLib
/**
* Build an periodic report e-mail with the changes
* in Tiki for the objects a user is watching.
*
* @package Tiki
* @subpackage Reports
*/
class Reports_Send_BuildEmail
{
//Sends the Email
public function sendEmail($user_data, $report_preferences, $report_cache) {
global $prefs, $smarty, $tikilib, $base_url;
include_once('lib/webmail/tikimaillib.php');
$mail = new TikiMail();
/**
* @param array $tikiPrefs list of Tiki preferences
* @return null
*/
public function __construct(array $tikiPrefs)
{
$this->tikiPrefs = $tikiPrefs;
}
protected function buildEmailBody($user_data, $report_preferences, $report_cache)
{
global $base_url;
$smarty = TikiLib::lib('smarty');
$tikilib = TikiLib::lib('tiki');
$smarty->assign('report_preferences', $report_preferences);
$smarty->assign('report_user', ucfirst($user_data['login']));
$smarty->assign('report_interval', ucfirst($report_preferences['interval']));
$smarty->assign('report_date', date("l d.m.Y"));
$smarty->assign('report_last_report_date', TikiLib::date_format($prefs['long_date_format'], strtotime($report_preferences['last_report'])));
$smarty->assign('report_last_report_date', TikiLib::date_format($this->tikiPrefs['long_date_format'], strtotime($report_preferences['last_report'])));
$smarty->assign('report_total_changes', count($report_cache));
if ($prefs['feature_contribution'] == 'y' && !empty($contributions)) {
global $contributionlib; include_once('lib/contribution/contributionlib.php');
$smarty->assign('mail_contributions', $contributionlib->print_contributions($contributions));
}
$smarty->assign('report_body', $this->makeHtmlEmailBody($report_cache, $report_preferences));
$userWatchesUrl = $base_url . 'tiki-user_watches.php';
......@@ -36,45 +47,11 @@ class reportsLib extends TikiLib
$smarty->assign('userWatchesUrl', $userWatchesUrl);
$mail->setUser($user_data['login']);
if (is_array($report_cache)) {
if (count($report_cache) == 1) {
$subject = tr('Report from %0 (1 change)', TikiLib::date_format($prefs['short_date_format'], time()));
} else {
$subject = tr('Report from %0 (%1 changes)', TikiLib::date_format($prefs['short_date_format'], time()), count($report_cache));
}
} else {
$subject = tr('Report from %0 (no changes)', TikiLib::date_format($prefs['short_date_format'], time()));
}
$mail->setSubject($subject);
$userlang = $tikilib->get_user_preference($user_data['login'], "language", $prefs['site_language']);
$userlang = $tikilib->get_user_preference($user_data['login'], "language", $this->tikiPrefs['site_language']);
$mail_data = $smarty->fetchLang($userlang, "mail/report.tpl");
if ($report_preferences['type']=='plain') {
$mail->setText($mail_data);
} else {
$mail->setHtml(nl2br($mail_data));
}
$mail->buildMessage();
$mail->send(array($user_data['email']));
return true;
}
//Makes time short
private function makeTime($time) {
if (date("d.m.Y", $time)==date("d.m.Y", time()-86400)) {
return tr("Yesterday %0", date("H:i", $time));
} elseif (date("d.m.Y", $time)==date("d.m.Y", time())) {
return tr("Today %0", date("H:i", $time));
} else {
return date("d.m.", $time)." ".date("H:i", $time);
}
return $mail_data;
}
/**
......@@ -270,7 +247,15 @@ class reportsLib extends TikiLib
return $body;
}
}
}
global $reportslib;
$reportslib = new reportsLib;
//Makes time short
private function makeTime($time) {
if (date("d.m.Y", $time)==date("d.m.Y", time()-86400)) {
return tr("Yesterday %0", date("H:i", $time));
} elseif (date("d.m.Y", $time)==date("d.m.Y", time())) {
return tr("Today %0", date("H:i", $time));
} else {
return date("d.m.", $time)." ".date("H:i", $time);
}
}
}
\ No newline at end of file
......@@ -85,7 +85,7 @@ class Reports_Users
array('interval' => $interval, 'view' => $view, 'type' => $type, 'always_email' => $always_email),
array('user' => $user)
);
}
}
}
/**
......
......@@ -9,10 +9,16 @@ class Reports_FactoryTest extends TikiTestCase
{
public function testBuild_shouldReturnInstances()
{
$instances = array('Reports_Users', 'Reports_Cache', 'Reports_Manager');
$classes = array('Reports_Users', 'Reports_Cache', 'Reports_Manager', 'Reports_Send');
foreach ($instances as $instance) {
$this->assertInstanceOf($instance, Reports_Factory::build($instance));
foreach ($classes as $className) {
$this->assertInstanceOf($className, Reports_Factory::build($className));
}
}
public function testBuild_shouldThrowExceptionForUnknownClass()
{
$this->setExpectedException('Exception', 'Unknown class Unknown_Class');
Reports_Factory::build('Unknown_Class');
}
}
\ No newline at end of file
......@@ -16,10 +16,11 @@ class Reports_ManagerTest extends TikiTestCase
protected function setUp()
{
$this->reportsUsers = $this->getMockBuilder('Reports_Users')->disableOriginalConstructor()->getMock();
$this->reportsCache = $this->getMockBuilder('Reports_Cache')->disableOriginalConstructor()->getMock();
$this->reportsSend = $this->getMockBuilder('Reports_Send')->disableOriginalConstructor()->getMock();
$this->usersLib = $this->getMockBuilder('UsersLib')->disableOriginalConstructor()->getMock();
$this->obj = new Reports_Manager($this->reportsUsers, $this->reportsCache);
$this->obj = new Reports_Manager($this->reportsUsers, $this->reportsCache, $this->reportsSend, $this->usersLib);
}
public function testDelete_shouldCallMethodToDeleteUserPreferenceAndMethodToDeleteCache()
......@@ -51,4 +52,18 @@ class Reports_ManagerTest extends TikiTestCase
$this->obj->addToCache($watches, $data);
}
public function testSave_shouldCallReportsUsersSave()
{
$user = 'admin';
$interval = 'daily';
$view = 'detailed';
$type = 'html';
$always_email = 1;
$this->reportsUsers->expects($this->once())->method('save')
->with($this->equalTo($user), $this->equalTo($interval), $this->equalTo($view), $this->equalTo($type), $this->equalTo($always_email));
$this->obj->save($user, $interval, $view, $type, $always_email);
}
}
\ No newline at end of file
......@@ -5,17 +5,17 @@
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
require_once 'lib/reportslib.php';
class ReportsLibTest extends TikiTestCase
class Reports_Send_BuildEmailTest extends TikiTestCase
{
protected $obj;
protected function setUp()
{
{
$this->obj = new Reports_Send_BuildEmail(array());
$this->defaultReportPreferences = array('type' => 'plain');
$this->obj = new reportsLib;
}
public function testMakeHtmlEmailBody_shouldReturnStringIfNothingHappened()
{
$this->assertEquals('Nothing has happened.', $this->obj->makeHtmlEmailBody(array(), $this->defaultReportPreferences));
......@@ -78,6 +78,5 @@ class ReportsLibTest extends TikiTestCase
$output = $this->obj->makeHtmlEmailBody($reportCache, $this->defaultReportPreferences);
$this->assertContains('12.09. 20:30: admin added a new comment to Tracker item name', $output);
}
}
}
}
\ No newline at end of file
......@@ -5,7 +5,9 @@
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
class Reports_SendTest extends TikiDatabaseTestCase
require_once('lib/webmail/tikimaillib.php');
class Reports_SendTest extends TikiTestCase
{
protected $obj;
......@@ -13,21 +15,17 @@ class Reports_SendTest extends TikiDatabaseTestCase
protected function setUp()
{
$db = TikiDb::get();
$this->obj = new Reports_Send($db);
$this->dt = new DateTime;
$this->dt->setTimestamp('1326909909');
parent::setUp();
$this->mail = $this->getMock('TikiMail');
$this->obj = new Reports_Send($this->dt, $this->mail);
}
public function getDataSet()
{
return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/fixtures/user_reports_dataset.xml');
}
public function test()
{
// under construction
}
}
\ No newline at end of file
......@@ -8,6 +8,5 @@
{/if}
{$report_body}
{if $mail_contributions}{tr}Contribution:{/tr} {$mail_contributions}{/if}
{tr _0=$userWatchesUrl}You are receiving notification emails grouped in a periodic digest. To receive them individually when posted instead, change your preferences at %0{/tr}
\ No newline at end of file
{tr _0=$userWatchesUrl}You are receiving notification emails grouped in a periodic digest. To receive them individually when posted instead, change your preferences at %0{/tr}
\ No newline at end of file
......@@ -10,17 +10,22 @@ include_once('tiki-setup.php');
$access->check_user($user);
$access->check_feature('feature_daily_report_watches');
$reportsUsers = Reports_Factory::build('Reports_Users');
$reportsManager = Reports_Factory::build('Reports_Manager');
//Enable User Reports
if (isset($_POST['report_preferences']) && $_POST['use_daily_reports'] == "true") {
$reportsUsers->save($user, $_POST['interval'], $_POST['view'], $_POST['type'], $_POST['always_email']);
$interval = filter_input(INPUT_POST, 'interval', FILTER_SANITIZE_STRING);
$view = filter_input(INPUT_POST, 'view', FILTER_SANITIZE_STRING);
$type = filter_input(INPUT_POST, 'type', FILTER_SANITIZE_STRING);
$always_email = filter_input(INPUT_POST, 'always_email', FILTER_SANITIZE_NUMBER_INT);
$reportsManager->save($user, $interval, $view, $type, $always_email);
header('Location: tiki-user_watches.php');
die;
}
//Disable User Reports
if (isset($_POST['report_preferences']) && $_POST['use_daily_reports'] != "true") {
$reportsUsers->delete($user);
$reportsManager->delete($user);
header('Location: tiki-user_watches.php');
die;
}
......@@ -5,35 +5,13 @@
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
include_once ('tiki-setup.php');
require_once ('tiki-setup.php');
if (php_sapi_name() != 'cli') {
$access->check_permission('tiki_p_admin');
}
if ($prefs['feature_daily_report_watches'] != 'y') {
die(tr('This feature is disabled'));
}
include_once ('lib/reportslib.php');
$reportsUsers = Reports_Factory::build('Reports_Users');
$reportsCache = Reports_Factory::build('Reports_Cache');
$access->check_feature('feature_daily_report_watches');
foreach ($reportsUsers->getUsersForReport() as $key => $user) {
$userReportPreferences = $reportsUsers->get($user);
$userData = $userlib->get_user_info($user);
// if email address isn't set, do nothing but clear the cache
if (!empty($userData['email'])) {
//Fetch cache
$cache = $reportsCache->get($user);
//Send email if there is a cache or if always_email = true
if (!empty($cache) || $userReportPreferences['always_email'])
$reportslib->sendEmail($userData, $userReportPreferences, $cache);
}
//Update Database
$reportsUsers->updateLastReport($userData['login']);
//Empty cache
$reportsCache->delete($userData['login']);
}
$reportsManager = Reports_Factory::build('Reports_Manager');
$reportsManager->send();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment