# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Group Object """ __version__ = "$Revision:33$"[11:-2] import string from random import choice import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo from OFS.SimpleItem import SimpleItem from zope.interface import implements from Products.ZCatalog.CatalogPathAwareness import CatalogAware from interfaces import IGroup class Group(CatalogAware, SimpleItem, Persistent, Explicit): """ """ meta_type = 'Group' security = ClassSecurityInfo() security.declareObjectPublic() implements(IGroup) manage_options = SimpleItem.manage_options def __init__(self, id): self.id=id self.grouptests = [] self.default_catalog = 'cat_groups' security.declarePrivate('manage_afterAdd') def manage_afterAdd(self, item, container): self.index_object() def setTitle(self, val): self.title = val def setDescription(self, val): self.description = val def setUser(self, user): self.user = str(user) def getUser(self): """ """ if not hasattr(self, 'user'): self.user = self.getOwnerTuple()[1] return self.user def setMembers(self, REQUEST): """ """ chars = string.letters + string.digits members = [] groupmembers = self.getMembers() count = int(REQUEST.get('count', 0)) mem_length = len(self.getMembers()) + 1 existedMembers = [] withUnValidEmail = [] for i in range(1, count+1): email = REQUEST.get('email'+str(i), None) fname = REQUEST.get('firstname'+str(i), None) lname = REQUEST.get('lastname'+str(i), None) if email == None and fname == None and lname == None: continue # mem_length += 1 if i < mem_length: # kui tegelane juba grupis, siis võta uid ja muuda infot student_uid = REQUEST.get('member'+str(i)) student = self.getStudentById(student_uid) if student['email'] == email: # kui kasutaja maili ei ole muudetud self.students._changeStudentInfo(student_uid, fname, lname, email) else: # kui on siis kontrolli ega uus mail juba kasutusel ei ole s_uid = self.students._checkStudent(email) if s_uid == None: # kui ei ole, siis saab muuta self.students._changeStudentInfo(student_uid, fname, lname, email) else: # kui on, siis maili muuta ei saa #print 'teavita hiljem kasutajat ka, et kellelgi teisel juba see aadress kasutusel ja muuta ei saa' self.students._changeStudentInfo(student_uid, fname, lname, student['email']) members.append(student_uid) else: # kui ei siis kontrolli maili ja kui sellise mailiga tegelast ei ole siis lisa student_uid = self.students._checkStudent(email) if student_uid == None: import re email = REQUEST.get('email'+str(i)) minfo = {} student_uid = '' for x in range(25): student_uid += choice(chars) minfo['uid'] = student_uid minfo['firstname'] = REQUEST.get('firstname'+str(i), '') minfo['lastname'] = REQUEST.get('lastname'+str(i), '') minfo['email'] = REQUEST.get('email'+str(i), '') minfo['ans_containers'] = [] if email != None: if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email): self.students._addStudent(minfo) members.append(student_uid) # check does this group has any tests and if has create student tokens and answer folders for those tests ?? self.createAnswerTestFolders(student_uid) else: withUnValidEmail.append(email) #print 'Email is not valid' else: # kui sellise mailiga tegelane on siis jätan meelde ja hiljem küsin kas lisan existedMembers.append(student_uid) #self.students._changeStudentInfo(student_uid, fname, lname) self.members = members self.setExisted(existedMembers) if existedMembers != []: return self.restrictedTraverse('members_confirm_page.html')(members=existedMembers) else: return REQUEST.RESPONSE.redirect('group_members.html') def createAnswerTestFolders(self, student_uid): #print "create folder for: ", student_uid tests = self.getTests() for test_id in tests: #print "test id: ", test_id test = self.getTestById(test_id) test.createMemberTokenFolder(student_uid, self.getId()) def setExisted(self, val): self.existed = val def getExisted(self): return self.existed def addExistedMembers(self, REQUEST, yes_button='', no_button=''): """ """ if yes_button: existed = self.getExisted() members = self.getMembers() for s in existed: members.append(s) self.createAnswerTestFolders(s) self.changeMemberInfo(members) self.setExisted(val=[]) return REQUEST.RESPONSE.redirect('group_members.html') def removeMember(self, student_uid): """ """ if student_uid != '': groupmembers = self.getMembers() for m in groupmembers: if m == student_uid: groupmembers.remove(m) self.changeMemberInfo(groupmembers) def changeMemberInfo(self, val): self.members = val def getTitle(self): return self.title def getDescription(self): return getattr(self, 'description', '') def getMembers(self): return getattr(self, 'members', []) def changeGroupInfo(self, REQUEST): title = REQUEST.get('group_title') desc = REQUEST.get('group_desc') self.setTitle(title) self.setDescription(desc) return REQUEST.RESPONSE.redirect(self.getId()+'/group_members.html') def readNames(self, namefile, REQUEST): """ read names from a file """ import csv chars = string.letters + string.digits members = self.getMembers() names = csv.reader(namefile, delimiter=',', quotechar='"') existedMembers = [] for name_info in names: email = name_info[2].strip() student_uid = self.students._checkStudent(email) fname = name_info[0].strip() lname = name_info[1].strip() if student_uid == None: minfo = {} student_uid = '' for x in range(25): student_uid += choice(chars) minfo['uid'] = student_uid minfo['firstname'] = name_info[0].strip() minfo['lastname'] = name_info[1].strip() minfo['email'] = name_info[2].strip() minfo['ans_containers'] = [] if minfo['email'] != '': self.students._addStudent(minfo) members.append(student_uid) else: #self.students._changeStudentInfo(student_uid, fname, lname) existedMembers.append(student_uid) self.members = members self.setExisted(existedMembers) if existedMembers != []: return self.restrictedTraverse('members_confirm_page.html')(members=existedMembers) return REQUEST.RESPONSE.redirect('group_members.html') def searchStudents(self, REQUEST): groupmembers = self.getMembers() searchword = REQUEST.get('searchword', None) if searchword == None: return [] allstudents = self.students.getAllStudents() results = [] for s in allstudents: if searchword in s.get('firstname') or searchword in s.get('lastname') or searchword in s.get('email'): if not s.get('uid') in groupmembers: results.append(s) return results def addMembers(self, REQUEST, add_members=''): """ """ if isinstance(add_members, type('')): add_members = [add_members,] members = self.getMembers() for m in add_members: members.append(m) self.members = members return REQUEST.RESPONSE.redirect('group_members.html') def addTest(self, test_id): grouptests = self.getTests() grouptests.append(test_id) self.setGroupTests(grouptests) def setGroupTests(self, tests): self.grouptests = tests def getTests(self): return getattr(self,'grouptests',[]) def getGroupStatsFile(self): """ """ tests = self.getTests() text = ';' for test_id in tests: test = self.getTestById(test_id) text += test.getTitle() + ';' gmembers = self.getMembers() for member_id in gmembers: member = self.getStudentById(member_id) mem_folders = member['ans_containers'] text += '\n'+member['email']+';' for test_id in tests: container = self.getContainerByTestId(mem_folders, test_id, self.getId()) sessions = container.getSessions() done = container.checkDoneOrNot() if done == True: test = self.getTestById(test_id) user_points = test.getUserTestPoints(sessions) test_points = test.getTestMaxPoints() text += self.roundNumber((user_points/test_points)*100)+'% ;' self.REQUEST.RESPONSE.setHeader('content-type', 'text/txt') self.REQUEST.RESPONSE.setHeader('Content-disposition','attachment; filename=group_statistics.txt') return text def getUserStatsFile(self, memberid): """ """ text = ';' member = self.getStudentById(memberid) mem_folders = member['ans_containers'] for folder_id in mem_folders: container = self.getContainerById(folder_id) test_id = container.getTestId() test = self.getTestById(test_id) text += test.getTitle() + '; ' text += '\n' + member['email'] + '; ' for folder_id in mem_folders: container = self.getContainerById(folder_id) done = container.checkDoneOrNot() if done == True: test_id = container.getTestId() test = self.getTestById(test_id) sessions = container.getSessions() user_points = test.getUserTestPoints(sessions) test_points = test.getTestMaxPoints() text += self.roundNumber((user_points/test_points)*100)+'%; ' self.REQUEST.RESPONSE.setHeader('content-type', 'text/txt') filename = member['firstname']+'_'+member['lastname']+'_statistics.txt' self.REQUEST.RESPONSE.setHeader('Content-disposition','attachment; filename='+filename) return text Globals.InitializeClass(Group) #EOF