/** * Method to attach notes to users. * * @param notes MoodleNote[] * @return MoodleNote[] * @throws MoodleRestNotesException * @throws MoodleRestException */ public static MoodleNote[] createNotes(MoodleNote[] notes) throws MoodleRestNotesException, MoodleRestException { int processedCount = 0; String functionCall = MoodleCallRestWebService.isLegacy() ? MoodleServices.MOODLE_NOTES_CREATE_NOTES.toString() : MoodleServices.CORE_NOTES_CREATE_NOTES.toString(); try { StringBuilder data = new StringBuilder(); if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestNotesException(); else data.append(MoodleCallRestWebService.getAuth()); data.append("&") .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString())) .append("=") .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString())); for (int i = 0; i < notes.length; i++) { if (notes[i] == null) throw new MoodleRestNotesException(MoodleRestNotesException.NOTES_NULL); if (notes[i].getUserId() == null) throw new MoodleRestNotesException(MoodleRestNotesException.USERID_NOT_SET); else data.append("&") .append( URLEncoder.encode("notes[" + i + "][userid]", MoodleServices.ENCODING.toString())) .append("=") .append( URLEncoder.encode("" + notes[i].getUserId(), MoodleServices.ENCODING.toString())); if (notes[i].getPublishState() == null) throw new MoodleRestNotesException(MoodleRestNotesException.PUBLISHSTATE_NULL); else data.append("&") .append( URLEncoder.encode( "notes[" + i + "][publishstate]", MoodleServices.ENCODING.toString())) .append("=") .append( URLEncoder.encode( notes[i].getPublishState(), MoodleServices.ENCODING.toString())); if (notes[i].getCourseId() == null) throw new MoodleRestNotesException(MoodleRestNotesException.COURSEID_NOT_SET); else data.append("&") .append( URLEncoder.encode( "notes[" + i + "][courseid]", MoodleServices.ENCODING.toString())) .append("=") .append( URLEncoder.encode( "" + notes[i].getCourseId(), MoodleServices.ENCODING.toString())); if (notes[i].getText() == null) throw new MoodleRestNotesException(MoodleRestNotesException.TEXT_NULL); else data.append("&") .append( URLEncoder.encode("notes[" + i + "][text]", MoodleServices.ENCODING.toString())) .append("=") .append(URLEncoder.encode(notes[i].getText(), MoodleServices.ENCODING.toString())); if (notes[i].getFormat() == null) { notes[i].setFormat("text"); } if (notes[i].getFormat().equals("text") || notes[i].getFormat().equals("html")) data.append("&") .append( URLEncoder.encode("notes[" + i + "][format]", MoodleServices.ENCODING.toString())) .append("=") .append(URLEncoder.encode(notes[i].getFormat(), MoodleServices.ENCODING.toString())); else throw new MoodleRestNotesException(MoodleRestNotesException.FORMAT_INCORRECT); if (notes[i].getClientNoteId() != null) data.append("&") .append( URLEncoder.encode( "notes[" + i + "][clientnoteid]", MoodleServices.ENCODING.toString())) .append("=") .append( URLEncoder.encode( notes[i].getClientNoteId(), MoodleServices.ENCODING.toString())); } data.trimToSize(); NodeList elements = MoodleCallRestWebService.call(data.toString()); for (int j = 0; j < elements.getLength(); j += 3, processedCount++) { for (int k = 0; k < 3; k++) { String content = elements.item(j + k).getTextContent(); String nodeName = elements .item(j + k) .getParentNode() .getAttributes() .getNamedItem("name") .getNodeValue(); notes[processedCount].setMoodleNoteField(nodeName, content); } } } catch (UnsupportedEncodingException ex) { Logger.getLogger(MoodleRestNotes.class.getName()).log(Level.SEVERE, null, ex); } return notes; }
public static void updateNotes(MoodleNote[] notes, MoodleWarning[] warnings) throws MoodleRestNotesException, UnsupportedEncodingException, MoodleRestException { if (MoodleCallRestWebService.isLegacy()) throw new MoodleRestNotesException(MoodleRestException.NO_LEGACY); String functionCall = MoodleServices.CORE_NOTES_UPDATE_NOTES.toString(); StringBuilder data = new StringBuilder(); if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestNotesException(); else data.append(MoodleCallRestWebService.getAuth()); data.append("&") .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString())) .append("=") .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString())); for (int i = 0; i < notes.length; i++) { data.append("&") .append(URLEncoder.encode("notes[" + i + "][id]", MoodleServices.ENCODING.toString())) .append("=") .append("" + notes[i].getNoteId()); data.append("&") .append( URLEncoder.encode( "notes[" + i + "][publishstate]", MoodleServices.ENCODING.toString())) .append("=") .append(notes[i].getPublishState()); data.append("&") .append(URLEncoder.encode("notes[" + i + "][text]", MoodleServices.ENCODING.toString())) .append("=") .append(notes[i].getText()); data.append("&") .append(URLEncoder.encode("notes[" + i + "][format]", MoodleServices.ENCODING.toString())) .append("=") .append("" + notes[i].getDescriptionFormat().toInt()); } data.trimToSize(); NodeList elements = MoodleCallRestWebService.call(data.toString()); ArrayList<MoodleWarning> warn = null; MoodleWarning warning = null; for (int j = 0; j < elements.getLength(); j++) { String parent = elements .item(j) .getParentNode() .getParentNode() .getParentNode() .getParentNode() .getAttributes() .getNamedItem("name") .getNodeValue(); String content = elements.item(j).getTextContent(); String nodeName = elements.item(j).getParentNode().getAttributes().getNamedItem("name").getNodeValue(); if (parent.equals("warnings")) { if (nodeName.equals("item")) { if (warn == null) { warn = new ArrayList<MoodleWarning>(); } warning = new MoodleWarning(content); warn.add(warning); } else { warning.setMoodleWarningField(nodeName, content); } } } if (warn != null) { if (warnings != null) { warnings = new MoodleWarning[warn.size()]; warnings = warn.toArray(warnings); } } }
public static void getNotes(Long[] ids) throws MoodleRestNotesException, MoodleRestException, UnsupportedEncodingException { if (MoodleCallRestWebService.isLegacy()) throw new MoodleRestNotesException(MoodleRestException.NO_LEGACY); // MoodleWarning[] warnings=null; String functionCall = MoodleServices.CORE_NOTES_GET_NOTES.toString(); StringBuilder data = new StringBuilder(); if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestNotesException(); else data.append(MoodleCallRestWebService.getAuth()); data.append("&") .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString())) .append("=") .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString())); for (int i = 0; i < ids.length; i++) { if (ids[i] == null) throw new MoodleRestNotesException(); else data.append("&") .append(URLEncoder.encode("notes[" + i + "]", MoodleServices.ENCODING.toString())) .append("=") .append(ids[i]); } data.trimToSize(); NodeList elements = MoodleCallRestWebService.call(data.toString()); // return warnings; }
/** * Returns the HTTP encoded string required for web service authentication. * * <p>Order of authentication methods: token then username/password, if token not initialised or * null if both methods not initialised. * * @return String containing HTTP encoded string or null. * @throws UnsupportedEncodingException */ public static String getAuth() throws UnsupportedEncodingException { StringBuilder data = new StringBuilder(); if (MoodleCallRestWebService.getToken() != null) { data.append(URLEncoder.encode("wstoken", "UTF-8")) .append("=") .append(URLEncoder.encode(MoodleCallRestWebService.getToken(), "UTF-8")); } else { if (MoodleCallRestWebService.getUsername() != null && MoodleCallRestWebService.getPassword() != null) { data.append(URLEncoder.encode("wsusername", "UTF-8")) .append("=") .append(URLEncoder.encode(MoodleCallRestWebService.getUsername(), "UTF-8")); data.append("&"); // Fix by César Martínez data.append(URLEncoder.encode("wspassword", "UTF-8")) .append("=") .append(URLEncoder.encode(MoodleCallRestWebService.getPassword(), "UTF-8")); } else return null; } return data.toString(); }