Skip to content

Commit

Permalink
update MoodleRestClient and ExtSocialActivityUtil so it can handle us…
Browse files Browse the repository at this point in the history
…er independent response of all activities liferay#196
  • Loading branch information
wolfien committed Feb 3, 2017
1 parent 847c0f9 commit 163469d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;

public class MoodleRestClient {
import de.unipotsdam.elis.activities.ActivitiesAdminPortlet;

/**
* http://localhost/moodle/webservice/rest/server.php?
* wstoken=eaf10af21b98618e8bf0ea473a5e8e24
* &wsfunction=webservice_get_recent_course_activities
* &moodlewsrestformat=json
*/
public class MoodleRestClient {

private final static String TARGET = "http://localhost";

private static String getToken(String username, String password) throws JSONException, ClientErrorException {
private static String getToken(String serviceName) throws JSONException, ClientErrorException {

String _target = ActivitiesAdminPortlet.getMoodleServiceEndpoint(); // set in admin portlet
String username = ActivitiesAdminPortlet.getMoodleServiceUsername();
String password = ActivitiesAdminPortlet.getMoodleServicePassword();

Client client = ClientBuilder.newClient();
WebTarget target = client.target(TARGET).path("moodle/login/token.php").queryParam("username", username)
.queryParam("password", password).queryParam("service", "webservice_recent_course_activities");
WebTarget target = client.target(_target).path("/login/token.php").queryParam("username", username)
.queryParam("password", password).queryParam("service", serviceName);

Response response = target.request(MediaType.APPLICATION_JSON_TYPE).get();

Expand All @@ -44,13 +41,15 @@ private static String getToken(String username, String password) throws JSONExce
return token;
}

public static JSONArray getLatestCourseNews(String username, String password, int startIndex, int endIndex)
public static JSONArray getLatestCourseNews(int startIndex, int endIndex)
throws JSONException, ClientErrorException {

String token = getToken(username, password);
String _target = ActivitiesAdminPortlet.getMoodleServiceEndpoint(); // set in admin portlet

String token = getToken("webservice_recent_course_activities");

Client client = ClientBuilder.newClient();
WebTarget target = client.target(TARGET).path("moodle/webservice/rest/server.php")
WebTarget target = client.target(_target).path("/webservice/rest/server.php")
.queryParam("wsfunction", "webservice_get_recent_course_activities")
.queryParam("startindex", startIndex).queryParam("count", endIndex - startIndex)
.queryParam("wstoken", token).queryParam("moodlewsrestformat", "json");
Expand All @@ -63,13 +62,15 @@ public static JSONArray getLatestCourseNews(String username, String password, in
return JSONFactoryUtil.createJSONArray(response.readEntity(String.class));
}

public static JSONArray getLatestCourseNews(String username, String password, long startTime, long endTime,
public static JSONArray getLatestCourseNews(long startTime, long endTime,
int startIndex, int endIndex) throws JSONException, ClientErrorException {

String token = getToken(username, password);
String _target = ActivitiesAdminPortlet.getMoodleServiceEndpoint(); // set in admin portlet

String token = getToken("webservice_recent_course_activities");

Client client = ClientBuilder.newClient();
WebTarget target = client.target(TARGET).path("moodle/webservice/rest/server.php")
WebTarget target = client.target(_target).path("/webservice/rest/server.php")
.queryParam("wsfunction", "webservice_get_recent_course_activities").queryParam("starttime", startTime)
.queryParam("endtime", endTime).queryParam("startindex", startIndex)
.queryParam("count", endIndex - startIndex).queryParam("wstoken", token)
Expand All @@ -82,4 +83,30 @@ public static JSONArray getLatestCourseNews(String username, String password, lo
// TODO: fix crash, if webservice_access_exception occurs
return JSONFactoryUtil.createJSONArray(response.readEntity(String.class));
}

/**
* add user of campus.UP to moodle, too receive activities.
* @param screenName
* @throws ClientErrorException
* @throws JSONException
*/
public static boolean addCampusUpUser(String screenName) throws JSONException, ClientErrorException {
String _target = ActivitiesAdminPortlet.getMoodleServiceEndpoint(); // set in admin portlet

String token = getToken("webservice_add_campus_up_user");

Client client = ClientBuilder.newClient();
WebTarget target = client.target(_target).path("/webservice/rest/server.php")
.queryParam("wsfunction", "webservice_add_user")
.queryParam("userid", screenName)
.queryParam("wstoken", token)
.queryParam("moodlewsrestformat", "json");

Response response = target.request(MediaType.APPLICATION_JSON_TYPE).get();

if (response.getStatus() != 200)
throw new ClientErrorException(response);

return true;//JSONFactoryUtil.createJSONArray(response.readEntity(String.class));
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
Expand Down Expand Up @@ -56,6 +57,22 @@ public class MoodleSocialActivityLocalServiceImpl extends MoodleSocialActivityLo
* moodle social activity local service.
*/

public MoodleSocialActivity getMostRecentMoodleSocialActivity() throws SystemException {

// select max published value
DynamicQuery subQuery = DynamicQueryFactoryUtil.forClass(MoodleSocialActivity.class)
.setProjection(ProjectionFactoryUtil.max("published"));

// select MoodleSocialActivity where published = max(published)
DynamicQuery query = DynamicQueryFactoryUtil.forClass(MoodleSocialActivity.class)
.add(PropertyFactoryUtil.forName("published").eq(subQuery));

List<?> result = MoodleSocialActivityLocalServiceUtil.dynamicQuery(query, 0, 1);
if (result.size() > 0)
return (MoodleSocialActivity) result.get(0);
return null;
}

public MoodleSocialActivity getMostRecentMoodleSocialActivity(long userId) throws SystemException {
DynamicQuery query = DynamicQueryFactoryUtil.forClass(MoodleSocialActivity.class)
.add(PropertyFactoryUtil.forName("userId").eq(new Long(userId)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<%
List<SocialActivitySet> results = null;
ExtSocialActivityUtil.requestNewMoodleActivities(user.getUserId(), user.getScreenName(), PrincipalThreadLocal.getPassword());
//ExtSocialActivityUtil.requestNewMoodleActivities(user.getUserId(), user.getScreenName(), PrincipalThreadLocal.getPassword());
int count = 0;
long total = 0;
Expand Down

0 comments on commit 163469d

Please sign in to comment.