# -*- coding: utf-8 # Copyright 2005 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField, TextField from Products.Archetypes.public import LinesWidget, TextAreaWidget, StringWidget, SelectionWidget, RichWidget from Products.Archetypes.public import BaseContent, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Permissions import * from BaseFunctions import BaseFunctions from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME, MODULE_LIST, GRADE_PERCENT schema = BaseSchema + Schema(( LinesField('Target', required=1, searchable=1, accessor="getTarget", isMetadata=1, vocabulary='getClasses', widget=SelectionWidget( format="select", label="Target", label_msgid='label_target', description="Target whos get that task.", description_msgid='desc_target', i18n_domain="krihvel"), ), TextField('Instruction', accessor='getInstruction', isMetadata=0, validators = ('isTidyHtmlWithCleanup',), default_output_type = 'text/x-html-safe', allowable_content_types = ('text/html',), widget = RichWidget( label="Instruction", description="Write here instruction for this task (what student supposed to do)", label_msgid='label_instruction', description_msgid='desc_instruction', visible={'view':'visible','edit':'visible'}, i18n_domain="krihvel", rows = 10) ), StringField('grade', accessor='getGrade', isMetadata=0, default='-', widget = StringWidget( label="Grade", description="", label_msgid='label_grade', description_msgid='desc_grade', visible={'view':'invisible','edit':'invisible'}, i18n_domain="krihvel") ), TextField('comment', accessor='getComment', isMetadata=0, default='-', widget = TextAreaWidget( label="Comment", description="", label_msgid='label_comment', description_msgid='desc_comment', visible={'view':'invisible','edit':'invisible'}, i18n_domain="krihvel") ), )) class BaseTask(BaseContent, BaseFunctions): """ Template tasks. """ meta_type = "BaseTask" archetype_name = "BaseTask" global_allow = 1 allowed_content_types = () security = ClassSecurityInfo() _at_rename_after_creation = True content_icon='task_icon.gif' schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/task_choice', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/task_choice', 'permissions': ('View',), }, ) def createTask(self, REQUEST): """ manage_afterPortalFactoryCreate """ exercise = REQUEST.get("choice") typestool = getToolByName(self, 'portal_types') puf_type = getattr(typestool, exercise, None) if not puf_type: return 0 puf_type.global_allow = 1 self.aq_parent._delObject(self.id) new_id = self.invokeFactory(exercise, id=self.id, title="") puf_type.global_allow = 0 new_obj = getattr(self, new_id) if exercise=="WordMap": new_obj.initializeVariables() return new_obj def getExercises(self): """ All Exercises list """ return MODULE_LIST def getAllImages(self): """ Gets all images from users homefolder and returns list of them """ #query_string = {'Type': ('Image')} #pcatalog = getToolByName(self, 'portal_catalog') #results = pcatalog.searchResults(query_string) #return results user = str(REQUEST.AUTHENTICATED_USER) homefolder = self.portal_membership.getHomeFolder(user) return id def getImagesFromUsersHomeFolder(self, user): """ Gets all images from users homefolder and returns list of them """ user=str(user) homefolder = self.portal_membership.getHomeFolder(user) result =[] for img in homefolder.objectValues('ATImage'): result.append(img) for folder in homefolder.objectValues('ATFolder'): for img in folder.objectValues('ATImage'): result.append(img) return result def getExerciseJS(self, ex): """ Exercise JS""" return "javascript:setChoice('"+ex+"');" def renderStringForJS(self, string): """ render string for JS""" return "'"+str(string)+"'" def renderString2ForJS(self, string1, string2): """ render string for JS""" return "'"+str(string1)+str(string2)+"'" def getAnswers(self, target): """ Answers """ query_string = {'Type': ('PaperAnswer','CrosswordAnswer', 'WordMap'), 'SearchableText': {'operator': 'or', 'query': (target,)}} pcatalog = getToolByName(self, 'portal_catalog') results = pcatalog.searchResults(query_string) return results registerType(BaseTask, PROJECT_NAME) class BaseAnswer(BaseContent, BaseFunctions): """ Template answers. """ meta_type = "BaseAnswer" archetype_name = "BaseAnswer" global_allow = 0 allowed_content_types = [] security = ClassSecurityInfo() content_icon='vastus.gif' schema = schema def calculateGrade(self, percent): """ get rating """ grade = 0 while percent >= GRADE_PERCENT[grade]: grade = grade + 1 return grade def testGrade(self): """ test grade """ if self.grade == '-': return self.calculateGrade(self.getRating()) return self.grade def calculatePercent(self,correct,total): """ calculates percent """ return correct*100/total def saveComment(self, REQUEST): """ Saves Comments """ self.grade = REQUEST.get('grade') self.comment = REQUEST.get('comment') return 1 registerType(BaseAnswer, PROJECT_NAME)