diff --git a/lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/tool/beans/SimplePageBean.java b/lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/tool/beans/SimplePageBean.java index 72a84732d491..3a9082eeed69 100644 --- a/lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/tool/beans/SimplePageBean.java +++ b/lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/tool/beans/SimplePageBean.java @@ -151,6 +151,8 @@ import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * Backing bean for Simple pages @@ -6738,6 +6740,28 @@ public void addMultimedia() { if (!checkCsrf()) return; + // For the sole case of adding multimedia as content links above another item + // (i.e., not the Add Content button at the top of the Lessons page, nor the Add Content '+' + // button at the bottom of a section), the items below the newly inserted content links + // need to have their sequence values revised, especially if there is more than one item to insert. + // First, collect these items in the itemsToFix list before inserting the new content links (which + // affects item sequencing for the page). After inserting the new content links, revise the sequence + // values for itemsToFix. + List itemsToFix = null; + boolean fixItemSequence = StringUtils.isNotEmpty(addBefore) && !StringUtils.startsWith(addBefore, "-"); + if (fixItemSequence) { + List items = getItemsOnPage(getCurrentPageId()); + items.sort(Comparator.comparing(SimplePageItem::getSequence)); + long addBeforeItemId = Long.valueOf(addBefore); + int addBeforeIndex = IntStream.range(0, items.size()) + .filter(i -> addBeforeItemId == items.get(i).getId()) + .findFirst() + .orElse(-1); + if (addBeforeIndex >= 0) { + itemsToFix = items.stream().skip(addBeforeIndex).collect(Collectors.toList()); + } + } + // SAK-41846 - Initialize counters to keep track of files to add and the item sequence values to adjust totalMultimediaFilesToAdd = multipartMap.size(); remainingMultimediaFilesToAdd = totalMultimediaFilesToAdd; @@ -6763,6 +6787,14 @@ public void addMultimedia() { fileindex++; } } + + if (fixItemSequence) { + for (SimplePageItem itemToFix : itemsToFix) { + itemToFix.setSequence(itemToFix.getSequence() + totalMultimediaFilesToAdd); + update(itemToFix); + } + } + } catch (Exception exception) { log.error(exception.getMessage(), exception); } finally { @@ -6772,7 +6804,6 @@ public void addMultimedia() { totalMultimediaFilesToAdd = 0; remainingMultimediaFilesToAdd = 0; } - } public void addMultimediaFile(MultipartFile file, String name){