Alberto Ruiz

Create a content item in the course TOC

Blog Post created by Alberto Ruiz on Dec 28, 2017

I want to share my experience creating a "blank page" content item in the course TOC programatically.

 

My motivation was to add the Collaborate recordings download links from a course to a content in order to keep them available after restoring the course in other instance of Learn after archiving, because the pk1 and the Collaborate plugin tables are not kept.

 

The core of the solution is this code:

 

  CourseToc newToc = new CourseToc();

  newToc.setTargetType(Target.CONTENT_ITEM);
  newToc.setCourseId(course.getId());
  newToc.setLabel("Recordings");
  newToc.setLaunchInNewWindow(false);
  newToc.setIsEnabled(true);
  newToc.setAllowGuests(false);

  Content content = new Content();

  content.setTitle("Recordings");
  content.setCourseId(course.getId());
  content.setIsAvailable(true);
  content.setAllowGuests(false);
  content.setIsFolder(false);
  content.setLaunchInNewWindow(false);
  content.setContentHandler("resource/x-bb-blankpage");
  content.setBody(FormattedText.toFormattedText(contentString));

  ContentDbPersister.Default.getInstance().persist(content);

  newToc.setContentId(content.getId());

  CourseTocDbPersister.Default.getInstance().persist(newToc );

  CourseMapManagerFactory.getInstance().invalidateCache(course.getId());

 

The contentString variable contains the HTML to show in the blank page content.

 

Hope this will help somebody who want to create any kind of content programatically, not just my particular user case.

Outcomes