public async Task <IActionResult> Download(DownloadSubmissionViewModel downloadSubmissionViewModel)
        {
            // Will be > -1 if we got here because user clicked one of the student selection links
            // next to one of the section names
            int iSectionStudents = downloadSubmissionViewModel.SectionsAndStudents.FindIndex
                                   (
                ss => ss.SectionsAndStudentsSubmitButton != null
                                   );

            downloadSubmissionViewModel.IndexForSectionStudentsView = iSectionStudents;

            if (iSectionStudents != -1)
            {
                // Display the "select students" form controls
                return(View("Download", downloadSubmissionViewModel));
            }

            // Either we posted here from the select-a-student form, or from the primary
            // download submissions form
            if (downloadSubmissionViewModel.DownloadSubmitButton == null)
            {
                // We posted here from the select-a-student form, so render the
                // main download form controls (with the updated student selections)
                return(View("Download", downloadSubmissionViewModel));
            }

            // The download submission button was clicked, so we're finally ready to initiate the download
            return(await DownloadSubmissionsAsync(downloadSubmissionViewModel));
        }
        public async Task <IActionResult> Download(string sectionName)
        {
            // Gets list of sections and students from db for user to choose from
            var downloadCandidates = await SubmissionService.GetCheckpointDownloadCandidateListAsync(
                ClassroomName,
                ProjectName,
                CheckpointName);

            List <SectionsAndStudents> sectionStudents = downloadCandidates.Select
                                                         (
                dc => new SectionsAndStudents()
            {
                SectionId   = dc.Section.Id,
                SectionName = new SelectListItem()
                {
                    Text  = dc.Section.DisplayName,
                    Value = dc.Section.Name,

                    // Whichever section the user had clicked on before
                    // choosing to download should be checked by default
                    Selected = (dc.Section.Name == sectionName)
                },
                SelectedStudents = dc.Users.Select
                                   (
                    user => new StudentToDownload()
                {
                    Selected  = true,
                    FirstName = user.User.FirstName,
                    LastName  = user.User.LastName,
                    Id        = user.User.Id,
                    Submitted = user.Submitted
                }
                                   )
                                   .OrderBy(ss => ss.LastName)
                                   .ThenBy(ss => ss.FirstName)
                                   .ToList()
            }
                                                         )
                                                         .OrderBy(sas => sas.SectionName.Text)
                                                         .ToList();

            DownloadSubmissionViewModel viewModel = new DownloadSubmissionViewModel()
            {
                IndexForSectionStudentsView = -1,
                Format              = ProjectSubmissionDownloadFormat.All,
                IncludeUnsubmitted  = true,
                SectionsAndStudents = sectionStudents,
            };

            return(View("Download", viewModel));
        }
        /// <summary>
        /// Once the user has chosen the download properties, made the student selections, and clicked
        /// the main form submit button, this initiates the actual download
        /// </summary>
        /// <param name="downloadSubmissionViewModel">ViewModel for the form</param>
        public async Task <IActionResult> DownloadSubmissionsAsync(DownloadSubmissionViewModel downloadSubmissionViewModel)
        {
            var selectedStudents = downloadSubmissionViewModel.SectionsAndStudents
                                   .Where
                                   (
                sas => sas.SectionName.Selected
                                   )
                                   .SelectMany
                                   (
                sas => sas.SelectedStudents
                .Where
                (
                    ss => ss.Selected &&
                    (ss.Submitted || downloadSubmissionViewModel.IncludeUnsubmitted)
                )
                .Select
                (
                    ss => ss.Id
                )
                                   ).ToList();

            if (selectedStudents.Count == 0)
            {
                return(NotFound());
            }

            var archiveContents = await SubmissionService.DownloadSubmissionsAsync
                                  (
                ClassroomName,
                ProjectName,
                CheckpointName,
                selectedStudents,
                downloadSubmissionViewModel.Format
                                  );

            var timestamp = TimeZoneProvider.ToUserLocalTime(DateTime.UtcNow)
                            .ToString("yyyyMMdd-HHmmss");

            var filename = $"{Classroom.Name}-{Project.Name}-{Checkpoint.Name}-{timestamp}.zip";

            return(File(archiveContents, "application/zip", filename));
        }