Back to index

freevial  1.2
database.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 #
00004 # Freevial
00005 # Database Class
00006 #
00007 # Copyright (C) 2007, 2008 The Freevial Team
00008 #
00009 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
00010 #
00011 # This program is free software: you can redistribute it and/or modify
00012 # it under the terms of the GNU General Public License as published by
00013 # the Free Software Foundation, either version 3 of the License, or
00014 # (at your option) any later version.
00015 #
00016 # This program is distributed in the hope that it will be useful,
00017 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 # GNU General Public License for more details.
00020 #
00021 # You should have received a copy of the GNU General Public License
00022 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 #
00024 
00025 from random import shuffle, sample
00026 from copy import deepcopy
00027 import gettext
00028 
00029 class Database:
00030        
00031        def __init__( self, num, name, language, description, players, authors, time, image, sound ):
00032               
00033               # General Database Data
00034               self.num = num       # provisional, should be removed soon
00035               self.name = unicode(name)
00036               self.language = unicode(language)
00037               self.description = unicode(description)
00038               self.players = unicode(players)
00039               self.authors = unicode(authors)
00040               self.time = [ time[0], time[1] ]
00041               self.image = image
00042               self.sound = sound
00043               
00044               # Questions
00045               self._questions = []
00046               self._old_questions = []
00047               
00048               # Internal Variables
00049               self._shuffled = False
00050        
00051        def __len__( self ):
00052               """ Returns the total amount of questions in this database. """
00053               
00054               return len( self._questions ) + len( self._old_questions )
00055        
00056        def _shuffle( self ):
00057               """ Shuffles the question list. """
00058               
00059               shuffle(self._questions)
00060               self._shuffled = True
00061        
00062        def _get_question( self ):
00063               """ Returns the next question in a list, where the first answer
00064                   is the correct one. """
00065               
00066               if len(self._questions) == 0:
00067                      print _(u'All questions in category «%s» have been answered. Reshuffling...') %  self.name
00068                      self._questions = deepcopy(self._old_questions)
00069                      self._old_questions = []
00070                      self.shuffled = False
00071               
00072               if not self._shuffled:
00073                      self._shuffle()
00074               
00075               self._old_questions.append( self._questions.pop() )
00076               
00077               return self._old_questions[-1]
00078        
00079        def addQuestion( self, question, answ1, answ2, answ3, author, comment):
00080               
00081               self._questions.append( [question, answ1, answ2, answ3, 1, author, comment] )
00082        
00083        def question( self ):
00084               """ Returns the next question in a dictionary (with the answers
00085                   in a random position). """
00086               
00087               data = self._get_question()
00088               answer_order = sample(xrange(1, 4), 3)
00089               
00090               question = {}
00091               question['text'] = data[0]
00092               question['opt1'] = data[answer_order[0]]
00093               question['opt2'] = data[answer_order[1]]
00094               question['opt3'] = data[answer_order[2]]
00095               question['answer'] = answer_order.index(1) + 1
00096               question['author'] = data[5]
00097               question['comment'] = data[6]
00098               
00099               return question
00100        
00101        def currentQuestionNumber( self ):
00102               
00103               return len(self._old_questions)