# -*- coding: utf-8 # Copyright 2005 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField, TextField, ReferenceField from Products.Archetypes.public import LinesWidget, TextAreaWidget, StringWidget, MultiSelectionWidget, RichWidget, ReferenceWidget from Widgets import TagsWidget, TagsField 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", index='KeywordIndex:schema', multiValued=1, vocabulary='getClasses', widget=MultiSelectionWidget( format="select", label="Target", label_msgid='label_target', description="Target whos get that task.", description_msgid='desc_target', visible={'view':'invisible','edit':'visible'}, i18n_domain="krihvel"), ), TagsField('tags', accessor = 'getTags', searchable = 1, index='KeywordIndex:schema', widget = TagsWidget( label = 'Tags', label_msgid='label_tags', description="Tags and keywords separated by comma", description_msgid='desc_tags', visible={'view':'invisible','edit':'visible'}, 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") ), ReferenceField('refsToThisTask', accessor = 'getRefsToThisTask', relationship = 'References', mutator = 'addRefsToThisTask', allowed_types = ('MathAnswer','PairsAnswer','PaperAnswer','CrosswordAnswer','FillInAnswer','ImageEditorRasterAnswer','WordMapAnswer'), multiValued = True, widget = ReferenceWidget( visible = {'view':'invisible', 'edit':'invisible'}, ), ), )) 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',), }, { 'id':'report', 'name':'Report', 'action':'string:${object_url}/report_view', 'permissions': ('Modify portal content',), }, ) security.declareProtected('View', 'createTask') def createTask(self, REQUEST): """ manage_afterPortalFactoryCreate """ exercise = REQUEST.get("choice") folder_id = REQUEST.get("folder") #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) user = str(REQUEST.AUTHENTICATED_USER) homefolder = self.portal_membership.getHomeFolder(user) if hasattr(homefolder, folder_id): folder = getattr(homefolder, folder_id) else: folder = homefolder #new_id = folder.invokeFactory(exercise, id=self.id, title="") #puf_type.global_allow = 0 #new_obj = getattr(folder, new_id) #if exercise=="WordMap": # new_obj.initializeVariables() #return new_obj return self.REQUEST.RESPONSE.redirect(folder.absolute_url()+'/createObject?type_name='+exercise) #http://localhost:8080/krihvel/Members/metz/createObject?type_name=BaseTask security.declareProtected('View', 'getExercises') def getExercises(self): """ All Exercises list """ return MODULE_LIST security.declareProtected('View', 'getAllImages') 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 security.declareProtected('View', 'getImagesFromUsersHomeFolder') 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('KFolder'): for img in folder.objectValues('ATImage'): result.append(img) return result security.declareProtected('View', 'getImagesFromGallery') def getImagesFromGallery(self): """ Gets all images from users homefolder and returns list of them """ result =[] gallery = getattr(self, "gallery") for img in gallery.objectValues('ATImage'): result.append(img) for folder in gallery.objectValues('Gallery'): for img in folder.objectValues('ATImage'): result.append(img) return result security.declareProtected('View', 'getExerciseJS') def getExerciseJS(self, ex): """ Exercise JS""" return "javascript:setChoice('"+ex+"');" security.declareProtected('View', 'renderStringForJS') def renderStringForJS(self, string): """ render string for JS""" return "'"+str(string)+"'" security.declareProtected('View', 'renderString2ForJS') def renderString2ForJS(self, string1, string2): """ render string for JS""" return "'"+str(string1)+str(string2)+"'" security.declareProtected('View', 'getAnswers') 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 security.declareProtected('Modify portal content', 'deleteObject') def deleteObject(self): """ delete object """ parent = self.aq_parent #parent._delObject(self.id) return self.REQUEST.RESPONSE.redirect(parent.absolute_url()+'/view') 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 security.declareProtected('View', 'calculateGrade') def calculateGrade(self, percent): """ get rating """ grade = 0 while percent >= GRADE_PERCENT[grade]: grade = grade + 1 return grade security.declareProtected('View', 'testGrade') def testGrade(self): """ test grade """ if self.grade == '-': return self.calculateGrade(self.getRating()) return self.grade security.declareProtected('View', 'calculatePercent') def calculatePercent(self,correct,total): """ calculates percent """ return correct*100/total security.declareProtected('View', 'saveComment') # it is view because students can also mark and comment answers def saveComment(self, REQUEST): """ Saves Comments """ self.grade = REQUEST.get('grade') self.comment = REQUEST.get('comment') return 1 registerType(BaseAnswer, PROJECT_NAME)