/**
* 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>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.user;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.WindowManager;
import org.olat.core.gui.Windows;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.form.Form;
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.elements.MultipleSelectionElement;
import org.olat.core.gui.components.form.flexible.impl.FormBasicController;
import org.olat.core.gui.components.form.flexible.impl.FormEvent;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.DefaultController;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.formelements.CheckBoxElement;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.gui.translator.Translator;
import org.olat.core.helpers.Settings;
import org.olat.core.id.Identity;
import org.olat.core.util.Util;
import org.olat.core.util.prefs.Preferences;
import org.olat.core.util.prefs.PreferencesFactory;
/**
* Initial Date: Apr 27, 2004
*
* @author gnaegi Comment: This controller allows the user to edit the
* preferences of any subject. Make sure you check for security when
* creating this controller since this controller does not have any
* security checks whatsoever.
*/
public class ChangePrefsController extends DefaultController {
private static final String PACKAGE = ChangePrefsController.class.getPackage().getName();
private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(PersonalSettingsController.class);
private VelocityContainer myContent;
private PackageTranslator translator;
private Controller prefsCtr;
private Identity changeableIdentity;
private AjaxForm ajaxForm;
private AccessibilityForm accessibilityForm;
private UserManager um = UserManager.getInstance();
private ReplayUrlsFormController replayUrlsForm;
/**
* Constructor for the change user preferences controller
*
* @param ureq The user request
* @param wControl The current window controller
* @param changeableIdentity The subject whose profile should be changed
*/
public ChangePrefsController(UserRequest ureq, WindowControl wControl, Identity changeableIdentity) {
super(wControl);
this.changeableIdentity = changeableIdentity;
this.translator = new PackageTranslator(PACKAGE, ureq.getLocale());
myContent = new VelocityContainer("settings", VELOCITY_ROOT + "/prefs.html", translator, this);
prefsCtr = new PreferencesFormController(ureq, wControl, changeableIdentity);
prefsCtr.addControllerListener(this);
myContent.put("prefs", prefsCtr.getInitialComponent());
// check if ajax mode is enabled by default for all users
boolean ajaxOnGlobally = Settings.isAjaxGloballyOn();
// check if user has turned on ajax mode
Preferences prefs = PreferencesFactory.getInstance().getPreferencesFor(changeableIdentity, false);
Boolean ajaxOnB = (Boolean) prefs.get(WindowManager.class, "ajax-beta-on");
boolean ajaxOnByPrefs;
if (ajaxOnB == null) {
// indicate that the user settings is overwritten if no pref saved yet
ajaxOnByPrefs = ajaxOnGlobally;
} else {
ajaxOnByPrefs = ajaxOnB.booleanValue();
}
ajaxForm = new AjaxForm("ajaxform", translator, ajaxOnByPrefs, ajaxOnGlobally);
ajaxForm.addListener(this);
myContent.put("ajaxform", ajaxForm);
// load accessibility form
Boolean web2aEnabled = (Boolean) prefs.get(WindowManager.class, "web2a-beta-on");
accessibilityForm = new AccessibilityForm("accessibilityform", translator, (web2aEnabled == null ? Boolean.FALSE : web2aEnabled));
accessibilityForm.addListener(this);
myContent.put("accessibilityform", accessibilityForm);
if (ureq.getUserSession().getRoles().isOLATAdmin()) {
replayUrlsForm = new ReplayUrlsFormController(ureq, getWindowControl(), this.changeableIdentity);
replayUrlsForm.addControllerListener(this);
myContent.put("replayurlsform", replayUrlsForm.getInitialComponent());
}
setInitialComponent(myContent);
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.components.Component,
* org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Component source, Event event) {
if (source == myContent) {
if (event.getCommand().equals("exeBack")) {
fireEvent(ureq, Event.CANCELLED_EVENT);
}
} else if (source == ajaxForm) {
if (event == Form.EVNT_VALIDATION_OK) {
Preferences prefs = ureq.getUserSession().getGuiPreferences();
prefs.putAndSave(WindowManager.class, "ajax-beta-on", Boolean.valueOf(ajaxForm.isAjaxOn()));
getWindowControl().setInfo(translator.translate("preferences.successful"));
// update user session
ureq.getUserSession().getSessionInfo().setWebModeFromUreq(ureq);
//
}
} else if (source == accessibilityForm) {
Preferences prefs = ureq.getUserSession().getGuiPreferences();
ureq.getUserSession().getGuiPreferences();
boolean useWeb2a = accessibilityForm.isWeb2aModeOn();
prefs.putAndSave(WindowManager.class, "web2a-beta-on", useWeb2a);
if (useWeb2a) {
getWindowControl().getWindowBackOffice().getWindowManager().setAjaxEnabled(false);
getWindowControl().getWindowBackOffice().getWindowManager().setForScreenReader(true);
} else {
getWindowControl().getWindowBackOffice().getWindowManager().setForScreenReader(false);
//if user does not have an gui preference it will be only enabled if globally on and browser is capable
Boolean ajaxOn = (Boolean) ureq.getUserSession().getGuiPreferences().get(WindowManager.class, "ajax-beta-on");
if (ajaxOn != null) {
Windows.getWindows(ureq).getWindowManager().setAjaxEnabled(ajaxOn.booleanValue());
} else {
// enable ajax if olat configured and browser matching
Windows.getWindows(ureq).getWindowManager().setAjaxWanted(ureq, true);
}
}
getWindowControl().getWindowBackOffice().getWindow().setDirty(true); // force full page refresh
getWindowControl().setInfo(translator.translate("preferences.successful"));
// update user session
ureq.getUserSession().getSessionInfo().setWebModeFromUreq(ureq);
}
}
public void event(UserRequest ureq, Controller source, Event event) {
if (source == prefsCtr) {
if (event == Event.DONE_EVENT) {
fireEvent(ureq, Event.DONE_EVENT);
} else if (event == Event.CANCELLED_EVENT) {
fireEvent(ureq, Event.CANCELLED_EVENT);
}
}
}
/**
* @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
*/
protected void doDispose() {
if (replayUrlsForm != null) {
replayUrlsForm.dispose();
replayUrlsForm = null;
}
if (prefsCtr != null) {
prefsCtr.dispose();
prefsCtr = null;
}
}
}
class AjaxForm extends Form {
private CheckBoxElement ajaxOnByPreferences;
/**
* @param name Name of the form
*/
public AjaxForm(String name, Translator translator, boolean ajaxOn, boolean ajaxOnGlobally) {
super(name, translator);
CheckBoxElement ajaxOnGloballyEl = new CheckBoxElement("ajaxon.global.label", ajaxOnGlobally);
ajaxOnGloballyEl.setReadOnly(true);
addFormElement("ajaxonglobal", ajaxOnGloballyEl);
ajaxOnByPreferences = new CheckBoxElement("ajaxon.label", ajaxOn);
addFormElement("ajaxon", ajaxOnByPreferences);
addSubmitKey("save", "save");
}
public boolean isAjaxOn() {
return ajaxOnByPreferences.isChecked();
}
public void setAjaxOn(boolean ajaxOn) {
ajaxOnByPreferences.setChecked(ajaxOn);
}
public boolean validate() {
return true;
}
}
class AccessibilityForm extends Form {
private CheckBoxElement web2aModePreferences;
/**
* @param name Name of the form
*/
public AccessibilityForm(String name, Translator translator, boolean web2aModeOn) {
super(name, translator);
web2aModePreferences = new CheckBoxElement("accessibility.web2aMode.label", web2aModeOn);
addFormElement("web2aModeOn", web2aModePreferences);
addSubmitKey("save", "save");
}
public boolean isWeb2aModeOn() {
return web2aModePreferences.isChecked();
}
public boolean validate() {
return true;
}
}
class ReplayUrlsFormController extends FormBasicController {
boolean replayableUrlsOn;
private MultipleSelectionElement checkbox;
Identity changeableIdentity;
private Preferences prefs;
private String IS_ON = "ison";
ReplayUrlsFormController(UserRequest ureqII, WindowControl wControlII, Identity changeableIdentityII) {
super(ureqII, wControlII);
// init value from db props if existent
Boolean replayableUrlsOnByPrefs;
changeableIdentity = changeableIdentityII;
prefs = PreferencesFactory.getInstance().getPreferencesFor(changeableIdentity, false);
replayableUrlsOnByPrefs = (Boolean) prefs.get(WindowManager.class, "replayURLs");
if (replayableUrlsOnByPrefs == null) {
// not saved yet -> false
replayableUrlsOn = false;
} else {
replayableUrlsOn = replayableUrlsOnByPrefs.booleanValue();
}
//
initForm(ureqII);
}
@Override
protected void doDispose() {
// TODO Auto-generated method stub
}
@Override
protected void formOK(UserRequest ureq) {
//
}
@Override
protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) {
if (source == checkbox) {
if (checkbox.isSelected(0)) {
// selected -> true
prefs.putAndSave(WindowManager.class, "replayURLs", Boolean.TRUE);
} else {
// not selected -> false
prefs.putAndSave(WindowManager.class, "replayURLs", Boolean.FALSE);
}
}
}
@Override
protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
checkbox = uifactory.addCheckboxesVertical("checkbox", "replayurl.active", formLayout, new String[] { IS_ON }, new String[] { "" }, null, 1);
checkbox.select(IS_ON, replayableUrlsOn);
// register for on click event to hide/disable other elements
checkbox.addActionListener(listener, FormEvent.ONCLICK);
}
}