Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/reviewpanel.py
blob: 4fa464918e2975a6f7f1c5140dd9537596884123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# -*- coding: utf-8 -*-
#Copyright (c) 2010, Kirk Winans

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import gtk
from os import environ
from os.path import join, basename
import hippo

import shutil
import tempfile
from gettext import gettext as _
import logging
from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT
from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.icon import Icon
from sugar.graphics.palette import Palette
from port.widgets import ToggleToolButton
from port.widgets import CanvasRoundBox, ToolComboBox
#from port import chooser

from sugar.datastore import datastore
import xml.etree.ElementTree
from datetime import date
import time
_logger = logging.getLogger("test-activity")

_1_DAY = 60 * 60 * 24
_2_DAYS = _1_DAY * 2
_4_DAYS = _2_DAYS * 2
_1_WEEK = _1_DAY * 7
_2_WEEKS = _1_WEEK * 2
_1_MONTH = _2_WEEKS * 2

_REVIEWED = False

#
# Review Panel for the Flashcard Activity
#
class ReviewPanel(gtk.VBox):

    def __init__(self, deckfile, box, catagory, expired_only):
        gtk.VBox.__init__(self)

        self.catagory = catagory
        self.box_number = self.get_box_number(box)
        self.expired_only = expired_only

        self.view_box = gtk.HBox()
        self.entry_box = gtk.HBox()
        self.button_box = gtk.HButtonBox()
        self.answer_box = gtk.HBox()

        self.frontlabel = gtk.Label(_('Front:'))

        self.viewcard = gtk.TextView()
        self.textbuffer = self.viewcard.get_buffer()
        self.viewcard.set_editable(False)
        self.viewcard.set_wrap_mode(gtk.WRAP_WORD)
        self.viewcard.set_justification(gtk.JUSTIFY_CENTER)
        self.viewcard.set_pixels_above_lines(40)

        self.entrylabel = gtk.Label(_('Back:'))

        self.answer_entry = gtk.Entry()
        
        self.answerlabel = gtk.Label(_('The answer was:'))

        self.correct = gtk.TextView()
        self.correcttextbuffer = self.correct.get_buffer()
        self.correct.set_editable(False)
        self.correct.set_wrap_mode(gtk.WRAP_WORD)
        self.correct.set_justification(gtk.JUSTIFY_CENTER)
        self.correct.set_pixels_above_lines(40)

        _checkbutton = ToolButton(tooltip=_('Check Answer!'))
        _checkbutton.connect('clicked', self._check_answer_cb)
        _checkbutton.set_icon_widget(
                self.make_label('next', ' ' + _('Check Answer!')))

        _nextcard = ToolButton(tooltip=_('Next Card'))
        _nextcard.connect('clicked', self._next_card_cb)
        _nextcard.set_icon_widget(
            self.make_label('next', ' ' + _('Next Card')))

        self.view_box.pack_start(self.frontlabel)
        self.view_box.pack_start(self.viewcard)
        
        self.entry_box.pack_start(self.entrylabel)
        self.entry_box.pack_start(self.answer_entry)

        self.button_box.pack_start(_checkbutton)
        self.button_box.pack_start(_nextcard)

        self.answer_box.pack_start(self.answerlabel)
        self.answer_box.pack_start(self.correct)
        

        self.pack_start(self.view_box)
        self.pack_start(self.entry_box)
        self.pack_start(self.answer_box)
        self.pack_start(self.button_box)

        self.frontlabel.show()
        self.viewcard.show()
        
        self.entrylabel.show()
        self.answer_entry.show()

        self.answerlabel.show()
        self.correct.show()

        _checkbutton.show()
        _nextcard.show()

        self.view_box.show()
        self.entry_box.show()
        self.answer_box.show()
        self.button_box.show()

        self.current_date = time.time()

        self.deckfile = deckfile

        self.tree = xml.etree.ElementTree.parse(self.deckfile)

        self.deck = self.get_selected_deck()

        self.current_card_index = 0

        self.card = self.next_card()

        self.card_reviewed = False

        self.textbuffer.set_text(self.card.find("front").text)

    # Returns the box number of the selected box
    def get_box_number(self, box):
        if box == "Every Day":
            return "0"
        elif box == "Every 2 Days":
            return "1"
        elif box == "Every 4 Days":
            return "2"
        elif box == "Every Week":
            return "3"
        elif box == "Every 2 Weeks":
            return "4"
        elif box == "Every Month":
            return "5"
        else:
            return box

        
    # Check answer callback
    def _check_answer_cb(self, button):
        self.correcttextbuffer.set_text(self.check_answer())
        self.card_reviewed = True
        

    def _next_card_cb(self, button):
        if self.card_reviewed:
           self.answer_entry.set_text("") 
           self.card = self.next_card()
           self.textbuffer.set_text(self.card.find("front").text)
           self.correcttextbuffer.set_text("")
           self.card_reviewed = False


    # Checks the answer submitted, returns "Correct" or the back of the card
    def check_answer(self):
        if self.answer_entry.get_text() == self.card.find("back").text:
            if self.expired_only: 
                new_stage = int(self.card.find("stage").text) + 1
                self.update_card(new_stage)
            return "Correct!"

        else:
            if self.expired_only:
                self.update_card(0)
            return self.card.find("back").text

    # Returns the selected deck, based on expiration, 
    # the box, and the category
    def get_selected_deck(self):
        if self.expired_only:
            selected_deck = self.get_all_expired()
        
        else:
            selected_deck = self.tree.getroot()

        selected_deck = self.get_selected_box(selected_deck)

        selected_deck = self.get_selected_catagory(selected_deck)

        return selected_deck
                 

    # Returns all the expired cards
    def get_all_expired(self):
        deck = self.tree.getroot()
        expired_deck = []
        for card in deck:
            if self.card_is_expired(card):
                expired_deck = expired_deck + [card]
        return expired_deck

    # Returns True if the card is expired
    def card_is_expired(self, card):
        card_stage = card.find("stage").text
        card_date = float(card.find("last_reviewed").text)
        date_difference = self.current_date - card_date
        if (card_stage == "0") and (date_difference > _1_DAY):
            return True

        elif (card_stage == "1") and (date_difference > _2_DAYS):
            return True

        elif (card_stage == "2") and (date_difference > _4_DAYS):
            return True

        elif (card_stage == "3") and (date_difference > _1_WEEK):
            return True

        elif (card_stage == "4") and (date_difference > _2_WEEKS):
            return True

        elif (card_stage == "5") and (date_differnce > _1_MONTH):
            return True

        else:
            return False

    # Modifies the deck for the selected box, returns the deck
    def get_selected_box(self, selected_deck):
        if self.box_number != "All":
            selected_box = []
            for card in selected_deck:
                if card.find("stage").text == self.box_number:
                    selected_box = selected_box + [card]
            return selected_box
        else:
            return selected_deck  

    # Modifies the deck for teh selected category, returns the deck
    def get_selected_catagory(self, selected_deck):
        catagory_deck = []
        if self.catagory != "All":
            for card in selected_deck:
                if card.find("catagory").text == self.catagory:
                    catagory_deck = catagory_deck + [card]
            return catagory_deck
        else:
            return selected_deck

    # returns the next card to be reviewed, if there are no more,
    # returns an empty card
    def next_card(self):
        if len(self.deck) > 0 and self.current_card_index < len(self.deck):
            next_card = self.deck[self.current_card_index]

            self.current_card_index += 1

            return next_card

        else:
            return self.gen_empty_card()

            
    # Returns an empty card that states there are no Cards to Review
    def gen_empty_card(self):
        
        empty_card = xml.etree.ElementTree.Element("card")

        e_c_front = xml.etree.ElementTree.Element("front")
        e_c_back = xml.etree.ElementTree.Element("back")
        e_c_last = xml.etree.ElementTree.Element("last_reviewed")
        e_c_stage = xml.etree.ElementTree.Element("stage")
        e_c_catagory = xml.etree.ElementTree.Element("catagory")
    
        e_c_front.text = "No Cards to Review!"
        e_c_back.text = "None"
        e_c_last.text = str(time.time())
        e_c_stage.text = "0"
        e_c_catagory.text = "None"

        empty_card.append(e_c_front)
        empty_card.append(e_c_back)
        empty_card.append(e_c_last)
        empty_card.append(e_c_stage)
        empty_card.append(e_c_catagory)

        return empty_card

    # updates the card after it has been reviewed to reflect
    # the new box and its last reviewed date
    def update_card(self, stage):

        self.card.find("last_reviewed").text = str(time.time())
        self.card.find("stage").text = str(stage)

        self.tree.write(self.deckfile)

    # Gets the icon for the button and makes the label
    # Written by Simon Schampijer
    def make_label(self, icon_name, label):
        label_box = gtk.HBox()
        icon = Icon(
                icon_name=icon_name,
                icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR)
        label_box.pack_start(icon, False)
        label = gtk.Label(label)
        label.modify_fg(gtk.STATE_NORMAL,
                style.COLOR_TOOLBAR_GREY.get_gdk_color())
        label_box.pack_start(label)
        label_box.show_all()
        return label_box