Search in sources :

Example 66 with User

use of org.olat.core.id.User in project openolat by klemens.

the class NotificationHelper method getFormatedName.

/**
 * returns "firstname lastname" or a translated "user unknown" for a given
 * identity
 *
 * @param ident
 * @return
 */
public static String getFormatedName(Identity ident) {
    String formattedName;
    if (ident == null) {
        Translator trans = Util.createPackageTranslator(NotificationNewsController.class, I18nManager.getInstance().getLocaleOrDefault(null));
        return trans.translate("user.unknown");
    } else {
        // Optimize: use from cache to not re-calculate user properties over and over again
        formattedName = userPropertiesCache.get(ident.getKey());
        if (formattedName != null) {
            return formattedName;
        }
    }
    Translator trans = Util.createPackageTranslator(NotificationNewsController.class, I18nManager.getInstance().getLocaleOrDefault(ident.getUser().getPreferences().getLanguage()));
    User user = ident.getUser();
    if (user == null) {
        formattedName = trans.translate("user.unknown");
    } else {
        // grap user properties from context
        List<UserPropertyHandler> propertyHandlers = UserManager.getInstance().getUserPropertyHandlersFor(NotificationHelper.class.getName(), false);
        String[] properties = new String[propertyHandlers.size()];
        for (int i = 0; i < propertyHandlers.size(); i++) {
            UserPropertyHandler propHandler = propertyHandlers.get(i);
            String prop = propHandler.getUserProperty(user, trans.getLocale());
            if (StringHelper.containsNonWhitespace(prop)) {
                properties[i] = prop;
            } else {
                properties[i] = "-";
            }
        }
        formattedName = trans.translate("user.formatted", properties);
    }
    // put formatted name in cache, times out after 5 mins
    userPropertiesCache.put(ident.getKey(), formattedName);
    return formattedName;
}
Also used : User(org.olat.core.id.User) Translator(org.olat.core.gui.translator.Translator) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Example 67 with User

use of org.olat.core.id.User in project openolat by klemens.

the class EvalUserPropertyFunction method call.

/**
 * @see com.neemsoft.jmep.FunctionCB#call(java.lang.Object[])
 */
public Object call(Object[] inStack) {
    /*
		 * argument check
		 */
    if (inStack.length > 2) {
        String name = getFunctionName(functionType);
        return handleException(new ArgumentParseException(ArgumentParseException.NEEDS_FEWER_ARGUMENTS, name, "", "error.fewerargs", "solution.providetwo.attrvalue"));
    } else if (inStack.length < 2) {
        String name = getFunctionName(functionType);
        return handleException(new ArgumentParseException(ArgumentParseException.NEEDS_MORE_ARGUMENTS, name, "", "error.moreargs", "solution.providetwo.attrvalue"));
    }
    /*
		 * argument type check
		 */
    if (!(inStack[0] instanceof String)) {
        String name = getFunctionName(functionType);
        return handleException(new ArgumentParseException(ArgumentParseException.WRONG_ARGUMENT_FORMAT, name, "", "error.argtype.attributename", "solution.example.name.infunction"));
    }
    if (!(inStack[1] instanceof String)) {
        String name = getFunctionName(functionType);
        return handleException(new ArgumentParseException(ArgumentParseException.WRONG_ARGUMENT_FORMAT, name, "", "error.argtype.attribvalue", "solution.example.name.infunction"));
    } else {
        String propValue = (String) inStack[1];
        if (!StringHelper.containsNonWhitespace(propValue)) {
            String name = getFunctionName(functionType);
            return handleException(new ArgumentParseException(ArgumentParseException.WRONG_ARGUMENT_FORMAT, name, "", "error.argtype.attribvalue", "solution.example.whiteSpace"));
        }
    }
    /*
		 * check reference integrity
		 */
    CourseEditorEnv cev = getUserCourseEnv().getCourseEditorEnv();
    if (cev != null) {
        // return a valid value to continue with condition evaluation test
        return defaultValue();
    }
    /*
		 * the real function evaluation which is used during run time
		 */
    String propName = (String) inStack[0];
    String searchValue = (String) inStack[1];
    Identity ident = getUserCourseEnv().getIdentityEnvironment().getIdentity();
    if (ident == null) {
        return defaultValue();
    }
    User user = ident.getUser();
    String userValue = user.getPropertyOrIdentityEnvAttribute(propName, null);
    boolean match = false;
    boolean debug = log.isDebug();
    if (debug) {
        log.debug("value    : " + userValue);
        log.debug("searchValue: " + searchValue);
        log.debug("fT       :  " + functionType);
    }
    if (StringHelper.containsNonWhitespace(userValue)) {
        match = checkPropertyValue(searchValue, userValue);
    }
    if (debug) {
        log.debug("identity '" + ident.getName() + "' tested on properties '" + propName + "' to have value '" + searchValue + "' user's value was '" + userValue + "', match=" + match);
    }
    return match ? ConditionInterpreter.INT_TRUE : ConditionInterpreter.INT_FALSE;
}
Also used : User(org.olat.core.id.User) CourseEditorEnv(org.olat.course.editor.CourseEditorEnv) Identity(org.olat.core.id.Identity)

