Index: lams_central/src/java/org/lamsfoundation/lams/web/qb/QbCollectionController.java =================================================================== diff -u -r3ac00c8cbf0ef8d92fb504727e16ca8f4e3e13c8 -r374f47ea57a62a77e6faf1bb9713de1f9f53c1be --- lams_central/src/java/org/lamsfoundation/lams/web/qb/QbCollectionController.java (.../QbCollectionController.java) (revision 3ac00c8cbf0ef8d92fb504727e16ca8f4e3e13c8) +++ lams_central/src/java/org/lamsfoundation/lams/web/qb/QbCollectionController.java (.../QbCollectionController.java) (revision 374f47ea57a62a77e6faf1bb9713de1f9f53c1be) @@ -71,6 +71,10 @@ public String showUserCollections(Model model) throws Exception { Integer userId = getUserId(); List collections = qbService.getUserCollections(userId); + for (QbCollection collection : collections) { + collection.setShareableWithOrganisations( + qbService.getShareableWithOrganisations(collection.getUid(), userId)); + } QbCollection privateCollection = qbService.getUserPrivateCollection(userId); collections.add(privateCollection); @@ -178,15 +182,15 @@ if (StringUtils.isBlank(excluded)) { ArrayNode includedJSON = JsonUtil.readArray(included); for (int index = 0; index < includedJSON.size(); index++) { - qbService.addQuestionToCollection(targetCollectionUid, includedJSON.get(index).asLong()); + qbService.addQuestionToCollection(targetCollectionUid, includedJSON.get(index).asLong(), copy); } } else { ArrayNode excludedJSON = JsonUtil.readArray(excluded); Set excludedSet = new HashSet<>(); for (int index = 0; index < excludedJSON.size(); index++) { excludedSet.add(excludedJSON.get(index).asLong()); } - qbService.addQuestionToCollection(sourceCollectionUid, targetCollectionUid, excludedSet); + qbService.addQuestionToCollection(sourceCollectionUid, targetCollectionUid, excludedSet, copy); } } @@ -202,6 +206,12 @@ qbService.removeCollection(collectionUid); } + @RequestMapping("/shareCollection") + @ResponseBody + public void shareCollection(@RequestParam long collectionUid, @RequestParam int organisationId) { + qbService.shareCollection(collectionUid, organisationId); + } + private Integer getUserId() { HttpSession ss = SessionManager.getSession(); UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER); Index: lams_central/web/qb/collection.jsp =================================================================== diff -u -r3ac00c8cbf0ef8d92fb504727e16ca8f4e3e13c8 -r374f47ea57a62a77e6faf1bb9713de1f9f53c1be --- lams_central/web/qb/collection.jsp (.../collection.jsp) (revision 3ac00c8cbf0ef8d92fb504727e16ca8f4e3e13c8) +++ lams_central/web/qb/collection.jsp (.../collection.jsp) (revision 374f47ea57a62a77e6faf1bb9713de1f9f53c1be) @@ -34,11 +34,11 @@ display: inline; } - .targetCollectionSelect { + .targetCollectionSelect, .targetOrganisationSelect { width: 300px; } - .targetCollectionDiv { + .targetCollectionDiv, .targetOrganisationDiv { float: right; } @@ -255,6 +255,25 @@ }); } } + + function shareCollection(button) { + var grid = $(button).closest('.collectionButtons').siblings(".ui-jqgrid").find('.collection-grid'), + collectionUid = grid.data('collectionUid'), + organisationId = $(button).closest('.collectionButtons').find('.targetOrganisationSelect').val(); + + $.ajax({ + 'url' : 'qb/collection/shareCollection.do', + 'type' : 'POST', + 'dataType' : 'text', + 'data' : { + 'collectionUid' : collectionUid, + 'organisationId': organisationId + }, + 'cache' : false + }).done(function(){ + document.location.reload(); + }); + } @@ -275,7 +294,9 @@ selected="selected" - > + > + + @@ -286,6 +307,27 @@
+
+ Share collection with + + +
+ +
+ Shared with organisations +
    + +
  • +
    +
