Index: tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/IMdlChoiceService.java =================================================================== diff -u -r416e2087ef866a7d0ebc58e3d7e307a65ced335f -r7a10d149f243fd4df6f17cf754f1a35b7cb483e5 --- tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/IMdlChoiceService.java (.../IMdlChoiceService.java) (revision 416e2087ef866a7d0ebc58e3d7e307a65ced335f) +++ tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/IMdlChoiceService.java (.../IMdlChoiceService.java) (revision 7a10d149f243fd4df6f17cf754f1a35b7cb483e5) @@ -45,6 +45,7 @@ public static final String EXT_SERVER_METHOD_EXPORT = "export"; public static final String EXT_SERVER_METHOD_OUTPUT = "output"; public static final String EXT_SERVER_METHOD_EXPORT_PORTFOLIO = "export_portfolio"; + public static final String EXT_SERVER_METHOD_EXPORT_GET_CHOICES = "getoptions"; /** * Makes a copy of the default content and assigns it a newContentID @@ -172,8 +173,8 @@ * @param toolSessionId * @return */ - public int getExternalToolOutputInt(String outputName, MdlChoice mdlChoice, Long userId, String extToolContentId, - Long toolSessionId); + public boolean getExternalToolOutputBoolean(String outputName, MdlChoice mdlChoice, Long userId, String extToolContentId, + Long toolSessionId, String choiceID); /** * Converts the customCSV parameter into a hashmap Index: tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/MdlChoiceOutputFactory.java =================================================================== diff -u -rbfce66d52d56f00f5cdb64d3240af2d282fb8528 -r7a10d149f243fd4df6f17cf754f1a35b7cb483e5 --- tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/MdlChoiceOutputFactory.java (.../MdlChoiceOutputFactory.java) (revision bfce66d52d56f00f5cdb64d3240af2d282fb8528) +++ tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/MdlChoiceOutputFactory.java (.../MdlChoiceOutputFactory.java) (revision 7a10d149f243fd4df6f17cf754f1a35b7cb483e5) @@ -21,71 +21,135 @@ */ package org.lamsfoundation.lams.tool.mdchce.service; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; +import org.lamsfoundation.lams.learningdesign.BranchCondition; import org.lamsfoundation.lams.tool.OutputFactory; +import org.lamsfoundation.lams.tool.OutputType; import org.lamsfoundation.lams.tool.ToolOutput; import org.lamsfoundation.lams.tool.ToolOutputDefinition; import org.lamsfoundation.lams.tool.exception.ToolException; +import org.lamsfoundation.lams.tool.mdchce.dto.MdlChoiceOutputDTO; import org.lamsfoundation.lams.tool.mdchce.model.MdlChoice; +import org.lamsfoundation.lams.tool.mdchce.model.MdlChoiceUser; + /** * Creates the output definitions for mdlchoice tool Adapter tool. */ public class MdlChoiceOutputFactory extends OutputFactory { - protected final static String OUTPUT_NAME_LEARNER_CHOICE = "learner.choice.output"; + protected final static String LEARNER_CHOICE_OUTPUT = "learner.choice.output"; - public MdlChoiceOutputFactory() { + public MdlChoiceOutputFactory() { + } + + /** + * @see org.lamsfoundation.lams.tool.OutputDefinitionFactory#getToolOutputDefinitions(java.lang.Object) + */ + public SortedMap getToolOutputDefinitions(Object toolContentObject) + throws ToolException { + + TreeMap definitionMap = new TreeMap(); + + if (toolContentObject != null) { + List choices = (List) toolContentObject; + + if (choices == null || choices.size() == 0) + { + log.error("Unable to build output definitions for moodel choice, no options have been added yet"); + } + else{ + ToolOutputDefinition definition = buildBooleanSetOutputDefinition(LEARNER_CHOICE_OUTPUT); + if (definition.getDefaultConditions() == null) + definition.setDefaultConditions(new ArrayList()); + + List defaultConditions = definition.getDefaultConditions(); + + int order = 1; + for (MdlChoiceOutputDTO dto : choices) { + String name = buildConditionName(LEARNER_CHOICE_OUTPUT, "" + dto.getId()); + defaultConditions.add(new BranchCondition(null, null, new Integer(order++), name, dto.getChoice(), + OutputType.OUTPUT_BOOLEAN.toString(), null, null, Boolean.TRUE.toString())); + order++; + } + + definition.setShowConditionNameOnly(Boolean.TRUE); + definitionMap.put(LEARNER_CHOICE_OUTPUT, definition); + } + } else { + log.error("Unable to build output definitions for Vote as no tool content object supplied."); } - /** - * @see org.lamsfoundation.lams.tool.OutputDefinitionFactory#getToolOutputDefinitions(java.lang.Object) - */ - public SortedMap getToolOutputDefinitions( - Object toolContentObject) throws ToolException { - TreeMap definitionMap = new TreeMap(); + return definitionMap; + } - ToolOutputDefinition definition = buildLongOutputDefinition( - OUTPUT_NAME_LEARNER_CHOICE); - definitionMap.put(OUTPUT_NAME_LEARNER_CHOICE, definition); + public SortedMap getToolOutput(List names, IMdlChoiceService dlChoiceService, + Long toolSessionId, Long learnerId, MdlChoice mdlChoice, Long extSessionId) { - return definitionMap; + TreeMap map = new TreeMap(); + if (names == null || names.contains(LEARNER_CHOICE_OUTPUT)) { + map.put(LEARNER_CHOICE_OUTPUT, getExtToolOutput(LEARNER_CHOICE_OUTPUT, dlChoiceService, + mdlChoice, learnerId, extSessionId.toString(), toolSessionId)); } + return map; - public SortedMap getToolOutput(List names, - IMdlChoiceService dlChoiceService, Long toolSessionId, - Long learnerId, MdlChoice mdlChoice, Long extSessionId) { + } - TreeMap map = new TreeMap(); - if (names == null || names.contains(OUTPUT_NAME_LEARNER_CHOICE)) { - map.put(OUTPUT_NAME_LEARNER_CHOICE, getExtToolOutput( - OUTPUT_NAME_LEARNER_CHOICE, dlChoiceService, mdlChoice, - learnerId, extSessionId.toString(), toolSessionId)); - } - return map; + public ToolOutput getToolOutput(String name, IMdlChoiceService dlChoiceService, Long toolSessionId, Long learnerId, + MdlChoice getToolOutput, Long extSessionId) { + if (name != null) { + return getExtToolOutput(name, dlChoiceService, getToolOutput, learnerId, extSessionId.toString(), + toolSessionId); } + return null; - public ToolOutput getToolOutput(String name, - IMdlChoiceService dlChoiceService, Long toolSessionId, - Long learnerId, MdlChoice getToolOutput, Long extSessionId) { - if (name != null) { - return getExtToolOutput(name, dlChoiceService, getToolOutput, - learnerId, extSessionId.toString(), toolSessionId); - } - return null; + } + public ToolOutput getExtToolOutput(String outputName, IMdlChoiceService mdlChoiceService, MdlChoice mdlChoice, + Long userId, String extToolContentId, Long toolSessionId) { + + MdlChoiceUser user = mdlChoiceService.getUserByUserIdAndSessionId(userId, toolSessionId); + boolean condition = this.isChoiceSelected(outputName, mdlChoiceService, mdlChoice, userId, extToolContentId, toolSessionId); + return new ToolOutput(outputName, getI18NText(outputName, true), condition); + } + + + private boolean isChoiceSelected(String outputName, IMdlChoiceService mdlChoiceService, MdlChoice mdlChoice, + Long userId, String extToolContentId, Long toolSessionId) + { + String[] dcNames = splitConditionName(outputName); + if (dcNames[1] == null || dcNames[1].length() == 0) { + log.error("Unable to convert the display order to an int for tool output " + + LEARNER_CHOICE_OUTPUT + + ". Returning false. Name doesn't contain the display order. Condition name was: " + outputName); + return false; } + + String choiceOutputName = ""; + String choiceId = ""; + try { + choiceOutputName = dcNames[0]; + choiceId = dcNames[1]; + } catch (Exception e) { + log.error("Problem retrieving outputs" + + LEARNER_CHOICE_OUTPUT + + ". Returning false. Number format exception thrown. Condition name was: " + outputName, e); + return false; + } - public ToolOutput getExtToolOutput(String outputName, - IMdlChoiceService mdlChoiceService, MdlChoice mdlChoice, - Long userId, String extToolContentId, Long toolSessionId) { - int number = mdlChoiceService.getExternalToolOutputInt(outputName, - mdlChoice, userId, extToolContentId, toolSessionId); - return new ToolOutput(outputName, getI18NText(outputName, true), - new Long(number)); + if (userId != null) { + + return mdlChoiceService.getExternalToolOutputBoolean(choiceOutputName, mdlChoice, userId, extToolContentId, + toolSessionId, choiceId); } + return false; + } + } Index: tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/MdlChoiceService.java =================================================================== diff -u -r416e2087ef866a7d0ebc58e3d7e307a65ced335f -r7a10d149f243fd4df6f17cf754f1a35b7cb483e5 --- tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/MdlChoiceService.java (.../MdlChoiceService.java) (revision 416e2087ef866a7d0ebc58e3d7e307a65ced335f) +++ tool_adapters/moodle/lams_tool_mdlchoice/src/java/org/lamsfoundation/lams/tool/mdchce/service/MdlChoiceService.java (.../MdlChoiceService.java) (revision 7a10d149f243fd4df6f17cf754f1a35b7cb483e5) @@ -27,27 +27,24 @@ import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; +import java.io.StringReader; import java.net.URLConnection; import java.net.URLEncoder; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.SortedMap; -import java.util.TreeMap; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + import org.apache.log4j.Logger; import org.lamsfoundation.lams.contentrepository.AccessDeniedException; import org.lamsfoundation.lams.contentrepository.ICredentials; @@ -73,10 +70,14 @@ import org.lamsfoundation.lams.tool.ToolOutputDefinition; import org.lamsfoundation.lams.tool.ToolSessionExportOutputData; import org.lamsfoundation.lams.tool.ToolSessionManager; +import org.lamsfoundation.lams.tool.exception.DataMissingException; +import org.lamsfoundation.lams.tool.exception.SessionDataExistsException; +import org.lamsfoundation.lams.tool.exception.ToolException; import org.lamsfoundation.lams.tool.mdchce.dao.IMdlChoiceConfigItemDAO; import org.lamsfoundation.lams.tool.mdchce.dao.IMdlChoiceDAO; import org.lamsfoundation.lams.tool.mdchce.dao.IMdlChoiceSessionDAO; import org.lamsfoundation.lams.tool.mdchce.dao.IMdlChoiceUserDAO; +import org.lamsfoundation.lams.tool.mdchce.dto.MdlChoiceOutputDTO; import org.lamsfoundation.lams.tool.mdchce.model.MdlChoice; import org.lamsfoundation.lams.tool.mdchce.model.MdlChoiceConfigItem; import org.lamsfoundation.lams.tool.mdchce.model.MdlChoiceSession; @@ -85,16 +86,18 @@ import org.lamsfoundation.lams.tool.mdchce.util.MdlChoiceException; import org.lamsfoundation.lams.tool.mdchce.util.MdlChoiceToolContentHandler; import org.lamsfoundation.lams.tool.mdchce.util.WebUtility; -import org.lamsfoundation.lams.tool.exception.DataMissingException; -import org.lamsfoundation.lams.tool.exception.SessionDataExistsException; -import org.lamsfoundation.lams.tool.exception.ToolException; import org.lamsfoundation.lams.tool.service.ILamsToolService; import org.lamsfoundation.lams.usermanagement.dto.UserDTO; import org.lamsfoundation.lams.util.FileUtil; import org.lamsfoundation.lams.util.HashUtil; import org.lamsfoundation.lams.util.audit.IAuditService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; /** * An implementation of the IMdlChoiceService interface. @@ -192,8 +195,8 @@ */ public Long copyExternalToolContent(HashMap params) throws ToolException, Exception { - String cloneServletUrl = mdlChoiceConfigItemDAO.getConfigItemByKey(MdlChoiceConfigItem.KEY_EXTERNAL_TOOL_SERVLET) - .getConfigValue(); + String cloneServletUrl = mdlChoiceConfigItemDAO.getConfigItemByKey( + MdlChoiceConfigItem.KEY_EXTERNAL_TOOL_SERVLET).getConfigValue(); // add the method to the params params.put(EXT_SERVER_PARAM_METHOD, EXT_SERVER_METHOD_CLONE); @@ -259,8 +262,8 @@ } } - public int getExternalToolOutputInt(String outputName, MdlChoice mdlChoice, Long userId, String extToolContentId, - Long toolSessionId) { + public boolean getExternalToolOutputBoolean(String outputName, MdlChoice mdlChoice, Long userId, String extToolContentId, + Long toolSessionId, String optionID) { MdlChoiceUser user = this.getUserByUserIdAndSessionId(userId, toolSessionId); ExtServerOrgMap extServerMap = getExtServerOrgMap(); @@ -276,10 +279,11 @@ params.put(EXT_SERVER_PARAM_EXT_TOOL_CONTENT_ID, extToolContentId); params.put(EXT_SERVER_PARAM_METHOD, EXT_SERVER_METHOD_OUTPUT); params.put(EXT_SERVER_PARAM_OUTPUT_NAME, URLEncoder.encode(outputName, "UTF8")); + params.put("optionID", URLEncoder.encode(optionID, "UTF8")); InputStream is = WebUtility.getResponseInputStreamFromExternalServer(outputServletUrl, params); BufferedReader isReader = new BufferedReader(new InputStreamReader(is)); - int ret = Integer.parseInt(isReader.readLine()); + boolean ret = Boolean.parseBoolean(isReader.readLine()); return ret; } catch (Exception e) { logger.debug("Failed getting external output", e); @@ -316,8 +320,8 @@ return null; } - return mdlChoiceOutputFactory.getToolOutput(name, this, toolSessionId, learnerId, session.getMdlChoice(), session - .getExtSessionId()); + return mdlChoiceOutputFactory.getToolOutput(name, this, toolSessionId, learnerId, session.getMdlChoice(), + session.getExtSessionId()); } /** @@ -336,9 +340,74 @@ if (mdchce == null) { mdchce = getDefaultContent(); } - return mdlChoiceOutputFactory.getToolOutputDefinitions(mdchce); + + List choices = getPossibleChoices(mdchce); + + //HashMap map = new HashMap(); + //map.put("toolContent", mdchce); + //map.put("choices", choices); + + return mdlChoiceOutputFactory.getToolOutputDefinitions(choices); } + private List getPossibleChoices(MdlChoice mdchce) { + try { + if (mdchce.getExtToolContentId() == null) + { + return new ArrayList(); + } + + String servletUrl = mdlChoiceConfigItemDAO + .getConfigItemByKey(MdlChoiceConfigItem.KEY_EXTERNAL_TOOL_SERVLET).getConfigValue(); + + // Get the required params, then call the eternal server + HashMap params = this.getRequiredExtServletParams(mdchce); + params.put(EXT_SERVER_PARAM_METHOD, EXT_SERVER_METHOD_EXPORT_GET_CHOICES); + params.put(EXT_SERVER_PARAM_EXT_TOOL_CONTENT_ID, mdchce.getExtToolContentId().toString()); + + // Make the request + InputStream is = WebUtility.getResponseInputStreamFromExternalServer(servletUrl, params); + BufferedReader isReader = new BufferedReader(new InputStreamReader(is)); + String str = isReader.readLine(); + if (str == null) { + throw new UserInfoFetchException("Fail to clone choice in .LRN:" + + " - No data returned from external server"); + } + + return getChoicesFromString(str); + + } catch (Exception e) { + logger.error("Failed to call external server to copy tool content" + e); + throw new ToolException("Failed to call external server to copy tool content" + e); + } + + } + + private List getChoicesFromString(String string) { + try { + List choices = new ArrayList(); + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document document = db.parse(new InputSource(new StringReader(string))); + NodeList list = document.getElementsByTagName("choice"); + + for (int i = 0; i < list.getLength(); i++) { + NamedNodeMap markerNode = ((Node) list.item(i)).getAttributes(); + String choice = markerNode.getNamedItem("option").getNodeValue(); + long id = Long.parseLong(markerNode.getNamedItem("optionID").getNodeValue()); + MdlChoiceOutputDTO dto = new MdlChoiceOutputDTO(id, choice); + choices.add(dto); + } + + return choices; + + } catch (Exception e) { + logger.error("Problem parsing choices xml", e); + throw new ToolException("Problem parsing choices xml" + e); + } + } + public String hash(ExtServerOrgMap serverMap, String extUsername, String timestamp) { String serverId = serverMap.getServerid(); String serverKey = serverMap.getServerkey(); @@ -649,8 +718,8 @@ Object toolPOJO = exportContentService.importToolContent(toolContentPath, mdlChoiceToolContentHandler, fromVersion, toVersion); if (!(toolPOJO instanceof MdlChoice)) - throw new ImportToolContentException("Import MdlChoice tool content failed. Deserialized object is " - + toolPOJO); + throw new ImportToolContentException( + "Import MdlChoice tool content failed. Deserialized object is " + toolPOJO); MdlChoice mdlChoice = (MdlChoice) toolPOJO; // reset it to new toolContentId mdlChoice.setToolContentId(toolContentId);