Example 68 with User

use of org.olat.core.id.User in project openolat by klemens.

the class CustomDBController method exportDb.

private void exportDb(UserRequest ureq, final String category) {
    final ICourse course = CourseFactory.loadCourse(courseKey);
    String label = ExportUtil.createFileNameWithTimeStamp("DBS_" + course.getCourseTitle(), "xls");
    MediaResource export = new OpenXMLWorkbookResource(label) {

        @Override
        protected void generate(OutputStream out) {
            try (OpenXMLWorkbook workbook = new OpenXMLWorkbook(out, 1)) {
                List<CourseDBEntry> content = courseDbManager.getValues(course, null, category, null);
                OpenXMLWorksheet exportSheet = workbook.nextWorksheet();
                // create the headers
                Row headerRow = exportSheet.newRow();
                headerRow.addCell(0, translate("customDb.category"), workbook.getStyles().getHeaderStyle());
                headerRow.addCell(1, translate("customDb.entry.identity"), workbook.getStyles().getHeaderStyle());
                headerRow.addCell(2, translate("customDb.entry.name"), workbook.getStyles().getHeaderStyle());
                headerRow.addCell(3, translate("customDb.entry.value"), workbook.getStyles().getHeaderStyle());
                for (CourseDBEntry entry : content) {
                    User user = entry.getIdentity().getUser();
                    String name = user.getProperty(UserConstants.FIRSTNAME, null) + " " + user.getProperty(UserConstants.LASTNAME, null);
                    Row dataRow = exportSheet.newRow();
                    dataRow.addCell(0, entry.getCategory(), null);
                    dataRow.addCell(1, name, null);
                    if (StringHelper.containsNonWhitespace(entry.getName())) {
                        dataRow.addCell(2, entry.getName(), null);
                    }
                    if (entry.getValue() != null) {
                        dataRow.addCell(3, entry.getValue().toString(), null);
                    }
                }
            } catch (IOException e) {
                logError("", e);
            }
        }
    };
    ureq.getDispatchResult().setResultingMediaResource(export);
}
Also used : User(org.olat.core.id.User) OpenXMLWorkbookResource(org.olat.core.util.openxml.OpenXMLWorkbookResource) OutputStream(java.io.OutputStream) OpenXMLWorkbook(org.olat.core.util.openxml.OpenXMLWorkbook) ICourse(org.olat.course.ICourse) MediaResource(org.olat.core.gui.media.MediaResource) Row(org.olat.core.util.openxml.OpenXMLWorksheet.Row) IOException(java.io.IOException) OpenXMLWorksheet(org.olat.core.util.openxml.OpenXMLWorksheet)

Example 69 with User

use of org.olat.core.id.User in project openolat by klemens.

the class GetUserPropertyFunction method call.

/**
 * @see org.olat.course.condition.interpreter.AbstractFunction#call(java.lang.Object[])
 */
public Object call(Object[] inStack) {
    /*
		 * argument check
		 */
    if (inStack.length > 1) {
        return handleException(new ArgumentParseException(ArgumentParseException.NEEDS_FEWER_ARGUMENTS, name, "", "error.fewerargs", "solution.providetwo.attrvalue"));
    } else if (inStack.length < 1) {
        return handleException(new ArgumentParseException(ArgumentParseException.NEEDS_MORE_ARGUMENTS, name, "", "error.moreargs", "solution.providetwo.attrvalue"));
    }
    /*
		 * argument type check
		 */
    if (!(inStack[0] instanceof String)) {
        return handleException(new ArgumentParseException(ArgumentParseException.WRONG_ARGUMENT_FORMAT, name, "", "error.argtype.attributename", "solution.example.name.infunction"));
    }
    CourseEditorEnv cev = getUserCourseEnv().getCourseEditorEnv();
    if (cev != null) {
        // return emtyp string to continue with condition evaluation test
        return defaultValue();
    }
    Identity ident = getUserCourseEnv().getIdentityEnvironment().getIdentity();
    if (ident == null) {
        return defaultValue();
    }
    User user = ident.getUser();
    String propertyName = (String) inStack[0];
    // always use default locale
    String propertyValue = user.getProperty(propertyName, null);
    // case that we just ignore it.
    return (propertyValue == null ? defaultValue() : propertyValue);
}
Also used : User(org.olat.core.id.User) CourseEditorEnv(org.olat.course.editor.CourseEditorEnv) Identity(org.olat.core.id.Identity)

Example 70 with User

use of org.olat.core.id.User in project openolat by klemens.

the class CoachingDAOTest method getUsers.

