/** * 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(); }