From 3ef8e546f23c2efef5beb57c14a07f841017cd7a Mon Sep 17 00:00:00 2001
From: Daniel Haag <daniel.haag@uibk.ac.at>
Date: Thu, 7 Jul 2016 11:44:41 +0200
Subject: [PATCH] OPENOLAT-330: added catalog dropdown element to the course
 creation dialog, show only one create button always opening the wizard

---
 ...CreateCourseRepositoryEntryController.java | 72 ++++++++++++++-----
 .../course/_i18n/LocalStrings_de.properties   |  5 +-
 .../course/_i18n/LocalStrings_en.properties   |  4 ++
 3 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/src/main/java/at/ac/uibk/course/CreateCourseRepositoryEntryController.java b/src/main/java/at/ac/uibk/course/CreateCourseRepositoryEntryController.java
index adaceb9acbb..127b8a5c179 100644
--- a/src/main/java/at/ac/uibk/course/CreateCourseRepositoryEntryController.java
+++ b/src/main/java/at/ac/uibk/course/CreateCourseRepositoryEntryController.java
@@ -25,6 +25,7 @@ package at.ac.uibk.course;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.List;
 
@@ -68,12 +69,13 @@ import org.olat.course.editor.PublishProcess;
 import org.olat.course.editor.PublishSetInformations;
 import org.olat.course.editor.StatusDescription;
 import org.olat.course.tree.CourseEditorTreeModel;
+import org.olat.repository.CatalogEntry;
 import org.olat.repository.RepositoryEntry;
 import org.olat.repository.RepositoryManager;
-import org.olat.repository.controllers.EntryChangedEvent;
-import org.olat.repository.controllers.EntryChangedEvent.Change;
 import org.olat.repository.handlers.RepositoryHandler;
+import org.olat.repository.manager.CatalogManager;
 import org.olat.repository.ui.author.CreateEntryController;
+import org.olat.repository.ui.catalog.CatalogEntryNodeComparator;
 import org.olat.resource.accesscontrol.ACService;
 import org.olat.resource.accesscontrol.manager.ACMethodDAO;
 import org.olat.resource.accesscontrol.model.AccessMethod;
@@ -96,9 +98,6 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 
 	public static final Event CREATION_WIZARD = new Event("start_wizard");
 
-	private FormLink wizardButton;
-	// private TextBoxAutocompleteElement displaynameEl;
-
 	private RepositoryEntry addedEntry;
 	private final RepositoryHandler handler;
 
@@ -137,12 +136,17 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 			CreateCourseModel.ACCESSIBILITY_PWD,
 			CreateCourseModel.ACCESSIBILITY_PERIOD };
 
+	@Autowired
+	protected CatalogManager catalogManager;
+	
 	private Translator translator;
 
 	private CreateCourseModel createCourseModel;
 
 	private FormItemContainer flc;
 
+	private SingleSelection catalogDropdownElement;
+
 	public CreateCourseRepositoryEntryController(UserRequest ureq,
 			WindowControl wControl, RepositoryHandler handler) {
 		super(ureq, wControl);
@@ -334,25 +338,40 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 		periodEndTextElement.setVisible(false);
 
 		refreshRadioAccessibility();
+
+		Comparator<CatalogEntry> byCatalogRoot = (c1, c2) -> Boolean.compare(
+				c1.getParent() != null, c2.getParent() != null);
+
+		List<CatalogEntry> catEntryList = catalogManager.getAllCatalogNodes();
+		catEntryList.sort(byCatalogRoot.thenComparing(new CatalogEntryNodeComparator(getLocale())));
+
+		String[] catalogKeys = catEntryList.stream()
+				.map(e -> e.getKey().toString())
+				.toArray(String[]::new);
+		String[] catalogValues = catEntryList.stream()
+				.map(e -> e.getParent() != null ? e.getName() : translator.translate("form.catalog.noentry"))
+				.toArray(String[]::new);
+		String[] catalogStyles = catEntryList.stream()
+				.map(e -> CatalogEntry.TYPE_LEAF == e.getType()?"o_icon_leaf":"o_icon_node")
+				.toArray(String[]::new);
+		
+		catalogDropdownElement = uifactory
+				.addDropdownSingleselect("form.catalog.entry", formLayout, catalogKeys, catalogValues, catalogStyles);
+		catalogDropdownElement.setVisible(false);
 		
 		/************************************************************************************************
-		 * Add Create Wizard and Cancel Buttons
+		 * Add Create and Cancel Buttons
 		 ************************************************************************************************/
 
 		FormLayoutContainer buttonContainer = FormLayoutContainer
 				.createButtonLayout("buttonContainer", getTranslator());
 		formLayout.add("buttonContainer", buttonContainer);
 		buttonContainer.setElementCssClass("o_sel_repo_save_details");
+		
 		FormSubmit submit = uifactory.addFormSubmitButton(
 				"cmd.create.ressource", buttonContainer);
 		submit.setElementCssClass("o_sel_author_create_submit");
 
-		// if (handler.isPostCreateWizardAvailable()) {
-		wizardButton = uifactory.addFormLink("form.course.startwizard.button",
-				buttonContainer, Link.BUTTON);
-		wizardButton.setElementCssClass("o_sel_author_create_wizard");
-		// }
-
 		uifactory.addFormCancelButton("cancel", buttonContainer, ureq,
 				getWindowControl());
 	}
