Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java =================================================================== diff -u -rd97ff2b497d597eb35cf7aa9e413ab0835695bfe -r3ce122302ef39b55ee77933833caa16189148453 --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java (.../ExportPortfolioService.java) (revision d97ff2b497d597eb35cf7aa9e413ab0835695bfe) +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java (.../ExportPortfolioService.java) (revision 3ce122302ef39b55ee77933833caa16189148453) @@ -52,6 +52,7 @@ import org.lamsfoundation.lams.learningdesign.dao.ITransitionDAO; import org.lamsfoundation.lams.lesson.LearnerProgress; import org.lamsfoundation.lams.lesson.Lesson; +import org.lamsfoundation.lams.lesson.dao.ILessonDAO; import org.lamsfoundation.lams.tool.Tool; import org.lamsfoundation.lams.tool.ToolAccessMode; import org.lamsfoundation.lams.tool.ToolSession; @@ -80,7 +81,9 @@ private ITransitionDAO transitionDAO; private IActivityDAO activityDAO; private ILearnerService learnerService; + private ILessonDAO lessonDAO; + /** * @param learnerService The learnerService to set. */ @@ -102,6 +105,13 @@ this.activityDAO = activityDAO; } + /** + * @param lessonDAO The lessonDAO to set. + */ + public void setLessonDAO(ILessonDAO lessonDAO) { + this.lessonDAO = lessonDAO; + } + /** * @param lamsCoreToolService The lamsCoreToolService to set. */ @@ -176,7 +186,37 @@ return exports; } + public Portfolio[] exportPortfolioForTeacher(Long lessonId, Cookie[] cookies) + { + Lesson lesson = lessonDAO.getLesson(lessonId); + Vector portfolios = null; //each portfolio contains information about its activity, ordered in the same sequence as the ordered activity list. + Portfolio[] exports = null; + + if (lesson==null) + { + String error="lesson cannot be null"; + throw new ExportPortfolioException(error); + } + Vector activities = getOrderedActivityList(lesson.getLearningDesign()); + + try + { + portfolios = setupPortfolios(activities, ToolAccessMode.TEACHER, null); + + exports = doExport(portfolios, ExportPortfolioConstants.EXPORT_TMP_DIR, cookies); + + } + catch (LamsToolServiceException e) + { + throw new ExportPortfolioException(e); + } + return exports; + + + } + + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#exportPortfolioForStudent(java.lang.Long, org.lamsfoundation.lams.usermanagement.User,boolean) */ public Portfolio[] exportPortfolioForStudent(Long learnerProgressId, User user, boolean anonymity, Cookie[] cookies) { @@ -379,12 +419,17 @@ public Portfolio[] doExport(Vector portfolios, String tempDirectoryName, Cookie[] cookies) { Iterator i = portfolios.iterator(); - // String baseSubDirectoryName = "Activity"; String activitySubDirectory; String exportURL; - String toolLink; - + String toolLink; String mainFileName = null; + + //create the root directory for the export + if(!createDirectory(ExportPortfolioConstants.EXPORT_TMP_DIR)) + { + throw new ExportPortfolioException("The export directory cannot be created. Cannot continue"); + } + //iterate through the list of portfolios, create subdirectory, while(i.hasNext()) { @@ -449,6 +494,53 @@ return created; } + /** + * Helper method which calls the FileUtil to create directory. + * It will check whether the directory exists, if so, then it will + * overwrite the existing directory. (It is assumed that the cron jobs + * should delete the directory + files anyway, after the export is done) + * @param parentDir The name of the parent directory + * @param subDir The name of the child directory to create. + * @return true is the subdirectory was created, false otherwise + */ + private boolean createDirectory(String directory) + { + boolean created=false; + if (FileUtil.directoryExist(directory)) + { + //delete directory and create a new one + try + { + if (FileUtil.deleteDirectory(directory)) + { + created = FileUtil.createDirectory(directory); + } + else + { + throw new ExportPortfolioException("Could not delete the temporary directory " + directory + ", please manually delete this directory in order to proceed."); + } + + } + catch(FileUtilException e) + { + throw new ExportPortfolioException("An error has occurred while trying to create the temporary directory for the export. Reason: ", e); + } + } + else + { + try + { + created = FileUtil.createDirectory(directory); + } + catch(FileUtilException e) + { + throw new ExportPortfolioException("An error has occurred while trying to create the temporary directory for the export. Reason: ", e); + } + + } + return created; + } + /** @see org.lamsfoundation.lams.learning.export.service.IExportPortfolioService#exportToolPortfolio(org.lamsfoundation.lams.learning.export.Portfolio) */ public String connectToToolViaExportURL(String exportURL, Cookie[] cookies) Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/IExportPortfolioService.java =================================================================== diff -u -r3a80f5adeb4a1215bb9bb022ff67ccae09a629b7 -r3ce122302ef39b55ee77933833caa16189148453 --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/IExportPortfolioService.java (.../IExportPortfolioService.java) (revision 3a80f5adeb4a1215bb9bb022ff67ccae09a629b7) +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/IExportPortfolioService.java (.../IExportPortfolioService.java) (revision 3ce122302ef39b55ee77933833caa16189148453) @@ -73,6 +73,7 @@ */ public Portfolio[] exportPortfolioForTeacher(Lesson lesson, Cookie[] cookies); + public Portfolio[] exportPortfolioForTeacher(Long lessonId, Cookie[] cookies); /** * The main method that performs the export for the student. * It will get the list of activities that the user has completed Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/MainExportServlet.java =================================================================== diff -u -r1dcfef5e945bc872af2e90df3804846e315b1ee8 -r3ce122302ef39b55ee77933833caa16189148453 --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/MainExportServlet.java (.../MainExportServlet.java) (revision 1dcfef5e945bc872af2e90df3804846e315b1ee8) +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/MainExportServlet.java (.../MainExportServlet.java) (revision 3ce122302ef39b55ee77933833caa16189148453) @@ -8,8 +8,13 @@ import org.apache.log4j.Logger; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.PrintWriter; +import java.lang.StringBuffer; import java.net.MalformedURLException; import javax.servlet.ServletException; @@ -19,12 +24,22 @@ import javax.servlet.http.Cookie; import org.lamsfoundation.lams.tool.ToolAccessMode; +import org.lamsfoundation.lams.usermanagement.User; import org.lamsfoundation.lams.util.HttpUrlConnectionUtil; import org.lamsfoundation.lams.util.FileUtil; import org.lamsfoundation.lams.util.FileUtilException; import org.lamsfoundation.lams.util.WebUtil; import org.lamsfoundation.lams.learning.export.ExportPortfolioException; import org.lamsfoundation.lams.learning.export.ExportPortfolioConstants; +import org.lamsfoundation.lams.learning.export.Portfolio; +import org.lamsfoundation.lams.learning.export.service.IExportPortfolioService; +import org.lamsfoundation.lams.learning.export.service.ExportPortfolioService; +import org.lamsfoundation.lams.learning.export.service.ExportPortfolioServiceProxy; +import org.lamsfoundation.lams.learning.service.ILearnerService; +import org.lamsfoundation.lams.learning.service.LearnerServiceProxy; +import org.lamsfoundation.lams.learning.web.util.LearningWebUtil; +import org.lamsfoundation.lams.lesson.LearnerProgress; +import org.lamsfoundation.lams.lesson.Lesson; @@ -55,17 +70,18 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ExportPortfolioException{ - + + /** Get the cookies that were sent along with this request, then pass it onto export service */ + Cookie[] cookies = request.getCookies(); - String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath(); + /* String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath(); String url = basePath + "/exportPortfolio.do"; String urlWithParameters = null; String mode = WebUtil.readStrParam(request, WebUtil.PARAM_MODE); - /** Get the cookies that were sent along with this request, then pass it onto export service */ - Cookie[] cookies = request.getCookies(); + //create the directory if (createTemporaryDirectory(ExportPortfolioConstants.EXPORT_TMP_DIR)) { @@ -102,8 +118,44 @@ } //forward somewhere + */ + String mainFileName = ExportPortfolioConstants.EXPORT_TMP_DIR + File.separator + ExportPortfolioConstants.MAIN_EXPORT_FILENAME; + IExportPortfolioService exportService = ExportPortfolioServiceProxy.getExportPortfolioService(this.getServletContext()); + String htmlOutput=null; + + Portfolio[] portfolios = null; + String mode = WebUtil.readStrParam(request, WebUtil.PARAM_MODE); + if (mode.equals(ToolAccessMode.LEARNER.toString())) + { + //get the learnerprogress id + User learner = LearningWebUtil.getUserData(request, this.getServletContext()); + LearnerProgress learnerProgress = LearningWebUtil.getLearnerProgressByUser(request, this.getServletContext()); + Long progressId = learnerProgress.getLearnerProgressId(); + + portfolios = exportService.exportPortfolioForStudent(progressId, learner, true, cookies); + } + else if(mode.equals(ToolAccessMode.TEACHER.toString())) + { + //get the lesson data + //done in the monitoring environment + Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID")); + //ILearnerService learnerService = LearnerServiceProxy.getLearnerService(this.getServletContext()); + //Lesson lesson = learnerService.getLesson(lessonID); + + //portfolios = exportService.exportPortfolioForTeacher(lesson.getLearningDesign().getLearningDesignId(), cookies); + portfolios = exportService.exportPortfolioForTeacher(lessonID, cookies); + } + htmlOutput = generateMainPage(portfolios); + + BufferedWriter fileout = new BufferedWriter(new FileWriter(mainFileName)); + fileout.write(htmlOutput); + fileout.close(); + + PrintWriter out = response.getWriter(); + out.println(htmlOutput); + } /** @@ -163,7 +215,7 @@ * @param directory The name of the directory to create * @return true if the directory has been created, false otherwise */ - private boolean createTemporaryDirectory(String directory) +/* private boolean createTemporaryDirectory(String directory) { boolean created=false; if (FileUtil.directoryExist(directory)) @@ -201,6 +253,25 @@ return created; + } */ + private String generateMainPage(Portfolio[] portfolios) + { + StringBuffer htmlPage = new StringBuffer(); + htmlPage.append("Export Portfolio"); + htmlPage.append("

Portfolio

Activities

"); + htmlPage.append("
    "); + for (int i=0; i"); + htmlPage.append(portfolios[i].getActivityName()) + .append(" ") + .append(portfolios[i].getActivityDescription()).append(""); + htmlPage.append(""); + } + htmlPage.append("
"); + htmlPage.append("