Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* frentix GmbH, Switzerland, http://www.frentix.com
* <p>
*/
package org.olat.user.propertyhandlers;
import java.util.Locale;
import java.util.Map;
import org.olat.core.gui.components.form.ValidationError;
import org.olat.core.gui.components.form.flexible.FormItem;
import org.olat.core.gui.components.form.flexible.FormItemContainer;
import org.olat.core.gui.components.form.flexible.FormUIFactory;
import org.olat.core.gui.components.form.flexible.elements.SelectionElement;
import org.olat.core.gui.components.form.flexible.elements.SingleSelection;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.User;
import org.olat.core.util.Util;
import org.olat.user.AbstractUserPropertyHandler;
import org.olat.user.UserManager;
/**
*
* A User Property Handler for a Yes/No Property.<br />
* (originated from http://jira.openolat.org/browse/OO-98)
*
* This renders two radio-buttons with "yes" "no" options. (which brings a clear
* distinction from the homeprofile (vcard) checkbox in the homeprofileform)
*
* @author strentini, sergio.trentini@frentix.com, http://www.frentix.com
* @date 30.01.2012
*
*/
public class GenericYesNoPropertyHandler extends AbstractUserPropertyHandler {
private static final String KEY_YES = "gchpr.y";
private static final String KEY_NO = "gchpr.n";
private static final String VAL_YES = "true";
private static final String VAL_NO = "false";
@Override
public FormItem addFormItem(Locale locale, User user, String usageIdentifyer, boolean isAdministrativeUser, FormItemContainer formItemContainer) {
SelectionElement sElem = null;
Translator trans = Util.createPackageTranslator(this.getClass(), locale);
sElem = FormUIFactory.getInstance().addRadiosHorizontal(getName(), i18nFormElementLabelKey(), formItemContainer,
new String[] { KEY_YES, KEY_NO }, new String[] { trans.translate("yes"), trans.translate("no") });
// pre-select yes/no
String internalValue = getInternalValue(user);

srosse
committed
if (isValidValue(user, internalValue, null, null)) {
if (VAL_YES.equals(internalValue))
sElem.select(KEY_YES, true);
if (VAL_NO.equals(internalValue))
sElem.select(KEY_NO, true);
}
UserManager um = UserManager.getInstance();
if (um.isUserViewReadOnly(usageIdentifyer, this) && !isAdministrativeUser) {
sElem.setEnabled(false);
}
if (um.isMandatoryUserProperty(usageIdentifyer, this)) {
sElem.setMandatory(true);
}
return sElem;
}
@Override
public void updateUserFromFormItem(User user, FormItem formItem) {
String internalValue = getStringValue(formItem);
setInternalValue(user, internalValue);
}
@Override

srosse
committed
public boolean isValid(User user, FormItem formItem, Map<String,String> formContext) {
// the formItem is always valid. if no radio-button is selected, the
// value is just "NO"
// this is also ok if item is mandatory...
return true;
}
@Override
public String getUserProperty(User user, Locale locale) {
Translator trans = Util.createPackageTranslator(this.getClass(), locale);
String internalValue = getInternalValue(user);
if (VAL_YES.equals(internalValue))
return trans.translate("yes");
if (VAL_NO.equals(internalValue))
return trans.translate("no");
return "-";
}
@Override

srosse
committed
public boolean isValidValue(User user, String value, ValidationError validationError, Locale locale) {
// a value is valid if it is either val_no or val_yes
return (VAL_NO.equals(value) || VAL_YES.equals(value));
}
@Override
public String getStringValue(FormItem formItem) {
if (formItem instanceof SingleSelection) {
SingleSelection sItem = (SingleSelection) formItem;
if (sItem.isSelected(0))
return VAL_YES;
}
return VAL_NO;
}
@Override
public String getStringValue(String displayValue, Locale locale) {
return displayValue;
}
}