/**
  * Dump rendered insight for stream and email, plus the email subject line and optional message.
  * @param  Insight  $insight
  * @param  Instance $instance
  * @param  str   $message default null
  * @return void
  */
 protected function dumpRenderedInsight(Insight $insight, Instance $instance, $message = null)
 {
     if (isset($message)) {
         $this->debug("<h4 style=\"text-align: center; margin-top: 20px;\">{$message}</h4>");
     }
     $terms = new InsightTerms($instance->network);
     $email_subject_line = $terms->swapInSecondPerson($instance->network_username, $insight->headline);
     $this->debug("<h4 style=\"text-align: center; margin-top: 20px;\">Email Subject: " . $email_subject_line . "</h4>");
     $this->debug($this->getRenderedInsightInHTML($insight));
     $this->debug($this->getRenderedInsightInEmail($insight));
 }
 /**
  * Return email subject line based on the insight headline of a high or medium insight (converted to second person).
  * If neither exist, use generic headline text.
  * @param str $daily_or_weekly "Daily" or "Weekly"
  * @param arr $insights Insight objects
  * @return str
  */
 public function getEmailMessageSubjectLine($daily_or_weekly, $insights)
 {
     $num_insights = count($insights);
     $insight_headline_subject = null;
     // Use a HIGH emphasis insight headline as the email subject line
     foreach ($insights as $insight) {
         if ($insight->emphasis == Insight::EMPHASIS_HIGH) {
             $terms = new InsightTerms($insight->instance->network);
             $insight_headline_subject = $terms->swapInSecondPerson($insight->instance->network_username, strip_tags(html_entity_decode($insight->headline, ENT_NOQUOTES, 'UTF-8')));
             break;
         }
     }
     // If no HIGH insights existed, check medium
     if (!isset($insight_headline_subject)) {
         foreach ($insights as $insight) {
             if ($insight->emphasis == Insight::EMPHASIS_MED) {
                 $terms = new InsightTerms($insight->instance->network);
                 $insight_headline_subject = $terms->swapInSecondPerson($insight->instance->network_username, strip_tags(html_entity_decode($insight->headline, ENT_NOQUOTES, 'UTF-8')));
                 break;
             }
         }
     }
     // If neither high nor medium are available, use a generic headline
     if (!isset($insight_headline_subject)) {
         if ($daily_or_weekly == "Daily") {
             $subject_line_choices = array("ThinkUp has new insights for you! Take a look", "You have new insights from ThinkUp", "Your new insights from ThinkUp", "New ThinkUp insights are ready for you", "These are your latest ThinkUp insights", "A few new ThinkUp insights for you", "New ThinkUp insights are waiting for you", "ThinkUp: Today's insights", "These are your ThinkUp insights for " . date('l', $this->current_timestamp));
             if ($num_insights > 1) {
                 $subject_line_choices[] = "ThinkUp found %total insights for you today. Here's a look.";
                 $subject_line_choices[] = "You have %total new insights from ThinkUp";
             }
         } else {
             $subject_line_choices = array("This week was great! ThinkUp's got details", "How did you do online this week? Here are your ThinkUp insights", "Your ThinkUp insights this week", "New ThinkUp insights are ready for you", "This week's ThinkUp insights");
         }
         $rand_index = TimeHelper::getTime() % count($subject_line_choices);
         $subject = $subject_line_choices[$rand_index];
         $subject = str_replace('%total', number_format($num_insights), $subject);
     } else {
         $subject = $insight_headline_subject;
     }
     return $subject;
 }
 public function testSwapInSecondPerson()
 {
     $terms = new InsightTerms('twitter');
     $username = '******';
     $text = "2 interesting people followed @buffysummers.";
     $new_text = "2 interesting people followed you.";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "@buffysummers has it good";
     $new_text = "You have it good";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $terms = new InsightTerms('facebook');
     $username = '******';
     $text = "Buffy Summers's status update got 17 comments.";
     $new_text = "Your status update got 17 comments.";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Looks like it will be 8 weeks before Willow Rosenberg reaches 100,000 followers.";
     $new_text = "Looks like it will be 8 weeks before you reach 100,000 followers.";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Willow Rosenberg reaches 100,000 followers in 8 weeks.";
     $new_text = "You reach 100,000 followers in 8 weeks.";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Hey, did you see that Xander Harris followed Willow Rosenberg?";
     $new_text = "Hey, did you see that Xander Harris followed you?";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Where in the world is Willow Rosenberg?";
     $new_text = "Where in the world are you?";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Is Willow Rosenberg the best?";
     $new_text = "Are you the best?";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Willow Rosenberg hasn't replied to Andre Durand in over a year.";
     $new_text = "You haven't replied to Andre Durand in over a year.";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "That's why Willow Rosenberg hasn't replied to Andre Durand in over a year.";
     $new_text = "That's why you haven't replied to Andre Durand in over a year.";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "5 people thought Willow Rosenberg was worth retweeting";
     $new_text = "5 people thought you were worth retweeting";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Willow Rosenberg was on fire this week!";
     $new_text = "You were on fire this week!";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
     $username = '******';
     $text = "Congratulations on the congrats, Willow Rosenberg!";
     $new_text = "Congratulations on the congrats!";
     $result = $terms->swapInSecondPerson($username, $text);
     $this->assertEqual($result, $new_text);
 }
 public function testTwitter()
 {
     $today = date('Y-m-d');
     $insight_dao = DAOFactory::getDAO('InsightDAO');
     $post_builders = array();
     $post_builders[] = FixtureBuilder::build('posts', array('in_reply_to_user_id' => 1, 'author_username' => 'testy', 'network' => $this->instance->network, 'pub_date' => date('Y-m-d', strtotime('January 9')), 'author_user_id' => $this->instance->network_user_id, 'post_text' => "I hate it all."));
     $post_builders[] = FixtureBuilder::build('posts', array('in_reply_to_user_id' => 1, 'author_username' => 'testy', 'network' => $this->instance->network, 'pub_date' => date('Y-m-d', strtotime('January 9')), 'author_user_id' => $this->instance->network_user_id, 'post_text' => "Thanks everyone"));
     $post_builders[] = FixtureBuilder::build('posts', array('in_reply_to_user_id' => 2, 'author_username' => 'testy', 'network' => $this->instance->network, 'pub_date' => date('Y-m-d', strtotime('January 9')), 'author_user_id' => $this->instance->network_user_id, 'post_text' => "Thanks everyone"));
     $insight_plugin = new ThanksgivingWhoYouThankedInsight();
     $insight_plugin->generateInsight($this->instance, null, $posts, 3);
     $result = $insight_dao->getInsight($insight_plugin->slug, $this->instance->id, $today);
     $data = unserialize($result->related_data);
     $this->assertEqual($result->headline, 'Who @Thankster was thankful for in ' . date('Y'));
     $this->assertEqual($result->text, 'These are all the people @Thankster thanked this year.');
     $this->assertEqual(count($data['people']), 2);
     $this->assertEqual($data['people'][0]->username, 'two');
     $this->assertEqual($data['people'][1]->username, 'one');
     $this->assertEqual($data['hero_image']['alt_text'], $result->headline);
     $this->assertEqual($data['hero_image']['img_link'], 'https://www.flickr.com/photos/voght/2441818832/');
     $this->debug($this->getRenderedInsightInHTML($result));
     $this->debug($this->getRenderedInsightInEmail($result));
     $terms = new InsightTerms('twitter');
     $notification_line = $terms->swapInSecondPerson($this->instance->network_username, $result->headline);
     $this->assertEqual($notification_line, 'Who you were thankful for in ' . date('Y'));
 }