public function isSent() { return SentWordCard::where('word_id', $this->id)->count() >= 1; }
/** * We know what words to push by looking in sentwords * @param $id Category id * @param $words array words from this very category * @return array pushWords */ public function getSentWordsOrigin($id, $words) { $pushWords = []; $state = 1; // $c = new Illuminate\Database\Eloquent\Collection; // dd($c); while ($state <= 1) { $wordsCount = $words->count(); if ($wordsCount == 0) { return []; } $randomWord = $words[rand(0, $wordsCount - 1)]; if (!$randomWord) { continue; } $this->info("Picked random word: " . $randomWord->word . ' (' . $randomWord->id . ')'); if (SentWordCard::where('word_id', $randomWord->id)->where('category_id', $id)->first()) { //GET()? if ($wordsCount == SentWordCard::where('category_id', $id)->count()) { SentWordCard::where('category_id', $id)->delete(); $this->info('Count reached'); } if ($newPushWord = SentWordCard::create(['category_id' => $id, 'word_id' => $randomWord->id])) { $this->comment('New sent word created: ' . $randomWord->id . ' (' . $id . ')'); if ($state == 1) { array_push($pushWords, ['word_id' => $newPushWord->word_id, 'category_id' => $newPushWord->category_id]); $this->info('Sent word pushed to queue: ' . $randomWord->id . ' (' . $id . ')'); } } $state++; } } return $pushWords; }
/** * @param $id Category id * @param $words array words from this very category * @return array pushWords */ public function getSentWords($id, $words) { $pushWords = []; while (count($pushWords) < 3) { $wordsCount = $words->count(); if ($wordsCount < 3) { return []; } $randomWord = $words[rand(0, $wordsCount - 1)]; if (!$randomWord) { continue; } if (SentWordCard::where('word_id', $randomWord->id)->where('category_id', $id)) { if ($wordsCount == SentWordCard::where('category_id', $id)->count()) { SentWordCard::where('category_id', $id)->delete(); } if ($newPushWord = SentWordCard::create(['category_id' => $id, 'word_id' => $randomWord->id])) { array_push($pushWords, $newPushWord->toArray()); } } } return $pushWords; }
/** * Получение слов для словаря * * @return Response */ public function sentwords() { $user = User::find(Input::get('user_id')); $lastWordID = Input::has('id_last_word') ? Input::get('id_last_word') : -1; $words = []; if (!$user) { return $this->respondNotFound('user not found'); } function sortByArray(array $array, array $orderArray) { $ordered = []; foreach ($orderArray as $key => $value) { foreach ($array as $elem) { if ($elem['id'] == $value) { $ordered[] = $elem; } } } return $ordered; } $daywordID = Setting::first()->word_id; if ($lastWordID == '-1') { /* just registered */ $dayWords = SentWordCard::where('category_id', 0)->take(20)->get(); } else { $lastWord = SentWordCard::where('word_id', $lastWordID)->orderBy('id', 'DESC')->first(); /* usual case */ $dayWords = SentWordCard::where('category_id', '0')->where('id', '>', $lastWord->id)->orderBy('id', 'DESC')->take(20)->get(); //@TODO Bug if subscriptions is more than 20 } if ($lastWordID != $daywordID) { foreach ($dayWords as $dayWord) { $dayWordIds[] = $dayWord->word_id; } $tempWords = WordCard::whereIn('id', $dayWordIds)->get()->toArray(); $properOrder = array_reverse(sortByArray($tempWords, $dayWordIds)); foreach ($properOrder as $word) { $word['type'] = 0; $words[] = $word; } //TODO use array map } else { // Inluce day word if there is none $dw = WordCard::find($daywordID)->toArray(); $dw['type'] = 0; $words[0] = $dw; } /* Subscriptions */ foreach ($user->subscriptions as $subscription) { $subs[] = $subscription->id; } if (isset($subs)) { if (isset($lastWord)) { $catwords = SentWordCard::whereIn('category_id', $subs)->where('id', '>', $lastWord->id)->get(); } else { $catwords = SentWordCard::whereIn('category_id', $subs)->get(); } if ($catwords->isEmpty()) { return ['words' => [], 'id_dayword' => $daywordID]; } foreach ($catwords as $catword) { $subCatIDs[] = $catword->word_id; } $tempCatWords = WordCard::whereIn('id', $subCatIDs)->get()->toArray(); foreach ($tempCatWords as $catword) { $catword['type'] = 1; array_push($words, $catword); } } $result['words'] = $words; $result['id_dayword'] = $daywordID; return $result; }