Example #1
0
 /**
  * Method to create Tweet model from Json array
  */
 static function loadFromJSON($obj)
 {
     if (!isset($obj['geo']) || !isset($obj['geo']['coordinates'])) {
         return null;
     }
     $tweet = new Tweet();
     $tweet->setAttributes(['id' => $obj['id'], 'text' => $obj['text'], 'latitude' => $obj['geo']['coordinates'][0], 'longitude' => $obj['geo']['coordinates'][1], 'profile_image_url' => $obj['user']['profile_image_url'], 'user_name' => $obj['user']['name'], 'screen_name' => $obj['user']['screen_name']]);
     if ($tweet->validate() && $tweet->save()) {
         return $tweet;
     } else {
         return null;
     }
 }
 /**
  * Creates a Tweet Object and saves it to the DB 
  * 
  * @param Array $tweet JSON Tweet structure
  * @param Array $regex regular expression to parse the rating and Entry - http://regex101.com/r/cB6oU6/7
  * @return int id of the tweet or null if an error happened
  */
 private function CreateTweetEntry($tweet, $contest_id, $regex)
 {
     $min_rating = 1;
     $max_rating = 10;
     $TweetUser = $this->GetOrAddUser($tweet["user"]);
     $CreateDate = DateTime::createFromFormat("D M d H:i:s T Y", $tweet["created_at"]);
     // Create new Tweet object
     $Tweet = new Tweet();
     $Tweet->id = $tweet["id_str"];
     $Tweet->created_at = $CreateDate->format('Y-m-d H:i:s');
     $Tweet->text = trim($tweet["text"]);
     $Tweet->user_id = $TweetUser->id;
     $Tweet->contest_id = $contest_id;
     $Tweet->needs_validation = false;
     // possible vote manipulation!
     if ($tweet["retweeted"] == true || array_key_exists("retweeted_status", $tweet)) {
         $Tweet->needs_validation = true;
     }
     $rating = $this->ParseRating($regex["rating"], $Tweet->text);
     $entryNr = $this->ParseEntryId($regex["entry"], $Tweet->text);
     if ($rating == null) {
         $this->stdout("Rating could not be parsed for Tweet ID " . $Tweet->id . "\n", Console::FG_RED);
         $Tweet->needs_validation = true;
     } else {
         // check if rating is inside constraints
         if ($rating > $max_rating) {
             $this->stdout("Rating outside of constraints Tweet ID " . $Tweet->id . "\n", Console::FG_RED);
             $Tweet->rating = $max_rating;
         } else {
             if ($rating < $min_rating) {
                 $this->stdout("Rating outside of constraints Tweet ID " . $Tweet->id . "\n", Console::FG_RED);
                 $Tweet->rating = $min_rating;
             } else {
                 $Tweet->rating = $rating;
             }
         }
     }
     if ($entryNr == null) {
         $Tweet->needs_validation = true;
     } else {
         $Entry = Entry::findOne(['contest_id' => $contest_id, 'contest_entry_id' => $entryNr]);
         if ($Entry != null) {
             $Tweet->entry_id = $Entry->id;
             // check if we have to set a new max rating
             if ($Tweet->rating > $Entry->max_rating && $Tweet->rating >= $min_rating && $Tweet->rating <= $max_rating) {
                 $Entry->max_rating = round($Tweet->rating, 2);
             }
             if (($Tweet->rating < $Entry->min_rating || $Entry->min_rating == 0) && $Tweet->rating >= $min_rating && $Tweet->rating <= $max_rating) {
                 $Entry->min_rating = round($Tweet->rating, 2);
             }
             $Entry->save();
             // remove old entries vom voting
             $this->ExcludeOldTweetEntry($contest_id, $Tweet->user_id, $Entry->id, $Tweet->id);
         } else {
             $Tweet->needs_validation = true;
         }
     }
     if ($Tweet->validate() && $Tweet->save()) {
         // remove old entries vom voting
         if ($Tweet->needs_validation == false) {
             $this->ExcludeOldTweetEntry($Tweet->contest_id, $Tweet->user_id, $Tweet->entry_id, $Tweet->id);
         }
         $this->stdout("Tweet ID " . $Tweet->id . " for Contest ID " . $contest_id . " saved!\n");
         $this->stdout("Entry: " . $Tweet->entry_id . ", Rating: " . $Tweet->rating . "\n");
         return $Tweet;
     } else {
         $this->stdout("Tweet already in Database! Tweet ID " . $Tweet->id . "\n", Console::FG_RED);
         return null;
     }
 }