Пример #1
0
 partial void DeleteSection(Section instance);
Пример #2
0
 partial void UpdateSection(Section instance);
Пример #3
0
 partial void InsertSection(Section instance);
Пример #4
0
        //This method adds entry information
        //and also file contents to the database
        //whenever new Questions are added
        Boolean AddSolution(HttpPostedFileBase hpf, String FileName, UploadModel model)
        {
            AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext();

            //Disect the file name for it's file properties
            String[] properties = FileName.Split(new char[1] { '_' });
            String Textbook_Title = properties[0];
            String Unit_Title = properties[1];
            String Chapter_Title = properties[2];
            String Section_Title = properties[3];
            String Page_Number = properties[4];
            String Question_Number = properties[5].Split(new char[1] { '.' })[0];//Truncate ".pdf" from the end of the file name
            String Practice_Problem = null;
            if (properties.Length > 6) { Practice_Problem = properties[6]; }//An 7th argument indicates a Practice Problem
            if (Practice_Problem != null) { Practice_Problem = properties[6].Split(new char[1] { '.' })[0]; }//Truncate ".pdf" from the end of the file name

            //Search teh database for this Textbook
            IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks
                                                      where theTextbooks.Title.Equals(Textbook_Title)
                                                      select theTextbooks;
            Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>();
            if (TextbookResults.Length == 0)//The Textbook does not yet exists
            {
                //Create a new Textbook
                AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook();

                //Populate the Textbook with the properties extracted from the file name
                theTextbook.Title = Textbook_Title;

                if (theTextbook.Title.Length < 1)
                {
                    return false;
                }

                db.Textbooks.InsertOnSubmit(theTextbook);
                db.SubmitChanges();
            }

            //Search teh database for this Unit
            IQueryable<Unit> RetrievedUnits = from theUnits in db.Units
                                              where theUnits.Textbook_Title.Equals(Textbook_Title)
                                              && theUnits.Unit_Title.Equals(Unit_Title)
                                              select theUnits;
            Unit[] UnitResults = RetrievedUnits.ToArray<Unit>();
            if (UnitResults.Length == 0)//The Unit does not yet exists
            {
                //Create a new Unit
                AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit();

                //Populate the Unit with the properties extracted from the file name
                theUnit.Textbook_Title = Textbook_Title;
                theUnit.Unit_Title = Unit_Title;
                //Populate the relational Id's based on previous hierarchical entries
                theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;

                db.Units.InsertOnSubmit(theUnit);
                db.SubmitChanges();
            }

            //Search the database for this Chapter
            IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters
                                                    where theChapters.Textbook_Title.Equals(Textbook_Title)
                                                    && theChapters.Unit_Title.Equals(Unit_Title)
                                                    && theChapters.Chapter_Title.Equals(Chapter_Title)
                                                    select theChapters;
            Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>();
            if (ChapterResults.Length == 0)//The Chapter does not yet exists
            {
                //Create a new Chapter
                AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter();

                //Populate the Chapter with the properties extracted from the file name
                theChapter.Textbook_Title = Textbook_Title;
                theChapter.Unit_Title = Unit_Title;
                theChapter.Chapter_Title = Chapter_Title;
                //Populate the relational Id's based on previous hierarchical entries
                theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;

                if (theChapter.Chapter_Title.Equals("MACOSX"))
                {
                    return false;//"Error: You have attempted to add a chapter titled MACOSX...  ...  ...  gAwD! O.o  ...skipping...";
                }

                db.Chapters.InsertOnSubmit(theChapter);
                db.SubmitChanges();
            }

            //Search teh database for this Section
            IQueryable<Section> RetrievedSections = from theSections in db.Sections
                                                    where theSections.Textbook_Title.Equals(Textbook_Title)
                                                    && theSections.Unit_Title.Equals(Unit_Title)
                                                    && theSections.Chapter_Title.Equals(Chapter_Title)
                                                    && theSections.Section_Title.Equals(Section_Title)
                                                    select theSections;
            Section[] SectionResults = RetrievedSections.ToArray<Section>();
            if (SectionResults.Length == 0)//The Section does not yet exists
            {
                //Create a new Section
                AnswerApp.Models.Section theSection = new AnswerApp.Models.Section();

                //Populate the Section with the properties extracted from the file name
                theSection.Textbook_Title = Textbook_Title;
                theSection.Unit_Title = Unit_Title;
                theSection.Chapter_Title = Chapter_Title;
                theSection.Section_Title = Section_Title;
                //Populate the relational Id's based on previous hierarchical entries
                theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;

                if (theSection.Section_Title.Length < 1)
                {
                    return false;
                }

                db.Sections.InsertOnSubmit(theSection);
                db.SubmitChanges();
            }

            //Search teh database for this Page
            IQueryable<Page> RetrievedPages = from thePages in db.Pages
                                              where thePages.Textbook_Title.Equals(Textbook_Title)
                                              //&& thePages.Unit_Title.Equals(Unit_Title)
                                              //&& thePages.Chapter_Title.Equals(Chapter_Title)
                                              //&& thePages.Section_Title.Equals(Section_Title)
                                              && thePages.Page_Number.Equals(Page_Number)
                                              select thePages;
            Page[] PageResults = RetrievedPages.ToArray<Page>();
            if (PageResults.Length == 0)//The Page does not yet exists
            {
                //Create a new Page
                AnswerApp.Models.Page thePage = new AnswerApp.Models.Page();

                //Populate the Page with the properties extracted from the file name
                thePage.Textbook_Title = Textbook_Title;
                thePage.Unit_Title = Unit_Title;
                thePage.Chapter_Title = Chapter_Title;
                thePage.Section_Title = Section_Title;
                thePage.Page_Number = Page_Number;
                //Populate the relational Id's based on previous hierarchical entries
                thePage.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                thePage.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                thePage.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                thePage.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;

                int test;
                if (!int.TryParse(thePage.Page_Number, out test))
                {
                    return false;
                }

                db.Pages.InsertOnSubmit(thePage);
                db.SubmitChanges();
            }

            //Search teh database for this Question
            IQueryable<Question> retrieved = from theAnswers in db.Questions
                                             where theAnswers.Textbook_Title.Equals(Textbook_Title)
                                             && theAnswers.Unit_Title.Equals(Unit_Title)
                                             && theAnswers.Chapter_Title.Equals(Chapter_Title)
                                             && theAnswers.Section_Title.Equals(Section_Title)
                                             && theAnswers.Page_Number.Equals(Page_Number)
                                             && theAnswers.Question_Number.Equals(Question_Number)
                                             select theAnswers;
            Question[] results = retrieved.ToArray<Question>();
            if (results.Length != 0)//The Answer already exists
            {
                //Use the existing Question
                AnswerApp.Models.Question theQuestion = results.First();

                if (Practice_Problem != null)//This is a Practice Problem
                {
                    theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                    theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                }
                else//(Practice_Problem == null) This is an Answer
                {
                    theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                    theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                }
            }
            else//(results.Length == 0) This is a new Answer
            {
                //Create a new Question
                AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question();

                //Populate the Question with the properties extracted from the file name
                theQuestion.Textbook_Title = Textbook_Title;
                theQuestion.Unit_Title = Unit_Title;
                theQuestion.Chapter_Title = Chapter_Title;
                theQuestion.Section_Title = Section_Title;
                theQuestion.Page_Number = Page_Number;
                theQuestion.Question_Number = Question_Number;
                //Populate the relational Id's based on previous hierarchical entries
                theQuestion.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                theQuestion.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                theQuestion.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                theQuestion.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;
                theQuestion.Page_Id = db.Pages.Single(d => d.Page_Number.Equals(Page_Number)).Page_Id;

                if (Practice_Problem != null)//This is a Practice Problem
                {
                    theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                    theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                }
                else//(Practice_Problem == null) This is an Answer
                {
                    theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                    theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                }

                int test;
                if (!int.TryParse(theQuestion.Page_Number, out test))
                {
                    return false;
                }

                //Insert the new Question into the database
                db.Questions.InsertOnSubmit(theQuestion);
            }

            db.SubmitChanges();//Commit the changes to the database.
            return true;
        }
