Ejemplo n.º 1
0
 public AttemptViewModel(AttemptHTML a)
 {
     attemptHTML = a;
 }
Ejemplo n.º 2
0
        //after Seat emails are sent, this deals with the master email.
        //It cannot be moved while sending.
        //masterEmail.Move(OutlookProvider.AssignedWorkSubfolder());
        //masterEmail.Close(OlInspectorClose.olDiscard);
        //Timer delayedCloseTimer;
        //bool sending = false;
        //void setDelayedCloseTimer(MailItem mi, MAPIFolder moveToFolder)
        //{
        //    if (delayedCloseTimer == null)
        //        delayedCloseTimer = new Timer();
        //    delayedCloseTimer.Interval = 100; // 0.1 second
        //    delayedCloseTimer.Tick += (sender, e) =>
        //        {
        //            if (!sending && mi != null)
        //            {
        //                //rename the attachments
        //                //List<string> tempAttachments = new List<string>();
        //                //for (int a = 1; a < mi.Attachments.Count + 1; a++)
        //                //{
        //                //    tempAttachments.Add(OutlookProvider.renameAttachment(mi.Attachments[1], mi.Subject));
        //                //    mi.Attachments.Remove(1);
        //                //}
        //                //foreach (string a in tempAttachments)
        //                //    mi.Attachments.Add(a);

        //                //label the subject
        //                //mi.Subject = "***TEACHER COPY*** " + mi.Subject;

        //                //copy to new folder
        //                //if (moveToFolder != null)
        //                //    mi.Save();
        //                    //mi.Move(moveToFolder); //CRASHES

        //                //mi.Close(OlInspectorClose.olDiscard);


        //                delayedCloseTimer.Stop();
        //                mi = null;
        //                delayedCloseTimer = null;
        //            }
        //        };
        //    delayedCloseTimer.Start();
        //}

        //assign on item send
        void Application_ItemSend(object Item, ref bool Cancel)
        {
            //sending = true;
            MailItem mail = Item as MailItem;

            if (!Settings.Default.ItemSendEventEnabled ||
                mail == null ||
                OutlookProvider.getMarkedWorkProperty(mail))
            {
                return;
            }

            //GET PDF
            byte[] bytes = OutlookProvider.getAllPDFAttachments(mail).FirstOrDefault();

            if (bytes == null)
            {
                return; //just a normal email message!
            }
            //System.Windows.MessageBox.Show("no attachments", "", MessageBoxButton.OK, MessageBoxImage.Error);

            AuthoredWork work = new PDF(bytes).getWork <AuthoredWork>();

            if (work == null)
            {
                Cancel = false;
                //System.Windows.MessageBox.Show("unable to read attached PDF ", "", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var e    = mail.Recipients.GetEnumerator();
            var list = new List <Recipient>();

            while (e.MoveNext())
            {
                list.Add((Recipient)e.Current);
            }

            if (list.Count > 1)
            {
                Cancel = System.Windows.MessageBox.Show("you can only assign work to a single recipient. Would you like to send this email anyway? Any returned pdfs will NOT be markable.", "!", MessageBoxButton.YesNo) == MessageBoxResult.No;
                return;
            }
            if (!OutlookProvider.isGroup(list[0].AddressEntry))
            {
                Cancel = System.Windows.MessageBox.Show("you can only assign work to a distribution list. Would you like to send this email anyway? Any returned pdfs will NOT be markable.", "!", MessageBoxButton.YesNo) == MessageBoxResult.No;
                return;
            }
            IEnumerable <OutlookSeat> recipients = OutlookProvider.getSeats(mail.Recipients);


            string             groupName = mail.To.Split(new[] { ';' })[0];
            Group              group     = activeDb.getOrCreateGroup(groupName);
            IEnumerable <Seat> seats     = activeDb.getOrCreateOutlookSeats(recipients, group);

            if (group == null)
            {
                Cancel = System.Windows.MessageBox.Show("Error creating class. Would you like to send this email anyway? Any returned pdfs will NOT be markable.", "", MessageBoxButton.YesNo) == MessageBoxResult.No;
                return;
            }
            if (!work.assignmentHTML.QuestionAssignmentHTMLs.Any())
            {
                Cancel = System.Windows.MessageBox.Show("This assignment does not contain any questions and will not be saved in the markbook. Would you like to send this email anyway? Any returned pdfs will NOT be markable.", "", MessageBoxButton.YesNo) == MessageBoxResult.No;
                return;
            }

            MAPIFolder assignedWorkFolder = OutlookProvider.AssignedWorkSubfolder(groupName);

            var tasks = new List <Task>();

            //EMAILS PARALLEL!
            foreach (Grouping <Attempt, Seat> attemptGroup in activeDb.getNewAttemptsFromAuthoredWork(
                         group,
                         mail.Subject,
                         Properties.Settings.Default.shuffleQuestionsEnabled,
                         Properties.Settings.Default.markLimitValue,
                         seats,
                         work._repeatedRows))
            {
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    AssignedWork assignedWork = new AssignedWork(
                        attemptGroup.Key.GroupID,
                        attemptGroup.Key.AssignmentNum,
                        attemptGroup.Key.ShuffleSeed,
                        OutlookProvider.currentUserEmailAddress
                        );

                    AttemptHTML html    = new AttemptHTML(attemptGroup.Key);
                    string fileToAttach = html.AttemptPDFTempFile(SimplerAES.asEncryptedString(assignedWork), false);

                    MailItem emailForSeat;
                    emailForSeat = (MailItem)this.Application.CreateItem(OlItemType.olMailItem);
                    emailForSeat.SaveSentMessageFolder = assignedWorkFolder;

                    if (!string.IsNullOrEmpty(fileToAttach))
                    {
                        emailForSeat.Attachments.Add(fileToAttach);
                    }

                    emailForSeat.To       = string.Empty;
                    emailForSeat.BCC      = string.Join(";", attemptGroup.Select(s => s.Email));
                    emailForSeat.Subject  = mail.Subject;
                    emailForSeat.HTMLBody = mail.HTMLBody + Settings.Default.AssignEmailFooter;
                    emailForSeat.Send();
                }));
            } //end foreach

            Task.WaitAll(tasks.ToArray());

            Cancel = true;
            //sending = false;
            activeDb.SaveChanges();
            System.Windows.MessageBox.Show("Emails have been sent to students. You can close this window.", "", MessageBoxButton.OK);
        }
