1 /************************************************************************************
3 * Copyright (c) 2008 Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel.
5 * Ce logiciel est diffuse sous les termes et conditions de la licence CECILL.
6 * Voir le fichier joint Licence_CeCILL_V2-fr.txt pour plus de details.
8 ************************************************************************************/
9 package com.pentila.evalcomp.manager.ressource.moodle;
11 import java.math.BigInteger;
12 import java.net.MalformedURLException;
14 import java.rmi.RemoteException;
15 import java.util.ArrayList;
16 import java.util.List;
19 import com.pentila.evalcomp.domain.definition.RessourceDefinition;
20 import com.pentila.evalcomp.domain.transit.RessourceFolder;
21 import com.pentila.evalcomp.domain.transit.RessourceItem;
22 import com.pentila.evalcomp.domain.transit.RessourceMoodleCourse;
23 import com.pentila.evalcomp.moodlews.CourseRecord;
24 import com.pentila.evalcomp.moodlews.EditUsersInput;
25 import com.pentila.evalcomp.moodlews.GetCoursesReturn;
26 import com.pentila.evalcomp.moodlews.GetUsersReturn;
27 import com.pentila.evalcomp.moodlews.LoginReturn;
28 import com.pentila.evalcomp.moodlews.MoodleWSBindingStub;
29 import com.pentila.evalcomp.moodlews.UserDatum;
30 import com.pentila.evalcomp.moodlews.UserRecord;
32 // TODO: Auto-generated Javadoc
34 * The Class DefinitionManager.
36 public class MoodleWebService {
38 /** The moodle wsurl. */
39 private String moodleWSURL = "http://localhost:8890/moodle19/wspp/service_pp.php";
41 /** The user login. */
42 private String userLogin = "admin";
45 private String userPWD = "12345";
47 /** The moodle base url. */
48 private String moodleBaseURL = "http://193.48.120:8890/moodle19/course/view.php?id={0}";
50 /** The role teacher. */
51 private String roleTeacher = "3";
53 /** The role student. */
54 private String roleStudent = "5";
57 private static MoodleWSBindingStub stub = null;
60 private static String sesskey = "";
63 private static BigInteger client = null;
66 * Instantiates a new moodle web service.
68 public MoodleWebService() {
75 * @return true, if successful
77 private boolean moodleLogin() {
80 endPoint = new URL(moodleWSURL);
81 } catch (MalformedURLException e1) {
86 stub = new MoodleWSBindingStub(endPoint, null);
87 } catch (Exception e) {
92 LoginReturn loginReturn;
94 loginReturn = stub.login(userLogin, userPWD);
95 } catch (RemoteException e) {
98 sesskey = loginReturn.getSessionkey();
99 client = loginReturn.getClient();
106 * @return true, if successful
108 private static boolean moodleLogout() {
109 boolean logoutReturn;
111 logoutReturn = stub.logout(client, sesskey);
112 } catch (RemoteException e) {
119 * Gets the workspace.
121 * @param login the login
123 * @return the workspace
125 public RessourceItem getWorkspace(String login) {
126 // TODO Auto-generated method stub
127 RessourceFolder moodleCourses = new RessourceFolder();
128 moodleCourses.setEditable(false);
130 moodleCourses.setUrl("");
131 boolean log = moodleLogin();
133 return moodleCourses;
135 List<RessourceItem> results = new ArrayList<RessourceItem>();
137 results = _getMoodleCourses(login);
138 } catch (RemoteException e) {
141 moodleCourses.setItemList(results);
143 return moodleCourses;
147 * _get moodle courses.
149 * @param login the login
151 * @return the list< ressource item>
153 * @throws RemoteException the remote exception
155 public List<RessourceItem> _getMoodleCourses(String login)
156 throws RemoteException {
158 List<RessourceItem> tcourses = new ArrayList<RessourceItem>();
159 GetCoursesReturn myCourses = stub.get_my_courses_byusername(client,
160 sesskey, login, "fullname");
161 for (CourseRecord course : myCourses.getCourses()) {
162 RessourceMoodleCourse myTcourse = getTCourseRecordFromCourseRecord(course);
163 myTcourse.setPath(moodleBaseURL);
164 tcourses.add(myTcourse);
170 * Sets the permission.
173 * @param suid the suid
174 * @param role the role
176 public void setPermission(RessourceDefinition rd, Set<String> suid,
179 String roleId = roleTeacher;
180 if (!role.equals("")) {
181 roleId = roleStudent;
183 boolean log = moodleLogin();
185 for (String username : suid) {
187 assignRoleToUserInCourse(username, rd.getRessourceKey(),
189 } catch (RemoteException e) {
190 // TODO Auto-generated catch block
199 * Removes the permission.
202 * @param suid the suid
203 * @param role the role
205 public void removePermission(RessourceDefinition rd, Set<String> suid,
208 String roleId = roleTeacher;
209 if (!role.equals("")) {
210 roleId = roleStudent;
212 boolean log = moodleLogin();
214 for (String username : suid) {
216 unassignRoleToUserInCourse(username, rd.getRessourceKey(),
218 } catch (RemoteException e) {
219 // TODO Auto-generated catch block
228 * Assign role to user in course.
230 * @param username the username
231 * @param courseid the courseid
232 * @param roleid the roleid
234 * @throws RemoteException the remote exception
236 private static void assignRoleToUserInCourse(String username,
237 String courseid, String roleid) throws RemoteException {
238 UserRecord myNewUser = getUser(username);
239 if (myNewUser.getId().equals(new BigInteger("0"))) {
240 // si le user n'existe pas nous le creons
241 addUser(username, true);
242 myNewUser = getUser(username);
244 String[] userids = new String[1];
245 userids[0] = myNewUser.getUsername();
247 .assignUsers(client, sesskey, courseid, userids, "username",
252 * Unassign role to user in course.
254 * @param username the username
255 * @param courseid the courseid
256 * @param roleid the roleid
258 * @throws RemoteException the remote exception
260 private static void unassignRoleToUserInCourse(String username,
261 String courseid, String roleid) throws RemoteException {
262 UserRecord myNewUser = getUser(username);
263 if (!myNewUser.getId().equals(new BigInteger("0"))) {
264 String[] userids = new String[1];
265 userids[0] = myNewUser.getUsername();
266 stub.unassignUsers(client, sesskey, courseid, userids, "username",
274 * @param username the username
278 * @throws RemoteException the remote exception
280 private static UserRecord getUser(String username) throws RemoteException {
281 GetUsersReturn myUser = stub.get_user(client, sesskey, username,
283 UserRecord[] usersFound = myUser.getUsers();
284 for (UserRecord uf : usersFound) {
285 if (uf.getUsername().equals(username)) {
289 return usersFound[0];
295 * @param username the username
296 * @param casAuth the cas auth
298 * @throws RemoteException the remote exception
300 private static void addUser(String username, boolean casAuth)
301 throws RemoteException {
303 EditUsersInput usersToAdd = new EditUsersInput();
304 UserDatum[] usersDatumToAdd = new UserDatum[1];
305 UserRecord myNewUser = getUser(username);
306 if (myNewUser.getId().equals(new BigInteger("0"))) {
307 UserDatum myUserToAdd = getUserDatumFromUserRecord(myNewUser);
308 myUserToAdd.setAction("Add");
309 myUserToAdd.setUsername(username);
310 myUserToAdd.setDescription("");
312 myUserToAdd.setAuth("cas");
314 myUserToAdd.setAuth("manual");
317 usersDatumToAdd[0] = myUserToAdd;
318 usersToAdd.setUsers(usersDatumToAdd);
320 stub.edit_users(client, sesskey, usersToAdd);
326 * Gets the user datum from user record.
330 * @return the user datum from user record
332 private static UserDatum getUserDatumFromUserRecord(UserRecord ur) {
333 String description = "none";
334 if (ur.getDescription() != null) {
335 description = ur.getDescription();
337 UserDatum ud = new UserDatum("Update", ur.getConfirmed(), ur
338 .getPolicyagreed(), ur.getDeleted(), ur.getUsername(),
339 "manual", "", ur.getIdnumber(), ur.getFirstname(), ur
340 .getLastname(), ur.getEmail(), ur.getIcq(), ur
341 .getSkype(), ur.getYahoo(), ur.getAim(), ur.getMsn(),
342 ur.getPhone1(), ur.getPhone2(), ur.getInstitution(), ur
343 .getDepartment(), ur.getAddress(), ur.getCity(), ur
344 .getCountry(), ur.getLang(), ur.getTimezone(), ur
345 .getLastip(), description, new BigInteger("1"));
350 * Gets the t course record from course record.
354 * @return the t course record from course record
356 private RessourceMoodleCourse getTCourseRecordFromCourseRecord(
358 RessourceMoodleCourse tcr = new RessourceMoodleCourse(cr.getError(), cr
359 .getId(), cr.getCategory(), cr.getSortorder(),
360 cr.getPassword(), cr.getFullname(), cr.getShortname(), cr
361 .getIdnumber(), cr.getSummary(), cr.getFormat(), cr
362 .getShowgrades(), cr.getNewsitems(), cr.getTeacher(),
363 cr.getTeachers(), cr.getStudent(), cr.getStudents(), cr
364 .getGuest(), cr.getStartdate(), cr.getEnrolperiod(), cr
365 .getNumsections(), cr.getMarker(), cr.getMaxbytes(), cr
366 .getVisible(), cr.getHiddensections(), cr
367 .getGroupmode(), cr.getGroupmodeforce(), cr.getLang(),
368 cr.getTheme(), cr.getCost(), cr.getTimecreated(), cr
369 .getTimemodified(), cr.getMetacourse());
374 * Gets the moodle wsurl.
376 * @return the moodle wsurl
378 public String getMoodleWSURL() {
383 * Sets the moodle wsurl.
385 * @param moodleWSURL the new moodle wsurl
387 public void setMoodleWSURL(String moodleWSURL) {
388 this.moodleWSURL = moodleWSURL;
392 * Gets the user login.
394 * @return the user login
396 public String getUserLogin() {
401 * Sets the user login.
403 * @param userLogin the new user login
405 public void setUserLogin(String userLogin) {
406 this.userLogin = userLogin;
412 * @return the user pwd
414 public String getUserPWD() {
421 * @param userPWD the new user pwd
423 public void setUserPWD(String userPWD) {
424 this.userPWD = userPWD;
428 * Gets the moodle base url.
430 * @return the moodle base url
432 public String getMoodleBaseURL() {
433 return moodleBaseURL;
437 * Sets the moodle base url.
439 * @param moodleBaseURL the new moodle base url
441 public void setMoodleBaseURL(String moodleBaseURL) {
442 this.moodleBaseURL = moodleBaseURL;
446 * Gets the role teacher.
448 * @return the role teacher
450 public String getRoleTeacher() {
455 * Sets the role teacher.
457 * @param roleTeacher the new role teacher
459 public void setRoleTeacher(String roleTeacher) {
460 this.roleTeacher = roleTeacher;
464 * Gets the role student.
466 * @return the role student
468 public String getRoleStudent() {
473 * Sets the role student.
475 * @param roleStudent the new role student
477 public void setRoleStudent(String roleStudent) {
478 this.roleStudent = roleStudent;
482 * Return the url to the moodle ressource.
484 * @param pluginKey the plugin key
485 * @param ressourceKey the ressource key
486 * @param mode the mode
488 * @return the ressource url
490 public String getRessourceUrl(String pluginKey, String ressourceKey,
493 if (mode.equals("")) {
494 // acces utilisateur simple
495 url = getMoodleBaseURL() + "/course/view.php?id=" + ressourceKey;
497 // acces teacher donc visualisation des resultats
498 boolean log = moodleLogin();
501 UserRecord myUser = getUser(mode);
502 url = getMoodleBaseURL()
503 + "/course/user.php?mode=complete&id="
504 + ressourceKey + "&user="
505 + myUser.getId().toString();
506 } catch (RemoteException e) {
507 // On retourne l'acces vers le cours
508 url = getMoodleBaseURL() + "/course/view.php?id="
513 url = getMoodleBaseURL() + "/course/view.php?id="
520 /****** METHODES ISSUES DES TESTS DE COMMUNICATION AVEC MOODLE *****/
522 * public static void main(String[] args) throws RemoteException,
523 * MalformedURLException {
527 * //modifyCourseTest(); //listUsersTest(); //UserRecord myUser =
528 * getUserTest("stagiaire1"); //delUserTest(myUser);
529 * //addUserTest("stagiaire1", true); // 5 --> student // 3 --> teacher
530 * //assignRoleToUserInCourseTest("stagiaire1", "7", "5");
531 * //assignRoleToUserInCourseTest("stagiaire1", "7", "3");
532 * unassignRoleToUserInCourseTest("stagiaire1", "7", "5");
538 * private static boolean moodleLogin() { URL endPoint; try { endPoint = new
539 * URL("http://localhost:8890/moodle19/wspp/service_pp.php"); } catch
540 * (MalformedURLException e1) { return false; }
542 * try { stub = new MoodleWSBindingStub(endPoint, null); } catch (AxisFault
543 * e) { e.printStackTrace(); }
545 * LoginReturn loginReturn; try { loginReturn = stub.login("admin",
546 * "12345"); } catch (RemoteException e) { return false; } sesskey =
547 * loginReturn.getSessionkey(); client = loginReturn.getClient(); return
550 * private static boolean moodleLogout() { boolean logoutReturn; try {
551 * logoutReturn = stub.logout(client, sesskey); } catch (RemoteException e)
552 * { return false; } return logoutReturn; }
554 * private static void assignRoleToUserInCourseTest(String username, String
555 * courseid, String roleid) throws RemoteException { UserRecord myNewUser =
556 * getUserTest(username); if (!myNewUser.getId().equals(new
557 * BigInteger("0"))) { String[] userids = new String[1]; userids[0] =
558 * myNewUser.getUsername(); AssignUsersReturn ret = stub.assignUsers(client,
559 * sesskey, courseid, userids, "username", roleid);
560 * System.out.println(ret.getUsers().length); } }
562 * private static void unassignRoleToUserInCourseTest(String username,
563 * String courseid, String roleid) throws RemoteException { UserRecord
564 * myNewUser = getUserTest(username); if (!myNewUser.getId().equals(new
565 * BigInteger("0"))) { String[] userids = new String[1]; userids[0] =
566 * myNewUser.getUsername(); UnassignUsersReturn ret =
567 * stub.unassignUsers(client, sesskey, courseid, userids, "username",
568 * roleid); System.out.println(ret.getUsers().length); } }
570 * private static void listUsersTest() throws RemoteException {
572 * GetUsersReturn myUser = stub.get_user(client, sesskey, null, null);
573 * UserRecord[] usersFound = myUser.getUsers();
574 * System.out.println("------"); for (UserRecord uf: usersFound) {
575 * System.out.println(uf.getUsername()); System.out.println(uf.getId()); }
576 * System.out.println("------");
580 * private static UserRecord getUserTest(String username) throws
583 * GetUsersReturn myUser = stub.get_user(client, sesskey, username,
584 * "username"); UserRecord[] usersFound = myUser.getUsers(); for (UserRecord
585 * uf: usersFound) { if (uf.getUsername().equals(username)) { return uf; } }
586 * return usersFound[0]; }
588 * private static void delUserTest(UserRecord userRecord) throws
591 * EditUsersInput usersToDelete = new EditUsersInput(); UserDatum[]
592 * usersDatumToDelete = new UserDatum[1]; UserDatum myUserToDelete =
593 * getUserDatumFromUserRecord(userRecord);
594 * myUserToDelete.setAction("Delete"); usersDatumToDelete[0] =
595 * myUserToDelete; usersToDelete.setUsers(usersDatumToDelete);
597 * stub.edit_users(client, sesskey, usersToDelete);
601 * private static void addUserTest(String username, boolean casAuth) throws
604 * EditUsersInput usersToAdd = new EditUsersInput(); UserDatum[]
605 * usersDatumToAdd = new UserDatum[1]; UserRecord myNewUser =
606 * getUserTest(username); if (myNewUser.getId().equals(new BigInteger("0")))
607 * { UserDatum myUserToAdd = getUserDatumFromUserRecord(myNewUser);
608 * myUserToAdd.setAction("Add"); myUserToAdd.setUsername(username);
609 * myUserToAdd.setDescription(""); if (casAuth) {
610 * myUserToAdd.setAuth("cas"); } else { myUserToAdd.setAuth("manual"); }
612 * usersDatumToAdd[0] = myUserToAdd; usersToAdd.setUsers(usersDatumToAdd);
614 * stub.edit_users(client, sesskey, usersToAdd); }
619 * private static void modifyCourseTest() throws RemoteException {
621 * CourseRecord myCourse = null;
623 * GetCategoriesReturn myCategories = stub.get_categories(client, sesskey);
625 * for (CategoryRecord cat: myCategories.getCategories()) {
626 * System.out.println("Dans la categorie: " + cat.getName());
627 * GetCoursesReturn myCourses = stub.get_courses_bycategory(client, sesskey,
628 * cat.getId()); for (CourseRecord course: myCourses.getCourses()) {
629 * System.out.println("Le cours " + course.getFullname() + "(" +
630 * course.getId() + ")");
634 * UserRecord[] te=stub.get_teachers(client, sesskey, "" + course.getId(),
635 * "id").getUsers(); for (int i=0; i< te.length;i++) System.out.println (
636 * te[i].getError()+"\t"+te[i].getId()+ "\t"+te[i].getUsername()+
637 * "\t"+te[i].getFirstname()+ "\t"+te[i].getLastname()+
638 * "\t"+te[i].getEmail() ); } }
640 * EditCoursesInput editedCourses = new EditCoursesInput();
642 * CourseDatum cD = getCourseDatumFromCourseRecord(myCourse);
644 * cD.setFullname("Ohh ca marche"); cD.setSummary("super la description!!");
645 * System.out.println(cD.getIdnumber()); System.out.println(cD.getId());
647 * System.out.println(cD.getFullname());
649 * //Set<CourseDatum> coursesDatumSet = new HashSet<CourseDatum>();
650 * //coursesDatumSet.add(cD);
652 * CourseDatum[] coursesDatumArray = new CourseDatum[1];//(CourseDatum[])
653 * coursesDatumSet.toArray(); coursesDatumArray[0] = cD;
654 * editedCourses.setCourses(coursesDatumArray); EditCoursesOutput
655 * returnCourses = stub.edit_courses(client, sesskey, editedCourses);
656 * System.out.println(returnCourses.getCourses().length);
658 * CourseRecord returnedCourse = ((CourseRecord)
659 * returnCourses.getCourses()[0]); System.out.println( returnedCourse );
660 * System.out.println( returnedCourse.getFullname() ); System.out.println(
661 * returnedCourse.getError() );
665 * private static CourseDatum getCourseDatumFromCourseRecord(CourseRecord
666 * cr) { CourseDatum tcr = new CourseDatum( "Update", cr.getId(),
667 * cr.getCategory(), cr.getSortorder(), cr.getPassword(), cr.getFullname(),
668 * cr.getShortname(), cr.getIdnumber(), cr.getSummary(), cr.getFormat(),
669 * cr.getShowgrades(), cr.getNewsitems(), cr.getTeacher(), cr.getTeachers(),
670 * cr.getStudent(), cr.getStudents(), cr.getGuest(), cr.getStartdate(),
671 * cr.getEnrolperiod(), cr.getNumsections(), cr.getMarker(),
672 * cr.getMaxbytes(), cr.getVisible(), cr.getHiddensections(),
673 * cr.getGroupmode(), cr.getGroupmodeforce(), cr.getLang(), cr.getTheme(),
674 * cr.getCost(), cr.getTimecreated(), cr.getTimemodified(),
675 * cr.getMetacourse() ); return tcr; }
677 * private static UserDatum getUserDatumFromUserRecord(UserRecord ur) {
678 * String description = "none"; if (ur.getDescription() != null) {
679 * description = ur.getDescription(); } UserDatum ud = new UserDatum(
680 * "Update", ur.getConfirmed(), ur.getPolicyagreed(), ur.getDeleted(),
681 * ur.getUsername(), "manual", "", ur.getIdnumber(), ur.getFirstname(),
682 * ur.getLastname(), ur.getEmail(), ur.getIcq(), ur.getSkype(),
683 * ur.getYahoo(), ur.getAim(), ur.getMsn(), ur.getPhone1(), ur.getPhone2(),
684 * ur.getInstitution(), ur.getDepartment(), ur.getAddress(), ur.getCity(),
685 * ur.getCountry(), ur.getLang(), ur.getTimezone(), ur.getLastip(),
686 * description, new BigInteger("1")); return ud; }