Пример #5
0
        //This method adds entry information
        //and also file contents to the database
        //whenever new Questions are added using
        //the bulk uploader
        String AddFromZip(byte[] data, String FileName)
        {
            AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext();

            if (FileName == null) { return "Error: Archive file name is null."; }
            if (FileName.Equals("")) { return "Error: No file name."; }
            if (FileName.Length < 2) { return "Error: Archive file name too short."; }

            String _Info = "";
            //Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"User successfully updated\");", true);

            //Disect the file name for it's file properties
            FileName = FileName.Replace(".pdf", "");//Remove file extension
            String[] ParsedFileName = FileName.Split(new char[1] { '/' });//separate file and path text
            String PathlessFileName = ParsedFileName.Last<String>();//The last string will be the file name
            if (PathlessFileName.Length < 1) { return "Error: The directory tree is empty.  "; }
            String Path = FileName.Replace(PathlessFileName, "");//Everything but the file name is the path(test)

            String Textbook_Title = null;
            String Unit_Title = null;
            String Chapter_Title = null;
            String Section_Title = null;
            String Page_Number = null;
            String Question_Number = null;
            String Practice_Problem_Answer = null;

            String[] PathlessFileNameParsed = PathlessFileName.Split(new char[2] { '_', ' ' });
            if (PathlessFileNameParsed.Length == 3)//This is a practice problem
            {
                _Info += "Practice Problem";
                if (PathlessFileName.Contains("p") || PathlessFileName.Contains("q"))//If the file name contains p or q
                {
                    Boolean Page = false, Question = false, Answer = false;
                    foreach (String theElement in PathlessFileNameParsed)
                    {
                        if (theElement.Contains("p"))//this must be a page number
                        {
                            Page_Number = theElement.Replace("p", "").Replace("g", "");
                            Page = true;
                        }
                        else if (theElement.Contains("q"))//this must be a question number
                        {
                            Question_Number = theElement.Replace("q", "");
                            Question = true;
                        }
                        else
                        {
                            Practice_Problem_Answer = theElement;
                            //_Info += "[" + Practice_Problem_Answer + "]";
                            Answer = true;
                        }
                    }
                    if ((Page == false) || (Question == false) || (Answer == false))
                    {
                        _Info += "Error: File naming convention not followed \"" + PathlessFileName + "\".  The parser could not understand the file name of the practice problem because it is ambiguous.  skipping...";
                    }
                }
                else//No symbols were added thus the order of the numbers in the file name determines their meaning
                {
                    if (PathlessFileNameParsed.Length > 0) { Page_Number = PathlessFileNameParsed[0]; }
                    if (PathlessFileNameParsed.Length > 1) { Question_Number = PathlessFileNameParsed[1]; }
                    if (PathlessFileNameParsed.Length > 2) { Practice_Problem_Answer = PathlessFileNameParsed[2]; }
                }
            }
            else if (PathlessFileNameParsed.Length == 2)//This is a solution
            {
                _Info += "Solution ";
                if (PathlessFileName.Contains("p") || PathlessFileName.Contains("q"))//If the file name contains p or q
                {
                    Boolean Page = false, Question = false;
                    foreach (String theElement in PathlessFileNameParsed)//use the p and q to denote page and question
                    {
                        if (theElement.Contains("p"))//this must be a page number
                        {
                            Page_Number = theElement.Replace("p", "").Replace("g", "");
                            Page = true;
                        }
                        else if (theElement.Contains("q"))//this must be a question number
                        {
                            Question_Number = theElement.Replace("q", "");
                            Question = true;
                        }
                        else
                        {
                            _Info += "Error: File naming convention not followed \"" + PathlessFileName + "\".  Missing one or more 'p' and or 'q' disambiguation parameters.  skipping...";
                        }
                    }
                    if ((Page == false) || (Question == false))
                    {
                        _Info += "Error: File naming convention not followed \"" + PathlessFileName + "\".  The parser could not understand the file name of the solution because it is ambiguous.  skipping...";
                    }
                }
                else//No symbols were added thus the order of the numbers in the file name determines their meaning
                {
                    if (PathlessFileNameParsed.Length > 0) { Page_Number = PathlessFileNameParsed[0]; }
                    if (PathlessFileNameParsed.Length > 1) { Question_Number = PathlessFileNameParsed[1]; }
                }
            }
            else//(PathlessFileNameParsed.Length < 2) || (PathlessFileNameParsed.Length > 3)
            {
                _Info += "Error: File naming convention not followed \"" + PathlessFileName + "\".  " + PathlessFileNameParsed.Length + "is not the right number of elements.  skipping...";
            }

            String[] PathParsed = Path.Split(new char[2] { '_', '/' });
            if (PathParsed.Length < 3)
            {
                return "Error uploading files: The path labeling convention had not been followed and as a result the path is ambiguous.  The parser cannot complete this uploaduntil the path has been corrected.  skipping...";
            }
            //Section_Title = PathParsed[PathParsed.Length - 2];
            //Chapter_Title = PathParsed[PathParsed.Length - 3];
            //Unit_Title = PathParsed[PathParsed.Length - 4];
            //Textbook_Title = PathParsed[PathParsed.Length - 5];
            if(PathParsed.Length < 4){ return "Error: The path could not be understood because it was too short.  skipping..."; }
            Textbook_Title = PathParsed[0];
            Unit_Title = PathParsed[1];
            Chapter_Title = PathParsed[2];
            Section_Title = PathParsed[3];

            _Info += Path + PathlessFileName + ".pdf ► " + Textbook_Title + " ► " + Unit_Title + " ► " + Chapter_Title + " ► " + Section_Title + " ► Page " + Page_Number + " ► Question " + Question_Number + " ► Practice Problem Answer " + Practice_Problem_Answer;

            //Search the database for this Textbook
            IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks
                                                      where theTextbooks.Title.Equals(Textbook_Title)
                                                      select theTextbooks;
            Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>();
            if (TextbookResults.Length == 0)//The Textbook does not yet exists
            {
                //Create a new Textbook
                AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook();

                //Populate the Textbook with the properties extracted from the file name
                theTextbook.Title = Textbook_Title;

                if(theTextbook.Title.Length < 1)
                {
                    return "Warning: You have attempted to add a textbook without a title.  skipping...  ";
                }
                db.Textbooks.InsertOnSubmit(theTextbook);
                db.SubmitChanges();
            }

            //Search teh database for this Unit
            IQueryable<Unit> RetrievedUnits = from theUnits in db.Units
                                              where theUnits.Textbook_Title.Equals(Textbook_Title)
                                              && theUnits.Unit_Title.Equals(Unit_Title)
                                              select theUnits;
            Unit[] UnitResults = RetrievedUnits.ToArray<Unit>();
            if (UnitResults.Length == 0)//The Unit does not yet exists
            {
                //Create a new Unit
                AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit();

                //Populate the Unit with the properties extracted from the file name
                theUnit.Textbook_Title = Textbook_Title;
                theUnit.Unit_Title = Unit_Title;
                //Populate the relational Id's based on previous hierarchical entries
                theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;

                if (theUnit.Unit_Title.Length < 1)
                {
                    return "Warning: You have attempted to add a gUnit without a Unit Title.  skipping...";
                }

                db.Units.InsertOnSubmit(theUnit);
                db.SubmitChanges();
            }

            //Search the database for this Chapter
            IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters
                                                    where theChapters.Textbook_Title.Equals(Textbook_Title)
                                                    && theChapters.Unit_Title.Equals(Unit_Title)
                                                    && theChapters.Chapter_Title.Equals(Chapter_Title)
                                                    select theChapters;
            Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>();
            if (ChapterResults.Length == 0)//The Chapter does not yet exists
            {
                //Create a new Chapter
                AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter();

                //Populate the Chapter with the properties extracted from the file name
                theChapter.Textbook_Title = Textbook_Title;
                theChapter.Unit_Title = Unit_Title;
                theChapter.Chapter_Title = Chapter_Title;
                //Populate the relational Id's based on previous hierarchical entries
                theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;//YOU ARE HERE!!! (This needs tobe rewritten in case multiple textbooks ahve the same unit titles within them

                if (theChapter.Chapter_Title.Equals("MACOSX"))
                {
                    return "Warning: You have attempted to add a chapter titled MACOSX...  ...  ...  gAwD! O.o  ...skipping...";
                }

                db.Chapters.InsertOnSubmit(theChapter);
                db.SubmitChanges();
            }

            //Search teh database for this Section
            IQueryable<Section> RetrievedSections = from theSections in db.Sections
                                                    where theSections.Textbook_Title.Equals(Textbook_Title)
                                                    && theSections.Unit_Title.Equals(Unit_Title)
                                                    && theSections.Chapter_Title.Equals(Chapter_Title)
                                                    && theSections.Section_Title.Equals(Section_Title)
                                                    select theSections;
            Section[] SectionResults = RetrievedSections.ToArray<Section>();
            if (SectionResults.Length == 0)//The Section does not yet exists
            {
                //Create a new Section
                AnswerApp.Models.Section theSection = new AnswerApp.Models.Section();

                //Populate the Section with the properties extracted from the file name
                theSection.Textbook_Title = Textbook_Title;
                theSection.Unit_Title = Unit_Title;
                theSection.Chapter_Title = Chapter_Title;
                theSection.Section_Title = Section_Title;
                //Populate the relational Id's based on previous hierarchical entries
                theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;

                if (Section_Title.Length < 1)
                {
                    return "Warning: You have attempted to add a section that does not have a title.  skipping...";
                }

                db.Sections.InsertOnSubmit(theSection);
                db.SubmitChanges();
            }

            //Search teh database for this Page
            IQueryable<Page> RetrievedPages = from thePages in db.Pages
                                              where thePages.Textbook_Title.Equals(Textbook_Title)
                                              && thePages.Unit_Title.Equals(Unit_Title)
                                              && thePages.Chapter_Title.Equals(Chapter_Title)
                                              && thePages.Section_Title.Equals(Section_Title)
                                              && thePages.Page_Number.Equals(Page_Number)
                                              select thePages;
            Page[] PageResults = RetrievedPages.ToArray<Page>();
            int Page_Id = -1;
            if (PageResults.Length == 0)//The Page does not yet exist
            {
                //Create a new Page
                AnswerApp.Models.Page thePage = new AnswerApp.Models.Page();
                Page_Id = thePage.Page_Id;
                //Populate the Page with the properties extracted from the file name
                thePage.Textbook_Title = Textbook_Title;
                thePage.Unit_Title = Unit_Title;
                thePage.Chapter_Title = Chapter_Title;
                thePage.Section_Title = Section_Title;
                thePage.Page_Number = Page_Number;
                //Populate the relational Id's based on previous hierarchical entries
                thePage.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                thePage.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                thePage.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                RetrievedSections = from theSections in db.Sections
                                    where theSections.Textbook_Title.Equals(Textbook_Title)
                                    && theSections.Unit_Title.Equals(Unit_Title)
                                    && theSections.Chapter_Title.Equals(Chapter_Title)
                                    && theSections.Section_Title.Equals(Section_Title)
                                    select theSections;
                thePage.Section_Id = RetrievedSections.First().Section_Id;// db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;

                int test;
                if (!int.TryParse(thePage.Page_Number, out test))
                {
                    return "Error: You have attempted to add a page with a page number of '" + thePage.Page_Number + "'.  The question number must be a number.  You will have to correct this and try again.  skipping...  ";
                }

                db.Pages.InsertOnSubmit(thePage);
                db.SubmitChanges();
            }

            //Search teh database for this Question
            IQueryable<Question> retrieved = from theAnswers in db.Questions
                                             where theAnswers.Textbook_Title.Equals(Textbook_Title)
                                             && theAnswers.Unit_Title.Equals(Unit_Title)
                                             && theAnswers.Chapter_Title.Equals(Chapter_Title)
                                             && theAnswers.Section_Title.Equals(Section_Title)
                                             && theAnswers.Page_Number.Equals(Page_Number)
                                             && theAnswers.Question_Number.Equals(Question_Number)
                                             select theAnswers;
            Question[] results = retrieved.ToArray<Question>();
            if (results.Length != 0)//The Answer already exists
            {
                //Use the existing Question
                AnswerApp.Models.Question theQuestion = results.First();

                if (Practice_Problem_Answer != null)//This is a Practice Problem
                {
                    theQuestion.Practice_Problem = data;
                    theQuestion.Practice_Problem_Answer = Practice_Problem_Answer;
                }
                else//(Practice_Problem == null) This is an Answer
                {
                    theQuestion.Answer = data;
                    //theQuestion.Practice_Problem_Answer = Practice_Problem_Answer;
                }
            }
            else//(results.Length == 0) This is a new Answer
            {
                //Create a new Question
                AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question();

                //Populate the Question with the properties extracted from the file name
                theQuestion.Textbook_Title = Textbook_Title;
                theQuestion.Unit_Title = Unit_Title;
                theQuestion.Chapter_Title = Chapter_Title;
                theQuestion.Section_Title = Section_Title;
                theQuestion.Page_Number = Page_Number;
                theQuestion.Question_Number = Question_Number;
                //Populate the relational Id's based on previous hierarchical entries
                theQuestion.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                theQuestion.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                theQuestion.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                //theQuestion.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;
                RetrievedSections = from theSections in db.Sections
                                    where theSections.Textbook_Title.Equals(Textbook_Title)
                                    && theSections.Unit_Title.Equals(Unit_Title)
                                    && theSections.Chapter_Title.Equals(Chapter_Title)
                                    && theSections.Section_Title.Equals(Section_Title)
                                    select theSections;
                theQuestion.Section_Id = RetrievedSections.First().Section_Id;// db.Questions.Single(d => d.Question_Title.Equals(Question_Title)).Question_Id;
                if (Page_Id != -1) { theQuestion.Page_Id = Page_Id; }
                else
                {
                    //try
                    //{
                        //theQuestion.Page_Id = db.Pages.Single(d => d.Page_Number.Equals(Page_Number)).Page_Id;
                    RetrievedPages = from thePages in db.Pages
                                                      where thePages.Textbook_Title.Equals(Textbook_Title)
                                                      //&& thePages.Unit_Title.Equals(Unit_Title)
                                                      //&& thePages.Chapter_Title.Equals(Chapter_Title)
                                                      //&& thePages.Section_Title.Equals(Section_Title)
                                                      && thePages.Page_Number.Equals(Page_Number)
                                                      select thePages;
                    theQuestion.Page_Id = RetrievedPages.First().Page_Id; //results = retrieved.ToArray<Question>();
                    /*}
                    catch
                    {
                        IQueryable<Page> OldPages = from thePages in db.Pages
                                                    where thePages.Textbook_Title.Equals(Textbook_Title)
                                                    //&& thePages.Unit_Title.Equals(Unit_Title)
                                                    //&& thePages.Chapter_Title.Equals(Chapter_Title)
                                                    //&& thePages.Section_Title.Equals(Section_Title)
                                                    //&& thePages.Page_Number.Equals(Page_Number)
                                                    select thePages;
                        Page[] theOldPages = OldPages.ToArray<Page>();
                        return "Error: The page you are attempting to add, Page " + theQuestion.Page_Number + ", already exists in a previously uploaded section with a different title.  The title of the previously uploaded section is: " + theQuestion.Section_Title + ".  The Title of the section you are presently uploading is: " + theOldPages[0].Section_Title + ".  You will have to appropriately alter the section headings of your zipped directory and try again.  skipping...";
                    }*/
                }

                int test;
                if (!int.TryParse(theQuestion.Question_Number, out test))
                {
                    return "Error: You have attempted to add a question numbered '" + theQuestion.Question_Number + "'.  The question number must be a number.  You will have to correct this and try again.  skipping...  ";
                }

                if (Practice_Problem_Answer != null)//This is a Practice Problem
                {
                    theQuestion.Practice_Problem = data;
                    theQuestion.Practice_Problem_Answer = Practice_Problem_Answer;
                }
                else//(Practice_Problem == null) This is an Answer
                {
                    theQuestion.Answer = data;
                    //theQuestion.Practice_Problem_Answer = Practice_Problem_Answer;
                }

                //Insert the new Question into the database
                db.Questions.InsertOnSubmit(theQuestion);
            }

            db.SubmitChanges();//Commit the changes to the database.*/
            return _Info;
        }
