Example #1
0
 public static function getRandomCards($sentCards, $take = 20, $category = null)
 {
     $result = [];
     if ($category) {
         $cards = $category->wordcards->toArray();
         $count = $category->wordcards()->count();
     } else {
         $cards = WordCard::all()->toArray();
         $count = WordCard::count();
     }
     $countdown = $count;
     $countdown = 500;
     while (count($result) < $take) {
         //			if ($countdown != 0) $countdown--; else {
         //				return $result;
         //			}
         if (empty($cards)) {
             return $result;
         }
         if (count($cards) < 1) {
             $card = array_shift($cards);
         } else {
             $index = rand(0, $count);
         }
         if (array_key_exists($index, $cards)) {
             $card = $cards[$index];
             unset($cards[$index]);
         } else {
             $card = array_shift($cards);
         }
         if (isset($card)) {
             $sentCardsExists = array_filter($sentCards, function ($sentCard) use($card) {
                 return $sentCard['word_id'] == $card['id'];
             });
             $resultCardsExists = array_filter($result, function ($oldCard) use($card) {
                 return $oldCard['id'] == $card['id'];
             });
             if (count($sentCardsExists) == 0 && count($resultCardsExists) == 0) {
                 array_push($result, $card);
             }
             //				var_dump(count($cards));
             //				var_dump('=============');
         }
     }
     return $result;
 }
Example #2
0
 public function getDayWord()
 {
     $words = WordCard::all();
     $wordsCount = WordCard::count();
     while (true) {
         $randomWord = $words[rand(0, $words->count() - 1)];
         if (!$randomWord) {
             continue;
         }
         if (SentWordCard::where('word_id', $randomWord->id)->where('category_id', 0)) {
             if ($wordsCount == SentWordCard::where('category_id', 0)->count()) {
                 SentWordCard::where('category_id', 0)->delete();
             }
             if ($dayWord = SentWordCard::create(['category_id' => 0, 'word_id' => $randomWord->id])) {
                 break;
             }
         }
     }
     return $dayWord->toArray();
 }