from AccessControl.SecurityInfo import ClassSecurityInfo from Products.Five import BrowserView from zope.component import getUtility from Products.iva.interfaces import IStatistics, IUserManager, IFLE from Products.iva.interfaces import ICourseContext, IJamArtefact, IJamSession, IMessage, IWebtopFolder, IWebtopItem, INote from AccessControl import getSecurityManager import time from Products.iva.common import translate import datetime class IVA(BrowserView): def __init__(self, context, request): self.request = request self.context = context def getCurrentCourseId(self): """ get current course id for currently logged in user """ um = getUtility(IUserManager) userid = str(getSecurityManager().getUser()) ui = getattr(um, userid, None) if not ui: return 0 return ui.get_jooksev_kursus() def registerVisit(self): #print "--- registering visit" um = getUtility(IUserManager) cid = self.getCurrentCourseId() userLocation = um.kasutajaasukoht(self.request) s = getUtility(IStatistics) s._registerVisit(self.request, cid, userLocation) userid = str(getSecurityManager().getUser()) if INote.providedBy(self.context): self.context.update_reader(userid) s._updateStat(self.request, 'notesRead') self.context.uncache_unread_notes(userid) if ICourseContext.providedBy(self.context): self.context.update_reader(userid) s._updateStat(self.request, 'visit_knowledgebuilding') if IJamArtefact.providedBy(self.context): self.context.update_reader(userid) self.context.uncache_unread_artefacts(userid) if IJamSession.providedBy(self.context): s._updateStat(self.request, 'visit_jamming') if IMessage.providedBy(self.context): self.context.loetud = 1 if IWebtopFolder.providedBy(self.context): if not self.context.hasPermissionToView(self.request): return self.request.RESPONSE.redirect(self.request.HTTP_REFERER) s._updateStat(self.request, 'foldersOpened') id = self.context.getId() if not (id[0]=='c' and id[1:].isdigit()) and id != 'gf': s._viewedObject(self.request, self.context.getRelativeURL(self.context)) elif IWebtopItem.providedBy(self.context): s._viewedObject(self.request, self.context.getRelativeURL(self.context)) return 0 def get_formatted_current_date(self): """Return date formatted depending on user's language.""" return time.strftime(translate(self.context,'short_date_format',default="%Y-%m-%d"), time.localtime()) def get_printable_date(self, dates): """Return date formatted""" #return "11-22-33" return time.strftime(translate(self.context, 'short_date_format', default="%Y-%m-%d"), time.localtime(dates)) def is_logged_in(self, uname): return uname in getUtility(IStatistics).__of__(getUtility(IFLE)).active_users() def get_users_courses(self, uname): """ return user's courses """ #fle = getUtility(IFLE) fle = self.context.fle_root() u = fle.acl_users.getUser(uname) if u is None: return "E"*99 return fle.acl_users._getGroupsForPrincipal(fle.acl_users.getUser(uname)) def get_global_last_active(self, uname): """ return user's last global active time """ val = getUtility(IStatistics).getUserGlobalStat(uname, 'lastAccessGlobal') try: val = float(val) except TypeError: if isinstance(val, type(datetime.datetime(1,2,3))): val = time.mktime(val.utctimetuple()) if val == 0: return "-" return time.strftime(translate(self.context, 'timestamp_format', default="%H:%M %Y-%m-%d"), time.localtime(val)) def search_users(self, first_name='', last_name='', uname='', organization='', exclude=[], sort_on='get_last_name'): """ search users from zcatalog. exclude - excludes usernames, must be a list of username """ #print "cccccccccccccccccccccccccccccccccc", self.context # getObject wants acquisition... we remove getObject and can use getUtility again um = getUtility(IUserManager) #um = self.context.fle_root().fle_users if organization == '-1': organization='' elif not organization: organization='' else: organization = um.getAffiliation(int(organization)) query = { 'get_first_name':first_name, 'get_last_name':last_name, 'get_uname':uname, 'get_organization':organization, } results = um.userinfo_zcatalog(query) tmp = [] if exclude: for x in results: if x.get_uname in exclude: continue tmp.append(x) else: tmp = results #print "results", tmp return tmp def test(self, opt, tru, fals=None): if opt: return tru return fals def lastActive(self, c): """ last active date on course c """ stool = getUtility(IStatistics).__of__(getUtility(IFLE)) tmp = stool.getCourseStatWOUser(int(c.get_id), 'max(lastAccess)') if tmp is None: return 0 return tmp def lastModify(self, c): """ last modification date on course c """ stool = getUtility(IStatistics).__of__(getUtility(IFLE)) tim = float(0) for x in ('modified_bookshelf','modified_knowledgebuilding','modified_jamming'): try: tmp = stool.getCourseStat(int(c.get_id), x) except IndexError: tmp = 0 try: tmp = float(tmp) except TypeError: tmp = float(0) try: tim = float(tim) except TypeError: tim = float(0) if tmp > tim: tim = tmp return float(tim)