+
+
Index: lams_common/src/java/org/lamsfoundation/lams/commonContext.xml =================================================================== diff -u -r399b35bc9ee55acf1d64596c09b96fe5aee73709 -r374f47ea57a62a77e6faf1bb9713de1f9f53c1be --- lams_common/src/java/org/lamsfoundation/lams/commonContext.xml (.../commonContext.xml) (revision 399b35bc9ee55acf1d64596c09b96fe5aee73709) +++ lams_common/src/java/org/lamsfoundation/lams/commonContext.xml (.../commonContext.xml) (revision 374f47ea57a62a77e6faf1bb9713de1f9f53c1be) @@ -542,6 +542,7 @@ + Index: lams_common/src/java/org/lamsfoundation/lams/qb/model/QbCollection.java =================================================================== diff -u -r8b28898e77f3b0abafb08346c55fae0c590cad96 -r374f47ea57a62a77e6faf1bb9713de1f9f53c1be --- lams_common/src/java/org/lamsfoundation/lams/qb/model/QbCollection.java (.../QbCollection.java) (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) +++ lams_common/src/java/org/lamsfoundation/lams/qb/model/QbCollection.java (.../QbCollection.java) (revision 374f47ea57a62a77e6faf1bb9713de1f9f53c1be) @@ -51,6 +51,9 @@ @OrderBy("name") private List organisations = new ArrayList<>(); + @Transient + private List shareableWithOrganisations = new ArrayList<>(); + public Long getUid() { return uid; } @@ -87,6 +90,14 @@ this.organisations = organisations; } + public List getShareableWithOrganisations() { + return shareableWithOrganisations; + } + + public void setShareableWithOrganisations(List shareableWithOrganisations) { + this.shareableWithOrganisations = shareableWithOrganisations; + } + @Override public boolean equals(Object o) { QbCollection other = (QbCollection) o; Index: lams_common/src/java/org/lamsfoundation/lams/qb/service/IQbService.java =================================================================== diff -u -r730a7378ea82a88512217331afdbd1f0f0346d61 -r374f47ea57a62a77e6faf1bb9713de1f9f53c1be --- lams_common/src/java/org/lamsfoundation/lams/qb/service/IQbService.java (.../IQbService.java) (revision 730a7378ea82a88512217331afdbd1f0f0346d61) +++ lams_common/src/java/org/lamsfoundation/lams/qb/service/IQbService.java (.../IQbService.java) (revision 374f47ea57a62a77e6faf1bb9713de1f9f53c1be) @@ -70,14 +70,16 @@ void removeCollection(long collectionUid); - Organisation shareCollection(QbCollection collection, int organisationId); + List getShareableWithOrganisations(long collectionUid, int userId); - void unshareCollection(QbCollection collection, int organisationId); + Organisation shareCollection(long collectionUid, int organisationId); - void addQuestionToCollection(long collectionUid, long qbQuestionUid); + void unshareCollection(long collectionUid, int organisationId); + void addQuestionToCollection(long collectionUid, long qbQuestionUid, boolean copy); + void addQuestionToCollection(long sourceCollectionUid, long targetCollectionUid, - Collection excludedQbQuestionUids); + Collection excludedQbQuestionUids, boolean copy); void removeQuestionFromCollection(long collectionUid, long qbQuestionUid); Index: lams_common/src/java/org/lamsfoundation/lams/qb/service/QbService.java =================================================================== diff -u -r3ac00c8cbf0ef8d92fb504727e16ca8f4e3e13c8 -r374f47ea57a62a77e6faf1bb9713de1f9f53c1be --- lams_common/src/java/org/lamsfoundation/lams/qb/service/QbService.java (.../QbService.java) (revision 3ac00c8cbf0ef8d92fb504727e16ca8f4e3e13c8) +++ lams_common/src/java/org/lamsfoundation/lams/qb/service/QbService.java (.../QbService.java) (revision 374f47ea57a62a77e6faf1bb9713de1f9f53c1be) @@ -1,8 +1,10 @@ package org.lamsfoundation.lams.qb.service; import java.security.InvalidParameterException; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -27,6 +29,8 @@ import org.lamsfoundation.lams.qb.model.QbQuestion; import org.lamsfoundation.lams.tool.service.ILamsCoreToolService; import org.lamsfoundation.lams.usermanagement.Organisation; +import org.lamsfoundation.lams.usermanagement.Role; +import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; import org.lamsfoundation.lams.util.Configuration; import org.lamsfoundation.lams.util.ConfigurationKeys; import org.lamsfoundation.lams.util.WebUtil; @@ -44,6 +48,8 @@ private ILamsCoreToolService lamsCoreToolService; + private IUserManagementService userManagementService; + @Override public QbQuestion getQbQuestionByUid(Long qbQuestionUid) { return qbDAO.getQbQuestionByUid(qbQuestionUid); @@ -305,15 +311,23 @@ } @Override - public Organisation shareCollection(QbCollection collection, int organisationId) { + public Organisation shareCollection(long collectionUid, int organisationId) { + QbCollection collection = (QbCollection) qbDAO.find(QbCollection.class, collectionUid); + if (collection.getUserId() == null || collection.isPersonal()) { + throw new InvalidParameterException("Attempt to share a private or the public question bank collection"); + } Organisation organisation = (Organisation) qbDAO.find(Organisation.class, organisationId); collection.getOrganisations().add(organisation); qbDAO.update(collection); return organisation; } @Override - public void unshareCollection(QbCollection collection, int organisationId) { + public void unshareCollection(long collectionUid, int organisationId) { + QbCollection collection = (QbCollection) qbDAO.find(QbCollection.class, collectionUid); + if (collection.getUserId() == null || collection.isPersonal()) { + throw new InvalidParameterException("Attempt to unshare a private or the public question bank collection"); + } Iterator orgIterator = collection.getOrganisations().iterator(); while (orgIterator.hasNext()) { if (orgIterator.next().getOrganisationId().equals(organisationId)) { @@ -325,17 +339,27 @@ } @Override - public void addQuestionToCollection(long collectionUid, long qbQuestionUid) { - qbDAO.addCollectionQuestion(collectionUid, qbQuestionUid); + public void addQuestionToCollection(long collectionUid, long qbQuestionUid, boolean copy) { + long addQbQuestionUid = qbQuestionUid; + if (copy) { + QbQuestion question = getQbQuestionByUid(qbQuestionUid); + QbQuestion newQuestion = question.clone(); + newQuestion.setQuestionId(getMaxQuestionId()); + newQuestion.setVersion(1); + newQuestion.setCreateDate(new Date()); + qbDAO.insert(newQuestion); + addQbQuestionUid = newQuestion.getUid(); + } + qbDAO.addCollectionQuestion(collectionUid, addQbQuestionUid); } @Override public void addQuestionToCollection(long sourceCollectionUid, long targetCollectionUid, - Collection excludedQbQuestionUids) { + Collection excludedQbQuestionUids, boolean copy) { Collection includedUids = qbDAO.getCollectionQuestionUidsExcluded(sourceCollectionUid, excludedQbQuestionUids); for (Long uid : includedUids) { - addQuestionToCollection(targetCollectionUid, uid); + addQuestionToCollection(targetCollectionUid, uid, copy); } } @@ -352,6 +376,32 @@ } } + @Override + public List getShareableWithOrganisations(long collectionUid, int userId) { + QbCollection collection = (QbCollection) qbDAO.find(QbCollection.class, collectionUid); + if (collection.getUserId() == null || collection.isPersonal()) { + return null; + } + + List result = new ArrayList<>(); + Map> roles = userManagementService.getRolesForUser(userId); + for (Entry> roleEntry : roles.entrySet()) { + Integer organisationId = roleEntry.getKey(); + for (Organisation organisation : collection.getOrganisations()) { + if (organisation.getOrganisationId().equals(organisationId)) { + organisationId = null; + break; + } + } + if (organisationId != null && roleEntry.getValue().contains(Role.ROLE_AUTHOR)) { + Organisation organisation = (Organisation) qbDAO.find(Organisation.class, organisationId); + result.add(organisation); + } + } + + return result; + } + public void setQbDAO(IQbDAO qbDAO) { this.qbDAO = qbDAO; } @@ -363,4 +413,8 @@ public void setLamsCoreToolService(ILamsCoreToolService lamsCoreToolService) { this.lamsCoreToolService = lamsCoreToolService; } + + public void setUserManagementService(IUserManagementService userManagementService) { + this.userManagementService = userManagementService; + } } \ No newline at end of file