Ejemplo n.º 3
0
        public CompletedWork(
            AssignedWork paramAssignedWork,
            IEnumerable <Field> paramFields,
            SchoolContext paramDb,
            string paramTeacherUserCheck,
            Func <IEnumerable <Seat>, Seat> paramStudentPickerMethod,
            string senderEmail,
            DateTime paramWhenReturned)
        {
            if (!paramFields.Any())
            {
                errorMessage = "PDF contained no responses";
            }
            //if (paramAssignedWork.teacherEmailAddress != paramTeacherUserCheck)
            //    errorMessage = "Assigned from different teacher account, " + paramAssignedWork.teacherEmailAddress;

            //initialise
            studentPickerMethod = paramStudentPickerMethod;
            assignedWork        = paramAssignedWork;
            _db = paramDb;

            //assignment
            if (string.IsNullOrEmpty(errorMessage))
            {
                assignment = _db.Assignments.Find(assignedWork.GroupId, assignedWork.AssignmentNum);
                if (assignment == null)
                {
                    errorMessage = "assignment not found in database";
                }
            }

            //group
            if (string.IsNullOrEmpty(errorMessage))
            {
                group = _db.Groups.Find(assignedWork.GroupId); //get the seats too
                if (group == null)
                {
                    errorMessage = "group not found in database";
                }
            }

            //attempts
            if (string.IsNullOrEmpty(errorMessage))
            {
                _allAttempts = _db.Attempts.Where(a =>
                                                  a.GroupID == assignedWork.GroupId &&
                                                  a.AssignmentNum == assignedWork.AssignmentNum &&
                                                  a.ShuffleSeed == assignedWork.ShuffleSeed
                                                  ).Include(at => at.Seat).ToList();

                //update responses from pdf and assign scores
                if (!_allAttempts.Any())
                {
                    errorMessage = "attempt not found in database";
                }
            }

            //sender
            if (string.IsNullOrEmpty(errorMessage))
            {
                attempt = _allAttempts.SingleOrDefault(r => r.Seat.Email == senderEmail || r.Seat.AlternateEmail == senderEmail);
                if (attempt != null)
                {
                    sender = attempt.Seat;
                }
                else
                {
                    sender = group.Seats.SingleOrDefault(s => s.Email == senderEmail || s.AlternateEmail == senderEmail) ??
                             studentPickerMethod(group.Seats);
                    if (sender == null)
                    {
                        errorMessage = "student not found in database";
                    }
                }
            }

            //attempt
            if (string.IsNullOrEmpty(errorMessage))
            {
                if (attempt == null)
                {
                    attempt = _allAttempts.SingleOrDefault(s => s.GroupID == sender.GroupID && s.SeatNum == sender.SeatNum);
                    if (attempt == null)
                    {
                        attempt               = _db.Attempts.Create();
                        attempt.ShuffleSeed   = assignedWork.ShuffleSeed;
                        attempt.SeatNum       = sender.SeatNum;
                        attempt.AssignmentNum = assignedWork.AssignmentNum;
                        attempt.GroupID       = assignedWork.GroupId;
                        attempt.whenAssigned  = _allAttempts.First().whenAssigned;
                        attempt.AttemptNum    = ++assignment.AttemptCount;

                        _db.Attempts.Add(attempt);
                    }
                }
                attempt.UpdateResponseValuesFromPDF(paramFields, _db);
                attempt.whenReturned = paramWhenReturned;

                attemptHTML = new AttemptHTML(attempt);
                attemptHTML.CalculateAndUpdateAttemptScore();
                attempt.updateScore();
            }
            //optional user marking - changing some scores
        }