/// <summary>
        ///  Over rides OnPreRender.
        /// </summary>
        /// <param name="e">An EventArgs that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            string exceptionMessage = string.Empty;

            try
            {
                this.SetResourceText();

                AssignmentProperties assignmentProperties = SlkStore.LoadAssignmentProperties(AssignmentItemIdentifier, SlkRole.Instructor);

                using (SPSite site = new SPSite(assignmentProperties.SPSiteGuid, SPContext.Current.Site.Zone))
                {
                    using (SPWeb web = site.OpenWeb(assignmentProperties.SPWebGuid))
                    {
                        if (!(SlkStore.IsInstructor(web)))
                        {
                            exceptionMessage = PageCulture.Resources.CommentedFilesNoAccessException;

                            throw new SafeToDisplayException(exceptionMessage);
                        }
                    }
                }

                //Checks if all learners have completed the assignment.
                if (!IsPostBack)
                {
                    bool isAssignmentsCompleted = true;
                    m_assignmentProperties = SlkStore.GetGradingProperties(AssignmentItemIdentifier);
                    ReadOnlyCollection <LearnerAssignmentProperties> learnersGradingCollection = m_assignmentProperties.Results;
                    foreach (LearnerAssignmentProperties learnerGrading in learnersGradingCollection)
                    {
                        if (learnerGrading.Status != LearnerAssignmentState.Completed)
                        {
                            isAssignmentsCompleted = false;
                        }
                    }
                    if (!isAssignmentsCompleted)
                    {
                        exceptionMessage = PageCulture.Resources.CommentedFilesAssignmnetsNotCompleted;

                        throw new SafeToDisplayException(exceptionMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                this.contentPanel.Visible = false;
                this.errorBanner.Clear();
                this.errorBanner.AddError(ErrorType.Error, ex.Message);
            }
        }
Пример #2
0
        // TODO: Needs error handling. Remove catch all exceptions and ignore as that is worse than none.

        /// <summary>See <see cref="Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase.OnInit"/>.</summary>
        protected override void OnLoad(EventArgs e)
        {
            AssignmentProperties assignmentProperties = SlkStore.LoadAssignmentProperties(AssignmentItemIdentifier, SlkRole.Instructor);

            using (AssignmentDownloader downloader = new AssignmentDownloader(assignmentProperties))
            {
                string zippedFileName = downloader.ZippedFileName;
                String userAgent      = Request.Headers["User-Agent"];
                if (userAgent.Contains("MSIE 7.0"))
                {
                    zippedFileName = zippedFileName.Replace(" ", "%20");
                }

                using (Stream iStream = new FileStream(downloader.ZippedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // Buffer to read 10K bytes in chunk:
                    byte[] buffer = new Byte[10000];

                    // Total bytes to read:
                    long dataToRead = iStream.Length;

                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + zippedFileName + ".zip\"");

                    // Read the bytes.
                    while (dataToRead > 0)
                    {
                        // Verify that the client is connected.
                        if (Response.IsClientConnected)
                        {
                            int length = iStream.Read(buffer, 0, 10000);
                            Response.OutputStream.Write(buffer, 0, length);
                            Response.Flush();

                            dataToRead = dataToRead - length;
                        }
                        else
                        {
                            //prevent infinite loop if user disconnects
                            dataToRead = -1;
                        }
                    }
                }
                Response.Close();
            }
        }
        /// <summary>
        /// This function uploads the instructor commented files
        /// to the corresponding student folder on the dropbox list.
        /// </summary>
        /// <param name="zippedFileName">The name of the zipped file</param>
        /// <param name="tempFolderPath">Temp directory path</param>
        private List <AssignmentUploadTracker> UploadCommentedFiles(string zippedFileName, string tempFolderPath)
        {
            //Keeps track of the uploading progress;
            List <AssignmentUploadTracker> uploadTrackers = new List <AssignmentUploadTracker>();
            AssignmentUploadTracker        currentUploadTracker;

            try
            {
                AssignmentProperties assignmentProperties = SlkStore.LoadAssignmentProperties(AssignmentItemIdentifier, SlkRole.Instructor);

                using (SPSite site = new SPSite(assignmentProperties.SPSiteGuid, SPContext.Current.Site.Zone))
                {
                    using (SPWeb web = site.OpenWeb(assignmentProperties.SPWebGuid))
                    {
                        SPList dropBoxList = web.Lists[PageCulture.Resources.DropBoxDocLibName];

                        //Gets the name of the assignment folder.
                        string assignmentFolderName = zippedFileName.Split('.')[0];

                        /* Searching for the assignment folder using the naming format: "AssignmentTitle AssignmentCreationDate"
                         * (This is the naming format defined in AssignmentProperties.aspx.cs page) */
                        SPQuery query = new SPQuery();
                        query.Folder = dropBoxList.RootFolder;
                        query.Query  = "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + assignmentFolderName + "</Value></Eq></Where>";
                        SPListItemCollection assignmentFolders = dropBoxList.GetItems(query);

                        try
                        {
                            if (assignmentFolders.Count == 0)
                            {
                                // The assignment folder does not exits on the dropbox list.
                                throw new Exception(assignmentFolderName + " " + PageCulture.Resources.CommentedFilesNoAssignmentFolderException);
                            }
                        }
                        catch (Exception assignmetNotFoundEx)
                        {
                            this.contentPanel.Visible = false;
                            this.errorBanner.Clear();
                            errorBanner.AddError(ErrorType.Error, assignmetNotFoundEx.Message);
                            return(null);
                        }

                        //Gets the assignment SP folder on the dropbox list.
                        SPFolder assignmentFolder = assignmentFolders[0].Folder;

                        string[]         commentedFilesPaths;
                        string           extractedFolderPath       = tempFolderPath + "\\" + assignmentFolderName;
                        DirectoryInfo    extractedAssignmentfolder = new DirectoryInfo(extractedFolderPath);
                        Stream           assignmentFileStream;
                        byte[]           assignmentContents;
                        string           studentFolderName = string.Empty;
                        string[]         studentFolders;
                        string           assignmentFileName  = string.Empty;
                        int              uploadFilescounter  = 0;
                        int              ignoredFilescounter = 0;
                        int              missedFilescounter  = 0;
                        SPFileCollection orgFiles;
                        bool             fileFound          = false;
                        bool             commentedFileFound = false;

                        if (extractedAssignmentfolder.Exists)
                        {
                            //Gets an array of the students assignment folders paths.
                            studentFolders = Directory.GetDirectories(extractedFolderPath);


                            //loop on each student folder to upload the instuctor commented files.
                            foreach (string studentFolderPath in studentFolders)
                            {
                                //keeps track of the current upload status
                                currentUploadTracker = new AssignmentUploadTracker();

                                //Gets the student assignment folder
                                studentFolderName = studentFolderPath.Split('\\')[studentFolderPath.Split('\\').Length - 1];

                                //Tracking the upload operation: save the student folder name
                                currentUploadTracker.StudentFolderName = studentFolderName;

                                //Gets the student assignment SP folder of the current assignment
                                query        = new SPQuery();
                                query.Folder = assignmentFolder;
                                query.Query  = "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + studentFolderName + "</Value></Eq></Where>";
                                SPListItemCollection assignmentSubFolders = dropBoxList.GetItems(query);

                                if (assignmentSubFolders.Count == 0)
                                {
                                    //Tracking the upload operation: The student folder does not exists
                                    currentUploadTracker.IsCompleted = false;

                                    //throw new Exception(PageCulture.Resources.SubmittedFilesNoAssignmentSubFolderException);
                                }
                                else
                                {
                                    SPFolder assignmentSubFolder = assignmentSubFolders[0].Folder;

                                    //Gets the commented files of the current student assignment
                                    commentedFilesPaths = Directory.GetFiles(studentFolderPath);

                                    //Gets the assignment original files
                                    orgFiles = assignmentSubFolder.Files;

                                    //Reset tracking counters
                                    uploadFilescounter  = 0;
                                    ignoredFilescounter = 0;
                                    missedFilescounter  = 0;

                                    //loop on each file to upload it to the student assignment SP folder
                                    foreach (string assignmentFilePath in commentedFilesPaths)
                                    {
                                        fileFound = false;

                                        foreach (SPFile assignmentFile in orgFiles)
                                        {
                                            //Gets ths assignment file name
                                            assignmentFileName = assignmentFilePath.Split('\\')[assignmentFilePath.Split('\\').Length - 1];

                                            //checks that file exists on ths SP assignment folder
                                            if (assignmentFile.Name == assignmentFileName)
                                            {
                                                fileFound = true;
                                            }
                                        }
                                        //The file exists
                                        if (fileFound)
                                        {
                                            uploadFilescounter++;

                                            assignmentFileStream = File.OpenRead(assignmentFilePath);
                                            assignmentContents   = new byte[assignmentFileStream.Length];
                                            assignmentFileStream.Read(assignmentContents, 0, (int)assignmentFileStream.Length);
                                            assignmentFileStream.Close();

                                            web.AllowUnsafeUpdates = true;

                                            //Uploads the current file.
                                            assignmentSubFolder.Files.Add(assignmentFileName, assignmentContents, true);
                                        }
                                        else
                                        {
                                            ignoredFilescounter++;
                                        }
                                    }
                                    //loop on each file on the student assignment SP folder
                                    // to check if there files missed
                                    foreach (SPFile assignmentFile in orgFiles)
                                    {
                                        if ((bool)assignmentFile.Item["IsLatest"])
                                        {
                                            commentedFileFound = false;

                                            foreach (string assignmentFilePath in commentedFilesPaths)
                                            {
                                                //Gets ths assignment file name
                                                assignmentFileName = assignmentFilePath.Split('\\')[assignmentFilePath.Split('\\').Length - 1];

                                                //checks that file exists on ths SP assignment folder
                                                if (assignmentFile.Name == assignmentFileName)
                                                {
                                                    commentedFileFound = true;
                                                }
                                            }
                                            //The file does not exists
                                            if (!commentedFileFound)
                                            {
                                                missedFilescounter++;
                                            }
                                        }
                                    }
                                    //Tracking the upload operation: The number of files missed
                                    currentUploadTracker.MissedFilesCount = missedFilescounter;

                                    //Tracking the upload operation: The number of files uploaded
                                    currentUploadTracker.UploadedFilesCount = uploadFilescounter;

                                    //Tracking the upload operation: The number of files ignored.
                                    currentUploadTracker.IgnoredFilesCount = ignoredFilescounter;

                                    //Tracking the upload operation: The upload operation is completed.
                                    currentUploadTracker.IsCompleted = true;
                                }

                                uploadTrackers.Add(currentUploadTracker);
                            }

                            bool studentFolderFound = false;
                            AssignmentUploadTracker currentTracker;

                            //checks if a sttudent folder was not uploaded by the teacher.
                            if (assignmentFolder.SubFolders.Count != studentFolders.Length)
                            {
                                //loops on each student folder under the assignment folder
                                foreach (SPFolder studentFolder in assignmentFolder.SubFolders)
                                {
                                    studentFolderFound = false;

                                    //loops on each student folder upload by the teacher
                                    foreach (string studentFolderPath in studentFolders)
                                    {
                                        //Gets the student assignment folder name
                                        studentFolderName = studentFolderPath.Split('\\')[studentFolderPath.Split('\\').Length - 1];
                                        if (studentFolder.Name == studentFolderName)
                                        {
                                            studentFolderFound = true;
                                        }
                                    }
                                    //student folder was not uploaded
                                    if (!studentFolderFound)
                                    {
                                        //tracking the student.
                                        currentTracker = new AssignmentUploadTracker();
                                        currentTracker.StudentFolderName  = studentFolder.Name;
                                        currentTracker.IsCompleted        = false;
                                        currentTracker.UploadedFilesCount = -1;

                                        uploadTrackers.Add(currentTracker);
                                    }
                                }
                            }
                            web.Update();
                            web.Dispose();

                            //Delete the compressed file and extracted folder
                            //from the windows temp directory
                            File.Delete(tempFolderPath + "\\" + zippedFileName);
                            Directory.Delete(tempFolderPath + "\\" + assignmentFolderName, true);
                        }
                    }
                }
            }


            catch (Exception ex)
            {
                this.contentPanel.Visible = false;
                this.errorBanner.Clear();
                this.errorBanner.AddException(SlkStore, ex);
            }

            return(uploadTrackers);
        }
 /// <summary>Loads the assignment properties.</summary>
 public static AssignmentProperties Load(AssignmentItemIdentifier assignmentItemIdentifier, SlkStore store)
 {
     return(store.LoadAssignmentProperties(assignmentItemIdentifier, SlkRole.Instructor));
 }