Skip to content
Snippets Groups Projects
Commit f16d10ce authored by srosse's avatar srosse
Browse files

OO-3505: export comments and ratings of user

parent 649b4cbe
No related branches found
No related tags found
No related merge requests found
Showing
with 294 additions and 28 deletions
......@@ -31,9 +31,6 @@ import org.olat.core.commons.services.notifications.NotificationsManager;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.user.UserDataDeletable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -49,9 +46,7 @@ import org.springframework.stereotype.Service;
* @author gnaegi
*/
@Service
public class CommentAndRatingServiceImpl implements CommentAndRatingService, UserDataDeletable {
private static final OLog log = Tracing.createLoggerFor(CommentAndRatingServiceImpl.class);
public class CommentAndRatingServiceImpl implements CommentAndRatingService {
@Autowired
private UserRatingsDAO userRatingsDao;
......@@ -173,14 +168,6 @@ public class CommentAndRatingServiceImpl implements CommentAndRatingService, Use
return delCount;
}
@Override
public void deleteUserData(Identity identity, String newDeletedUserName) {
int rows = userRatingsDao.deleteRatings(identity);
log.audit(rows + " rating deleted");
int comments = userCommentsDao.deleteAllComments(identity);
log.audit(comments + " rating erased");
}
private void markPublisherNews(UserComment comment) {
if (comment == null) return;
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.core.commons.services.commentAndRating.manager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Locale;
import org.olat.core.commons.persistence.DB;
import org.olat.core.commons.services.commentAndRating.model.UserComment;
import org.olat.core.commons.services.commentAndRating.model.UserRating;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.id.context.BusinessControlFactory;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.StringHelper;
import org.olat.core.util.openxml.OpenXMLWorkbook;
import org.olat.core.util.openxml.OpenXMLWorksheet;
import org.olat.core.util.openxml.OpenXMLWorksheet.Row;
import org.olat.core.util.resource.OresHelper;
import org.olat.modules.portfolio.Binder;
import org.olat.modules.portfolio.Page;
import org.olat.modules.portfolio.manager.PageDAO;
import org.olat.modules.qpool.QuestionItem;
import org.olat.modules.qpool.manager.QuestionItemDAO;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;
import org.olat.user.UserDataDeletable;
import org.olat.user.UserDataExportable;
import org.olat.user.manager.ManifestBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* Initial date: 28 mai 2018<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
@Service
public class CommentAndRatingUserDataManager implements UserDataDeletable, UserDataExportable {
private static final OLog log = Tracing.createLoggerFor(CommentAndRatingUserDataManager.class);
@Autowired
private DB dbInstance;
@Autowired
private PageDAO pageDao;
@Autowired
private QuestionItemDAO questionItemDao;
@Autowired
private UserRatingsDAO userRatingsDao;
@Autowired
private UserCommentsDAO userCommentsDao;
@Autowired
private RepositoryManager repositoryManager;
@Override
public void deleteUserData(Identity identity, String newDeletedUserName) {
int rows = userRatingsDao.deleteRatings(identity);
log.audit(rows + " rating deleted");
int comments = userCommentsDao.deleteAllComments(identity);
log.audit(comments + " rating erased");
}
@Override
public String getExporterID() {
return "comments.ratings";
}
@Override
public void export(Identity identity, ManifestBuilder manifest, File archiveDirectory, Locale locale) {
exportRatings(identity, manifest, archiveDirectory);
exportComments(identity, manifest, archiveDirectory);
}
private void exportComments(Identity identity, ManifestBuilder manifest, File archiveDirectory) {
List<UserComment> comments = userCommentsDao.getComments(identity);
if(comments == null || comments.isEmpty()) return;
dbInstance.commitAndCloseSession();
File noteArchive = new File(archiveDirectory, "Comments.xlsx");
try(OutputStream out = new FileOutputStream(noteArchive);
OpenXMLWorkbook workbook = new OpenXMLWorkbook(out, 1)) {
OpenXMLWorksheet sheet = workbook.nextWorksheet();
sheet.setHeaderRows(1);
Row header = sheet.newRow();
header.addCell(0, "Comment");
header.addCell(1, "Resource");
header.addCell(2, "URL");
for(UserComment comment:comments) {
Row row = sheet.newRow();
row.addCell(0, comment.getComment());
Location location = resolveLocation(comment.getResName(), comment.getResId());
if(StringHelper.containsNonWhitespace(location.getName())) {
row.addCell(1, location.getName());
}
if(StringHelper.containsNonWhitespace(location.getUrl())) {
row.addCell(2, location.getUrl());
}
}
} catch (IOException e) {
log.error("Unable to export xlsx", e);
}
manifest.appendFile("Bookings.xlsx");
}
private void exportRatings(Identity identity, ManifestBuilder manifest, File archiveDirectory) {
List<UserRating> ratings = userRatingsDao.getAllRatings(identity);
if(ratings == null || ratings.isEmpty()) return;
dbInstance.commitAndCloseSession();
File ratingsArchive = new File(archiveDirectory, "Ratings.xlsx");
try(OutputStream out = new FileOutputStream(ratingsArchive);
OpenXMLWorkbook workbook = new OpenXMLWorkbook(out, 1)) {
OpenXMLWorksheet sheet = workbook.nextWorksheet();
sheet.setHeaderRows(1);
Row header = sheet.newRow();
header.addCell(0, "Rating");
header.addCell(1, "Resource");
header.addCell(2, "URL");
for(UserRating rating:ratings) {
Row row = sheet.newRow();
row.addCell(0, rating.getRating().toString());
Location location = resolveLocation(rating.getResName(), rating.getResId());
if(StringHelper.containsNonWhitespace(location.getName())) {
row.addCell(1, location.getName());
}
if(StringHelper.containsNonWhitespace(location.getUrl())) {
row.addCell(2, location.getUrl());
}
}
} catch (IOException e) {
log.error("Unable to export xlsx", e);
}
manifest.appendFile("Bookings.xlsx");
}
private Location resolveLocation (String resName, Long resId) {
String name = null;
String businessPath = null;
if("RepositoryEntry".equals(resName)) {
RepositoryEntry entry = repositoryManager.lookupRepositoryEntry(resId, false);
if(entry != null) {
name = entry.getDisplayname();
businessPath = "[RepositoryEntry:" + entry.getKey() + "]";
}
} else if("QuestionItem".equals(resName)) {
QuestionItem item = questionItemDao.loadById(resId);
if(item != null) {
name = item.getTitle();
businessPath = "[QPool:0][QuestionItem:" + item.getKey() + "]";
}
} else if("Page".equals(resName)) {
Page page = pageDao.loadByKey(resId);
if(page != null) {
name = page.getTitle();
if(page.getSection() != null) {
Binder binder = page.getSection().getBinder();
businessPath = "[PortfolioV2:0][Binder:" + binder.getKey() + "][Entries:0][Entry:" + page.getKey() + "]";
} else {
businessPath = "[PortfolioV2:0][MyPages:0][Entry:" + page.getKey() + "]";
}
}
} else if("EPDefaultMap".equals(resName)) {
name = "Mappe (v 1.0)";
} else if("LibrarySite".equals(resName)) {
name = "Library";
} else {
OLATResourceable resourceable = OresHelper.createOLATResourceableInstance(resName, resId);
RepositoryEntry entry = repositoryManager.lookupRepositoryEntry(resourceable, false);
if(entry != null) {
name = entry.getDisplayname();
businessPath = "[RepositoryEntry:" + entry.getKey() + "]";
}
}
String url = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath);
return new Location(name, url);
}
private static class Location {
private final String name;
private final String url;
public Location(String name, String url) {
this.url = url;
this.name = name;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
}
}
......@@ -30,7 +30,6 @@ import javax.persistence.TypedQuery;
import org.olat.basesecurity.IdentityRef;
import org.olat.core.commons.persistence.DB;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.commons.services.commentAndRating.CommentAndRatingLoggingAction;
import org.olat.core.commons.services.commentAndRating.UserCommentsDelegate;
import org.olat.core.commons.services.commentAndRating.model.UserComment;
......@@ -104,10 +103,10 @@ public class UserCommentsDAO {
TypedQuery<UserComment> query;
if (resSubPath == null) {
// special query when sub path is null
query = DBFactory.getInstance().getCurrentEntityManager()
query = dbInstance.getCurrentEntityManager()
.createQuery("select comment from usercomment as comment where resName=:resname AND resId=:resId AND resSubPath is NULL", UserComment.class);
} else {
query = DBFactory.getInstance().getCurrentEntityManager()
query = dbInstance.getCurrentEntityManager()
.createQuery("select comment from usercomment as comment where resName=:resname AND resId=:resId AND resSubPath=:resSubPath", UserComment.class)
.setParameter("resSubPath", resSubPath);
}
......@@ -115,6 +114,14 @@ public class UserCommentsDAO {
.setParameter("resId", ores.getResourceableId())
.getResultList();
}
public List<UserComment> getComments(IdentityRef identity) {
String query = "select comment from usercomment as comment where comment.creator.key=:identityKey";
return dbInstance.getCurrentEntityManager()
.createQuery(query, UserComment.class)
.setParameter("identityKey", identity.getKey())
.getResultList();
}
public UserComment updateComment(UserComment comment, String newCommentText) {
// First reload parent from cache to prevent stale object or cache issues
......@@ -200,10 +207,10 @@ public class UserCommentsDAO {
TypedQuery<Number> query;
if (resSubPath == null) {
// special query when sub path is null
query = DBFactory.getInstance().getCurrentEntityManager()
query = dbInstance.getCurrentEntityManager()
.createQuery("select count(*) from usercomment where resName=:resname AND resId=:resId AND resSubPath is NULL", Number.class);
} else {
query = DBFactory.getInstance().getCurrentEntityManager()
query = dbInstance.getCurrentEntityManager()
.createQuery("select count(*) from usercomment where resName=:resname AND resId=:resId AND resSubPath=:resSubPath", Number.class)
.setParameter("resSubPath", resSubPath);
}
......
......@@ -85,6 +85,14 @@ public class UserRatingsDAO {
.setHint("org.hibernate.cacheable", Boolean.TRUE)
.getResultList();
}
public List<UserRating> getAllRatings(IdentityRef identity) {
String sb = "select rating from userrating as rating where rating.creator.key=:identityKey";
return dbInstance.getCurrentEntityManager()
.createQuery(sb, UserRating.class)
.setParameter("identityKey", identity.getKey())
.getResultList();
}
public float getRatingAverage(OLATResourceable ores, String resSubPath) {
StringBuilder sb = new StringBuilder();
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.core.gui;
import org.olat.core.gui.control.winmgr.AJAXFlags;
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.portfolio.manager;
import java.io.File;
......
......@@ -777,13 +777,13 @@ public class ACFrontendManager implements ACService, UserDataExportable {
OpenXMLWorksheet sheet = workbook.nextWorksheet();
sheet.setHeaderRows(1);
Row row = sheet.newRow();
row.addCell(0, "Status");
row.addCell(1, "Booking number");
row.addCell(2, "Date");
row.addCell(3, "Content");
row.addCell(4, "Method");
row.addCell(5, "Total");
Row header = sheet.newRow();
header.addCell(0, "Status");
header.addCell(1, "Booking number");
header.addCell(2, "Date");
header.addCell(3, "Content");
header.addCell(4, "Method");
header.addCell(5, "Total");
List<OrderTableItem> orders = findOrderItems(null, identity, null, null, null, null, 0, -1, null);
for(OrderTableItem order:orders) {
......
......@@ -20,7 +20,6 @@
package org.olat.user.manager;
import org.olat.core.CoreSpringFactory;
import org.olat.core.commons.services.taskexecutor.LongRunnable;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.user.UserDataExportService;
......@@ -31,7 +30,7 @@ import org.olat.user.UserDataExportService;
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class UserDataExportTask implements LongRunnable {
public class UserDataExportTask implements /* Long */ Runnable {
private static final long serialVersionUID = 6931074116105090545L;
......
......@@ -4,6 +4,7 @@ bookings=Buchungen
calendars=Kalendareintr\u00E4ge
certificates=Certifikaten
chat=Chat
comments.ratings=Kommentaren und Bewertungen
disclaimer=Nutzungsbedigung
display.portrait=Profilbild
download.data=Dateien herunterladen
......
......@@ -4,6 +4,7 @@ bookings=Bookings
calendars=Calendars
certificates=Certificates
chat=Chat messages
comments.ratings=Comments and ratings
disclaimer=Disclaimer
display.portrait=Published image
download.data=Download the data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment