public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) { if ($instance->network == 'facebook') { parent::generateInsight($instance, $user, $last_week_of_posts, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); $should_generate_insight = self::shouldGenerateWeeklyInsight('gender_analysis', $instance, null, false, $day_of_week = 5, null, $excluded_networks = array('twitter', 'instagram', 'foursquare', 'google+')); if ($should_generate_insight) { $fav_post_dao = DAOFactory::getDAO('FavoritePostDAO'); $female_likes_total = 0; $male_likes_total = 0; $female_comments_total = 0; $male_comments_total = 0; foreach ($last_week_of_posts as $post) { $gender_fav = $fav_post_dao->getGenderOfFavoriters($post->post_id, $post->network); $this->logger->logInfo("Likes by gender for {$post->post_id} are " . Utils::varDumpToString($gender_fav), __METHOD__ . ',' . __LINE__); if (isset($gender_fav['female_likes_count'])) { $female_likes_total = $female_likes_total + intval($gender_fav['female_likes_count']); } if (isset($gender_fav['male_likes_count'])) { $male_likes_total = $male_likes_total + intval($gender_fav['male_likes_count']); } $gender_comm = $fav_post_dao->getGenderOfCommenters($post->post_id, $post->network); $this->logger->logInfo("Comments by gender for {$post->post_id} are " . Utils::varDumpToString($gender_comm), __METHOD__ . ',' . __LINE__); if (isset($gender_comm['female_comment_count'])) { $female_comments_total = $female_comments_total + intval($gender_comm['female_comment_count']); } if (isset($gender_comm['male_comment_count'])) { $male_comments_total = $male_comments_total + intval($gender_comm['male_comment_count']); } $this->logger->logInfo("Male likes total is now {$male_likes_total}", __METHOD__ . ',' . __LINE__); $this->logger->logInfo("Female likes total is now {$female_likes_total}", __METHOD__ . ',' . __LINE__); $this->logger->logInfo("Male comments total is now {$male_comments_total}", __METHOD__ . ',' . __LINE__); $this->logger->logInfo("Female comments total is now {$female_comments_total}", __METHOD__ . ',' . __LINE__); } $female_total = $female_likes_total + $female_comments_total; $male_total = $male_likes_total + $male_comments_total; $total_gender_data = $female_total + $male_total; $this->logger->logInfo("Of {$total_gender_data} bits of gender data the last week of posts, " . "{$female_total} were female and {$male_total} were male", __METHOD__ . ',' . __LINE__); // Only generate this insight if there is gender data for at least 3 comments and/or likes if ($total_gender_data >= 3) { $gender_data = array('gender' => 'value', 'female' => $female_total, 'male' => $male_total); $headlines = array("%genderucase reacted to %username's %posts the most", "%genderucase responded to %username's %posts the most", "%username's %posts resonated with %genderlcase"); $headlines_100percent = array("Only %genderlcase reacted to %username's %posts", "Only %genderlcase responded to %username's %posts", "%username's %posts resonated with %genderlcase"); if ($female_total > $male_total) { if ($male_total == 0) { $headline = $this->getVariableCopy($headlines_100percent, array('genderlcase' => 'women')); } else { $headline = $this->getVariableCopy($headlines, array('genderucase' => 'Women', 'genderlcase' => 'women')); } $text = "This past week, <strong>" . number_format($female_total) . " likes and comments</strong> on " . $instance->network_username . "'s status updates were by people who identify as female, compared to " . number_format($male_total) . " by people who identify as male."; } elseif ($male_total > $female_total) { if ($female_total == 0) { $headline = $this->getVariableCopy($headlines_100percent, array('genderlcase' => 'men')); } else { $headline = $this->getVariableCopy($headlines, array('genderucase' => 'Men', 'genderlcase' => 'men')); } $text = "This past week, <strong>" . number_format($male_total) . " likes and comments</strong> on " . $instance->network_username . "'s status updates were by people who identify as male, compared to " . number_format($female_total) . " by people who identify as female."; } else { $headlines = array("Both genders reacted to %username's %posts equally", "Both genders responded to %username's %posts equally", "%username's %posts resonated with both genders"); $headline = $this->getVariableCopy($headlines); $text = "This past week, people who identify as male and female liked and commented on " . "{$this->username}'s status updates at the same rate."; } $my_insight = new Insight(); $my_insight->slug = 'gender_analysis'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->headline = $headline; $my_insight->text = $text; $my_insight->date = $this->insight_date; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPieChart($gender_data); $this->insight_dao->insertInsight($my_insight); } else { $this->logger->logInfo("Not enough gender data to generate insight", __METHOD__ . ',' . __LINE__); } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); } else { $this->logger->logInfo("Not generating insight for non-Facebook network", __METHOD__ . ',' . __LINE__); } } }
public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) { parent::generateInsight($instance, $user, $last_week_of_posts, $number_days); $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__); if ($instance->network != 'facebook') { $this->logger->logInfo("Done generating insight (Skipped non-Facebook)", __METHOD__ . ',' . __LINE__); return; } $year = date('Y'); $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight($this->slug, $instance, $insight_date = "{$year}-{$this->run_date}", $regenerate = false, $day_of_year = $this->run_date); if (!$should_generate_insight) { $this->logger->logInfo("Done generating insight (Skipped)", __METHOD__ . ',' . __LINE__); return; } $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__); $post_dao = DAOFactory::getDAO('PostDAO'); $fav_post_dao = DAOFactory::getDAO('FavoritePostDAO'); $post_iterator = $post_dao->getThisYearOfPostsIterator($instance->network_user_id, $instance->network); $males = 0; $females = 0; foreach ($post_iterator as $post) { $faves = $fav_post_dao->getGenderOfFavoriters($post->post_id, $post->network); $females += empty($faves['female_likes_count']) ? 0 : intval($faves['female_likes_count']); $males += empty($faves['male_likes_count']) ? 0 : intval($faves['male_likes_count']); $comments = $fav_post_dao->getGenderOfCommenters($post->post_id, $post->network); $females += empty($comments['female_comment_count']) ? 0 : intval($comments['female_comment_count']); $males += empty($comments['male_comment_count']) ? 0 : intval($comments['male_comment_count']); } if ($females + $males < 10) { $this->logger->logInfo("Done generating insight (Not enough data)", __METHOD__ . ',' . __LINE__); return; } if ($males == $females) { $who = 'women and men equally'; } elseif ($females > $males) { $who = 'women'; } else { $who = 'men'; } $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance); $qualified_year = ""; if (date('Y', strtotime($earliest_pub_date)) == date('Y')) { if (date('n', strtotime($earliest_pub_date)) > 1) { //not January //Earliest post was this year; figure out what month we have data since this year $since = date('F', strtotime($earliest_pub_date)); $qualified_year = " (at least since " . $since . ")"; } } $female_text = $females == 1 ? 'like or comment' : 'likes and comments'; $text = 'This year, <strong>' . number_format($females) . ' ' . $female_text . '</strong> on ' . $this->username . '\'s status updates were ' . 'by people who identify as female, compared to <strong>' . number_format($males) . '</strong> by people who identify as male' . $qualified_year . '.'; $insight = new Insight(); $insight->instance_id = $instance->id; $insight->slug = $this->slug; $insight->date = "{$year}-{$this->run_date}"; $insight->headline = $this->username . "'s status updates resonated with {$who} in {$year}"; $insight->text = $text; $insight->filename = basename(__FILE__, ".php"); $insight->emphasis = Insight::EMPHASIS_HIGH; $insight->header_image = $user->avatar; $insight->setPieChart(array('gender' => 'value', 'female' => $females, 'male' => $males)); $this->insight_dao->insertInsight($insight); }