@@ -414,20 +433,16 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 	@Override
 	protected void formOK(UserRequest ureq) {
 		doCreate(ureq);
+		fireEvent(ureq, CREATION_WIZARD);
+		/*
 		fireEvent(ureq, Event.DONE_EVENT);
 		fireEvent(ureq, new EntryChangedEvent(addedEntry, getIdentity(),
-				Change.added));
+				Change.added));*/
 	}
 
 	@Override
 	protected void formInnerEvent(UserRequest ureq, FormItem source,
 			FormEvent event) {
-		if (wizardButton == source) {
-			if (validateFormLogic(ureq)) {
-				doCreate(ureq);
-				fireEvent(ureq, CREATION_WIZARD);
-			}
-		}
 		if (source == ownerAddButton) {
 			removeAsListenerAndDispose(usc);
 			usc = new UserSearchController(ureq, getWindowControl(), true,
@@ -506,6 +521,8 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 			}
 			radioAccess.setVisible(true);
 
+			catalogDropdownElement.setVisible(false);
+			
 			flc.getComponent().setDirty(true);
 		} else if (event instanceof EntriesChosenEvent) {
 			EntriesChosenEvent ece = (EntriesChosenEvent) event;
@@ -573,12 +590,16 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 			radioSync.setVisible(true);
 			radioSync.select("form.user.sync.sis", true);
 
+			catalogDropdownElement.setVisible(true);
+
 			for (Identity tutor : filterAvailableUsers(sc.getTutors())) {
 				createCourseModel.addAdditionalOwner(tutor);
 			}
 		} else {
 			radioSync.setEnabled(false);
 			radioSync.setVisible(false);
+
+			catalogDropdownElement.setVisible(false);
 		}
 
 		ownerCheckBoxes.setVisible(true);
@@ -778,6 +799,19 @@ public class CreateCourseRepositoryEntryController extends FormBasicController
 		 * external id
 		 */
 		if (isCreatedFromSisCourse) {
+			CatalogEntry newParent = catalogManager.loadCatalogEntry(Long.valueOf(catalogDropdownElement.getSelectedKey()));
+			// Do not create new catalog entries in the root category
+			if(newParent.getParent() != null) {
+				CatalogEntry newEntry = catalogManager.createCatalogEntry();
+				newEntry.setRepositoryEntry(addedEntry);
+				newEntry.setName(addedEntry.getDisplayname());
+				newEntry.setDescription(addedEntry.getDescription());
+				newEntry.setType(CatalogEntry.TYPE_LEAF);
+				newEntry.setOwnerGroup(BaseSecurityManager.getInstance().createAndPersistSecurityGroup());
+				// save entry
+				catalogManager.addCatalogEntry(newParent, newEntry);
+			}
+			
 			SisManager.getInstance()
 					.sendInvalidateCourseEvent(externalCourseId);
 		}
diff --git a/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_de.properties b/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_de.properties
index dc8f0224e19..32e21c17879 100644
--- a/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_de.properties
+++ b/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_de.properties
@@ -29,4 +29,7 @@ form.accessibility.pwd=Zugangscode
 form.accessibility.pwd.descr=Dieser Zugangskode muss beim ersten Betreten des Online-Kurses von den Studierenden eingegeben werden. (Gro\u00df- /Kleinschreibung beachten!)
 form.accessibility.pwd.empty=Bitte geben Sie einen Zugangskode ein.
 form.accessibility.period.begin=Von
-form.accessibility.period.end=Bis
\ No newline at end of file
+form.accessibility.period.end=Bis
+
+form.catalog.entry=Katalogeintrag
+form.catalog.noentry=Kein Eintrag
\ No newline at end of file
diff --git a/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_en.properties b/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_en.properties
index defcca8cee2..64f7a234af6 100644
--- a/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_en.properties
+++ b/src/main/java/at/ac/uibk/course/_i18n/LocalStrings_en.properties
@@ -30,3 +30,7 @@ form.accessibility.pwd.descr=Users have to enter the access code the first time,
 form.accessibility.pwd.empty=Please insert an access code.
 form.accessibility.period.begin=from
 form.accessibility.period.end=to
+
+form.catalog.entry=Catalog entry
+form.catalog.noentry=No entry
+
-- 
GitLab