Index: lams_central/src/java/org/lamsfoundation/lams/web/EtherpadController.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/web/EtherpadController.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/web/EtherpadController.java (revision 5648803ecd6be1b5c3e62d8a574bb1fd39661110) @@ -0,0 +1,67 @@ +package org.lamsfoundation.lams.web; + +import java.util.Map; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.commons.lang.StringUtils; +import org.lamsfoundation.lams.etherpad.EtherpadException; +import org.lamsfoundation.lams.etherpad.service.IEtherpadService; +import org.lamsfoundation.lams.etherpad.util.EtherpadUtil; +import org.lamsfoundation.lams.usermanagement.dto.UserDTO; +import org.lamsfoundation.lams.web.session.SessionManager; +import org.lamsfoundation.lams.web.util.AttributeNames; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping("/etherpad") +public class EtherpadController { + + @Autowired + IEtherpadService etherpadService; + + /** + * Creates or fetches an existing Etherpad pad using API client, sets a cookie. + * + * @param groupId + * LAMS-specific group ID (just any identifier); do not confuse with Etherpad groupId, readOnlyId or + * padId + * @param content + * initial content of Etherpad; ignored if null or Etherpad already exists + * @return etherpad pad ID understood by etherpad API + */ + @RequestMapping(path = "/getPad", produces = "application/json;charset=utf-8") + @ResponseBody + private String getPad(@RequestParam String groupId, @RequestParam(required = false) String content, + HttpServletResponse response) throws EtherpadException { + String preparedContent = EtherpadUtil.preparePadContent(content); + // try to create an Etherpad pad + Map padData = etherpadService.createPad(groupId, preparedContent); + + String etherpadGroupId = (String) padData.get("groupId"); + String etherpadReadOnlyId = (String) padData.get("readOnlyId"); + //don't allow sessions that has had problems with pad initialisation + if (StringUtils.isEmpty(etherpadGroupId) || StringUtils.isEmpty(etherpadReadOnlyId)) { + throw new EtherpadException( + "Etherpad has had problems with initialization. Please seek help from your teacher."); + } + + HttpSession ss = SessionManager.getSession(); + UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER); + + String userName = user.getFirstName() + " " + user.getLastName(); + String authorId = etherpadService.createAuthor(user.getUserID(), userName); + + // create cookie with ALL valid user session IDs so there can be multiple pads on a single page + Cookie cookie = etherpadService.createCookie(authorId, etherpadGroupId, true); + response.addCookie(cookie); + + return (String) padData.get("padId"); + } +} \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/etherpad/service/EtherpadService.java =================================================================== diff -u -ra2af9f04968a74dff35896c0ab309a35a1bbe100 -r5648803ecd6be1b5c3e62d8a574bb1fd39661110 --- lams_common/src/java/org/lamsfoundation/lams/etherpad/service/EtherpadService.java (.../EtherpadService.java) (revision a2af9f04968a74dff35896c0ab309a35a1bbe100) +++ lams_common/src/java/org/lamsfoundation/lams/etherpad/service/EtherpadService.java (.../EtherpadService.java) (revision 5648803ecd6be1b5c3e62d8a574bb1fd39661110) @@ -3,9 +3,12 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.servlet.http.Cookie; @@ -22,7 +25,7 @@ @SuppressWarnings("unchecked") @Override - public Map createPad(String groupIdentifier) throws EtherpadException { + public Map createPad(String groupIdentifier, String content) throws EtherpadException { EPLiteClient client = getClient(); Map result = new HashMap<>(); @@ -55,6 +58,10 @@ result.put("isPadAlreadyCreated", isPadAlreadyCreated); + if (!isPadAlreadyCreated && content != null) { + client.setHTML(padId, content); + } + // gets read-only id String etherpadReadOnlyId = (String) client.getReadOnlyID(padId).get("readOnlyID"); result.put("readOnlyId", etherpadReadOnlyId); @@ -105,22 +112,44 @@ return etherpadSessionCookie; } + @Override + @SuppressWarnings("unchecked") + public Cookie createCookie(String authorId, String etherpadGroupId, boolean includeAllGroups) + throws EtherpadException { + // search for already existing user's session at Etherpad server + Map etherpadSessions = getClient().listSessionsOfAuthor(authorId); + String etherpadSessionId = getExistingSessionID(authorId, etherpadGroupId, etherpadSessions, includeAllGroups); + return createCookie(etherpadSessionId); + } + /** * Returns valid Etherpad session. Returns existing one if finds such one and creates the new one otherwise + * + * @throws EtherpadException */ @Override - @SuppressWarnings("unchecked") public String getExistingSessionID(String authorId, String etherpadGroupId, Map etherpadSessions) throws EtherpadException { - String etherpadSessionId = null; + return getExistingSessionID(authorId, etherpadGroupId, etherpadSessions, false); + } + /** + * Returns valid Etherpad session. Returns existing one if finds such one and creates the new one otherwise + */ + @Override + @SuppressWarnings("unchecked") + public String getExistingSessionID(String authorId, String etherpadGroupId, Map etherpadSessions, + boolean includeAllGroups) throws EtherpadException { // search for already existing user's session boolean isValidForMoreThan1Hour = false; + String etherpadSessionId = null; + Set otherEtherpadSessionIds = new HashSet<>(); for (String etherpadSessionIdIter : etherpadSessions.keySet()) { Map sessessionAttributes = (Map) etherpadSessions .get(etherpadSessionIdIter); String groupIdIter = (String) sessessionAttributes.get("groupID"); - if (groupIdIter.equals(etherpadGroupId)) { + boolean isTargetGroup = groupIdIter.equals(etherpadGroupId); + if (includeAllGroups || isTargetGroup) { // check session expiration date long validUntil = (Long) sessessionAttributes.get("validUntil") * 1000; @@ -129,27 +158,29 @@ //use existing session if it's valid for more than 1 hour if (isValidForMoreThan1Hour) { - etherpadSessionId = etherpadSessionIdIter; - break; - + otherEtherpadSessionIds.add(etherpadSessionIdIter); + if (isTargetGroup) { + etherpadSessionId = etherpadSessionIdIter; + } } else { // can't delete expired sessions as Etherpad throws an exception. Nonetheless it returns expired // ones when client.listSessionsOfAuthor(authorId) is requested } } - } // if session with validity of more than 1 hour doesn't exist yet - create it if (etherpadSessionId == null) { EPLiteClient client = getClient(); Map map2 = client.createSession(etherpadGroupId, authorId, 24); etherpadSessionId = (String) map2.get("sessionID"); + otherEtherpadSessionIds.add(etherpadSessionId); } - return etherpadSessionId; + return includeAllGroups ? otherEtherpadSessionIds.stream().collect(Collectors.joining(",")) : etherpadSessionId; } + @Override @SuppressWarnings("unchecked") public String createAuthor(Integer userId, String userName) throws EtherpadException { EPLiteClient client = getClient(); Index: lams_common/src/java/org/lamsfoundation/lams/etherpad/service/IEtherpadService.java =================================================================== diff -u -r5ce80833bb0828de219fc5b59e9e73c365da8559 -r5648803ecd6be1b5c3e62d8a574bb1fd39661110 --- lams_common/src/java/org/lamsfoundation/lams/etherpad/service/IEtherpadService.java (.../IEtherpadService.java) (revision 5ce80833bb0828de219fc5b59e9e73c365da8559) +++ lams_common/src/java/org/lamsfoundation/lams/etherpad/service/IEtherpadService.java (.../IEtherpadService.java) (revision 5648803ecd6be1b5c3e62d8a574bb1fd39661110) @@ -15,25 +15,45 @@ String PREFIX_REGULAR_GROUP = "LAMS-group-"; /** - * Creates EPLiteClient that will make calls to Etherpad server. Throws DokumaranConfigurationException + * Creates EPLiteClient that will make calls to Etherpad server * - * @throws DokumaranConfigurationException - * if the etherpad is not configured appropriately (either server URL or API key). + * @throws EtherpadException + * if the Etherpad is not configured appropriately (either server URL or API key). */ EPLiteClient getClient() throws EtherpadException; - Map createPad(String groupIdentifier) throws EtherpadException; + /** + * Using API client creates/fetches Etherpad Pad. + * + * @param groupIdentifier + * LAMS-specific group ID (just any identifier); do not confuse with Etherpad groupId, readOnlyId or + * padId + * @param content + * initial content of Etherpad; ignored if null or Etherpad already exists + */ + Map createPad(String groupIdentifier, String content) throws EtherpadException; /** - * Constructs cookie to be stored at a clientside browser. + * Constructs cookie to be stored at a client side browser. */ Cookie createCookie(String etherpadSessionIds) throws EtherpadException; /** + * Constructs cookie to be stored at a client side browser. + */ + Cookie createCookie(String authorId, String etherpadGroupId, boolean includeAllGroup) throws EtherpadException; + + /** * Returns valid Etherpad session. Returns existing one if finds such one and creates the new one otherwise */ String getExistingSessionID(String authorId, String etherpadGroupId, Map etherpadSessions) throws EtherpadException; + /** + * Returns valid Etherpad session. Returns existing one if finds such one and creates the new one otherwise + */ + String getExistingSessionID(String authorId, String etherpadGroupId, Map etherpadSessions, + boolean includeAllGroups) throws EtherpadException; + String createAuthor(Integer userId, String userName) throws EtherpadException; } \ No newline at end of file Index: lams_tool_assessment/web/pages/monitoring/parts/usersummary.jsp =================================================================== diff -u -r91dd1fe3e865ca0e68c7548367cecbd1d58d83b4 -r5648803ecd6be1b5c3e62d8a574bb1fd39661110 --- lams_tool_assessment/web/pages/monitoring/parts/usersummary.jsp (.../usersummary.jsp) (revision 91dd1fe3e865ca0e68c7548367cecbd1d58d83b4) +++ lams_tool_assessment/web/pages/monitoring/parts/usersummary.jsp (.../usersummary.jsp) (revision 5648803ecd6be1b5c3e62d8a574bb1fd39661110) @@ -226,16 +226,16 @@ <%--Display Etherpad for each question --%>
- -
+
-
Index: lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/DokumaranService.java =================================================================== diff -u -r3b951be8d201e41a829d079895d30c9b51fd1dce -r5648803ecd6be1b5c3e62d8a574bb1fd39661110 --- lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/DokumaranService.java (.../DokumaranService.java) (revision 3b951be8d201e41a829d079895d30c9b51fd1dce) +++ lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/DokumaranService.java (.../DokumaranService.java) (revision 5648803ecd6be1b5c3e62d8a574bb1fd39661110) @@ -40,6 +40,7 @@ import org.lamsfoundation.lams.contentrepository.client.IToolContentHandler; import org.lamsfoundation.lams.etherpad.EtherpadException; import org.lamsfoundation.lams.etherpad.service.IEtherpadService; +import org.lamsfoundation.lams.etherpad.util.EtherpadUtil; import org.lamsfoundation.lams.learningdesign.service.ExportToolContentException; import org.lamsfoundation.lams.learningdesign.service.IExportToolContentService; import org.lamsfoundation.lams.learningdesign.service.ImportToolContentException; @@ -742,6 +743,7 @@ Long toolSessionId = session.getSessionId(); Long toolContentId = dokumaran.getContentId(); String groupIdentifier = DokumaranConstants.PREFIX_REGULAR_GROUP + toolSessionId; + String etherpadHtml = null; // in case sharedPadId is present - all sessions will share the same padId if (dokumaran.isSharedPadEnabled()) { @@ -767,31 +769,21 @@ dokumaranSessionDao.saveObject(session); return; } + } else { + etherpadHtml = EtherpadUtil.preparePadContent(dokumaran.getInstructions()); } Map result; + try { - result = etherpadService.createPad(groupIdentifier); + result = etherpadService.createPad(groupIdentifier, etherpadHtml); } catch (EtherpadException e) { throw new DokumaranApplicationException("Exception while creating an etherpad pad", e); } - EPLiteClient client = (EPLiteClient) result.get("client"); String groupId = (String) result.get("groupId"); - String padId = (String) result.get("padId"); - boolean isPadAlreadyCreated = (boolean) result.get("isPadAlreadyCreated"); - - session.setEtherpadGroupId(groupId); - // set initial content - if (!dokumaran.isSharedPadEnabled() || !isPadAlreadyCreated) { - String etherpadHtml = "" - + dokumaran.getInstructions().replaceAll("[\n\r\f]", "").replaceAll(" ", "") - + ""; - client.setHTML(padId, etherpadHtml); - } - - // gets read-only id String readOnlyId = (String) result.get("readOnlyId"); + session.setEtherpadGroupId(groupId); session.setEtherpadReadOnlyId(readOnlyId); dokumaranSessionDao.saveObject(session); Index: lams_tool_doku/web/pages/learning/learning.jsp =================================================================== diff -u -r8f7e92f0c90ad0462aa647b387dec6f14cf5012e -r5648803ecd6be1b5c3e62d8a574bb1fd39661110 --- lams_tool_doku/web/pages/learning/learning.jsp (.../learning.jsp) (revision 8f7e92f0c90ad0462aa647b387dec6f14cf5012e) +++ lams_tool_doku/web/pages/learning/learning.jsp (.../learning.jsp) (revision 5648803ecd6be1b5c3e62d8a574bb1fd39661110) @@ -38,7 +38,7 @@ - + - +