# -*- coding: utf-8 # $Id$ # # Copyright 2001, 2002 by IVA Team and contributors # # This file is part of IVA. # # IVA 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 2 of the License, or # (at your option) any later version. # # IVA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with IVA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ Contains class QuizManager. """ __version__ = '$Revision$'[11:-2] import Globals, OFS from Globals import Persistent from AccessControl import ClassSecurityInfo from TraversableWrapper import Traversable from Cruft import Cruft from common import perm_view, perm_edit, perm_manage, perm_add_lo from common import translate class QuizManager( Persistent, OFS.Folder.Folder, Traversable, Cruft ): """ QuizManager - place for quizzes """ security = ClassSecurityInfo() security.declareObjectPublic() meta_type = 'QuizManager' def __init__(self): """ init """ self.id = 'testid' def manage_afterAdd(self, item, container): """ add QTImages """ from QTImages import QTImages from Products.CMFCore.exceptions import BadRequest qti = QTImages() try: self._setObject(qti.id, qti) except BadRequest: pass security.declareProtected(perm_edit, 'index_html') def index_html(self): """ index_html """ return self.testihaldus() security.declareProtected(perm_edit, 'getQuizzes') def getQuizzes(self): """ get quizzes """ return self.objectValues('YlTest') security.declareProtected(perm_edit, 'get_n_of_quizzes') def get_n_of_quizzes(self): """ get number of quizzes """ return len(self.getQuizzes()) security.declareProtected(perm_edit, 'quizmanager_form_handler') def quizmanager_form_handler(self, REQUEST, testinimi='', rename='', delete='', export='', importdata='', results='', showhide='', randomize='', confirmation='', cancel=''): """ quizmanager form handler """ if importdata: return REQUEST.RESPONSE.redirect('qtImportForm') if not testinimi: return if export: return REQUEST.RESPONSE.redirect(self.find_URL_of_fle_root(REQUEST)+\ "/courses/"+ self.get_course_id_from_req(REQUEST)+"/iva_ims_export?testinimi="+testinimi) if rename: return REQUEST.RESPONSE.redirect(testinimi+"/ylTestSettingsForm?liikumisURL="+self.absolute_url()) if delete: if confirmation: self.testid._delObject(testinimi) elif cancel: return REQUEST.RESPONSE.redirect("index_html") else: return self.message_dialog2( title="Confirmation", message="Are you sure you want to delete quiz?", option1_name="cancel", option1_value="Cancel", option2_value="Delete", option2_name="confirmation", extra_value_name='delete', extra_values = ('yes',), item_name='testinimi', item_value=testinimi, handler="quizmanager_form_handler") REQUEST.RESPONSE.redirect("index_html") if export: return REQUEST.RESPONSE.redirect(testinimi+"/vahendus/ylesandedTekstina") if results: return REQUEST.RESPONSE.redirect(testinimi+"/kasutajateTulemused") if showhide: return REQUEST.RESPONSE.redirect(testinimi+"/lubadeViited") if randomize: getattr(self, testinimi).muudaKysimusteJuhuslikJarjekord() return REQUEST.RESPONSE.redirect("index_html") security.declareProtected(perm_edit, 'add_new_quiz') def add_new_quiz(self): """ we're adding new quiz. it returns 1. in YlTest it returns 0 """ return 1 security.declareProtected(perm_edit, 'add_quiz') def add_quiz(self, REQUEST): """ add quiz """ from YlTest import YlTest if not hasattr(self, 'nr'): self.nr=0 while hasattr(self.aq_self, 'test'+str(self.nr)): self.nr=self.nr+1 t=YlTest() t.id="test"+str(self.nr) self._setObject(t.id, t) t.meta.language=self.giveLanguage(REQUEST) return getattr(self, t.id) security.declareProtected(perm_view, 'getTypesPretty') def getTypesPretty(self, REQUEST): """ return types """ m=[translate(self, 'quiz', target=self.giveLanguage(REQUEST)), translate(self, 'survey', target=self.giveLanguage(REQUEST)), translate(self, 'selftest', target=self.giveLanguage(REQUEST))] return m security.declareProtected(perm_view, 'getAnswerVisibilityPretty') def getAnswerVisibilityPretty(self): """ return texts when answers are visible """ return ["only after the quiz has been closed", "always", "never"] security.declareProtected(perm_edit, 'add_new_quiz_handler') def add_new_quiz_handler(self, REQUEST, pealkiri='', kirjeldus='', tyyp=0, nahtavus=0, liikumisURL='', juhujarjestus=0, testipass='', lahendusaegPiiratud=0, lahendusaegTunnid=24, lahendusaegMinutid=0, ): """ add new quiz handler """ from input_checks import is_valid_title if not is_valid_title(pealkiri): return self.message_dialog( message='Invalid title', action='ylTestSettingsForm?pealkiri=%s&kirjeldus=%s&tyyp=%s&nahtavus=%s&juhujarjestus=%s&testipass=%s&lahendusaegPiiratud=%s&lahendusaegTunnid=%s&lahendusaegMinutid=%s' % (pealkiri,kirjeldus, tyyp, nahtavus, juhujarjestus, testipass, lahendusaegPiiratud, lahendusaegTunnid, lahendusaegMinutid) ) error = 0 if lahendusaegPiiratud: try: int(lahendusaegTunnid) except ValueError: error = 1 try: int(lahendusaegMinutid) except ValueError: error = 1 if error: return self.message_dialog( message='Time limit must be numeric!!', action='ylTestSettingsForm?pealkiri=%s&kirjeldus=%s&tyyp=%s&nahtavus=%s&juhujarjestus=%s&testipass=%s&lahendusaegPiiratud=%s&lahendusaegTunnid=%s&lahendusaegMinutid=%s' % (pealkiri,kirjeldus, tyyp, nahtavus, juhujarjestus, testipass, lahendusaegPiiratud, lahendusaegTunnid, lahendusaegMinutid) ) q = self.add_quiz(REQUEST) q.quiz_settings_handler(REQUEST, pealkiri, kirjeldus, tyyp, nahtavus, liikumisURL, juhujarjestus, testipass, lahendusaegPiiratud, lahendusaegTunnid, lahendusaegMinutid) return REQUEST.RESPONSE.redirect(self.absolute_url()) security.declareProtected(perm_view, 'isQuizClosed') def isQuizClosed(self): """ not public yet. This is default value """ return 1 security.declareProtected(perm_view, 'isQuizTimelimited') def isQuizTimelimited(self): """ quiz has timelimit. This is default value """ return 0 security.declareProtected(perm_view, 'isQuizPublicForGroups') def isQuizPublicForGroups(self): """ quiz is public for groups. This is default value """ return 0 security.declareProtected(perm_view, 'isQuizPublicForUsers') def isQuizPublicForUsers(self): """ quiz is available for some users. This is default value """ return 0 security.declareProtected(perm_edit, 'import_form_handler') def import_form_handler(self, REQUEST, file): """ import quizzes """ uname = REQUEST.AUTHENTICATED_USER.getUserName() if not file: return "file not found" from ImportIMS import Importer import tempfile, os from StringIO import StringIO out = StringIO() filename = tempfile.mktemp() f = open(filename,"w+b") f.write(file.read()) f.close() import_data=None imported = Importer(uname,self.fle_root(),'',getattr(self.fle_root().courses, self.get_course_id_from_req(REQUEST)),testonly=1,kasuta_jooksvat=1, out=out) imported.loadZip(filename) element = imported.loadFile('imsmanifest.xml') imported.processFile(self,element) # we can show "out" here but we just redirect os.remove(filename) return REQUEST.RESPONSE.redirect('index_html') Globals.InitializeClass(QuizManager)