Beispiel #1
0
 /**
  * Checks for flashcard ids in the selection that already exist in the
  * user's deck and remove them. Returns a selection of ids that don't
  * exist in the user's deck.
  * 
  * @return 
  * @param object $selIds
  */
 public static function checkSelection($userId, array $cards)
 {
     // create array of flashcard ids that are in the user's deck
     $userCards = ReviewsPeer::getFlashcardIds($userId);
     // remove ids in the selection that are already in the user's deck
     // to avoid doing INSERTs on existing records
     $dups = array_intersect($cards, $userCards);
     $selK = array();
     foreach ($cards as $id) {
         $selK[(int) $id] = 1;
     }
     foreach ($dups as $dupId) {
         unset($selK[(int) $dupId]);
     }
     return array_keys($selK);
 }
Beispiel #2
0
 /**
  * Add cards in Heisig order.
  * 
  * Selection should be a frame number to add up to,
  * or a number of cards to add "+10", filling in all missing cards in the RTK range.
  * 
  * @param string $selection  "56" (add up to 56), or "+20" (add 20 cards)
  * 
  * @return int   Number of cards in selection (also 0), or false if error
  */
 public function addHeisigRange($userId, $selection)
 {
     $this->itemIds = array();
     // get user's existing flashcard ids in RTK range
     $userCards = ReviewsPeer::getFlashcardIds($userId, 'rtk1+3');
     // map in an array, 1 means card exists, 0 it doesn't
     $inDeck = array();
     foreach ($userCards as $id) {
         $inDeck[(int) $id] = true;
     }
     // add a number of cards, or up to frame number, fill in the missing cards
     if (preg_match('/^\\+([0-9]+)$/', $selection, $matches)) {
         $range = $matches[1];
         if ($range < 1) {
             $this->request->setError('x', 'Invalid range of cards');
             return false;
         }
         for ($i = 1, $n = 0; $n < $range && $i <= rtkBook::MAXKANJI_VOL3; $i++) {
             if (!isset($inDeck[$i])) {
                 $this->itemIds[] = $i;
                 $n++;
             }
         }
     } else {
         $addTo = intval($selection);
         if (!rtkBook::isValidRtkFrameNum($addTo)) {
             $this->request->setError('x', sprintf('Invalid index number: "%s"', $selection));
             return false;
         }
         for ($i = 1; $i <= $addTo; $i++) {
             if (!isset($inDeck[$i])) {
                 $this->itemIds[] = $i;
             }
         }
     }
     return $this->getNumCards();
 }