EduNetworkBuilder/EduNetworkBuilder/PersonProfileForm.cs

354 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace EduNetworkBuilder
{
public partial class PersonProfileForm : Form
{
enum TopTab { profiletab=0, classworktab=1}
enum TreeDataTab { classtab=0, homeworktab=1, studenttab=2 }
PersonClass CurrentUser = null;
string FileName = "";
PersonClass ViewedUser = null;
SchoolworkClass ViewedSchoolwork = null;
public PersonProfileForm(string filename = "")
{
InitializeComponent();
FileName = filename;
//We do LocalSetup at load time. This saves us some grief.
}
public PersonProfileForm(PersonClass User)
{
InitializeComponent();
CurrentUser = User;
if (CurrentUser != null)
{
FileName = Path.Combine(CurrentUser.filepath, CurrentUser.UserName);
}
//We do LocalSetup at load time. This saves us some grief.
}
private void PersonProfileForm_Load(object sender, EventArgs e)
{
LocalSetup();
UpdateFormFromUser();
}
private void LanguagifyComponents()
{
lblFullName.Text = NB.Translate("PPF_FullName");
lblUsername.Text = NB.Translate("PPF_UserName");
TabClasswork.Text = NB.Translate("PPF_TabClasswork");
TabProfile.Text = NB.Translate("PPF_TabProfile");
btnExit.Text = NB.Translate("NB_exitToolStripMenuItem");
btnChangePassword.Text = NB.Translate("PPF_ChangePassword");
btnHWDelete.Text = NB.Translate("_Delete");
btnImportStudents.Text = NB.Translate("PPF_ImportStudents");
btnLaunch.Text = NB.Translate("PPF_Launch");
}
private void LocalSetup()
{
Icon = Properties.Resources.NBIco;
LanguagifyComponents();
if (CurrentUser == null && FileName == "")
{
//Prompt for a username
NBSettings oldsettings = NB.GetSettings(); //Grab the old settings.
String Dest = NB.TextPromptBox(NB.Translate("PPF_EnterUserName"));
if (Dest == "") { Close(); return; }//No name given or canceled.
if (Dest == null) { Close(); return; }
if (Dest[0] != '_') Dest = "_Teacher_" + Dest; //Make sure it begins with _
//Find a directory for it.
CurrentUser = new PersonClass(Dest, true); //Make an admin person class
//Choose an initial folder for the file:
OpenFileDialog OFD = new OpenFileDialog();
OFD.ValidateNames = false;
OFD.CheckFileExists = false;
OFD.CheckPathExists = true;
OFD.FileName = NB.Translate("PPF_SelectThisFolder");
OFD.ShowDialog();
CurrentUser.filepath = Path.GetDirectoryName(OFD.FileName);
CurrentUser.UserSettings.AutoStartPuzzles = false;
CurrentUser.UserSettings.ChosenLanguage = oldsettings.ChosenLanguage;
CurrentUser.UserSettings.LanguageHasBeenChosen = true;
}
else if (CurrentUser == null)
{
//Try to load the file. Close form & give error if it fails
try
{
//Load in from the file
}
catch(Exception e)
{
CurrentUser = null;
Close(); //we did not succeed. Exit out
}
}
tvClasswork.AfterSelect += tvClasswork_Click;
tbHWClass.LostFocus += SaveSchoolworkDataFromForm;
tbHWDescription.LostFocus += SaveSchoolworkDataFromForm;
tbHWName.LostFocus += SaveSchoolworkDataFromForm;
tbHWSubmitted.LostFocus += SaveSchoolworkDataFromForm;
dtpHWDue.LostFocus += SaveSchoolworkDataFromForm;
//Make sure we update any profile settings they change
tbFullName.LostFocus += SaveUserInfoFromForm;
}
private void DtpHWDue_LostFocus(object sender, EventArgs e)
{
throw new NotImplementedException();
}
public PersonClass Edit()
{
BuilderWindow BW = NB.GetBuilderWin();
if(BW != null)
{
BW.Hide();
this.ShowDialog();
BW.Show();
}
return CurrentUser;
}
public PersonClass AddSchoolwork(Network ToAdd)
{
if (CurrentUser == null) return CurrentUser;
BuilderWindow BW = NB.GetBuilderWin();
SchoolworkClass NewWork = new SchoolworkClass(ToAdd, CurrentUser);
CurrentUser.Projects.Add(NewWork);
UpdateFormFromUser(); //make sure we list the right homeworks.
ChangeTreeDataTab(TreeDataTab.homeworktab);
tvClasswork.ExpandAll();
tvClasswork.Update();
//Now we want to select the item.
foreach(TreeNode TN in tvClasswork.GetAllNodes())
{
TN.ExpandAll(); //Expand everything until we get to what we need.
if(TN.Tag is SchoolworkClass)
{
SchoolworkClass Check = (SchoolworkClass)TN.Tag;
if(Check == NewWork)
{
tvClasswork.SelectedNode = TN;
break;
}
}
}
if (BW != null)
{
BW.Hide();
this.tcTabControl.SelectedIndex = 1; //Jump to the items tab
this.ShowDialog();
BW.Show();
}
return CurrentUser;
}
private void UpdateFormFromUser()
{
if (CurrentUser == null) return;
tbFullName.Text = CurrentUser.FullName;
tbUsername.Text = CurrentUser.UserName;
if (CurrentUser.isAdmin) this.Text = "Admin: " + CurrentUser.UserName;
else this.Text = "Student: " + CurrentUser.UserName;
tvClasswork.Nodes.Clear();
TreeNode Node;
if (CurrentUser.isAdmin)
{
//We have a class tree
Node = new TreeNode(NB.Translate("PPF_Class"));
Node.Tag = "Class"; //Do not translate this
TreeNode Top = Node;
tvClasswork.Nodes.Add(Node);
foreach(SchoolworkClass SWC in CurrentUser.Projects)
{
Node = new TreeNode(SWC.Name);
Node.Tag = SWC; //Store the schoolwork record in with the node
Top.Nodes.Add(Node);
}
//we have a students tree
Node = new TreeNode(NB.Translate("PPF_Students"));
Node.Tag = "Students"; //Do not translate this
tvClasswork.Nodes.Add(Node);
}
}
private void SaveUserInfoFromForm()
{
CurrentUser.FullName = tbFullName.Text;
}
private void SaveUserInfoFromForm(object sender, EventArgs e)
{
SaveUserInfoFromForm();
}
private void btnExit_Click(object sender, EventArgs e)
{
//Save during closing
Close();
}
private void btnChangePassword_Click(object sender, EventArgs e)
{
string OldPassword = "";
if (CurrentUser.Password() != "")
{
OldPassword = NB.TextPromptBox(NB.Translate("PPF_OldPassword"), Properties.Resources.NBIco, true);
if(OldPassword != CurrentUser.Password())
{
MessageBox.Show(NB.Translate("PPF_PassMismatch"));
return;
}
}
//The passwords match, or there was no initial password.
string Password1 = NB.TextPromptBox(NB.Translate("PPF_NewPass"), Properties.Resources.NBIco, true);
if(Password1.Length < 4)
{
MessageBox.Show(NB.Translate("PPF_PassTooShort"));
return;
}
if(Password1 == OldPassword)
{
MessageBox.Show(NB.Translate("PPF_PassDidNotChange"));
return;
}
string Password2 = NB.TextPromptBox(NB.Translate("PPF_VerifyPass"), Properties.Resources.NBIco, true);
if(Password1 != Password2)
{
MessageBox.Show(NB.Translate("PPF_PassValidateMismatch"));
return;
}
CurrentUser.ChangePassword(Password1);
}
private void ChangeTreeDataTab(TreeDataTab WhatTo)
{
tcTabControl.SelectedIndex = (int)TopTab.classworktab;
tcTreeData.SelectedIndex = (int)WhatTo;
}
private void tvClasswork_Click(object sender, EventArgs e)
{
if (tvClasswork.SelectedNode == null) return;
TreeNode Node = tvClasswork.SelectedNode;
if (Node.Tag is SchoolworkClass)
{
SchoolworkClass SWC = (SchoolworkClass)Node.Tag;
//It is a homework. Select the homework tab and fill it
ChangeTreeDataTab(TreeDataTab.homeworktab);
UpdateFormFromSchoolwork(SWC);
}
if(Node.Tag is string)
{
string Selected = (string)Node.Tag;
if (Selected == "Class") //Do not translate this
ChangeTreeDataTab(TreeDataTab.classtab);
if (Selected == "Students") //Do not translate this
ChangeTreeDataTab(TreeDataTab.studenttab);
}
}
void SaveSchoolworkDataFromForm(object sender, EventArgs e)
{
SaveSchoolworkDataFromForm();
}
private void SaveSchoolworkDataFromForm()
{
if (ViewedSchoolwork == null) return;
ViewedSchoolwork.Class = tbHWClass.Text;
ViewedSchoolwork.Description = tbHWDescription.Text;
ViewedSchoolwork.Name = tbHWName.Text;
ViewedSchoolwork.DueDate = dtpHWDue.Value;
}
private void UpdateFormFromSchoolwork(SchoolworkClass What)
{
ViewedSchoolwork = What;
UpdateFormFromSchoolwork();
}
private void UpdateFormFromSchoolwork()
{
if (ViewedSchoolwork == null)
{
tbHWClass.Text = "";
tbHWDescription.Text = "";
tbHWName.Text ="";
tbHWSubmitted.Visible = false;
dtpHWDue.Visible = false;
lblHWDue.Visible = false;
btnHWDelete.Visible = false;
return;
}
tbHWClass.Text = ViewedSchoolwork.Class;
tbHWDescription.Text = ViewedSchoolwork.Description;
tbHWName.Text = ViewedSchoolwork.Name;
dtpHWDue.Visible = true;
lblHWDue.Visible = true;
if (ViewedSchoolwork.IsSumbitted)
{
tbHWSubmitted.Visible = true;
lblHWDue.Visible = true;
tbHWSubmitted.Text = ViewedSchoolwork.SaveDate.ToString();
}
else
{
tbHWSubmitted.Visible = false;
lblHWDue.Visible = false;
}
if (ViewedSchoolwork.DueDate < dtpHWDue.MinDate) ViewedSchoolwork.DueDate = dtpHWDue.MinDate;
dtpHWDue.Value = ViewedSchoolwork.DueDate;
if(CurrentUser != null && CurrentUser.isAdmin)
btnHWDelete.Visible = true; //Only the teacher can delete the homework
}
private void btnHWDelete_Click(object sender, EventArgs e)
{
if (ViewedSchoolwork == null) return;
if (CurrentUser != null && CurrentUser.isAdmin)//Only the teacher can delete the homework
{
DialogResult Sure = MessageBox.Show(NB.Translate("PPF_ConfirmHWDelete"), "", MessageBoxButtons.YesNoCancel);
if (Sure == DialogResult.Yes)
{
CurrentUser.Projects.Remove(ViewedSchoolwork);
ViewedSchoolwork = null;
UpdateFormFromUser();
UpdateFormFromSchoolwork();
}
}
}
private void btLaunch_Click(object sender, EventArgs e)
{
}
}
}