EduNetworkBuilder/EduNetworkBuilder/SchoolworkClass.cs

296 lines
11 KiB
C#
Raw Normal View History

2017-07-26 23:43:38 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
2017-07-29 18:46:39 +02:00
using System.IO;
2017-08-08 21:19:56 +02:00
using System.Windows.Forms;
2017-07-26 23:43:38 +02:00
namespace EduNetworkBuilder
{
/// <summary>
/// A class holding a project that needs to be done as either a test, quiz, or homework
/// </summary>
2018-03-15 23:12:20 +01:00
[Serializable]
2017-07-26 23:43:38 +02:00
public class SchoolworkClass
{
2017-08-01 00:01:03 +02:00
public Network theProject = null;
2017-07-26 23:43:38 +02:00
/// <summary>
/// The name of the project. Homework3, Quiz 4, etc.
/// </summary>
public string Name = "";
2017-08-02 20:03:14 +02:00
/// <summary>
/// The Suggested filename for this homework. The Name + ".eduh"
/// </summary>
public string FileName { get { return Name + ".eduh"; } }
2017-07-26 23:43:38 +02:00
/// <summary>
/// The short description of the project.
/// </summary>
2017-07-31 01:18:41 +02:00
public string Description = "";
2017-07-26 23:43:38 +02:00
/// <summary>
/// The class to which this project belongs. If the student is using this for multiple classes,
/// but more likely just for the teacher to organize things with.
/// </summary>
2017-07-31 01:18:41 +02:00
public string Class = "";
2017-07-26 23:43:38 +02:00
/// <summary>
/// The date / time that the project is due.
/// </summary>
2017-07-31 01:18:41 +02:00
public DateTime DueDate;
2017-07-26 23:43:38 +02:00
2017-07-31 01:18:41 +02:00
public bool isMaster = false; //Is this the original project? If so, we never replace it.
2017-07-26 23:43:38 +02:00
2017-07-31 01:18:41 +02:00
public bool IsSumbitted = false; //Is the student submitting this? If so, date-stamp it and lock it.
public bool IsGraded = false;
2017-07-31 01:18:41 +02:00
public DateTime SaveDate; //The date this file was saved.
public UInt64 ThisID { get; protected set; }
public HomeworkSolvedStatus HomeworkStatus = HomeworkSolvedStatus.NeverChecked;
2017-08-02 19:17:20 +02:00
protected SchoolworkClass() { }
public SchoolworkClass(XmlNode TheNode)
{
2017-08-08 02:27:59 +02:00
List<SchoolworkClass> WhatIs = Load(TheNode);
if (WhatIs.Count > 0)
{
2017-08-08 02:27:59 +02:00
SchoolworkClass.Clone(WhatIs[0], this);
//Move the packet messages across. They do not get cloned.
//We do this so we can store the results of an automated test
if(WhatIs[0].theProject != null && WhatIs[0].theProject.CountMessages() > 0)
{
foreach(PacketMessage one in WhatIs[0].theProject.GetAllMessages())
{
theProject.AddMessage(one);
}
}
}
}
2017-08-02 19:50:25 +02:00
public SchoolworkClass(string FileName)
{
Load(FileName);
}
public SchoolworkClass(Network ToAdd, PersonClass WhoFor)
{
if(WhoFor != null && WhoFor.UserSettings != null)
ThisID = WhoFor.UserSettings.IssueID();
theProject = ToAdd.Clone();
Name = theProject.PuzzleName;
Description = theProject.PuzzleName;
isMaster = true;
}
2017-08-02 19:17:20 +02:00
public SchoolworkClass Clone()
{
SchoolworkClass dest = new SchoolworkClass();
Clone(this, dest);
return dest;
}
2017-08-02 19:17:20 +02:00
public static void Clone(SchoolworkClass source, SchoolworkClass dest)
{
dest.ThisID = source.ThisID;
2017-08-02 19:17:20 +02:00
dest.Class = source.Class;
dest.Description = source.Description;
dest.DueDate = source.DueDate;
dest.isMaster = source.isMaster;
dest.IsSumbitted = source.IsSumbitted;
dest.Name = source.Name;
dest.SaveDate = source.SaveDate;
dest.theProject = source.theProject.Clone();
dest.IsGraded = source.IsGraded;
2017-08-02 19:17:20 +02:00
}
/// <summary>
/// Basically for sorting. First by class name, then due date, and then save date
/// </summary>
/// <param name="WhatTo"></param>
/// <returns></returns>
public int Compare(SchoolworkClass WhatTo)
{
int diff = Class.CompareTo(WhatTo.Class);
if (diff != 0) return diff;
diff = DueDate.CompareTo(WhatTo.DueDate);
if (diff != 0) return diff;
return SaveDate.CompareTo(WhatTo.SaveDate);
}
#region Load and Save
//Load and save functions
2017-07-29 18:46:39 +02:00
public void Load(string filename)
{
XmlDocument xmlDoc = new XmlDocument();
2017-08-08 21:19:56 +02:00
2017-07-29 18:46:39 +02:00
if (File.Exists(filename))
{
xmlDoc.Load(filename);
2017-08-08 21:19:56 +02:00
List<SchoolworkClass> results = Load(xmlDoc);
if (results.Count > 0) Clone(results[0], this);
}
}
public static List<SchoolworkClass> LoadMultiple(string filename)
{
XmlDocument xmlDoc = new XmlDocument();
List<SchoolworkClass> results = new List<SchoolworkClass>();
if (File.Exists(filename))
{
try
{
xmlDoc.Load(filename);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return results; //return an empty list
}
results.AddRange(Load(xmlDoc));
2017-07-29 18:46:39 +02:00
}
2017-08-08 21:19:56 +02:00
return results;
2017-07-29 18:46:39 +02:00
}
2017-08-06 02:35:15 +02:00
public static List<SchoolworkClass> Load(XmlNode TheNode)
{
2017-08-06 02:35:15 +02:00
SchoolworkClass currentOne = new SchoolworkClass();
List<SchoolworkClass> ThisList = new List<SchoolworkClass>();
bool AddedList = false;
foreach (XmlNode Individual in TheNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "edunetworkbuilderschoolwork":
2017-08-02 19:50:25 +02:00
case "schoolwork":
2017-08-06 02:35:15 +02:00
ThisList.AddRange(Load(Individual));
AddedList = true;
2017-08-02 19:50:25 +02:00
break;
case "id":
UInt64 tID;
UInt64.TryParse(Individual.InnerText, out tID);
2017-08-06 02:35:15 +02:00
currentOne.ThisID = tID;
break;
case "name":
2017-08-06 02:35:15 +02:00
currentOne.Name = Individual.InnerText;
break;
case "description":
2017-08-06 02:35:15 +02:00
currentOne.Description = Individual.InnerText;
break;
case "class":
2017-08-06 02:35:15 +02:00
currentOne.Class = Individual.InnerText;
break;
case "duedate":
2017-08-06 02:35:15 +02:00
DateTime.TryParse(Individual.InnerText, out currentOne.DueDate);
break;
case "savedate":
2017-08-06 02:35:15 +02:00
DateTime.TryParse(Individual.InnerText, out currentOne.SaveDate);
break;
case "homeworkstatus":
currentOne.HomeworkStatus = NB.ParseEnum<HomeworkSolvedStatus>(Individual.InnerText);
break;
case "ismaster":
2017-08-06 02:35:15 +02:00
bool.TryParse(Individual.InnerText, out currentOne.isMaster);
break;
case "isgraded":
2017-08-06 02:35:15 +02:00
bool.TryParse(Individual.InnerText, out currentOne.IsGraded);
break;
case "issubmitted":
2017-08-06 02:35:15 +02:00
bool.TryParse(Individual.InnerText, out currentOne.IsSumbitted);
break;
case "network":
2017-08-06 02:35:15 +02:00
currentOne.theProject = new Network();
currentOne.theProject.Load(Individual, "",false,true);
break;
}
}
}
2017-08-06 02:35:15 +02:00
if (!AddedList) ThisList.Add(currentOne);
return ThisList;
}
2017-07-29 18:46:39 +02:00
public void Save(string filename)
{
2017-08-02 19:50:25 +02:00
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create(filename, settings);
//Now we write the file:
writer.WriteStartDocument();
writer.WriteStartElement("EduNetworkBuilderSchoolwork");
writer.WriteComment("This is a schoolwork file for EduNetworkBuilder.");
Save(writer);
writer.WriteEndElement();
writer.WriteEndDocument();
2017-08-06 02:22:36 +02:00
writer.Flush();
writer.Close();
}
public static void Save(List<SchoolworkClass> homeworks, string filename)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create(filename, settings);
//Now we write the file:
writer.WriteStartDocument();
writer.WriteStartElement("EduNetworkBuilderSchoolwork");
writer.WriteComment("This is a schoolwork file for EduNetworkBuilder.");
foreach (SchoolworkClass swc in homeworks)
{
swc.Save(writer);
}
writer.WriteEndElement();
writer.WriteEndDocument();
2017-08-02 19:50:25 +02:00
writer.Flush();
writer.Close();
2017-07-29 18:46:39 +02:00
}
public void ChangeID(PersonClass WhoFor)
{
if (WhoFor != null && WhoFor.UserSettings != null)
ThisID = WhoFor.UserSettings.IssueID();
}
public HomeworkSolvedStatus CheckThatHomeworkIsSolved()
{
if(theProject != null)
{
HomeworkStatus = theProject.CheckThatHomeworkIsSolved();
}
return HomeworkStatus;
}
public void Save(XmlWriter writer)
{
//Save the language name
//save the number of items
//Save all the items
2017-07-29 18:46:39 +02:00
writer.WriteStartElement("schoolwork");
writer.WriteElementString("ID", ThisID.ToString());
writer.WriteElementString("Name", Name);
writer.WriteElementString("Description", Description);
writer.WriteElementString("Class", Class);
writer.WriteElementString("DueDate", DueDate.ToString());
writer.WriteElementString("SaveDate", SaveDate.ToString());
writer.WriteElementString("IsMaster", isMaster.ToString());
writer.WriteElementString("IsGraded", IsGraded.ToString());
if (HomeworkStatus != HomeworkSolvedStatus.NeverChecked)
writer.WriteElementString("HomeworkStatus", HomeworkStatus.ToString());
writer.WriteElementString("IsSubmitted", IsSumbitted.ToString());
2017-08-17 17:17:20 +02:00
if (theProject != null) theProject.Save(writer,true,true);//Save the messages also.
writer.WriteEndElement();
}
#endregion
2017-07-26 23:43:38 +02:00
}
}