Пример #6
0
        //Allows the user to upload new Answers along with their respective
        //Practice Problem and the answer to that Practice Problem
        public ActionResult Upload(UploadModel model, string returnUrl)
        {
            List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>();

            HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase;
            String FileName = null;

            foreach (string file in Request.Files)
            {
                hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                //string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName));
                string savedFileName = Path.Combine(Path.GetFileName(hpf.FileName));
                //hpf.SaveAs(savedFileName);//Replace this with database insertion
                FileName = Path.GetFileName(hpf.FileName);// hpf.FileName;

                r.Add(new ViewDataUploadFilesResult()
                {
                    Name = savedFileName,
                    Length = hpf.ContentLength
                });

                AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext();

                //Disect the file name for it's file properties
                String[] properties = FileName.Split(new char[1] { '_' });
                String Textbook_Title = properties[0];
                String Unit_Title = properties[1];
                String Chapter_Title = properties[2];
                String Section_Title = properties[3];
                String Page_Number = properties[4];
                String Question_Number = properties[5].Split(new char[1] { '.' })[0];//Truncate ".pdf" from the end of the file name
                String Practice_Problem = null;
                if (properties.Length > 6) { Practice_Problem = properties[6]; }//An 7th argument indicates a Practice Problem
                if (Practice_Problem != null) { Practice_Problem = properties[6].Split(new char[1] { '.' })[0]; }//Truncate ".pdf" from the end of the file name

                //Search teh database for this Textbook
                IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks
                                                          where theTextbooks.Title.Equals(Textbook_Title)
                                                          select theTextbooks;
                Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>();
                if (TextbookResults.Length == 0)//The Textbook does not yet exists
                {
                    //Create a new Textbook
                    AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook();

                    //Populate the Textbook with the properties extracted from the file name
                    theTextbook.Title = Textbook_Title;

                    db.Textbooks.InsertOnSubmit(theTextbook);
                    db.SubmitChanges();
                }

                //Search teh database for this Unit
                IQueryable<Unit> RetrievedUnits = from theUnits in db.Units
                                                  where theUnits.Textbook_Title.Equals(Textbook_Title)
                                                  && theUnits.Unit_Title.Equals(Unit_Title)
                                                  select theUnits;
                Unit[] UnitResults = RetrievedUnits.ToArray<Unit>();
                if (UnitResults.Length == 0)//The Unit does not yet exists
                {
                    //Create a new Unit
                    AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit();

                    //Populate the Unit with the properties extracted from the file name
                    theUnit.Textbook_Title = Textbook_Title;
                    theUnit.Unit_Title = Unit_Title;
                    //Populate the relational Id's based on previous hierarchical entries
                    theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;

                    db.Units.InsertOnSubmit(theUnit);
                    db.SubmitChanges();
                }

                //Search the database for this Chapter
                IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters
                                                        where theChapters.Textbook_Title.Equals(Textbook_Title)
                                                        && theChapters.Unit_Title.Equals(Unit_Title)
                                                        && theChapters.Chapter_Title.Equals(Chapter_Title)
                                                        select theChapters;
                Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>();
                if (ChapterResults.Length == 0)//The Chapter does not yet exists
                {
                    //Create a new Chapter
                    AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter();

                    //Populate the Chapter with the properties extracted from the file name
                    theChapter.Textbook_Title = Textbook_Title;
                    theChapter.Unit_Title = Unit_Title;
                    theChapter.Chapter_Title = Chapter_Title;
                    //Populate the relational Id's based on previous hierarchical entries
                    theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;

                    db.Chapters.InsertOnSubmit(theChapter);
                    db.SubmitChanges();
                }

                //Search teh database for this Section
                IQueryable<Section> RetrievedSections = from theSections in db.Sections
                                                        where theSections.Textbook_Title.Equals(Textbook_Title)
                                                        && theSections.Unit_Title.Equals(Unit_Title)
                                                        && theSections.Chapter_Title.Equals(Chapter_Title)
                                                        && theSections.Section_Title.Equals(Section_Title)
                                                        select theSections;
                Section[] SectionResults = RetrievedSections.ToArray<Section>();
                if (SectionResults.Length == 0)//The Section does not yet exists
                {
                    //Create a new Section
                    AnswerApp.Models.Section theSection = new AnswerApp.Models.Section();

                    //Populate the Section with the properties extracted from the file name
                    theSection.Textbook_Title = Textbook_Title;
                    theSection.Unit_Title = Unit_Title;
                    theSection.Chapter_Title = Chapter_Title;
                    theSection.Section_Title = Section_Title;
                    //Populate the relational Id's based on previous hierarchical entries
                    theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                    theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;

                    db.Sections.InsertOnSubmit(theSection);
                    db.SubmitChanges();
                }

                //Search teh database for this Page
                IQueryable<Page> RetrievedPages = from thePages in db.Pages
                                                  where thePages.Textbook_Title.Equals(Textbook_Title)
                                                  && thePages.Unit_Title.Equals(Unit_Title)
                                                  && thePages.Chapter_Title.Equals(Chapter_Title)
                                                  && thePages.Section_Title.Equals(Section_Title)
                                                  && thePages.Page_Number.Equals(Page_Number)
                                                  select thePages;
                Page[] PageResults = RetrievedPages.ToArray<Page>();
                if (PageResults.Length == 0)//The Page does not yet exists
                {
                    //Create a new Page
                    AnswerApp.Models.Page thePage = new AnswerApp.Models.Page();

                    //Populate the Page with the properties extracted from the file name
                    thePage.Textbook_Title = Textbook_Title;
                    thePage.Unit_Title = Unit_Title;
                    thePage.Chapter_Title = Chapter_Title;
                    thePage.Section_Title = Section_Title;
                    thePage.Page_Number = Page_Number;
                    //Populate the relational Id's based on previous hierarchical entries
                    thePage.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    thePage.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                    thePage.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                    thePage.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;

                    db.Pages.InsertOnSubmit(thePage);
                }

                //Search teh database for this Question
                IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                    where theAnswers.Textbook_Title.Equals(Textbook_Title)
                                                    && theAnswers.Unit_Title.Equals(Unit_Title)
                                                    && theAnswers.Chapter_Title.Equals(Chapter_Title)
                                                    && theAnswers.Section_Title.Equals(Section_Title)
                                                    && theAnswers.Page_Number.Equals(Page_Number)
                                                    && theAnswers.Question_Number.Equals(Question_Number)
                                                    select theAnswers;
                Question[] results = retrieved.ToArray<Question>();
                if (results.Length != 0)//The Answer already exists
                {
                    //Use the existing Question
                    AnswerApp.Models.Question theQuestion = results.First();

                    if (Practice_Problem != null)//This is a Practice Problem
                    {
                        theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }
                    else//(Practice_Problem == null) This is an Answer
                    {
                        theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }
                }
                else//(results.Length == 0) This is a new Answer
                {
                    //Create a new Question
                    AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question();

                    //Populate the Question with the properties extracted from the file name
                    theQuestion.Textbook_Title = Textbook_Title;
                    theQuestion.Unit_Title = Unit_Title;
                    theQuestion.Chapter_Title = Chapter_Title;
                    theQuestion.Section_Title = Section_Title;
                    theQuestion.Page_Number = Page_Number;
                    theQuestion.Question_Number = Question_Number;
                    //Populate the relational Id's based on previous hierarchical entries
                    theQuestion.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    theQuestion.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                    theQuestion.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                    theQuestion.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;
                    theQuestion.Page_Id = db.Pages.Single(d => d.Page_Number.Equals(Page_Number)).Page_Id;

                    if (Practice_Problem != null)//This is a Practice Problem
                    {
                        theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }
                    else//(Practice_Problem == null) This is an Answer
                    {
                        theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }

                    //Insert the new Question into the database
                    db.Questions.InsertOnSubmit(theQuestion);
                }

                db.SubmitChanges();//Commit the changes to the database.
            }
            if(User.Identity.Name.Equals("administrator")){}
            return View("Upload", r);
        }