diff --git a/tests/TestOfOwnerMySQLDAO.php b/tests/TestOfOwnerMySQLDAO.php index 3c066f58d9..e8003c3e06 100644 --- a/tests/TestOfOwnerMySQLDAO.php +++ b/tests/TestOfOwnerMySQLDAO.php @@ -112,6 +112,7 @@ public function testGetById() { $this->assertEqual($existing_owner->account_status, ''); $this->assertEqual($existing_owner->api_key, 'c9089f3c9adaf0186f6ffb1ee8d6501c'); $this->assertEqual($existing_owner->email_notification_frequency, 'both'); + $this->assertEqual($existing_owner->timezone, 'UTC'); } /** diff --git a/webapp/_lib/dao/class.OwnerMySQLDAO.php b/webapp/_lib/dao/class.OwnerMySQLDAO.php index 77e57d41c7..55647c0c52 100644 --- a/webapp/_lib/dao/class.OwnerMySQLDAO.php +++ b/webapp/_lib/dao/class.OwnerMySQLDAO.php @@ -64,7 +64,7 @@ public function getByEmail($email) { public function getById($id) { $q = 'SELECT id,full_name,email,is_admin,last_login,is_activated,password_token,' . - 'account_status,failed_logins,api_key,email_notification_frequency ' . + 'account_status,failed_logins,api_key,email_notification_frequency,timezone ' . 'FROM #prefix#owners AS o WHERE id = :id'; $vars = array( ':id'=>$id @@ -336,7 +336,7 @@ public function setEmailNotificationFrequency($email, $email_notification_freque SET email_notification_frequency=:email_notification_frequency WHERE email=:email"; if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); } - $stmt = $this->execute($q, array(':email_notification_frequency' => $email_notification_frequency, + $stmt = $this->execute($q, array(':email_notification_frequency' => $email_notification_frequency, ':email' => $email)); return $this->getUpdateCount($stmt); } diff --git a/webapp/plugins/insightsgenerator/insights/outreachpunchcard.php b/webapp/plugins/insightsgenerator/insights/outreachpunchcard.php index dbe9707f4f..606242d30e 100644 --- a/webapp/plugins/insightsgenerator/insights/outreachpunchcard.php +++ b/webapp/plugins/insightsgenerator/insights/outreachpunchcard.php @@ -38,8 +38,23 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number if (parent::shouldGenerateInsight('outreach_punchcard', $instance, $insight_date='today', $regenerate_existing_insight=false, $day_of_week=6, count($last_week_of_posts))) { - $cfg = Config::getInstance(); - $local_timezone = new DateTimeZone($cfg->getValue('timezone')); + + $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO'); + $owner_dao = DAOFactory::getDAO('OwnerDAO'); + + $owner_instance = $owner_instance_dao->getByInstance($instance->id); + $owner_id = $owner_instance[0]->owner_id; + $owner = $owner_dao->getById($owner_instance[0]->owner_id); + try { + $owner_timezone = new DateTimeZone($owner->timezone); + } catch (Exception $e) { + // In the odd case the owner has no or a malformed timezone + $cfg = Config::getInstance(); + $owner_timezone = new DateTimeZone($cfg->getValue('timezone')); + } + $now = new DateTime(); + $offset = timezone_offset_get($owner_timezone, $now); + $post_dao = DAOFactory::getDAO('PostDAO'); $punchcard = array(); @@ -62,32 +77,16 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number foreach ($responses as $response) { $response_pub_date = new DateTime($response->pub_date); - $response_dotw = date('N', - (date('U', - strtotime($response->pub_date)) + timezone_offset_get($local_timezone, $response_pub_date) - ) - ); // Day of the week - $response_hotd = date('G', - (date('U', - strtotime($response->pub_date)) + timezone_offset_get($local_timezone, $response_pub_date) - ) - ); // Hour of the day + $response_dotw = date('N', (date('U', strtotime($response->pub_date)+$offset))); // Day of week + $response_hotd = date('G', (date('U', strtotime($response->pub_date)+$offset))); // Hour of day $punchcard['responses'][$response_dotw][$response_hotd]++; $responses_chron[$response_hotd]++; } $post_pub_date = new DateTime($post->pub_date); - $post_dotw = date('N', - (date('U', - strtotime($post->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date) - ) - ); // Day of the week - $post_hotd = date('G', - (date('U', - strtotime($post->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date) - ) - ); // Hour of the day + $post_dotw = date('N', (date('U', strtotime($post->pub_date)+$offset))); // Day of the week + $post_hotd = date('G', (date('U', strtotime($post->pub_date)+$offset))); // Hour of the day $punchcard['posts'][$post_dotw][$post_hotd]++; } diff --git a/webapp/plugins/insightsgenerator/tests/TestOfOutreachPunchcardInsight.php b/webapp/plugins/insightsgenerator/tests/TestOfOutreachPunchcardInsight.php index 16cf5fd6c6..5ec73b2bdf 100644 --- a/webapp/plugins/insightsgenerator/tests/TestOfOutreachPunchcardInsight.php +++ b/webapp/plugins/insightsgenerator/tests/TestOfOutreachPunchcardInsight.php @@ -48,22 +48,17 @@ public function tearDown() { public function testOutreachPunchcardInsight() { $cfg = Config::getInstance(); - $local_timezone = new DateTimeZone($cfg->getValue('timezone')); + $install_timezone = new DateTimeZone($cfg->getValue('timezone')); + $owner_timezone = new DateTimeZone($test_timezone='America/Los_Angeles'); // Get data ready that insight requires $posts = self::getTestPostObjects(); $post_pub_date = new DateTime($posts[0]->pub_date); - $post_dotw = date('N', - (date('U', - strtotime($posts[0]->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date) - ) - ); // Day of the week - $post_hotd = date('G', - (date('U', - strtotime($posts[0]->pub_date)) + timezone_offset_get($local_timezone, $post_pub_date) - ) - ); // Hour of the day + $now = new DateTime(); + $offset = timezone_offset_get($owner_timezone, $now) - timezone_offset_get($install_timezone, $now); + $post_dotw = date('N', (date('U', strtotime($posts[0]->pub_date)))+ timezone_offset_get($owner_timezone, $now)); + $post_hotd = date('G', (date('U', strtotime($posts[0]->pub_date)))+ timezone_offset_get($owner_timezone, $now)); $builders = array(); @@ -71,63 +66,54 @@ public function testOutreachPunchcardInsight() { 'full_name'=>'Twitter User', 'avatar'=>'avatar.jpg', 'follower_count'=>36000, 'is_protected'=>0, 'network'=>'twitter', 'description'=>'A test Twitter User')); - $date_r = date("Y-m-d",strtotime('-1 day')); + $instance_id = 10; + $builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User', + 'email'=>'test@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0, + 'timezone' => $test_timezone)); + $builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>'1','instance_id'=>$instance_id)); - // Response between 1pm and 2pm + $install_offset = $install_timezone->getOffset(new DateTime()); + $date_r = date("Y-m-d",strtotime('-1 day')-$install_offset); + + // Response between 1pm and 2pm install time + $time = gmdate('Y-m-d H:i:s', strtotime('yesterday 13:11:09')); $builders[] = FixtureBuilder::build('posts', array('id'=>136, 'post_id'=>136, 'author_user_id'=>7654321, 'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg', 'network'=>'twitter', 'post_text'=>'This is a reply.', 'source'=>'web', - 'pub_date'=>$date_r.' 13:11:09', 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0)); + 'pub_date'=>$time, 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0)); - // Response between 1pm and 2pm + // Response between 1pm and 2pm install time + $time = gmdate('Y-m-d H:i:s', strtotime('yesterday 13:01:13')); $builders[] = FixtureBuilder::build('posts', array('id'=>137, 'post_id'=>137, 'author_user_id'=>7654321, 'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg', 'network'=>'twitter', 'post_text'=>'This is a reply.', 'source'=>'web', - 'pub_date'=>$date_r.' 13:01:13', 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0)); + 'pub_date'=>$time, 'in_reply_to_post_id'=>133, 'reply_count_cache'=>0, 'is_protected'=>0)); - // Response between 1pm and 2pm + // Response between 1pm and 2pm install time + $time = gmdate('Y-m-d H:i:s', strtotime('yesterday 13:13:56')); $builders[] = FixtureBuilder::build('posts', array('id'=>138, 'post_id'=>138, 'author_user_id'=>7654321, 'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg', 'network'=>'twitter', 'post_text'=>'This is a reply.', 'source'=>'web', - 'pub_date'=>$date_r.' 13:13:56', 'in_reply_to_post_id'=>135, 'reply_count_cache'=>0, 'is_protected'=>0)); + 'pub_date'=>$time, 'in_reply_to_post_id'=>135, 'reply_count_cache'=>0, 'is_protected'=>0)); - // Response between 11am and 12pm + // Response between 11am and 12pm install time + $time = gmdate('Y-m-d H:i:s', strtotime('yesterday 11:07:42')); $builders[] = FixtureBuilder::build('posts', array('id'=>139, 'post_id'=>139, 'author_user_id'=>7654321, 'author_username'=>'twitteruser', 'author_fullname'=>'Twitter User', 'author_avatar'=>'avatar.jpg', 'network'=>'twitter', 'source'=>'web', 'post_text'=>'RT @testeriffic: New Year\'s Eve! Feeling very gay today, but not very homosexual.', - 'pub_date'=>$date_r.' 11:07:42', 'in_retweet_of_post_id'=>134, 'reply_count_cache'=>0, 'is_protected'=>0)); - - $time1_low = new DateTime($date_r.' 13:00:00'); - $time1_high = new DateTime($date_r.' 14:00:00'); - $time1str_low = date('ga', - (date('U', - strtotime($date_r.' 13:00:00')) + timezone_offset_get($local_timezone, $time1_low) - ) - ); - $time1str_high = date('ga', - (date('U', - strtotime($date_r.' 14:00:00')) + timezone_offset_get($local_timezone, $time1_high) - ) - ); + 'pub_date'=>$time, 'in_retweet_of_post_id'=>134, 'reply_count_cache'=>0, 'is_protected'=>0)); + + $time1str_low = date('ga', (date('U', strtotime($date_r.' 13:00:00')) + $offset)); + $time1str_high = date('ga', (date('U', strtotime($date_r.' 14:00:00')) + $offset)); $time1str = $time1str_low." and ".$time1str_high; - $time2_low = new DateTime($date_r.' 13:00:00'); - $time2_high = new DateTime($date_r.' 14:00:00'); - $time2str_low = date('ga', - (date('U', - strtotime($date_r.' 11:00:00')) + timezone_offset_get($local_timezone, $time2_low) - ) - ); - $time2str_high = date('ga', - (date('U', - strtotime($date_r.' 12:00:00')) + timezone_offset_get($local_timezone, $time2_high) - ) - ); + $time2str_low = date('ga', (date('U', strtotime($date_r.' 11:00:00')) + $offset)); + $time2str_high = date('ga', (date('U', strtotime($date_r.' 12:00:00')) + $offset)); $time2str = $time2str_low." and ".$time2str_high; $instance = new Instance(); - $instance->id = 10; + $instance->id = $instance_id; $instance->network_username = 'testeriffic'; $instance->network = 'twitter'; $insight_plugin = new OutreachPunchcardInsight(); @@ -147,6 +133,12 @@ public function testOutreachPunchcardInsight() { } public function testOutreachPunchcardInsightNoResponse() { + $instance_id = 10; + $builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User', + 'email'=>'test@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0, + 'timezone' => 'UTC')); + $builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>'1','instance_id'=>$instance_id)); + // Get data ready that insight requires $posts = self::getTestPostObjects(); $instance = new Instance(); @@ -183,7 +175,7 @@ private function getTestPostObjects() { $p->post_id = $counter++; $p->network = 'twitter'; $p->post_text = $test_text; - $p->pub_date = date("Y-m-d H:i:s", strtotime('-2 days')); + $p->pub_date = gmdate("Y-m-d H:i:s", strtotime('-2 days')); $posts[] = $p; } return $posts;