public function testGetDaysInMonth() { $this->assertEqual(TimeHelper::getDaysInMonth(2014, 2), 28); $this->assertEqual(TimeHelper::getDaysInMonth(2013, 2), 28); $this->assertEqual(TimeHelper::getDaysInMonth(2012, 2), 29); $this->assertEqual(TimeHelper::getDaysInMonth(2014, 1), 31); $this->assertEqual(TimeHelper::getDaysInMonth(2014, 3), 31); $this->assertEqual(TimeHelper::getDaysInMonth(2014, 4), 30); $this->assertEqual(TimeHelper::getDaysInMonth(2014, 5), 31); }
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__); //Monthly $did_monthly = false; $should_generate_insight = $this->shouldGenerateMonthlyInsight($slug = 'monthly_best', $instance, $this->insight_date, $regenerate_existing_insight = false, $day_of_month = 1, $count_related_posts = null, $excluded_networks = null, $enable_bonus_alternate_day = false); if ($should_generate_insight) { $most_popular_post = null; $best_popularity_params = array('index' => 0, 'reply' => 0, 'retweet' => 0, 'like' => 0); $insight_text = ''; $post_dao = DAOFactory::getDAO('PostDAO'); //There's probably a better way to get the number of days in last month $last_month_time = strtotime('first day of last month'); $last_month_year = date('Y', $last_month_time); $last_month_month = date('m', $last_month_time); $days_of_posts_to_retrieve = TimeHelper::getDaysInMonth($last_month_year, $last_month_month); $last_months_posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username, $instance->network, $count = 0, $order_by = "pub_date", $in_last_x_days = $days_of_posts_to_retrieve, $iterator = false, $is_public = false); foreach ($last_months_posts as $post) { if ($post->network == 'instagram') { $photo_dao = DAOFactory::getDAO('PhotoDAO'); $post = $photo_dao->getPhoto($post->post_id, 'instagram'); } $reply_count = $post->reply_count_cache; $retweet_count = $post->retweet_count_cache; $fav_count = $post->favlike_count_cache; $popularity_index = 5 * $reply_count + 3 * $retweet_count + 2 * $fav_count; if ($popularity_index > $best_popularity_params['index']) { $best_popularity_params['index'] = $popularity_index; $best_popularity_params['reply'] = $reply_count; $best_popularity_params['retweet'] = $retweet_count; $best_popularity_params['like'] = $fav_count; $most_popular_post = $post; } } if (isset($most_popular_post)) { $my_insight = new Insight(); $my_insight->headline = $this->getVariableCopy(array('%username\'s best of %last_month'), array('last_month' => date('F', $last_month_time))); if ($instance->network == 'instagram' && $most_popular_post->is_short_video) { $my_insight->text = $this->getVariableCopy(array("Happy %new_month! %username's most popular Instagram post of %last_month %month_year was a video.", "Welcome to %new_month! This video was %username's most popular Instagram post " . "of %last_month %month_year."), array('last_month' => date('F', $last_month_time), 'month_year' => date('Y', $last_month_time), 'new_month' => date('F'))); } else { $my_insight->text = $this->getVariableCopy(array("Happy %new_month! This was %username's most popular %post of %last_month %month_year.", "Welcome to %new_month! Take a quick look back at %username's most popular %post " . "of %last_month %month_year."), array('last_month' => date('F', $last_month_time), 'month_year' => date('Y', $last_month_time), 'new_month' => date('F'))); } $my_insight->slug = 'monthly_best'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_HIGH; $my_insight->setPosts(array($most_popular_post)); $this->insight_dao->insertInsight($my_insight); $did_monthly = true; } } //Weekly if ($instance->network == 'twitter') { $day_of_week = 4; } elseif ($instance->network == 'instagram') { $day_of_week = 2; } else { $day_of_week = 0; } $should_generate_insight = self::shouldGenerateWeeklyInsight('weekly_best', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $day_of_week, count($last_week_of_posts)); if (!$did_monthly && $should_generate_insight) { $most_popular_post = null; $best_popularity_params = array('index' => 0, 'reply' => 0, 'retweet' => 0, 'like' => 0); $insight_text = ''; foreach ($last_week_of_posts as $post) { if ($post->network == 'instagram') { $photo_dao = DAOFactory::getDAO('PhotoDAO'); $post = $photo_dao->getPhoto($post->post_id, 'instagram'); } $reply_count = $post->reply_count_cache; $retweet_count = $post->retweet_count_cache; $fav_count = $post->favlike_count_cache; $popularity_index = 5 * $reply_count + 3 * $retweet_count + 2 * $fav_count; if ($popularity_index > $best_popularity_params['index']) { $best_popularity_params['index'] = $popularity_index; $best_popularity_params['reply'] = $reply_count; $best_popularity_params['retweet'] = $retweet_count; $best_popularity_params['like'] = $fav_count; $most_popular_post = $post; } } if (isset($most_popular_post)) { if ($instance->network !== 'instagram') { $headlines = array("{$this->username}'s biggest %post last week", "{$this->username}'s most popular %post last week"); } else { //Instagram: Be video-aware if ($most_popular_post->is_short_video) { $headlines = array("{$this->username}'s biggest post last week was a video", "{$this->username}'s most popular post last week was a video"); } else { $headlines = array("{$this->username}'s biggest %post last week", "{$this->username}'s most popular %post last week"); } } $insight_text = $this->username . " earned "; foreach ($best_popularity_params as $key => $value) { if ($value && $key != 'index') { $insight_text .= "<strong>" . $value . " " . $this->terms->getNoun($key, $value > 1) . "</strong>, "; } } $insight_text = rtrim($insight_text, ", "); $insight_text .= '.'; if (!(strpos($insight_text, ',') === false)) { $insight_text = substr_replace($insight_text, " and", strpos($insight_text, strrchr($insight_text, ',')), 1); } $simplified_post_date = date('Y-m-d', strtotime($most_popular_post->pub_date)); $my_insight = new Insight(); $my_insight->slug = 'weekly_best'; //slug to label this insight's content $my_insight->instance_id = $instance->id; $my_insight->date = $this->insight_date; //date is often this or $simplified_post_date $my_insight->headline = $this->getVariableCopy($headlines); $my_insight->text = $insight_text; // or just set a strong like "Greetings humans"; $my_insight->header_image = $header_image; $my_insight->filename = basename(__FILE__, ".php"); $my_insight->emphasis = Insight::EMPHASIS_MED; $my_insight->setPosts(array($most_popular_post)); $this->insight_dao->insertInsight($my_insight); } } $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__); }