/**
  * Get and render the tweets.
  *
  * If quantity not specified, fetch from env.
  *
  * @param null|int $quantity
  */
 public function get($quantity = null)
 {
     if (is_null($quantity)) {
         $quantity = $this->env('feeds_quantity');
     }
     $this->view->toJson($this->tweet->getFeeds($quantity));
 }
Beispiel #2
0
 public static function generateTestTweet($username, $id)
 {
     $model = new Tweet();
     $model->key = $id;
     $model->owner = $username;
     $model->text = "message #{$id}";
     $month = rand(1, 12);
     $day = rand(1, 28);
     $year = 2015;
     $date = new \DateTime("{$year}-{$month}-{$day}");
     $model->timestamp = $date->getTimestamp();
     $model->save(false);
 }
Beispiel #3
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;
     }
 }
Beispiel #4
0
 public function actionTest()
 {
     $list = Tweet::find()->all();
     foreach ($list as $twit) {
         $twit->saveIndex();
     }
 }
Beispiel #5
0
 /**
  * @param Tweet $tweet
  * @param array $tweetHashtags
  *
  * @throws Exception
  */
 private function importHashtags(Tweet $tweet, array $tweetHashtags)
 {
     foreach ($tweetHashtags as $tweetHashtag) {
         $hashtagFromDb = Hashtag::findOne($tweetHashtag);
         if (empty($hashtagFromDb)) {
             $hashtag = Hashtag::createInstanceFromParam($tweetHashtag);
             if (!$hashtag->save()) {
                 throw new Exception('Ошибка записи в базу данных: Таблица hashtag');
             }
             $tweet->link('hashtagTexts', $hashtag);
         } else {
             /**
              * @var Hashtag $hashtagFromDb
              */
             $tweet->link('hashtagTexts', $hashtagFromDb);
         }
     }
 }
 public function actionGenerateTestData($numUsers, $maxNumTweets)
 {
     $i = 0;
     $identifier = rand(0, 1000);
     while ($i < $numUsers) {
         User::generateTestUser("User" . $i . $identifier);
         $numTweets = rand(2, $maxNumTweets);
         $k = 0;
         while ($k < $numTweets) {
             Tweet::generateTestTweet("User" . $i . $identifier, $k);
             $k++;
         }
         $i++;
     }
     return $this->render('index');
 }
 /**
  * Updates all Entries of a Contest with votecount and avg rating
  * 
  * @param \app\models\Contest $Contest Contest Object
  */
 private function UpdateContestEntries($Contest)
 {
     $Entries = Entry::findAll(['contest_id' => $Contest->id]);
     foreach ($Entries as $Entry) {
         $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 Tweet model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Tweet the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Tweet::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #9
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTweets()
 {
     return $this->hasMany(Tweet::className(), ['contest_id' => 'id']);
 }
function getNumTweetsForIntervalByUsername($username, $startMonth, $startYear, $endMonth, $endYear)
{
    $startOfInterval = getStartOfMonth($startMonth, $startYear);
    $endOfInterval = getEndOfMonth($endMonth, $endYear);
    return Tweet::find()->where(['between', 'timestamp', $startOfInterval, $endOfInterval])->andWhere(['owner' => $username])->count();
}
Beispiel #11
0
 /**
  * @param $fromDateTime
  * @param $toDateTime
  *
  * @return Tweet[]
  */
 private function getTweets($fromDateTime, $toDateTime)
 {
     $tweets = Tweet::find()->byDate($fromDateTime, $toDateTime)->hashtagsViaJunctionTable()->all();
     return $tweets;
 }
 /**
  * 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();
 }
Beispiel #13
0
 /**
  * @param int $count
  *
  * @return array
  */
 public function findLastTweets($count)
 {
     $lastTweets = Tweet::find()->byLastOnes($count)->hashtagsViaJunctionTable()->all();
     return $this->composeTweetsForJSON($lastTweets);
 }
Beispiel #14
0
            <?php 
$ratings = Tweet::find()->select(['COUNT(*) AS rating_count', 'rating'])->where(['entry_id' => $model->id, 'needs_validation' => 0])->groupBy(['rating'])->asArray()->all();
$chart_labels = [];
$chart_values = [];
foreach ($ratings as $elem) {
    $chart_labels[] = $elem['rating'];
    $chart_values[] = $elem['rating_count'];
}
?>
            <?php 
echo ChartJs::widget(['type' => 'Bar', 'options' => ['height' => 250], 'clientOptions' => ['responsive' => true, 'maintainAspectRatio' => false], 'data' => ['labels' => $chart_labels, 'datasets' => [['label' => "My Second dataset", 'fillColor' => "rgba(151,187,205,0.5)", 'strokeColor' => "rgba(151,187,205,1)", 'pointColor' => "rgba(151,187,205,1)", 'pointStrokeColor' => "#fff", 'data' => $chart_values]]]]);
?>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <h2><?php 
echo Yii::t('app', 'Tweets');
?>
</h2>
        <?php 
Pjax::begin(['id' => 'tweet-grid']);
echo GridView::widget(['dataProvider' => new ActiveDataProvider(['query' => Tweet::find()->where(['entry_id' => $model->id, 'needs_validation' => 0, 'old_vote' => 0])]), 'columns' => [['attribute' => 'user.screen_name', 'value' => function ($data) {
    return $data->user->screen_name;
}], 'rating', ['attribute' => 'text', 'contentOptions' => ['class' => 'hidden-xs'], 'headerOptions' => ['class' => 'hidden-xs']], ['label' => Yii::t('app', 'Tweeted at'), 'attribute' => 'created_at', 'format' => ['datetime', 'php:d.m.Y - H:i:s'], 'contentOptions' => ['class' => 'hidden-xs'], 'headerOptions' => ['class' => 'hidden-xs']]]]);
Pjax::end();
?>
    </div>
</div>