Example #1
0
 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 profile change insight", __METHOD__ . ',' . __LINE__);
     //Bio changes
     if (($instance->network == 'twitter' || $instance->network == 'instagram') && $this->shouldGenerateInsight($this->slug_bio, $instance)) {
         $this->logger->logInfo("Should generate bio change tracker", __METHOD__ . ',' . __LINE__);
         $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
         $versions = $user_versions_dao->getRecentFriendsVersions($user, 7, array('description'));
         //$this->logger->logInfo(Utils::varDumpToString($versions), __METHOD__.','.__LINE__);
         $changes = array();
         $examined_users = array();
         foreach ($versions as $change) {
             $user_key = intval($change['user_key']);
             if (!in_array($user_key, $examined_users)) {
                 $examined_users[] = $user_key;
                 $last_description = $user_versions_dao->getVersionBeforeDay($user_key, date('Y-m-d'), 'description');
                 if ($last_description) {
                     $user_dao = DAOFactory::getDAO('UserDAO');
                     $user = $user_dao->getDetailsByUserKey($user_key);
                     if ($user && Utils::stripURLsOutOfText($user->description) !== Utils::stripURLsOutOfText($last_description['field_value'])) {
                         $changes[] = array('user' => $user, 'field_name' => 'description', 'field_description' => 'bio', 'before' => $last_description['field_value'], 'after' => $user->description);
                     }
                 }
             }
         }
         $this->logger->logInfo("Got " . count($changes) . " bio changes", __METHOD__ . ',' . __LINE__);
         if (count($changes) > 0) {
             $changes = array_slice($changes, 0, 10);
             $insight = new Insight();
             $insight->instance_id = $instance->id;
             $insight->slug = $this->slug_bio;
             $insight->date = $this->insight_date;
             $insight->filename = basename(__FILE__, ".php");
             $insight->emphasis = Insight::EMPHASIS_MED;
             $insight->related_data = array('changes' => $changes);
             $insight->text = $this->getTextBioChange($changes, $instance);
             $insight->headline = $this->getHeadlineBioChange($changes, $instance);
             if (count($changes) == 1) {
                 $insight->header_image = $changes[0]["user"]->avatar;
             }
             $this->insight_dao->insertInsight($insight);
         }
     }
     //Avatar changes
     if (($instance->network == 'twitter' || $instance->network == 'instagram') && $this->shouldGenerateInsight($this->slug_avatar, $instance)) {
         $this->logger->logInfo("Should generate avatar change tracker", __METHOD__ . ',' . __LINE__);
         $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
         $versions = $user_versions_dao->getRecentFriendsVersions($user, 7, array('avatar'));
         //$this->logger->logInfo(Utils::varDumpToString($versions), __METHOD__.','.__LINE__);
         $changes = array();
         $examined_users = array();
         $user_dao = DAOFactory::getDAO('UserDAO');
         foreach ($versions as $change) {
             $user_key = intval($change['user_key']);
             if (!in_array($user_key, $examined_users)) {
                 $examined_users[] = $user_key;
                 $last_version = $user_versions_dao->getVersionBeforeDay($user_key, date('Y-m-d'), 'avatar');
                 if ($last_version) {
                     $user = $user_dao->getDetailsByUserKey($user_key);
                     if ($user && $user->avatar !== $last_version['field_value']) {
                         $do_show_change = true;
                         //Extra check for ThinkUp LLC users
                         if (Utils::isThinkUpLLC()) {
                             $api_accessor = new ThinkUpLLCAPIAccessor();
                             $avatar_url1_https = preg_replace('/^http:(.+)$/', "https:\$1", $user->avatar);
                             $avatar_url2_https = preg_replace('/^http:(.+)$/', "https:\$1", $last_version['field_value']);
                             if ($instance->network == 'twitter') {
                                 //Get the original version of the avatar
                                 //https://dev.twitter.com/overview/general/user-profile-images-and-banners
                                 $avatar_url1_https = str_replace('_normal', '', $avatar_url1_https);
                                 $avatar_url2_https = str_replace('_normal', '', $avatar_url2_https);
                             }
                             $do_show_change = $api_accessor->didAvatarsChange($avatar_url1_https, $avatar_url2_https);
                             if (!$do_show_change) {
                                 $this->logger->logInfo("Skipping change for " . $avatar_url1_https . " and " . $avatar_url2_https, __METHOD__ . ',' . __LINE__);
                             }
                         }
                         if ($do_show_change) {
                             $changes[] = array('user' => $user, 'field_name' => 'avatar', 'field_description' => 'avatar', 'before' => $last_version['field_value'], 'after' => $user->avatar);
                         }
                     }
                 }
             }
         }
         $this->logger->logInfo("Got " . count($changes) . " avatar changes", __METHOD__ . ',' . __LINE__);
         if (count($changes) > 0) {
             $changes = array_slice($changes, 0, 10);
             $insight = new Insight();
             $insight->instance_id = $instance->id;
             $insight->slug = $this->slug_avatar;
             $insight->date = $this->insight_date;
             $insight->filename = basename(__FILE__, ".php");
             $insight->emphasis = Insight::EMPHASIS_MED;
             $insight->related_data = array('changes' => $changes);
             $insight->text = $this->getTextAvatarChange($changes, $instance);
             $insight->headline = $this->getHeadlineAvatarChange($changes, $instance);
             $this->insight_dao->insertInsight($insight);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }