+++ /dev/null
-/************************************************************************************
- *
- * Copyright (c) 2008 Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel.
- *
- * Ce logiciel est diffuse sous les termes et conditions de la licence CECILL.
- * Voir le fichier joint Licence_CeCILL_V2-fr.txt pour plus de details.
- *
- ************************************************************************************/
-package com.pentila.evalcomp.manager.ressource.moodle;
-
-import java.math.BigInteger;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.rmi.RemoteException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
-import com.pentila.evalcomp.domain.definition.RessourceDefinition;
-import com.pentila.evalcomp.domain.transit.RessourceFolder;
-import com.pentila.evalcomp.domain.transit.RessourceItem;
-import com.pentila.evalcomp.domain.transit.RessourceMoodleCourse;
-import com.pentila.evalcomp.moodlews.CourseRecord;
-import com.pentila.evalcomp.moodlews.EditUsersInput;
-import com.pentila.evalcomp.moodlews.GetCoursesReturn;
-import com.pentila.evalcomp.moodlews.GetUsersReturn;
-import com.pentila.evalcomp.moodlews.LoginReturn;
-import com.pentila.evalcomp.moodlews.MoodleWSBindingStub;
-import com.pentila.evalcomp.moodlews.UserDatum;
-import com.pentila.evalcomp.moodlews.UserRecord;
-
-// TODO: Auto-generated Javadoc
-/**
- * The Class DefinitionManager.
- */
-public class MoodleWebService {
-
- /** The moodle wsurl. */
- private String moodleWSURL = "http://localhost:8890/moodle19/wspp/service_pp.php";
-
- /** The user login. */
- private String userLogin = "admin";
-
- /** The user pwd. */
- private String userPWD = "12345";
-
- /** The moodle base url. */
- private String moodleBaseURL = "http://193.48.120:8890/moodle19/course/view.php?id={0}";
-
- /** The role teacher. */
- private String roleTeacher = "3";
-
- /** The role student. */
- private String roleStudent = "5";
-
- /** The stub. */
- private static MoodleWSBindingStub stub = null;
-
- /** The sesskey. */
- private static String sesskey = "";
-
- /** The client. */
- private static BigInteger client = null;
-
- /**
- * Instantiates a new moodle web service.
- */
- public MoodleWebService() {
- super();
- }
-
- /**
- * Moodle login.
- *
- * @return true, if successful
- */
- private boolean moodleLogin() {
- URL endPoint;
- try {
- endPoint = new URL(moodleWSURL);
- } catch (MalformedURLException e1) {
- return false;
- }
-
- try {
- stub = new MoodleWSBindingStub(endPoint, null);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
-
- LoginReturn loginReturn;
- try {
- loginReturn = stub.login(userLogin, userPWD);
- } catch (RemoteException e) {
- return false;
- }
- sesskey = loginReturn.getSessionkey();
- client = loginReturn.getClient();
- return true;
- }
-
- /**
- * Moodle logout.
- *
- * @return true, if successful
- */
- private static boolean moodleLogout() {
- boolean logoutReturn;
- try {
- logoutReturn = stub.logout(client, sesskey);
- } catch (RemoteException e) {
- return false;
- }
- return logoutReturn;
- }
-
- /**
- * Gets the workspace.
- *
- * @param login the login
- *
- * @return the workspace
- */
- public RessourceItem getWorkspace(String login) {
- // TODO Auto-generated method stub
- RessourceFolder moodleCourses = new RessourceFolder();
- moodleCourses.setEditable(false);
-
- moodleCourses.setUrl("");
- boolean log = moodleLogin();
- if (log == false) {
- return moodleCourses;
- }
- List<RessourceItem> results = new ArrayList<RessourceItem>();
- try {
- results = _getMoodleCourses(login);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- moodleCourses.setItemList(results);
- moodleLogout();
- return moodleCourses;
- }
-
- /**
- * _get moodle courses.
- *
- * @param login the login
- *
- * @return the list< ressource item>
- *
- * @throws RemoteException the remote exception
- */
- public List<RessourceItem> _getMoodleCourses(String login)
- throws RemoteException {
-
- List<RessourceItem> tcourses = new ArrayList<RessourceItem>();
- GetCoursesReturn myCourses = stub.get_my_courses_byusername(client,
- sesskey, login, "fullname");
- for (CourseRecord course : myCourses.getCourses()) {
- RessourceMoodleCourse myTcourse = getTCourseRecordFromCourseRecord(course);
- myTcourse.setPath(moodleBaseURL);
- tcourses.add(myTcourse);
- }
- return tcourses;
- }
-
- /**
- * Sets the permission.
- *
- * @param rd the rd
- * @param suid the suid
- * @param role the role
- */
- public void setPermission(RessourceDefinition rd, Set<String> suid,
- String role) {
-
- String roleId = roleTeacher;
- if (!role.equals("")) {
- roleId = roleStudent;
- }
- boolean log = moodleLogin();
- if (log == true) {
- for (String username : suid) {
- try {
- assignRoleToUserInCourse(username, rd.getRessourceKey(),
- roleId);
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- moodleLogout();
- }
-
- /**
- * Removes the permission.
- *
- * @param rd the rd
- * @param suid the suid
- * @param role the role
- */
- public void removePermission(RessourceDefinition rd, Set<String> suid,
- String role) {
-
- String roleId = roleTeacher;
- if (!role.equals("")) {
- roleId = roleStudent;
- }
- boolean log = moodleLogin();
- if (log == true) {
- for (String username : suid) {
- try {
- unassignRoleToUserInCourse(username, rd.getRessourceKey(),
- roleId);
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- moodleLogout();
- }
-
- /**
- * Assign role to user in course.
- *
- * @param username the username
- * @param courseid the courseid
- * @param roleid the roleid
- *
- * @throws RemoteException the remote exception
- */
- private static void assignRoleToUserInCourse(String username,
- String courseid, String roleid) throws RemoteException {
- UserRecord myNewUser = getUser(username);
- if (myNewUser.getId().equals(new BigInteger("0"))) {
- // si le user n'existe pas nous le creons
- addUser(username, true);
- myNewUser = getUser(username);
- }
- String[] userids = new String[1];
- userids[0] = myNewUser.getUsername();
- stub
- .assignUsers(client, sesskey, courseid, userids, "username",
- roleid);
- }
-
- /**
- * Unassign role to user in course.
- *
- * @param username the username
- * @param courseid the courseid
- * @param roleid the roleid
- *
- * @throws RemoteException the remote exception
- */
- private static void unassignRoleToUserInCourse(String username,
- String courseid, String roleid) throws RemoteException {
- UserRecord myNewUser = getUser(username);
- if (!myNewUser.getId().equals(new BigInteger("0"))) {
- String[] userids = new String[1];
- userids[0] = myNewUser.getUsername();
- stub.unassignUsers(client, sesskey, courseid, userids, "username",
- roleid);
- }
- }
-
- /**
- * Gets the user.
- *
- * @param username the username
- *
- * @return the user
- *
- * @throws RemoteException the remote exception
- */
- private static UserRecord getUser(String username) throws RemoteException {
- GetUsersReturn myUser = stub.get_user(client, sesskey, username,
- "username");
- UserRecord[] usersFound = myUser.getUsers();
- for (UserRecord uf : usersFound) {
- if (uf.getUsername().equals(username)) {
- return uf;
- }
- }
- return usersFound[0];
- }
-
- /**
- * Adds the user.
- *
- * @param username the username
- * @param casAuth the cas auth
- *
- * @throws RemoteException the remote exception
- */
- private static void addUser(String username, boolean casAuth)
- throws RemoteException {
-
- EditUsersInput usersToAdd = new EditUsersInput();
- UserDatum[] usersDatumToAdd = new UserDatum[1];
- UserRecord myNewUser = getUser(username);
- if (myNewUser.getId().equals(new BigInteger("0"))) {
- UserDatum myUserToAdd = getUserDatumFromUserRecord(myNewUser);
- myUserToAdd.setAction("Add");
- myUserToAdd.setUsername(username);
- myUserToAdd.setDescription("");
- if (casAuth) {
- myUserToAdd.setAuth("cas");
- } else {
- myUserToAdd.setAuth("manual");
- }
-
- usersDatumToAdd[0] = myUserToAdd;
- usersToAdd.setUsers(usersDatumToAdd);
-
- stub.edit_users(client, sesskey, usersToAdd);
- }
-
- }
-
- /**
- * Gets the user datum from user record.
- *
- * @param ur the ur
- *
- * @return the user datum from user record
- */
- private static UserDatum getUserDatumFromUserRecord(UserRecord ur) {
- String description = "none";
- if (ur.getDescription() != null) {
- description = ur.getDescription();
- }
- UserDatum ud = new UserDatum("Update", ur.getConfirmed(), ur
- .getPolicyagreed(), ur.getDeleted(), ur.getUsername(),
- "manual", "", ur.getIdnumber(), ur.getFirstname(), ur
- .getLastname(), ur.getEmail(), ur.getIcq(), ur
- .getSkype(), ur.getYahoo(), ur.getAim(), ur.getMsn(),
- ur.getPhone1(), ur.getPhone2(), ur.getInstitution(), ur
- .getDepartment(), ur.getAddress(), ur.getCity(), ur
- .getCountry(), ur.getLang(), ur.getTimezone(), ur
- .getLastip(), description, new BigInteger("1"));
- return ud;
- }
-
- /**
- * Gets the t course record from course record.
- *
- * @param cr the cr
- *
- * @return the t course record from course record
- */
- private RessourceMoodleCourse getTCourseRecordFromCourseRecord(
- CourseRecord cr) {
- RessourceMoodleCourse tcr = new RessourceMoodleCourse(cr.getError(), cr
- .getId(), cr.getCategory(), cr.getSortorder(),
- cr.getPassword(), cr.getFullname(), cr.getShortname(), cr
- .getIdnumber(), cr.getSummary(), cr.getFormat(), cr
- .getShowgrades(), cr.getNewsitems(), cr.getTeacher(),
- cr.getTeachers(), cr.getStudent(), cr.getStudents(), cr
- .getGuest(), cr.getStartdate(), cr.getEnrolperiod(), cr
- .getNumsections(), cr.getMarker(), cr.getMaxbytes(), cr
- .getVisible(), cr.getHiddensections(), cr
- .getGroupmode(), cr.getGroupmodeforce(), cr.getLang(),
- cr.getTheme(), cr.getCost(), cr.getTimecreated(), cr
- .getTimemodified(), cr.getMetacourse());
- return tcr;
- }
-
- /**
- * Gets the moodle wsurl.
- *
- * @return the moodle wsurl
- */
- public String getMoodleWSURL() {
- return moodleWSURL;
- }
-
- /**
- * Sets the moodle wsurl.
- *
- * @param moodleWSURL the new moodle wsurl
- */
- public void setMoodleWSURL(String moodleWSURL) {
- this.moodleWSURL = moodleWSURL;
- }
-
- /**
- * Gets the user login.
- *
- * @return the user login
- */
- public String getUserLogin() {
- return userLogin;
- }
-
- /**
- * Sets the user login.
- *
- * @param userLogin the new user login
- */
- public void setUserLogin(String userLogin) {
- this.userLogin = userLogin;
- }
-
- /**
- * Gets the user pwd.
- *
- * @return the user pwd
- */
- public String getUserPWD() {
- return userPWD;
- }
-
- /**
- * Sets the user pwd.
- *
- * @param userPWD the new user pwd
- */
- public void setUserPWD(String userPWD) {
- this.userPWD = userPWD;
- }
-
- /**
- * Gets the moodle base url.
- *
- * @return the moodle base url
- */
- public String getMoodleBaseURL() {
- return moodleBaseURL;
- }
-
- /**
- * Sets the moodle base url.
- *
- * @param moodleBaseURL the new moodle base url
- */
- public void setMoodleBaseURL(String moodleBaseURL) {
- this.moodleBaseURL = moodleBaseURL;
- }
-
- /**
- * Gets the role teacher.
- *
- * @return the role teacher
- */
- public String getRoleTeacher() {
- return roleTeacher;
- }
-
- /**
- * Sets the role teacher.
- *
- * @param roleTeacher the new role teacher
- */
- public void setRoleTeacher(String roleTeacher) {
- this.roleTeacher = roleTeacher;
- }
-
- /**
- * Gets the role student.
- *
- * @return the role student
- */
- public String getRoleStudent() {
- return roleStudent;
- }
-
- /**
- * Sets the role student.
- *
- * @param roleStudent the new role student
- */
- public void setRoleStudent(String roleStudent) {
- this.roleStudent = roleStudent;
- }
-
- /**
- * Return the url to the moodle ressource.
- *
- * @param pluginKey the plugin key
- * @param ressourceKey the ressource key
- * @param mode the mode
- *
- * @return the ressource url
- */
- public String getRessourceUrl(String pluginKey, String ressourceKey,
- String mode) {
- String url = "";
- if (mode.equals("")) {
- // acces utilisateur simple
- url = getMoodleBaseURL() + "/course/view.php?id=" + ressourceKey;
- } else {
- // acces teacher donc visualisation des resultats
- boolean log = moodleLogin();
- if (log == true) {
- try {
- UserRecord myUser = getUser(mode);
- url = getMoodleBaseURL()
- + "/course/user.php?mode=complete&id="
- + ressourceKey + "&user="
- + myUser.getId().toString();
- } catch (RemoteException e) {
- // On retourne l'acces vers le cours
- url = getMoodleBaseURL() + "/course/view.php?id="
- + ressourceKey;
- e.printStackTrace();
- }
- } else {
- url = getMoodleBaseURL() + "/course/view.php?id="
- + ressourceKey;
- }
- }
- return url;
- }
-
- /****** METHODES ISSUES DES TESTS DE COMMUNICATION AVEC MOODLE *****/
- /*
- * public static void main(String[] args) throws RemoteException,
- * MalformedURLException {
- *
- * moodleLogin();
- *
- * //modifyCourseTest(); //listUsersTest(); //UserRecord myUser =
- * getUserTest("stagiaire1"); //delUserTest(myUser);
- * //addUserTest("stagiaire1", true); // 5 --> student // 3 --> teacher
- * //assignRoleToUserInCourseTest("stagiaire1", "7", "5");
- * //assignRoleToUserInCourseTest("stagiaire1", "7", "3");
- * unassignRoleToUserInCourseTest("stagiaire1", "7", "5");
- *
- * moodleLogout();
- *
- * }
- *
- * private static boolean moodleLogin() { URL endPoint; try { endPoint = new
- * URL("http://localhost:8890/moodle19/wspp/service_pp.php"); } catch
- * (MalformedURLException e1) { return false; }
- *
- * try { stub = new MoodleWSBindingStub(endPoint, null); } catch (AxisFault
- * e) { e.printStackTrace(); }
- *
- * LoginReturn loginReturn; try { loginReturn = stub.login("admin",
- * "12345"); } catch (RemoteException e) { return false; } sesskey =
- * loginReturn.getSessionkey(); client = loginReturn.getClient(); return
- * true; }
- *
- * private static boolean moodleLogout() { boolean logoutReturn; try {
- * logoutReturn = stub.logout(client, sesskey); } catch (RemoteException e)
- * { return false; } return logoutReturn; }
- *
- * private static void assignRoleToUserInCourseTest(String username, String
- * courseid, String roleid) throws RemoteException { UserRecord myNewUser =
- * getUserTest(username); if (!myNewUser.getId().equals(new
- * BigInteger("0"))) { String[] userids = new String[1]; userids[0] =
- * myNewUser.getUsername(); AssignUsersReturn ret = stub.assignUsers(client,
- * sesskey, courseid, userids, "username", roleid);
- * System.out.println(ret.getUsers().length); } }
- *
- * private static void unassignRoleToUserInCourseTest(String username,
- * String courseid, String roleid) throws RemoteException { UserRecord
- * myNewUser = getUserTest(username); if (!myNewUser.getId().equals(new
- * BigInteger("0"))) { String[] userids = new String[1]; userids[0] =
- * myNewUser.getUsername(); UnassignUsersReturn ret =
- * stub.unassignUsers(client, sesskey, courseid, userids, "username",
- * roleid); System.out.println(ret.getUsers().length); } }
- *
- * private static void listUsersTest() throws RemoteException {
- *
- * GetUsersReturn myUser = stub.get_user(client, sesskey, null, null);
- * UserRecord[] usersFound = myUser.getUsers();
- * System.out.println("------"); for (UserRecord uf: usersFound) {
- * System.out.println(uf.getUsername()); System.out.println(uf.getId()); }
- * System.out.println("------");
- *
- * }
- *
- * private static UserRecord getUserTest(String username) throws
- * RemoteException {
- *
- * GetUsersReturn myUser = stub.get_user(client, sesskey, username,
- * "username"); UserRecord[] usersFound = myUser.getUsers(); for (UserRecord
- * uf: usersFound) { if (uf.getUsername().equals(username)) { return uf; } }
- * return usersFound[0]; }
- *
- * private static void delUserTest(UserRecord userRecord) throws
- * RemoteException {
- *
- * EditUsersInput usersToDelete = new EditUsersInput(); UserDatum[]
- * usersDatumToDelete = new UserDatum[1]; UserDatum myUserToDelete =
- * getUserDatumFromUserRecord(userRecord);
- * myUserToDelete.setAction("Delete"); usersDatumToDelete[0] =
- * myUserToDelete; usersToDelete.setUsers(usersDatumToDelete);
- *
- * stub.edit_users(client, sesskey, usersToDelete);
- *
- * }
- *
- * private static void addUserTest(String username, boolean casAuth) throws
- * RemoteException {
- *
- * EditUsersInput usersToAdd = new EditUsersInput(); UserDatum[]
- * usersDatumToAdd = new UserDatum[1]; UserRecord myNewUser =
- * getUserTest(username); if (myNewUser.getId().equals(new BigInteger("0")))
- * { UserDatum myUserToAdd = getUserDatumFromUserRecord(myNewUser);
- * myUserToAdd.setAction("Add"); myUserToAdd.setUsername(username);
- * myUserToAdd.setDescription(""); if (casAuth) {
- * myUserToAdd.setAuth("cas"); } else { myUserToAdd.setAuth("manual"); }
- *
- * usersDatumToAdd[0] = myUserToAdd; usersToAdd.setUsers(usersDatumToAdd);
- *
- * stub.edit_users(client, sesskey, usersToAdd); }
- *
- *
- * }
- *
- * private static void modifyCourseTest() throws RemoteException {
- *
- * CourseRecord myCourse = null;
- *
- * GetCategoriesReturn myCategories = stub.get_categories(client, sesskey);
- *
- * for (CategoryRecord cat: myCategories.getCategories()) {
- * System.out.println("Dans la categorie: " + cat.getName());
- * GetCoursesReturn myCourses = stub.get_courses_bycategory(client, sesskey,
- * cat.getId()); for (CourseRecord course: myCourses.getCourses()) {
- * System.out.println("Le cours " + course.getFullname() + "(" +
- * course.getId() + ")");
- *
- * myCourse = course;
- *
- * UserRecord[] te=stub.get_teachers(client, sesskey, "" + course.getId(),
- * "id").getUsers(); for (int i=0; i< te.length;i++) System.out.println (
- * te[i].getError()+"\t"+te[i].getId()+ "\t"+te[i].getUsername()+
- * "\t"+te[i].getFirstname()+ "\t"+te[i].getLastname()+
- * "\t"+te[i].getEmail() ); } }
- *
- * EditCoursesInput editedCourses = new EditCoursesInput();
- *
- * CourseDatum cD = getCourseDatumFromCourseRecord(myCourse);
- *
- * cD.setFullname("Ohh ca marche"); cD.setSummary("super la description!!");
- * System.out.println(cD.getIdnumber()); System.out.println(cD.getId());
- *
- * System.out.println(cD.getFullname());
- *
- * //Set<CourseDatum> coursesDatumSet = new HashSet<CourseDatum>();
- * //coursesDatumSet.add(cD);
- *
- * CourseDatum[] coursesDatumArray = new CourseDatum[1];//(CourseDatum[])
- * coursesDatumSet.toArray(); coursesDatumArray[0] = cD;
- * editedCourses.setCourses(coursesDatumArray); EditCoursesOutput
- * returnCourses = stub.edit_courses(client, sesskey, editedCourses);
- * System.out.println(returnCourses.getCourses().length);
- *
- * CourseRecord returnedCourse = ((CourseRecord)
- * returnCourses.getCourses()[0]); System.out.println( returnedCourse );
- * System.out.println( returnedCourse.getFullname() ); System.out.println(
- * returnedCourse.getError() );
- *
- * }
- *
- * private static CourseDatum getCourseDatumFromCourseRecord(CourseRecord
- * cr) { CourseDatum tcr = new CourseDatum( "Update", cr.getId(),
- * cr.getCategory(), cr.getSortorder(), cr.getPassword(), cr.getFullname(),
- * cr.getShortname(), cr.getIdnumber(), cr.getSummary(), cr.getFormat(),
- * cr.getShowgrades(), cr.getNewsitems(), cr.getTeacher(), cr.getTeachers(),
- * cr.getStudent(), cr.getStudents(), cr.getGuest(), cr.getStartdate(),
- * cr.getEnrolperiod(), cr.getNumsections(), cr.getMarker(),
- * cr.getMaxbytes(), cr.getVisible(), cr.getHiddensections(),
- * cr.getGroupmode(), cr.getGroupmodeforce(), cr.getLang(), cr.getTheme(),
- * cr.getCost(), cr.getTimecreated(), cr.getTimemodified(),
- * cr.getMetacourse() ); return tcr; }
- *
- * private static UserDatum getUserDatumFromUserRecord(UserRecord ur) {
- * String description = "none"; if (ur.getDescription() != null) {
- * description = ur.getDescription(); } UserDatum ud = new UserDatum(
- * "Update", ur.getConfirmed(), ur.getPolicyagreed(), ur.getDeleted(),
- * ur.getUsername(), "manual", "", ur.getIdnumber(), ur.getFirstname(),
- * ur.getLastname(), ur.getEmail(), ur.getIcq(), ur.getSkype(),
- * ur.getYahoo(), ur.getAim(), ur.getMsn(), ur.getPhone1(), ur.getPhone2(),
- * ur.getInstitution(), ur.getDepartment(), ur.getAddress(), ur.getCity(),
- * ur.getCountry(), ur.getLang(), ur.getTimezone(), ur.getLastip(),
- * description, new BigInteger("1")); return ud; }
- */
-}