	protected void zipSubmissions(Assignment2 assignment,
			List<AssignmentSubmission> submissionsWithHistory, OutputStream outputStream,
			StringBuilder exceptionMessage)
	{
		String assignmentTitle = assignment.getTitle();
		String contextId = externalLogic.getCurrentContextId();
		String currUserId = externalLogic.getCurrentUserId();
		List<String> viewableStudents = permissionLogic.getViewableStudentsForUserForItem(currUserId, assignment);
		Map<String, User> userIdUserMap = externalLogic.getUserIdUserMap(viewableStudents);
		String formatWithTime = bundle.getString("assignment2.assignment_grade_assignment.downloadall.filename_date_format_with_time");
		String formatNoTime = bundle.getString("assignment2.assignment_grade_assignment.downloadall.filename_date_format");
		DateFormat df_withTime = new SimpleDateFormat(formatWithTime, bundle.getLocale());
		DateFormat df_noTime = new SimpleDateFormat(formatNoTime, bundle.getLocale());
		if (!assignment.isGraded() && (submissionsWithHistory == null || submissionsWithHistory.isEmpty())) {
		    if (log.isDebugEnabled()) log.debug("Nothing to download!!");
		} else {
		    try
		    {
		        ZipOutputStream out = new ZipOutputStream(outputStream);
		        String root = Validator.escapeZipEntry(assignmentTitle) + Entity.SEPARATOR;
		        if (submissionsWithHistory != null && !submissionsWithHistory.isEmpty())
		        {
		            List<String> submissionFolderNames = new ArrayList<String>();
		            for (AssignmentSubmission s : submissionsWithHistory)
		            {
		                if (s.getSubmissionHistorySet() != null && !s.getSubmissionHistorySet().isEmpty()) {
		                    User submitterUser = userIdUserMap.get(s.getUserId());
		                    if (submitterUser != null) {
		                        String submissionFolder = submitterUser.getSortName();
		                        if (submissionFolderNames.contains(submissionFolder)) {
		                            submissionFolder = getUniqueFolderName(submissionFolder, submissionFolderNames);
		                        }
		                        submissionFolderNames.add(submissionFolder);
		                        Set<AssignmentSubmissionVersion> versionHistory = s.getSubmissionHistorySet();
		                        if (versionHistory != null && !versionHistory.isEmpty()) {
		                            List<String> versionFolderNames = new ArrayList<String>(); 
		                            for (AssignmentSubmissionVersion version : versionHistory) {
		                                if (version.getSubmittedDate() != null) {
		                                    String versionFolder = root + submissionFolder + Entity.SEPARATOR 
		                                    + df_withTime.format(version.getSubmittedDate());
		                                    if (versionFolderNames.contains(versionFolder)) {
		                                        versionFolder = getUniqueFolderName(versionFolder, versionFolderNames);
		                                    }
		                                    versionFolderNames.add(versionFolder);
		                                    ZipEntry versionFolderEntry = new ZipEntry(
		                                            versionFolder);
		                                    out.putNextEntry(versionFolderEntry);
		                                    out.flush();
		                                    out.closeEntry();
		                                    if (version.getSubmittedText() != null && version.getSubmittedText().trim().length() > 0)
		                                    {
		                                        ZipEntry textEntry = new ZipEntry(versionFolder + Entity.SEPARATOR 
		                                                + bundle.getString("assignment2.assignment_grade_assignment.downloadall.filename_submitted_text")
		                                                + ".txt");
		                                        out.putNextEntry(textEntry);
		                                        byte[] text = version.getSubmittedText().getBytes();
		                                        out.write(text);
		                                        textEntry.setSize(text.length);
		                                        out.closeEntry();
		                                    }
		                                    if (version.getSubmissionAttachSet() != null && !version.getSubmissionAttachSet().isEmpty()) {
		                                        zipAttachments(out, root + submissionFolder,
		                                                versionFolder, version.getSubmissionAttachSet());
		                                    }
		                                }
		                            }
		                        }
		                    }
		                }
		            }
		        }
		        if (assignment.isGraded() && assignment.getGradableObjectId() != null) {
		            StringBuilder gradesBuilder = new StringBuilder();
		            gradesBuilder.append(
		                    bundle.getFormattedMessage("assignment2.assignment_grade-assignment.downloadall.header",
		                            new Object[] {assignmentTitle}))
		                            .append("\n");
		            List<String> gradableStudents = permissionLogic.getGradableStudentsForUserForItem(currUserId, assignment);
		            if (gradableStudents != null && !gradableStudents.isEmpty()) {
		                Map<String, GradeInformation> userIdGradeMap = gradebookLogic.getGradeInformationForStudents(gradableStudents, assignment.getContextId(), assignment.getGradableObjectId());
		                for (String studentId : gradableStudents) {
		                    User student = userIdUserMap.get(studentId);
		                    if (student != null) {
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(student.getDisplayId());
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(",");
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(student.getSortName());
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(",");
		                        GradeInformation gradeInfo = userIdGradeMap.get(studentId);
		                        if (gradeInfo != null) {
		                            String gradebookGrade = "";
		                            String gradebookComment = "";
		                            if (gradeInfo.getGradebookGrade() != null) {
		                                gradebookGrade = gradeInfo.getGradebookGrade();
		                            }
		                            if (gradeInfo.getGradebookComment() != null) {
		                                gradebookComment = gradeInfo.getGradebookComment();
		                            }
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(gradebookGrade);
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(",");
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(gradebookComment);
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(",");
		                        } else {
		                            gradesBuilder.append(",,");
		                        }
		                        gradesBuilder.append("\n");
		                    }
		                }
		                ZipEntry gradesCSVEntry = new ZipEntry(root + assignmentTitle + "-" + contextId + "_" + df_noTime.format(new Date()) + ".csv");
		                out.putNextEntry(gradesCSVEntry);
		                byte[] grades = gradesBuilder.toString().getBytes();
		                out.write(grades);
		                gradesCSVEntry.setSize(grades.length);
		                out.closeEntry();
		            }
		        }
		        out.finish();
		        out.flush();
		        out.close();
		    }
		    catch (IOException e)
		    {
		        exceptionMessage.append("Cannot establish the IO to create zip file. ");
		        log
		        .debug(this
		                + ": getSubmissionsZip--IOException unable to create the zip file for assignment "
		                + assignmentTitle);
		    }
		}
	}

	protected void zipSubmissions(Assignment2 assignment,
			List<AssignmentSubmission> submissionsWithHistory, OutputStream outputStream,
			StringBuilder exceptionMessage)
	{
		String assignmentTitle = assignment.getTitle();
		String contextId = externalLogic.getCurrentContextId();
		String currUserId = externalLogic.getCurrentUserId();
		List<String> viewableStudents = permissionLogic.getViewableStudentsForUserForItem(currUserId, assignment);
		Map<String, User> userIdUserMap = externalLogic.getUserIdUserMap(viewableStudents);
		String formatWithTime = bundle.getString("assignment2.assignment_grade_assignment.downloadall.filename_date_format_with_time");
		String formatNoTime = bundle.getString("assignment2.assignment_grade_assignment.downloadall.filename_date_format");
		DateFormat df_withTime = new SimpleDateFormat(formatWithTime, bundle.getLocale());
		DateFormat df_noTime = new SimpleDateFormat(formatNoTime, bundle.getLocale());
		if (!assignment.isGraded() && (submissionsWithHistory == null || submissionsWithHistory.isEmpty())) {
		    if (log.isDebugEnabled()) log.debug("Nothing to download!!");
		} else {
		    try
		    {
		        ZipOutputStream out = new ZipOutputStream(outputStream);
		        String root = Validator.escapeZipEntry(assignmentTitle) + Entity.SEPARATOR;
		        if (submissionsWithHistory != null && !submissionsWithHistory.isEmpty())
		        {
		            List<String> submissionFolderNames = new ArrayList<String>();
		            for (AssignmentSubmission s : submissionsWithHistory)
		            {
		                if (s.getSubmissionHistorySet() != null && !s.getSubmissionHistorySet().isEmpty()) {
		                    User submitterUser = userIdUserMap.get(s.getUserId());
		                    if (submitterUser != null) {
		                        String submissionFolder = submitterUser.getSortName();
		                        if (submissionFolderNames.contains(submissionFolder)) {
		                            submissionFolder = getUniqueFolderName(submissionFolder, submissionFolderNames);
		                        }
		                        submissionFolderNames.add(submissionFolder);
		                        Set<AssignmentSubmissionVersion> versionHistory = s.getSubmissionHistorySet();
		                        if (versionHistory != null && !versionHistory.isEmpty()) {
		                            List<String> versionFolderNames = new ArrayList<String>(); 
		                            for (AssignmentSubmissionVersion version : versionHistory) {
		                                if (version.getSubmittedDate() != null) {
		                                    String versionFolder = root + submissionFolder + Entity.SEPARATOR 
		                                    + df_withTime.format(version.getSubmittedDate());
		                                    if (versionFolderNames.contains(versionFolder)) {
		                                        versionFolder = getUniqueFolderName(versionFolder, versionFolderNames);
		                                    }
		                                    versionFolderNames.add(versionFolder);
		                                    ZipEntry versionFolderEntry = new ZipEntry(
		                                            versionFolder + Entity.SEPARATOR);
		                                    out.putNextEntry(versionFolderEntry);
		                                    out.closeEntry();
		                                    if (version.getSubmittedText() != null && version.getSubmittedText().trim().length() > 0)
		                                    {
		                                        String submittedTextFileName = bundle.getString("assignment2.assignment_grade_assignment.downloadall.filename_submitted_text") + ".txt";
		                                        ZipEntry textEntry = new ZipEntry(versionFolder + Entity.SEPARATOR 
		                                                + submittedTextFileName);
		                                        out.putNextEntry(textEntry);
		                                        byte[] text = version.getSubmittedText().getBytes();
		                                        out.write(text);
		                                        textEntry.setSize(text.length);
		                                        out.closeEntry();
		                                    }
		                                    if (version.getSubmissionAttachSet() != null && !version.getSubmissionAttachSet().isEmpty()) {
		                                        zipAttachments(out, root + submissionFolder,
		                                                versionFolder, version.getSubmissionAttachSet());
		                                    }
		                                }
		                            }
		                        }
		                    }
		                }
		            }
		        }
		        if (assignment.isGraded() && assignment.getGradableObjectId() != null) {
		            StringBuilder gradesBuilder = new StringBuilder();
		            gradesBuilder.append(
		                    bundle.getFormattedMessage("assignment2.assignment_grade-assignment.downloadall.header",
		                            new Object[] {assignmentTitle}))
		                            .append("\n");
		            List<String> gradableStudents = permissionLogic.getGradableStudentsForUserForItem(currUserId, assignment);
		            if (gradableStudents != null && !gradableStudents.isEmpty()) {
		                Map<String, GradeInformation> userIdGradeMap = gradebookLogic.getGradeInformationForStudents(gradableStudents, assignment.getContextId(), assignment.getGradableObjectId());
		                for (String studentId : gradableStudents) {
		                    User student = userIdUserMap.get(studentId);
		                    if (student != null) {
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(student.getDisplayId());
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(",");
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(student.getSortName());
		                        gradesBuilder.append("\"");
		                        gradesBuilder.append(",");
		                        GradeInformation gradeInfo = userIdGradeMap.get(studentId);
		                        if (gradeInfo != null) {
		                            String gradebookGrade = "";
		                            String gradebookComment = "";
		                            if (gradeInfo.getGradebookGrade() != null) {
		                                gradebookGrade = gradeInfo.getGradebookGrade();
		                            }
		                            if (gradeInfo.getGradebookComment() != null) {
		                                gradebookComment = gradeInfo.getGradebookComment();
		                            }
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(gradebookGrade);
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(",");
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(gradebookComment);
		                            gradesBuilder.append("\"");
		                            gradesBuilder.append(",");
		                        } else {
		                            gradesBuilder.append(",,");
		                        }
		                        gradesBuilder.append("\n");
		                    }
		                }
		                ZipEntry gradesCSVEntry = new ZipEntry(root + assignmentTitle + "-" + contextId + "_" + df_noTime.format(new Date()) + ".csv");
		                out.putNextEntry(gradesCSVEntry);
		                byte[] grades = gradesBuilder.toString().getBytes();
		                out.write(grades);
		                gradesCSVEntry.setSize(grades.length);
		                out.closeEntry();
		            }
		        }
		        out.finish();
		        out.flush();
		        out.close();
		    }
		    catch (IOException e)
		    {
		        exceptionMessage.append("Cannot establish the IO to create zip file. ");
		        log
		        .debug(this
		                + ": getSubmissionsZip--IOException unable to create the zip file for assignment "
		                + assignmentTitle);
		    }
		}
	}