@Test
public void getUsers() throws URISyntaxException {
    URL coachingCourseUrl = CoachingLargeTest.class.getResource("CoachingCourse.zip");
    File coachingCourseFile = new File(coachingCourseUrl.toURI());
    RepositoryEntry re1 = CourseFactory.deployCourseFromZIP(coachingCourseFile, UUID.randomUUID().toString(), 4);
    RepositoryEntry re2 = CourseFactory.deployCourseFromZIP(coachingCourseFile, UUID.randomUUID().toString(), 4);
    RepositoryEntry re3 = CourseFactory.deployCourseFromZIP(coachingCourseFile, UUID.randomUUID().toString(), 4);
    dbInstance.commitAndCloseSession();
    // members of courses
    Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("User-Part-1");
    repositoryService.addRole(participant, re1, GroupRoles.participant.name());
    repositoryService.addRole(participant, re2, GroupRoles.participant.name());
    dbInstance.commitAndCloseSession();
    // groups
    BusinessGroup group2 = businessGroupService.createBusinessGroup(null, "Coaching-grp-1", "tg", null, null, false, false, re2);
    businessGroupRelationDao.addRole(participant, group2, GroupRoles.participant.name());
    BusinessGroup group3 = businessGroupService.createBusinessGroup(null, "Coaching-grp-1", "tg", null, null, false, false, re3);
    businessGroupRelationDao.addRole(participant, group3, GroupRoles.participant.name());
    dbInstance.commitAndCloseSession();
    // make statements participant 1
    effManager.createUserEfficiencyStatement(new Date(), 6.0f, true, participant, re1.getOlatResource());
    effManager.createUserEfficiencyStatement(new Date(), 4.0f, false, participant, re2.getOlatResource());
    effManager.createUserEfficiencyStatement(new Date(), 2.0f, false, participant, re3.getOlatResource());
    dbInstance.commitAndCloseSession();
    // make user infos
    userCourseInformationsManager.updateUserCourseInformations(re1.getOlatResource(), participant);
    userCourseInformationsManager.updateUserCourseInformations(re2.getOlatResource(), participant);
    userCourseInformationsManager.updateUserCourseInformations(re3.getOlatResource(), participant);
    dbInstance.commitAndCloseSession();
    // update props
    User partUser = participant.getUser();
    partUser.setProperty(UserConstants.FIRSTNAME, "Rei");
    partUser.setProperty(UserConstants.LASTNAME, "Ayanami");
    partUser.setProperty(UserConstants.EMAIL, "rei.ayanami@openolat.com");
    partUser = userManager.updateUser(partUser);
    dbInstance.commitAndCloseSession();
    List<UserPropertyHandler> userPropertyHandlers = userManager.getUserPropertyHandlersFor(UserListController.usageIdentifyer, false);
    // search by first name
    SearchCoachedIdentityParams params = new SearchCoachedIdentityParams();
    Map<String, String> props = new HashMap<>();
    props.put(UserConstants.FIRSTNAME, "re");
    params.setUserProperties(props);
    List<StudentStatEntry> stats = coachingDAO.getUsersStatisticsNative(params, userPropertyHandlers);
    Assert.assertNotNull(stats);
    Assert.assertFalse(stats.isEmpty());
    // check participant
    StudentStatEntry entryStat = getStudentStatEntry(participant, stats);
    Assert.assertNotNull(entryStat);
    Assert.assertEquals(3, entryStat.getCountRepo());
    Assert.assertEquals(3, entryStat.getInitialLaunch());
    Assert.assertEquals(1, entryStat.getCountPassed());
    Assert.assertEquals(2, entryStat.getCountFailed());
    Assert.assertEquals(0, entryStat.getCountNotAttempted());
    // search by user name
    SearchCoachedIdentityParams loginParams = new SearchCoachedIdentityParams();
    loginParams.setLogin(participant.getName());
    List<StudentStatEntry> loginStats = coachingDAO.getUsersStatisticsNative(loginParams, userPropertyHandlers);
    Assert.assertNotNull(loginStats);
    Assert.assertEquals(1, loginStats.size());
    // check participant
    StudentStatEntry loginStat = loginStats.get(0);
    Assert.assertNotNull(loginStat);
    Assert.assertEquals(3, loginStat.getCountRepo());
    Assert.assertEquals(3, loginStat.getInitialLaunch());
    Assert.assertEquals(1, loginStat.getCountPassed());
    Assert.assertEquals(2, loginStat.getCountFailed());
    Assert.assertEquals(0, loginStat.getCountNotAttempted());
}
Also used : User(org.olat.core.id.User) BusinessGroup(org.olat.group.BusinessGroup) HashMap(java.util.HashMap) RepositoryEntry(org.olat.repository.RepositoryEntry) URL(java.net.URL) Date(java.util.Date) StudentStatEntry(org.olat.modules.coach.model.StudentStatEntry) SearchCoachedIdentityParams(org.olat.modules.coach.model.SearchCoachedIdentityParams) Identity(org.olat.core.id.Identity) File(java.io.File) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler) CoachingLargeTest(org.olat.modules.coach.CoachingLargeTest) Test(org.junit.Test)

Aggregations

User (org.olat.core.id.User)260 Identity (org.olat.core.id.Identity)126 Test (org.junit.Test)82 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)52 HashMap (java.util.HashMap)28 Translator (org.olat.core.gui.translator.Translator)26 SecurityGroup (org.olat.basesecurity.SecurityGroup)20 Date (java.util.Date)18 ArrayList (java.util.ArrayList)16 Locale (java.util.Locale)16 FormItem (org.olat.core.gui.components.form.flexible.FormItem)16 File (java.io.File)14 VelocityContext (org.apache.velocity.VelocityContext)14 MailTemplate (org.olat.core.util.mail.MailTemplate)12 LDAPUser (org.olat.ldap.model.LDAPUser)12 UserManager (org.olat.user.UserManager)12 IOException (java.io.IOException)10 Map (java.util.Map)10 List (java.util.List)8 CloseableModalController (org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)8