public function actionGetentry() { $id = Yii::$app->request->get('id'); $tags = EntryTag::findAll(['entry_id' => $id]); $retTags = []; // TODO relations foreach ($tags as $k => $v) { $retTags[] = Tag::findOne($v['tag_id']); } Yii::$app->response->format = Response::FORMAT_JSON; return (object) ['data' => Entry::findOne($id), 'tags' => $retTags]; }
/** * 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; } }
/** * Updates all Entry of the Tweet with votecount and avg rating * * @param \app\models\Tweet $Tweet Tweet Object */ private function UpdateEntry($Tweet) { $Entry = Entry::findOne($Tweet->entry_id); $avgQuery = new Query(); $avgQuery->from(Tweet::tableName())->where(['entry_id' => $Entry->id, 'needs_validation' => 0, 'old_vote' => 0]); $avgRating = $avgQuery->average('rating'); $votestQuery = new Query(); $votestQuery->from(Tweet::tableName())->where(['entry_id' => $Entry->id, 'needs_validation' => 0, 'old_vote' => 0]); $votes = $votestQuery->count(); $Entry->votes = $votes; $Entry->avg_rating = round($avgRating, 2); $Entry->save(); }
/** * Finds the Entry model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Entry the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Entry::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }