Search in sources :

Example 11 with CourseVO

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

the class CoursesElementsTest method testUpdateRootNodeCoursePost.

@Test
public // fxdiff FXOLAT-122: course management
void testUpdateRootNodeCoursePost() throws IOException, URISyntaxException {
    assertTrue(conn.login("administrator", "openolat"));
    // create an empty course
    URI uri = getCoursesUri().queryParam("shortTitle", "course4").queryParam("title", "course4 long name").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    CourseVO course = conn.parse(response, CourseVO.class);
    assertNotNull(course);
    assertNotNull(course.getKey());
    assertNotNull(course.getEditorRootNodeId());
    // update the root node
    URI rootUri = getElementsUri(course).path("structure").path(course.getEditorRootNodeId()).build();
    HttpPost updateMethod = conn.createPost(rootUri, MediaType.APPLICATION_JSON);
    HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addTextBody("shortTitle", "Structure-0b").addTextBody("longTitle", "Structure-long-0b").addTextBody("objectives", "Structure-objectives-0b").build();
    updateMethod.setEntity(entity);
    HttpResponse newStructureResponse = conn.execute(updateMethod);
    int newStructureCode = newStructureResponse.getStatusLine().getStatusCode();
    assertTrue(newStructureCode == 200 || newStructureCode == 201);
    // check the response
    CourseNodeVO structureNode = conn.parse(newStructureResponse, CourseNodeVO.class);
    assertNotNull(structureNode);
    assertNotNull(structureNode.getId());
    assertEquals(structureNode.getShortTitle(), "Structure-0b");
    assertEquals(structureNode.getLongTitle(), "Structure-long-0b");
    assertEquals(structureNode.getLearningObjectives(), "Structure-objectives-0b");
    assertEquals(structureNode.getId(), course.getEditorRootNodeId());
    // check the real node
    ICourse realCourse = CourseFactory.loadCourse(course.getKey());
    CourseEditorTreeModel editorTreeModel = realCourse.getEditorTreeModel();
    CourseEditorTreeNode rootNode = (CourseEditorTreeNode) editorTreeModel.getRootNode();
    assertNotNull(rootNode);
    assertNotNull(rootNode.getIdent());
    assertNotNull(rootNode.getCourseNode());
    assertEquals(rootNode.getCourseNode().getShortTitle(), "Structure-0b");
    assertEquals(rootNode.getCourseNode().getLongTitle(), "Structure-long-0b");
    assertEquals(rootNode.getCourseNode().getLearningObjectives(), "Structure-objectives-0b");
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) HttpPost(org.apache.http.client.methods.HttpPost) CourseEditorTreeModel(org.olat.course.tree.CourseEditorTreeModel) HttpEntity(org.apache.http.HttpEntity) CourseNodeVO(org.olat.restapi.support.vo.CourseNodeVO) CourseEditorTreeNode(org.olat.course.tree.CourseEditorTreeNode) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 12 with CourseVO

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

the class CoursesWebService method createEmptyCourse.

/**
 * Creates an empty course, or a copy from a course if the parameter copyFrom is set.
 * @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
 * @param shortTitle The short title
 * @param title The title
 * @param sharedFolderSoftKey The repository entry key of a shared folder (optional)
 * @param copyFrom The course primary key key to make a copy from (optional)
 * @param initialAuthor The primary key of the initial author (optional)
 * @param noAuthor True to create a course without the author
 * @param request The HTTP request
 * @return It returns the id of the newly created Course
 */
@PUT
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response createEmptyCourse(@QueryParam("shortTitle") String shortTitle, @QueryParam("title") String title, @QueryParam("displayName") String displayName, @QueryParam("description") String description, @QueryParam("softKey") String softKey, @QueryParam("access") Integer access, @QueryParam("membersOnly") Boolean membersOnly, @QueryParam("externalId") String externalId, @QueryParam("externalRef") String externalRef, @QueryParam("authors") String authors, @QueryParam("location") String location, @QueryParam("managedFlags") String managedFlags, @QueryParam("sharedFolderSoftKey") String sharedFolderSoftKey, @QueryParam("copyFrom") Long copyFrom, @QueryParam("initialAuthor") Long initialAuthor, @QueryParam("setAuthor") @DefaultValue("true") Boolean setAuthor, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    CourseConfigVO configVO = new CourseConfigVO();
    configVO.setSharedFolderSoftKey(sharedFolderSoftKey);
    int accessInt = (access == null ? RepositoryEntry.ACC_OWNERS : access.intValue());
    boolean membersOnlyBool = (membersOnly == null ? false : membersOnly.booleanValue());
    if (!StringHelper.containsNonWhitespace(displayName)) {
        displayName = shortTitle;
    }
    ICourse course;
    UserRequest ureq = getUserRequest(request);
    Identity id = null;
    if (setAuthor != null && setAuthor.booleanValue()) {
        if (initialAuthor != null) {
            id = getIdentity(initialAuthor);
        }
        if (id == null) {
            id = ureq.getIdentity();
        }
    }
    if (copyFrom != null) {
        course = copyCourse(copyFrom, ureq, id, shortTitle, title, displayName, description, softKey, accessInt, membersOnlyBool, authors, location, externalId, externalRef, managedFlags, configVO);
    } else {
        course = createEmptyCourse(id, shortTitle, title, displayName, description, softKey, accessInt, membersOnlyBool, authors, location, externalId, externalRef, managedFlags, configVO);
    }
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    CourseVO vo = ObjectFactory.get(course);
    return Response.ok(vo).build();
}
Also used : CourseConfigVO(org.olat.restapi.support.vo.CourseConfigVO) 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) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 13 with CourseVO

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

the class UserCoursesTest method testTeachedCourses.

@Test
public void testTeachedCourses() throws IOException, URISyntaxException {
    // prepare a course with a tutor
    Identity teacher = JunitTestHelper.createAndPersistIdentityAsUser("Course-teacher-" + UUID.randomUUID().toString());
    ICourse course = CoursesWebService.createEmptyCourse(teacher, "A course to teach", "A course to teach", null);
    RepositoryEntry courseRe = repositoryManager.lookupRepositoryEntry(course, true);
    repositoryManager.setAccess(courseRe, RepositoryEntry.ACC_OWNERS, true);
    repositoryService.addRole(teacher, courseRe, GroupRoles.coach.name());
    dbInstance.commitAndCloseSession();
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(teacher.getName(), JunitTestHelper.PWD));
    // without paging
    URI request = UriBuilder.fromUri(getContextURI()).path("/users").path(teacher.getKey().toString()).path("/courses/teached").build();
    HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<CourseVO> courses = parseCourseArray(body);
    Assert.assertNotNull(courses);
    Assert.assertEquals(1, courses.size());
    // with paging
    URI pagedRequest = UriBuilder.fromUri(getContextURI()).path("/users").path(teacher.getKey().toString()).path("/courses/teached").queryParam("start", "0").queryParam("limit", "10").build();
    HttpGet pagedMethod = conn.createGet(pagedRequest, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
    HttpResponse pagedResponse = conn.execute(pagedMethod);
    Assert.assertEquals(200, pagedResponse.getStatusLine().getStatusCode());
    InputStream pagedBody = pagedResponse.getEntity().getContent();
    CourseVOes pagedCourses = conn.parse(pagedBody, CourseVOes.class);
    Assert.assertNotNull(pagedCourses);
    Assert.assertEquals(1, pagedCourses.getTotalCount());
    Assert.assertNotNull(pagedCourses.getCourses());
    Assert.assertEquals(1, pagedCourses.getCourses().length);
    conn.shutdown();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) CourseVOes(org.olat.restapi.support.vo.CourseVOes) Identity(org.olat.core.id.Identity) URI(java.net.URI) Test(org.junit.Test)

Example 14 with CourseVO

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

the class UserCoursesTest method testFavoritCourses.

@Test
public void testFavoritCourses() throws IOException, URISyntaxException {
    // prepare a course with a tutor
    Identity me = JunitTestHelper.createAndPersistIdentityAsUser("Course-teacher-" + UUID.randomUUID().toString());
    ICourse course = CoursesWebService.createEmptyCourse(me, "A course to teach", "A course to teach", null);
    RepositoryEntry courseRe = repositoryManager.lookupRepositoryEntry(course, true);
    repositoryManager.setAccess(courseRe, RepositoryEntry.ACC_USERS, false);
    markManager.setMark(courseRe, me, null, "[RepositoryEntry:" + courseRe.getKey() + "]");
    dbInstance.commitAndCloseSession();
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(me.getName(), JunitTestHelper.PWD));
    // without paging
    URI request = UriBuilder.fromUri(getContextURI()).path("/users").path(me.getKey().toString()).path("/courses/favorite").build();
    HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<CourseVO> courses = parseCourseArray(body);
    Assert.assertNotNull(courses);
    Assert.assertEquals(1, courses.size());
    // with paging
    URI pagedRequest = UriBuilder.fromUri(getContextURI()).path("/users").path(me.getKey().toString()).path("/courses/favorite").queryParam("start", "0").queryParam("limit", "10").build();
    HttpGet pagedMethod = conn.createGet(pagedRequest, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
    HttpResponse pagedResponse = conn.execute(pagedMethod);
    Assert.assertEquals(200, pagedResponse.getStatusLine().getStatusCode());
    InputStream pagedBody = pagedResponse.getEntity().getContent();
    CourseVOes pagedCourses = conn.parse(pagedBody, CourseVOes.class);
    Assert.assertNotNull(pagedCourses);
    Assert.assertEquals(1, pagedCourses.getTotalCount());
    Assert.assertNotNull(pagedCourses.getCourses());
    Assert.assertEquals(1, pagedCourses.getCourses().length);
    conn.shutdown();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) CourseVOes(org.olat.restapi.support.vo.CourseVOes) Identity(org.olat.core.id.Identity) URI(java.net.URI) Test(org.junit.Test)

Example 15 with CourseVO

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

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)

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