Search in sources :

Example 36 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.

the class UserCoursesWebService method getFavoritCourses.

/**
 * Retrieves the list of my favorite courses.
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The courses
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVOes}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param start The first result
 * @param limit Max result
 * @param httpRequest The HTTP request
 * @param request The REST request
 * @return The list of my favorite courses
 */
@GET
@Path("favorite")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getFavoritCourses(@QueryParam("start") @DefaultValue("0") Integer start, @QueryParam("limit") @DefaultValue("25") Integer limit, @Context HttpServletRequest httpRequest, @Context Request request) {
    List<String> courseType = Collections.singletonList("CourseModule");
    RepositoryManager rm = RepositoryManager.getInstance();
    if (MediaTypeVariants.isPaged(httpRequest, request)) {
        List<RepositoryEntry> repoEntries = rm.getFavoritLearningResourcesAsTeacher(identity, courseType, start, limit, RepositoryEntryOrder.nameAsc);
        int totalCount;
        if (repoEntries.size() < limit) {
            totalCount = repoEntries.size();
        } else {
            totalCount = rm.countFavoritLearningResourcesAsTeacher(identity, courseType);
        }
        CourseVO[] vos = toCourseVo(repoEntries);
        CourseVOes voes = new CourseVOes();
        voes.setCourses(vos);
        voes.setTotalCount(totalCount);
        return Response.ok(voes).build();
    } else {
        List<RepositoryEntry> repoEntries = rm.getFavoritLearningResourcesAsTeacher(identity, courseType, 0, -1, RepositoryEntryOrder.nameAsc);
        CourseVO[] vos = toCourseVo(repoEntries);
        return Response.ok(vos).build();
    }
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) RepositoryManager(org.olat.repository.RepositoryManager) RepositoryEntry(org.olat.repository.RepositoryEntry) CourseVOes(org.olat.restapi.support.vo.CourseVOes) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 37 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.

the class CourseWebService method publishCourse.

/**
 * Publish the course.
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The metadatas of the created course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The course not found
 * @param locale The course locale
 * @param request The HTTP request
 * @return It returns the metadatas of the published course.
 */
@POST
@Path("publish")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response publishCourse(@QueryParam("locale") Locale locale, @QueryParam("access") Integer access, @QueryParam("membersOnly") Boolean membersOnly, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    UserRequest ureq = getUserRequest(request);
    if (!isAuthorEditor(course, request) && !isInstitutionalResourceManager(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    int newAccess = access == null ? RepositoryEntry.ACC_USERS : access.intValue();
    boolean members = membersOnly == null ? false : membersOnly.booleanValue();
    CourseFactory.publishCourse(course, newAccess, members, ureq.getIdentity(), locale);
    CourseVO vo = ObjectFactory.get(course);
    return Response.ok(vo).build();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 38 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.

the class CoursesWebService method importCourse.

/**
 * Imports a course from a course archive zip file
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The metadatas of the imported course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param ownerUsername set the owner of the imported course to the user of this username.
 * @param request The HTTP request
 * @return It returns the imported course
 */
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response importCourse(@QueryParam("ownerUsername") String ownerUsername, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    Identity identity = null;
    // Set the owner of the imported course to the user defined in the parameter
    if (ownerUsername != null && !ownerUsername.isEmpty() && isAuthor(request)) {
        identity = BaseSecurityManager.getInstance().findIdentityByName(ownerUsername);
        if (identity == null) {
            return Response.serverError().status(Status.BAD_REQUEST).build();
        }
    }
    if (identity == null) {
        identity = ureq.getIdentity();
    }
    MultipartReader partsReader = null;
    try {
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        long length = tmpFile.length();
        if (length > 0) {
            Long accessRaw = partsReader.getLongValue("access");
            int access = accessRaw != null ? accessRaw.intValue() : RepositoryEntry.ACC_OWNERS;
            String membersOnlyRaw = partsReader.getValue("membersOnly");
            boolean membersonly = "true".equals(membersOnlyRaw);
            String softKey = partsReader.getValue("softkey");
            String displayName = partsReader.getValue("displayname");
            ICourse course = importCourse(ureq, identity, tmpFile, displayName, softKey, access, membersonly);
            CourseVO vo = ObjectFactory.get(course);
            return Response.ok(vo).build();
        }
        return Response.serverError().status(Status.NO_CONTENT).build();
    } catch (Exception e) {
        log.error("Error while importing a file", e);
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
    CourseVO vo = null;
    return Response.ok(vo).build();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) ICourse(org.olat.course.ICourse) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) File(java.io.File) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) MultipartReader(org.olat.restapi.support.MultipartReader) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 39 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.

the class CoursesWebService method getCourseList.

/**
 * Get all courses viewable by the authenticated user
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json, application/json;pagingspec=1.0
 * @response.representation.200.doc List of visible courses
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVOes}
 * @param start
 * @param limit
 * @param externalId Search with an external ID
 * @param externalRef Search with an external reference
 * @param managed (true / false) Search only managed / not managed groups
 * @param httpRequest The HTTP request
 * @param request The REST request
 * @return
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getCourseList(@QueryParam("start") @DefaultValue("0") Integer start, @QueryParam("limit") @DefaultValue("25") Integer limit, @QueryParam("managed") Boolean managed, @QueryParam("externalId") String externalId, @QueryParam("externalRef") String externalRef, @QueryParam("repositoryEntryKey") String repositoryEntryKey, @Context HttpServletRequest httpRequest, @Context Request request) {
    RepositoryManager rm = RepositoryManager.getInstance();
    Roles roles = getRoles(httpRequest);
    Identity identity = getIdentity(httpRequest);
    SearchRepositoryEntryParameters params = new SearchRepositoryEntryParameters(identity, roles, CourseModule.getCourseTypeName());
    params.setManaged(managed);
    if (StringHelper.containsNonWhitespace(externalId)) {
        params.setExternalId(externalId);
    }
    if (StringHelper.containsNonWhitespace(externalRef)) {
        params.setExternalRef(externalRef);
    }
    if (StringHelper.containsNonWhitespace(repositoryEntryKey) && StringHelper.isLong(repositoryEntryKey)) {
        try {
            params.setRepositoryEntryKeys(Collections.singletonList(new Long(repositoryEntryKey)));
        } catch (NumberFormatException e) {
            log.error("Cannot parse the following repository entry key: " + repositoryEntryKey);
        }
    }
    if (MediaTypeVariants.isPaged(httpRequest, request)) {
        int totalCount = rm.countGenericANDQueryWithRolesRestriction(params);
        List<RepositoryEntry> repoEntries = rm.genericANDQueryWithRolesRestriction(params, start, limit, true);
        CourseVO[] vos = toCourseVo(repoEntries);
        CourseVOes voes = new CourseVOes();
        voes.setCourses(vos);
        voes.setTotalCount(totalCount);
        return Response.ok(voes).build();
    } else {
        List<RepositoryEntry> repoEntries = rm.genericANDQueryWithRolesRestriction(params, 0, -1, false);
        CourseVO[] vos = toCourseVo(repoEntries);
        return Response.ok(vos).build();
    }
}
Also used : SearchRepositoryEntryParameters(org.olat.repository.model.SearchRepositoryEntryParameters) CourseVO(org.olat.restapi.support.vo.CourseVO) RepositoryManager(org.olat.repository.RepositoryManager) Roles(org.olat.core.id.Roles) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) RepositoryEntry(org.olat.repository.RepositoryEntry) CourseVOes(org.olat.restapi.support.vo.CourseVOes) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 40 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.

the class CoursesWebService method toCourseVo.

public static CourseVO[] toCourseVo(List<RepositoryEntry> repoEntries) {
    List<CourseVO> voList = new ArrayList<CourseVO>();
    int count = 0;
    for (RepositoryEntry repoEntry : repoEntries) {
        try {
            ICourse course = loadCourse(repoEntry.getOlatResource().getResourceableId());
            voList.add(ObjectFactory.get(repoEntry, course));
            if (count % 33 == 0) {
                DBFactory.getInstance().commitAndCloseSession();
            }
        } catch (Exception e) {
            log.error("Cannot load the course with this repository entry: " + repoEntry, e);
        }
    }
    CourseVO[] vos = new CourseVO[voList.size()];
    voList.toArray(vos);
    return vos;
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) ArrayList(java.util.ArrayList) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

CourseVO (org.olat.restapi.support.vo.CourseVO)70 Test (org.junit.Test)48 URI (java.net.URI)46 HttpResponse (org.apache.http.HttpResponse)46 RepositoryEntry (org.olat.repository.RepositoryEntry)34 HttpGet (org.apache.http.client.methods.HttpGet)26 ICourse (org.olat.course.ICourse)26 Identity (org.olat.core.id.Identity)20 InputStream (java.io.InputStream)18 Produces (javax.ws.rs.Produces)16 HttpEntity (org.apache.http.HttpEntity)16 HttpPut (org.apache.http.client.methods.HttpPut)16 File (java.io.File)14 HttpPost (org.apache.http.client.methods.HttpPost)14 CourseVOes (org.olat.restapi.support.vo.CourseVOes)14 URL (java.net.URL)12 GET (javax.ws.rs.GET)8 Path (javax.ws.rs.Path)8 UserRequest (org.olat.core.gui.UserRequest)8 RepositoryManager (org.olat.repository.RepositoryManager)8