update randomizing/ordering code and forgetCards()

instead of completely resetting a card like we did in resetCards() in the
past, forgetCards() just puts the card back in the new queue and leaves the
factor and revlog alone. If users want to complete reset a card, they'll need to
export it.
This commit is contained in:
Damien Elmes 2011-04-19 15:42:24 +09:00
parent 0df95eafc0
commit 82c3119c90
3 changed files with 45 additions and 28 deletions

View file

@ -231,9 +231,7 @@ qconf=?, conf=?, data=?""",
# flush the fact # flush the fact
fact.id = self.nextID("fid") fact.id = self.nextID("fid")
fact.flush() fact.flush()
# notice any new tags # randomize?
self.registerTags(fact.tags)
# if random mode, determine insertion point
if self.randomNew(): if self.randomNew():
due = random.randrange(0, 1000000) due = random.randrange(0, 1000000)
else: else:

View file

@ -14,7 +14,7 @@ from anki.hooks import runHook
# fixme: on upgrade cards are ordered but order defaults to random # fixme: on upgrade cards are ordered but order defaults to random
# revlog: # revlog:
# types: 0=lrn, 1=rev, 2=relrn, 3=cram, 4=resched # types: 0=lrn, 1=rev, 2=relrn, 3=cram
# positive intervals are in days (rev), negative intervals in seconds (lrn) # positive intervals are in days (rev), negative intervals in seconds (lrn)
# the standard Anki scheduler # the standard Anki scheduler
@ -382,9 +382,9 @@ limit %d""" % (self._groupLimit(), self.reportLimit), lim=self.dayCutoff)
"Remove failed cards from the learning queue." "Remove failed cards from the learning queue."
self.deck.db.execute(""" self.deck.db.execute("""
update cards set update cards set
due = edue, queue = 2 due = edue, queue = 2, mod = %d
where queue = 1 and type = 2 where queue = 1 and type = 2
""") """ % intTime())
# Reviews # Reviews
########################################################################## ##########################################################################
@ -747,21 +747,13 @@ queue = 2 %s and due <= :lim order by %s limit %d""" % (
# Resetting # Resetting
########################################################################## ##########################################################################
# - should remove all scheduling history so we don't show more new ca def forgetCards(self, ids):
def resetCards(self, ids=None): "Put cards back in the new queue."
"Reset progress on cards in IDS."
sql = """ sql = """
update cards set mod=%d, due=fid, type=0, queue=0, ivl=0, factor=0, reps=0, update cards set mod=%d, due=fid, type=0, queue=0, ivl=0, data=''""" % intTime()
lapses=0, grade=0, cycles=0, edue=0, data=''""" sids = ids2str(ids)
sql2 = "delete from revlog" sql += " where id in "+sids
if ids is None: self.deck.db.execute(sql)
lim = ""
else:
sids = ids2str(ids)
sql += " where id in "+sids
sql2 += " where cardId in "+sids
self.deck.db.execute(sql, now=time.time())
self.deck.db.execute(sql2)
if self.deck.randomNew(): if self.deck.randomNew():
# we need to re-randomize now # we need to re-randomize now
self.randomizeNewCards(ids) self.randomizeNewCards(ids)
@ -793,27 +785,27 @@ where id = :id""", vals)
# Reordering # Reordering
########################################################################## ##########################################################################
def randomizeNewCards(self, cardIds=None): def randomizeCards(self, cardIds=None):
"Randomize 'due' on all new cards." "Randomize 'due' on all new cards."
now = time.time() now = intTime()
query = "select distinct fid from cards where reps = 0" query = "select distinct fid from cards where type = 0"
if cardIds: if cardIds:
query += " and id in %s" % ids2str(cardIds) query += " and id in %s" % ids2str(cardIds)
fids = self.deck.db.list(query) fids = self.deck.db.list(query)
data = [{'fid': fid, data = [{'fid': fid,
'rand': random.uniform(0, now), 'rand': random.randrange(0, 1000000),
'now': now} for fid in fids] 'now': now} for fid in fids]
self.deck.db.executemany(""" self.deck.db.executemany("""
update cards update cards
set due = :rand + ord, set due = :rand + ord,
mod = :now mod = :now
where fid = :fid where fid = :fid
and type = 2""", data) and type = 0""", data)
def orderNewCards(self): def orderCards(self):
"Set 'due' to card creation time." "Set 'due' to card creation time."
self.deck.db.execute(""" self.deck.db.execute("""
update cards set update cards set
due = created, due = fid,
mod = :now mod = :now
where type = 2""", now=time.time()) where type = 0""", now=time.time())

View file

@ -663,3 +663,30 @@ def test_groupCounts():
assert tree[1][2] == 2 assert tree[1][2] == 2
assert tree[1][3] == 0 assert tree[1][3] == 0
assert tree[1][4] == 2 assert tree[1][4] == 2
def test_reorder():
d = getEmptyDeck()
# add a fact with default group
f = d.newFact()
f['Front'] = u"one"
d.addFact(f)
assert f.cards()[0].due == f.id
d.sched.randomizeCards()
assert f.cards()[0].due != f.id
d.sched.orderCards()
assert f.cards()[0].due == f.id
def test_forget():
d = getEmptyDeck()
# add a fact with default group
f = d.newFact()
f['Front'] = u"one"
d.addFact(f)
c = f.cards()[0]
c.queue = 2; c.type = 2; c.ivl = 100; c.due = 0
c.flush()
d.reset()
assert d.sched.counts() == (0, 0, 1)
d.sched.forgetCards([c.id])
d.reset()
assert d.sched.counts() == (1, 0, 0)